Skip to content

Commit 86013fa

Browse files
committed
Added testing framework. Added files for all needed tests. All tests written for the base action. Had to rename query action as it wouldn't import otherwise.
1 parent 1438a78 commit 86013fa

13 files changed

+500
-6
lines changed

actions/lib/base_action.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,20 @@ def generate_where_clause(self, sql_table, sql_obj, where_dict):
7070
overrides.
7171
Returns Query object
7272
"""
73+
return_dict = {}
7374
for key in where_dict.keys():
75+
value = where_dict[key]
7476
# All column names are reserved. Adding a '_' to the begging of the name
7577
new_key = "_" + key
7678

7779
# Add WHERE statement to the update object
7880
sql_obj = sql_obj.where(sql_table.c.get(key) == sqlalchemy.sql.bindparam(new_key))
7981

80-
# Replace key in dictionary with one that will work
81-
where_dict[new_key] = where_dict.pop(key)
82+
# Add correct key and value to the new dictionary so it can be returned and
83+
# used in the query
84+
return_dict[new_key] = value
8285

83-
return (sql_obj, where_dict)
86+
return (sql_obj, return_dict)
8487

8588
def generate_values(self, sql_obj, data_dict):
8689
"""Generate values statement for sql queries with the proper
@@ -138,7 +141,7 @@ def resolve_connection(self, kwargs_dict):
138141
:returns: a dictionary with the connection parameters (see: CONFIG_CONNECTION_KEYS)
139142
resolved.
140143
"""
141-
connection_name = self.get_del_arg('connection', kwargs_dict)
144+
connection_name = self.get_del_arg('connection', kwargs_dict, True)
142145
config_connection = None
143146
if connection_name:
144147
config_connection = self.config['sql'].get(connection_name)

actions/query.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
runner_type: "python-script"
44
description: "Query data from a SQL database"
55
enabled: true
6-
entry_point: query.py
6+
entry_point: generic_query.py
77
parameters:
88
connection:
99
type: string

requirements-tests.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
mock>=1.3.0,<2.0
22
unittest2>=1.1.0,<2.0
33
nose>=1.3.7
4-
4+
sqlalchemy

tests/fixtures/config_blank.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
---

tests/fixtures/config_good.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
sql:
3+
base:
4+
host: abc
5+
username: Administrator
6+
password: PassworD!
7+
database: test
8+
drivername: postgresql
9+
full:
10+
host: abc
11+
username: Administrator
12+
password: PassworD!
13+
port: 1234
14+
database: test
15+
drivername: mysql
16+
sqlite:
17+
database: path/to/test
18+
drivername: sqlite

tests/fixtures/config_partial.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
sql:
3+
missing-drivername:
4+
username: Administrator
5+
password: PassworD!
6+
database: Test
7+
missing-database:
8+
server: database.domian.tld
9+
password: PassworD!
10+
drivername: postgresql

tests/sql_base_action_test_case.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import yaml
2+
import json
3+
import logging
4+
5+
from st2tests.base import BaseActionTestCase
6+
7+
8+
class SqlBaseActionTestCase(BaseActionTestCase):
9+
__test__ = False
10+
11+
def setUp(self):
12+
super(SqlBaseActionTestCase, self).setUp()
13+
14+
self._config_good = self.load_yaml('config_good.yaml')
15+
self._config_blank = self.load_yaml('config_blank.yaml')
16+
self._config_partial = self.load_yaml('config_partial.yaml')
17+
18+
def tearDown(self):
19+
super(SqlBaseActionTestCase, self).tearDown()
20+
logging.disable(logging.NOTSET)
21+
22+
def load_yaml(self, filename):
23+
return yaml.safe_load(self.get_fixture_content(filename))
24+
25+
def load_json(self, filename):
26+
return json.loads(self.get_fixture_content(filename))
27+
28+
@property
29+
def config_good(self):
30+
return self._config_good
31+
32+
@property
33+
def config_blank(self):
34+
return self._config_blank
35+
36+
@property
37+
def config_partial(self):
38+
return self._config_partial

tests/test_action_delete.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from sql_base_action_test_case import SqlBaseActionTestCase
2+
3+
from lib.base_action import BaseAction
4+
from st2common.runners.base_action import Action
5+
from delete import SQLDeleteAction
6+
7+
# import mock
8+
9+
__all__ = [
10+
'TestActionSQLDeleteAction'
11+
]
12+
13+
14+
class TestActionSQLDeleteAction(SqlBaseActionTestCase):
15+
__test__ = True
16+
action_cls = SQLDeleteAction
17+
18+
def test_init(self):
19+
action = self.get_action_instance({})
20+
self.assertIsInstance(action, SQLDeleteAction)
21+
self.assertIsInstance(action, BaseAction)
22+
self.assertIsInstance(action, Action)

tests/test_action_insert.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from sql_base_action_test_case import SqlBaseActionTestCase
2+
3+
from lib.base_action import BaseAction
4+
from st2common.runners.base_action import Action
5+
from insert import SQLInsertAction
6+
7+
# import mock
8+
9+
__all__ = [
10+
'TestActionSQLInsertAction'
11+
]
12+
13+
14+
class TestActionSQLInsertAction(SqlBaseActionTestCase):
15+
__test__ = True
16+
action_cls = SQLInsertAction
17+
18+
def test_init(self):
19+
action = self.get_action_instance({})
20+
self.assertIsInstance(action, SQLInsertAction)
21+
self.assertIsInstance(action, BaseAction)
22+
self.assertIsInstance(action, Action)

0 commit comments

Comments
 (0)