diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/test_main.py | 65 | ||||
-rw-r--r-- | tests/test_market.py | 5 |
2 files changed, 50 insertions, 20 deletions
diff --git a/tests/test_main.py b/tests/test_main.py index 0b4745f..3735a3b 100644 --- a/tests/test_main.py +++ b/tests/test_main.py | |||
@@ -103,23 +103,29 @@ class MainTest(WebMockTestCase): | |||
103 | mock.patch("main.parse_config") as main_parse_config: | 103 | mock.patch("main.parse_config") as main_parse_config: |
104 | with self.subTest(debug=False): | 104 | with self.subTest(debug=False): |
105 | main_parse_args.return_value = self.market_args() | 105 | main_parse_args.return_value = self.market_args() |
106 | main_fetch_markets.return_value = [(1, {"key": "market_config"}, 3)] | 106 | main_fetch_markets.return_value = [(1, {"key": "market_config"}, 3, { "foo": "bar" })] |
107 | m = main.get_user_market("config_path.ini", 1) | 107 | m = main.get_user_market("config_path.ini", 1) |
108 | 108 | ||
109 | self.assertIsInstance(m, market.Market) | 109 | self.assertIsInstance(m, market.Market) |
110 | self.assertFalse(m.debug) | 110 | self.assertFalse(m.debug) |
111 | self.assertEqual("bar", m.options["foo"]) | ||
111 | main_parse_args.assert_called_once_with(["--config", "config_path.ini"]) | 112 | main_parse_args.assert_called_once_with(["--config", "config_path.ini"]) |
112 | 113 | ||
113 | main_parse_args.reset_mock() | 114 | main_parse_args.reset_mock() |
114 | with self.subTest(debug=True): | 115 | with self.subTest(debug=True): |
115 | main_parse_args.return_value = self.market_args(debug=True) | 116 | main_parse_args.return_value = self.market_args(debug=True) |
116 | main_fetch_markets.return_value = [(1, {"key": "market_config"}, 3)] | 117 | main_fetch_markets.return_value = [(1, {"key": "market_config"}, 3, { "foo": "bar" })] |
117 | m = main.get_user_market("config_path.ini", 1, debug=True) | 118 | m = main.get_user_market("config_path.ini", 1, debug=True) |
118 | 119 | ||
119 | self.assertIsInstance(m, market.Market) | 120 | self.assertIsInstance(m, market.Market) |
120 | self.assertTrue(m.debug) | 121 | self.assertTrue(m.debug) |
121 | main_parse_args.assert_called_once_with(["--config", "config_path.ini", "--debug"]) | 122 | main_parse_args.assert_called_once_with(["--config", "config_path.ini", "--debug"]) |
122 | 123 | ||
124 | def test_parse_liquidity(self): | ||
125 | self.assertEqual("high", main.parse_liquidity("high-liquidity")) | ||
126 | self.assertEqual("medium", main.parse_liquidity("medium-liquidity")) | ||
127 | self.assertIsNone(main.parse_liquidity("foo")) | ||
128 | |||
123 | def test_process(self): | 129 | def test_process(self): |
124 | with mock.patch("market.Market") as market_mock,\ | 130 | with mock.patch("market.Market") as market_mock,\ |
125 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: | 131 | mock.patch('sys.stdout', new_callable=StringIO) as stdout_mock: |
@@ -133,16 +139,16 @@ class MainTest(WebMockTestCase): | |||
133 | args_mock.after = "after" | 139 | args_mock.after = "after" |
134 | self.assertEqual("", stdout_mock.getvalue()) | 140 | self.assertEqual("", stdout_mock.getvalue()) |
135 | 141 | ||
136 | main.process("config", 3, 1, args_mock) | 142 | main.process("config", 3, 1, args_mock, "options") |
137 | 143 | ||
138 | market_mock.from_config.assert_has_calls([ | 144 | market_mock.from_config.assert_has_calls([ |
139 | mock.call("config", args_mock, market_id=3, user_id=1), | 145 | mock.call("config", args_mock, market_id=3, user_id=1, options="options"), |
140 | mock.call().process("action", before="before", after="after"), | 146 | mock.call().process("action", before="before", after="after"), |
141 | ]) | 147 | ]) |
142 | 148 | ||
143 | with self.subTest(exception=True): | 149 | with self.subTest(exception=True): |
144 | market_mock.from_config.side_effect = Exception("boo") | 150 | market_mock.from_config.side_effect = Exception("boo") |
145 | main.process(3, "config", 1, args_mock) | 151 | main.process(3, "config", 1, args_mock, "options") |
146 | self.assertEqual("Exception: boo\n", stdout_mock.getvalue()) | 152 | self.assertEqual("Exception: boo\n", stdout_mock.getvalue()) |
147 | 153 | ||
148 | def test_main(self): | 154 | def test_main(self): |
@@ -157,7 +163,10 @@ class MainTest(WebMockTestCase): | |||
157 | args_mock.user = "user" | 163 | args_mock.user = "user" |
158 | parse_args.return_value = args_mock | 164 | parse_args.return_value = args_mock |
159 | 165 | ||
160 | fetch_markets.return_value = [[3, "config1", 1], [1, "config2", 2]] | 166 | fetch_markets.return_value = [ |
167 | [3, "config1", 1, "options"], | ||
168 | [1, "config2", 2, "options"] | ||
169 | ] | ||
161 | 170 | ||
162 | main.main(["Foo", "Bar"]) | 171 | main.main(["Foo", "Bar"]) |
163 | 172 | ||
@@ -167,9 +176,10 @@ class MainTest(WebMockTestCase): | |||
167 | 176 | ||
168 | self.assertEqual(2, process.call_count) | 177 | self.assertEqual(2, process.call_count) |
169 | process.assert_has_calls([ | 178 | process.assert_has_calls([ |
170 | mock.call("config1", 3, 1, args_mock), | 179 | mock.call("config1", 3, 1, args_mock, "options"), |
171 | mock.call("config2", 1, 2, args_mock), | 180 | mock.call("config2", 1, 2, args_mock, "options"), |
172 | ]) | 181 | ]) |
182 | |||
173 | with self.subTest(parallel=True): | 183 | with self.subTest(parallel=True): |
174 | with mock.patch("main.parse_args") as parse_args,\ | 184 | with mock.patch("main.parse_args") as parse_args,\ |
175 | mock.patch("main.parse_config") as parse_config,\ | 185 | mock.patch("main.parse_config") as parse_config,\ |
@@ -183,7 +193,10 @@ class MainTest(WebMockTestCase): | |||
183 | args_mock.user = "user" | 193 | args_mock.user = "user" |
184 | parse_args.return_value = args_mock | 194 | parse_args.return_value = args_mock |
185 | 195 | ||
186 | fetch_markets.return_value = [[3, "config1", 1], [1, "config2", 2]] | 196 | fetch_markets.return_value = [ |
197 | [3, "config1", 1, "options"], | ||
198 | [1, "config2", 2, "options"] | ||
199 | ] | ||
187 | 200 | ||
188 | main.main(["Foo", "Bar"]) | 201 | main.main(["Foo", "Bar"]) |
189 | 202 | ||
@@ -196,10 +209,11 @@ class MainTest(WebMockTestCase): | |||
196 | self.assertEqual(2, process.call_count) | 209 | self.assertEqual(2, process.call_count) |
197 | process.assert_has_calls([ | 210 | process.assert_has_calls([ |
198 | mock.call.__bool__(), | 211 | mock.call.__bool__(), |
199 | mock.call("config1", 3, 1, args_mock), | 212 | mock.call("config1", 3, 1, args_mock, "options"), |
200 | mock.call.__bool__(), | 213 | mock.call.__bool__(), |
201 | mock.call("config2", 1, 2, args_mock), | 214 | mock.call("config2", 1, 2, args_mock, "options"), |
202 | ]) | 215 | ]) |
216 | |||
203 | with self.subTest(quiet=True): | 217 | with self.subTest(quiet=True): |
204 | with mock.patch("main.parse_args") as parse_args,\ | 218 | with mock.patch("main.parse_args") as parse_args,\ |
205 | mock.patch("main.parse_config") as parse_config,\ | 219 | mock.patch("main.parse_config") as parse_config,\ |
@@ -213,7 +227,10 @@ class MainTest(WebMockTestCase): | |||
213 | args_mock.user = "user" | 227 | args_mock.user = "user" |
214 | parse_args.return_value = args_mock | 228 | parse_args.return_value = args_mock |
215 | 229 | ||
216 | fetch_markets.return_value = [[3, "config1", 1], [1, "config2", 2]] | 230 | fetch_markets.return_value = [ |
231 | [3, "config1", 1, "options"], | ||
232 | [1, "config2", 2, "options"] | ||
233 | ] | ||
217 | 234 | ||
218 | main.main(["Foo", "Bar"]) | 235 | main.main(["Foo", "Bar"]) |
219 | 236 | ||
@@ -232,7 +249,10 @@ class MainTest(WebMockTestCase): | |||
232 | args_mock.user = "user" | 249 | args_mock.user = "user" |
233 | parse_args.return_value = args_mock | 250 | parse_args.return_value = args_mock |
234 | 251 | ||
235 | fetch_markets.return_value = [[3, "config1", 1], [1, "config2", 2]] | 252 | fetch_markets.return_value = [ |
253 | [3, "config1", 1, "options"], | ||
254 | [1, "config2", 2, "options"] | ||
255 | ] | ||
236 | 256 | ||
237 | main.main(["Foo", "Bar"]) | 257 | main.main(["Foo", "Bar"]) |
238 | 258 | ||
@@ -318,23 +338,32 @@ class MainTest(WebMockTestCase): | |||
318 | @mock.patch.object(main.dbs, "psql") | 338 | @mock.patch.object(main.dbs, "psql") |
319 | def test_fetch_markets(self, psql): | 339 | def test_fetch_markets(self, psql): |
320 | cursor_mock = mock.MagicMock() | 340 | cursor_mock = mock.MagicMock() |
321 | cursor_mock.__iter__.return_value = ["row_1", "row_2"] | 341 | cursor_mock.__iter__.return_value = [ |
342 | (1, "cfg", 1, "high-liquidity"), | ||
343 | (2, "cfg2", 3, "medium-liquidity") | ||
344 | ] | ||
322 | 345 | ||
323 | psql.cursor.return_value = cursor_mock | 346 | psql.cursor.return_value = cursor_mock |
324 | 347 | ||
325 | with self.subTest(user=None): | 348 | with self.subTest(user=None): |
326 | rows = list(main.fetch_markets(None)) | 349 | rows = list(main.fetch_markets(None)) |
327 | 350 | ||
328 | cursor_mock.execute.assert_called_once_with("SELECT id,config,user_id FROM market_configs WHERE status='enabled'") | 351 | cursor_mock.execute.assert_called_once_with("SELECT id,config,user_id,portfolio_profile FROM market_configs_augmented WHERE status='enabled'") |
329 | 352 | ||
330 | self.assertEqual(["row_1", "row_2"], rows) | 353 | self.assertEqual([ |
354 | (1, 'cfg', 1, {'liquidity': 'high'}), | ||
355 | (2, 'cfg2', 3, {'liquidity': 'medium'}) | ||
356 | ], rows) | ||
331 | 357 | ||
332 | cursor_mock.execute.reset_mock() | 358 | cursor_mock.execute.reset_mock() |
333 | with self.subTest(user=1): | 359 | with self.subTest(user=1): |
334 | rows = list(main.fetch_markets(1)) | 360 | rows = list(main.fetch_markets(1)) |
335 | 361 | ||
336 | cursor_mock.execute.assert_called_once_with("SELECT id,config,user_id FROM market_configs WHERE status='enabled' AND user_id = %s", [1]) | 362 | cursor_mock.execute.assert_called_once_with("SELECT id,config,user_id,portfolio_profile FROM market_configs_augmented WHERE status='enabled' AND user_id = %s", [1]) |
337 | 363 | ||
338 | self.assertEqual(["row_1", "row_2"], rows) | 364 | self.assertEqual([ |
365 | (1, 'cfg', 1, {'liquidity': 'high'}), | ||
366 | (2, 'cfg2', 3, {'liquidity': 'medium'}) | ||
367 | ], rows) | ||
339 | 368 | ||
340 | 369 | ||
diff --git a/tests/test_market.py b/tests/test_market.py index c029686..aeb9f8e 100644 --- a/tests/test_market.py +++ b/tests/test_market.py | |||
@@ -1126,12 +1126,13 @@ class ProcessorTest(WebMockTestCase): | |||
1126 | method_mock = mock.Mock() | 1126 | method_mock = mock.Mock() |
1127 | method_arguments.return_value = [ | 1127 | method_arguments.return_value = [ |
1128 | method_mock, | 1128 | method_mock, |
1129 | ["foo2", "foo"] | 1129 | ["foo2", "foo", "foo3"] |
1130 | ] | 1130 | ] |
1131 | self.m.options = { "foo3": "coucou"} | ||
1131 | method, args = processor.parse_args("action", {"foo": "bar", "foo2": "bar"}, {"foo": "bar2", "bla": "bla"}) | 1132 | method, args = processor.parse_args("action", {"foo": "bar", "foo2": "bar"}, {"foo": "bar2", "bla": "bla"}) |
1132 | 1133 | ||
1133 | self.assertEqual(method_mock, method) | 1134 | self.assertEqual(method_mock, method) |
1134 | self.assertEqual({"foo": "bar2", "foo2": "bar"}, args) | 1135 | self.assertEqual({"foo": "bar2", "foo2": "bar", "foo3": "coucou"}, args) |
1135 | 1136 | ||
1136 | with mock.patch.object(processor, "method_arguments") as method_arguments: | 1137 | with mock.patch.object(processor, "method_arguments") as method_arguments: |
1137 | method_mock = mock.Mock() | 1138 | method_mock = mock.Mock() |