|
| 1 | +from unittest import TestCase, mock |
| 2 | +from rest_framework.test import APIClient |
| 3 | + |
| 4 | +from api.core.serializers import strategy_id |
| 5 | + |
| 6 | + |
| 7 | +class TestStrategy(object): |
| 8 | + |
| 9 | + def __init__(self, name, classifier): |
| 10 | + self.name = name |
| 11 | + self.classifier = classifier |
| 12 | + |
| 13 | + def init_params(self): |
| 14 | + return {} |
| 15 | + |
| 16 | + |
| 17 | +class TestStrategyView(TestCase): |
| 18 | + strategies = [ |
| 19 | + TestStrategy('Test One', {'stochastic': True, 'memory_depth': 1}), |
| 20 | + TestStrategy('Test Two', {'stochastic': True, 'memory_depth': 1}), |
| 21 | + TestStrategy('Test Three', {'stochastic': True, 'memory_depth': 2}), |
| 22 | + TestStrategy('Test Four', {'stochastic': False, 'memory_depth': 2}), |
| 23 | + TestStrategy('Test Five', {'stochastic': False, 'memory_depth': 1}), |
| 24 | + ] |
| 25 | + strategies_index = {strategy_id(s): s for s in strategies} |
| 26 | + |
| 27 | + def setUp(self): |
| 28 | + self.client = APIClient() |
| 29 | + |
| 30 | + @mock.patch('api.core.views.axl.filtered_strategies') |
| 31 | + def test_lists_all_strategies(self, filtered_strategies): |
| 32 | + filtered_strategies.return_value = self.strategies |
| 33 | + response = self.client.get('/strategies/') |
| 34 | + self.assertEqual(200, response.status_code) |
| 35 | + self.assertEqual(5, len(response.data)) |
| 36 | + |
| 37 | + @mock.patch('api.core.views.StrategyViewSet.strategies_index', strategies_index) |
| 38 | + def test_retrieves_one_strategy(self): |
| 39 | + response = self.client.get('/strategies/testone/') |
| 40 | + self.assertEqual(200, response.status_code) |
| 41 | + self.assertEqual('Test One', response.data['name']) |
| 42 | + |
| 43 | + @mock.patch('api.core.views.StrategyViewSet.strategies_index', strategies_index) |
| 44 | + def test_strategy_does_not_exist(self): |
| 45 | + response = self.client.get('/strategies/notfound/') |
| 46 | + self.assertEqual(404, response.status_code) |
| 47 | + |
| 48 | + |
| 49 | + |
0 commit comments