|
| 1 | +from unittest.mock import Mock, call |
| 2 | + |
| 3 | +from tests.testcase import TestCase |
| 4 | +from hypernode_api_python.client import ( |
| 5 | + HypernodeAPIPython, |
| 6 | +) |
| 7 | + |
| 8 | + |
| 9 | +class TestGetAllFlows(TestCase): |
| 10 | + def setUp(self): |
| 11 | + self.client = HypernodeAPIPython(token="mytoken") |
| 12 | + self.mock_request = Mock() |
| 13 | + self.client.requests = self.mock_request |
| 14 | + self.flow1 = { |
| 15 | + "uuid": "e1db2b60-882d-4b43-8910-ce6d38ca5393", |
| 16 | + "state": None, |
| 17 | + "name": "create_backup", |
| 18 | + "created_at": "2023-03-05T14:13:21Z", |
| 19 | + "updated_at": None, |
| 20 | + "progress": {"running": [], "total": 0, "completed": 0}, |
| 21 | + "logbook": "my_app", |
| 22 | + "tracker": {"uuid": None, "description": None}, |
| 23 | + } |
| 24 | + self.flow2 = { |
| 25 | + "uuid": "03bd6e10-5493-4ee8-92fc-cc429faebead", |
| 26 | + "state": None, |
| 27 | + "name": "update_node", |
| 28 | + "created_at": "2023-03-05T14:01:56Z", |
| 29 | + "updated_at": None, |
| 30 | + "progress": {"running": [], "total": 0, "completed": 0}, |
| 31 | + "logbook": "my_app", |
| 32 | + "tracker": { |
| 33 | + "uuid": "0dd83d83-6b9b-4fb5-9665-79fcf8235069", |
| 34 | + "description": None, |
| 35 | + }, |
| 36 | + } |
| 37 | + |
| 38 | + def test_get_all_flows_returns_flows_if_only_one_page(self): |
| 39 | + self.mock_request.return_value.json.return_value = { |
| 40 | + "count": 2, |
| 41 | + "next": None, |
| 42 | + "previous": None, |
| 43 | + "results": [self.flow1, self.flow2], |
| 44 | + } |
| 45 | + |
| 46 | + ret = self.client.get_all_flows("my_app") |
| 47 | + |
| 48 | + expected_calls = [ |
| 49 | + call("GET", "/logbook/v1/logbooks/my_app/flows"), |
| 50 | + call().json(), |
| 51 | + ] |
| 52 | + self.assertEqual(expected_calls, self.mock_request.mock_calls) |
| 53 | + expected_results = [self.flow1, self.flow2] |
| 54 | + self.assertEqual(expected_results, ret) |
| 55 | + |
| 56 | + def test_get_all_flows_returns_flows_if_only_one_page_but_limited_results_requested( |
| 57 | + self, |
| 58 | + ): |
| 59 | + self.mock_request.return_value.json.return_value = { |
| 60 | + "count": 2, |
| 61 | + "next": None, |
| 62 | + "previous": None, |
| 63 | + "results": [self.flow1, self.flow2], |
| 64 | + } |
| 65 | + |
| 66 | + ret = self.client.get_all_flows("my_app", limit=1) |
| 67 | + |
| 68 | + expected_calls = [ |
| 69 | + call("GET", "/logbook/v1/logbooks/my_app/flows"), |
| 70 | + call().json(), |
| 71 | + ] |
| 72 | + self.assertEqual(expected_calls, self.mock_request.mock_calls) |
| 73 | + expected_results = [self.flow1] |
| 74 | + self.assertEqual(expected_results, ret) |
| 75 | + |
| 76 | + def test_get_all_flows_returns_flows_if_more_than_one_page(self): |
| 77 | + self.mock_request.return_value.json.side_effect = [ |
| 78 | + { |
| 79 | + "count": 101, |
| 80 | + "next": "https://api.hypernode.com/logbook/v1/logbooks/my_app/flows/?limit=50&offset=50", |
| 81 | + "previous": None, |
| 82 | + "results": [self.flow1, self.flow2] * 25, |
| 83 | + }, |
| 84 | + { |
| 85 | + "count": 101, |
| 86 | + "next": "https://api.hypernode.com/logbook/v1/logbooks/my_app/flows/?limit=50&offset=100", |
| 87 | + "previous": "https://api.hypernode.com/logbook/v1/logbooks/my_app/flows/?limit=50&offset=50", |
| 88 | + "results": [self.flow1, self.flow2] * 25, |
| 89 | + }, |
| 90 | + { |
| 91 | + "count": 101, |
| 92 | + "next": None, |
| 93 | + "previous": "https://api.hypernode.com/logbook/v1/logbooks/my_app/flows/?limit=50&offset=100", |
| 94 | + "results": [self.flow1], |
| 95 | + }, |
| 96 | + ] |
| 97 | + |
| 98 | + ret = self.client.get_all_flows("my_app") |
| 99 | + |
| 100 | + expected_calls = [ |
| 101 | + call("GET", "/logbook/v1/logbooks/my_app/flows"), |
| 102 | + call().json(), |
| 103 | + call("GET", "/logbook/v1/logbooks/my_app/flows/?limit=50&offset=50"), |
| 104 | + call().json(), |
| 105 | + call("GET", "/logbook/v1/logbooks/my_app/flows/?limit=50&offset=100"), |
| 106 | + call().json(), |
| 107 | + ] |
| 108 | + self.assertEqual(expected_calls, self.mock_request.mock_calls) |
| 109 | + expected_results = [self.flow1, self.flow2] * 50 + [self.flow1] |
| 110 | + self.assertEqual(expected_results, ret) |
| 111 | + |
| 112 | + def test_get_all_flows_returns_flows_if_more_than_one_page_but_limited_results_requested( |
| 113 | + self, |
| 114 | + ): |
| 115 | + self.mock_request.return_value.json.side_effect = [ |
| 116 | + { |
| 117 | + "count": 101, |
| 118 | + "next": "https://api.hypernode.com/logbook/v1/logbooks/my_app/flows/?limit=50&offset=50", |
| 119 | + "previous": None, |
| 120 | + "results": [self.flow1, self.flow2] * 25, |
| 121 | + }, |
| 122 | + { |
| 123 | + "count": 101, |
| 124 | + "next": "https://api.hypernode.com/logbook/v1/logbooks/my_app/flows/?limit=50&offset=100", |
| 125 | + "previous": "https://api.hypernode.com/logbook/v1/logbooks/my_app/flows/?limit=50&offset=50", |
| 126 | + "results": [self.flow1, self.flow2] * 25, |
| 127 | + }, |
| 128 | + { |
| 129 | + "count": 101, |
| 130 | + "next": None, |
| 131 | + "previous": "https://api.hypernode.com/logbook/v1/logbooks/my_app/flows/?limit=50&offset=100", |
| 132 | + "results": [self.flow1], |
| 133 | + }, |
| 134 | + ] |
| 135 | + |
| 136 | + ret = self.client.get_all_flows("my_app", limit=51) |
| 137 | + |
| 138 | + expected_calls = [ |
| 139 | + call("GET", "/logbook/v1/logbooks/my_app/flows"), |
| 140 | + call().json(), |
| 141 | + call("GET", "/logbook/v1/logbooks/my_app/flows/?limit=50&offset=50"), |
| 142 | + call().json(), |
| 143 | + ] |
| 144 | + self.assertEqual(expected_calls, self.mock_request.mock_calls) |
| 145 | + expected_results = [self.flow1, self.flow2] * 25 + [self.flow1] |
| 146 | + self.assertEqual(expected_results, ret) |
0 commit comments