Skip to content

Commit e4988d9

Browse files
committed
Add object_key_string_contains action to the asserts pack
1 parent dc9990c commit e4988d9

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

packs/asserts/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ Contains actions that perform assert valuations.
77
``object_key_number_equals`` - Given an object and a key in the object, evaluate whether value corresponding to key is equal to an expected numerical value.
88

99
``object_key_string_equals`` - Given an object and a key in the object, evaluate whether value corresponding to key is equal to an expected string value.
10+
11+
``object_key_string_contains`` - Given an object and a key in the object, evaluate whether value corresponding to key contains an expected string value.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import sys
2+
3+
from st2actions.runners.pythonrunner import Action
4+
5+
__all__ = [
6+
'AssertObjectKeyStringContains'
7+
]
8+
9+
10+
class AssertObjectKeyStringContains(Action):
11+
def run(self, object, key, value):
12+
if not isinstance(object, dict):
13+
raise ValueError('object shoud be of type "dict".')
14+
if key not in object:
15+
sys.stderr.write('KEY %s DOESN\'T EXIST.' % key)
16+
raise ValueError('Key %s doesn\'t exist in object %s' % (key, object))
17+
result = (value in object[key])
18+
if result:
19+
sys.stdout.write('EQUAL')
20+
else:
21+
sys.stdout.write('NOT EQUAL')
22+
raise ValueError('Value not found. Expected "%s", got "%s". ' % (value, object[key]))
23+
return result
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
description: Check if key in object contains a given string value.
3+
enabled: true
4+
entry_point: object_key_string_contains.py
5+
name: object_key_string_contains
6+
parameters:
7+
object:
8+
type: object
9+
description: Object input.
10+
required: true
11+
key:
12+
type: string
13+
description: Key in object to pick the value for to test.
14+
required: true
15+
value:
16+
type: string
17+
description: Expected string value.
18+
required: true
19+
runner_type: "python-script"

0 commit comments

Comments
 (0)