|
| 1 | +from unittest import TestCase |
| 2 | +from unittest.mock import Mock, patch |
| 3 | + |
| 4 | +from requests.models import Response |
| 5 | + |
| 6 | +from ergani.client import ErganiClient |
| 7 | +from ergani.models import BusinessBranch |
| 8 | + |
| 9 | +SINGLE_BRANCH_PAYLOAD = { |
| 10 | + "Aa": "0", |
| 11 | + "Address": "ΠΛΑΤΕΙΑ ΑΓ. ΘΕΟΔΩΡΩΝ 6 10561 ΑΘΗΝΑ", |
| 12 | + "YpiresiaSepe": "11010", |
| 13 | + "YpiresiaOaed": "101201", |
| 14 | + "Kad": "6210", |
| 15 | + "Kallikratis": "91860101", |
| 16 | + "StatusDescription": "Έναρξη απασχόλησης", |
| 17 | +} |
| 18 | + |
| 19 | +BRANCH_PAYLOAD = { |
| 20 | + "EX_BASE_02": { |
| 21 | + "Pararthma": [ |
| 22 | + { |
| 23 | + "Aa": "0", |
| 24 | + "Address": "ΑΓ. ΜΑΡΙΝΑΣ 19400 ΚΟΡΩΠΙ", |
| 25 | + }, |
| 26 | + { |
| 27 | + "Aa": "1", |
| 28 | + "Address": "ΛΕΒΙΔΟΥ 14562 ΚΗΦΙΣΙΑ", |
| 29 | + }, |
| 30 | + ] |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +SINGLE_BRANCH_RAW_PAYLOAD = { |
| 35 | + "Aa": "0", |
| 36 | + "Address": "ΠΛΑΤΕΙΑ ΑΓ. ΘΕΟΔΩΡΩΝ 6 10561 ΑΘΗΝΑ", |
| 37 | + "YpiresiaSepe": "11010", |
| 38 | + "YpiresiaOaed": "101201", |
| 39 | + "Kad": "6210", |
| 40 | + "Kallikratis": "91860101", |
| 41 | + "StatusDescription": "Έναρξη απασχόλησης", |
| 42 | +} |
| 43 | + |
| 44 | +BRANCH_RAW_PAYLOADS = [ |
| 45 | + { |
| 46 | + "Aa": "0", |
| 47 | + "Address": "ΑΓ. ΜΑΡΙΝΑΣ 19400 ΚΟΡΩΠΙ", |
| 48 | + }, |
| 49 | + { |
| 50 | + "Aa": "1", |
| 51 | + "Address": "ΛΕΒΙΔΟΥ 14562 ΚΗΦΙΣΙΑ", |
| 52 | + }, |
| 53 | +] |
| 54 | + |
| 55 | + |
| 56 | +class BusinessBranchTests(TestCase): |
| 57 | + def test_business_branch_parse_reads_single_branch_payload(self) -> None: |
| 58 | + self.assertEqual( |
| 59 | + BusinessBranch.parse(SINGLE_BRANCH_PAYLOAD), |
| 60 | + BusinessBranch( |
| 61 | + branch_number=0, |
| 62 | + address="ΠΛΑΤΕΙΑ ΑΓ. ΘΕΟΔΩΡΩΝ 6 10561 ΑΘΗΝΑ", |
| 63 | + sepe_service_code="11010", |
| 64 | + oaed_service_code="101201", |
| 65 | + business_branch_activity_code="6210", |
| 66 | + kallikratis_municipal_code="91860101", |
| 67 | + status_description="Έναρξη απασχόλησης", |
| 68 | + raw_payload=SINGLE_BRANCH_RAW_PAYLOAD, |
| 69 | + ), |
| 70 | + ) |
| 71 | + |
| 72 | + def test_business_branch_parse_many_reads_wrapped_response(self) -> None: |
| 73 | + self.assertEqual( |
| 74 | + BusinessBranch.parse_many(BRANCH_PAYLOAD), |
| 75 | + [ |
| 76 | + BusinessBranch( |
| 77 | + branch_number=0, |
| 78 | + address="ΑΓ. ΜΑΡΙΝΑΣ 19400 ΚΟΡΩΠΙ", |
| 79 | + sepe_service_code=None, |
| 80 | + oaed_service_code=None, |
| 81 | + business_branch_activity_code=None, |
| 82 | + kallikratis_municipal_code=None, |
| 83 | + status_description=None, |
| 84 | + raw_payload=BRANCH_RAW_PAYLOADS[0], |
| 85 | + ), |
| 86 | + BusinessBranch( |
| 87 | + branch_number=1, |
| 88 | + address="ΛΕΒΙΔΟΥ 14562 ΚΗΦΙΣΙΑ", |
| 89 | + sepe_service_code=None, |
| 90 | + oaed_service_code=None, |
| 91 | + business_branch_activity_code=None, |
| 92 | + kallikratis_municipal_code=None, |
| 93 | + status_description=None, |
| 94 | + raw_payload=BRANCH_RAW_PAYLOADS[1], |
| 95 | + ), |
| 96 | + ], |
| 97 | + ) |
| 98 | + |
| 99 | + def test_business_branch_parse_requires_object_payload(self) -> None: |
| 100 | + with self.assertRaises(ValueError): |
| 101 | + BusinessBranch.parse("invalid") |
| 102 | + |
| 103 | + def test_business_branch_parse_many_accepts_single_branch_payload(self) -> None: |
| 104 | + self.assertEqual( |
| 105 | + BusinessBranch.parse_many( |
| 106 | + {"EX_BASE_02": {"Pararthma": SINGLE_BRANCH_PAYLOAD}} |
| 107 | + ), |
| 108 | + [ |
| 109 | + BusinessBranch( |
| 110 | + branch_number=0, |
| 111 | + address="ΠΛΑΤΕΙΑ ΑΓ. ΘΕΟΔΩΡΩΝ 6 10561 ΑΘΗΝΑ", |
| 112 | + sepe_service_code="11010", |
| 113 | + oaed_service_code="101201", |
| 114 | + business_branch_activity_code="6210", |
| 115 | + kallikratis_municipal_code="91860101", |
| 116 | + status_description="Έναρξη απασχόλησης", |
| 117 | + raw_payload=SINGLE_BRANCH_RAW_PAYLOAD, |
| 118 | + ) |
| 119 | + ], |
| 120 | + ) |
| 121 | + |
| 122 | + def test_get_branch_details_uses_execute_service_and_parses_wrapped_response( |
| 123 | + self, |
| 124 | + ) -> None: |
| 125 | + client = ErganiClient("username", "password", "https://example.test") |
| 126 | + response = Mock(spec=Response) |
| 127 | + response.json.return_value = BRANCH_PAYLOAD |
| 128 | + |
| 129 | + with patch.object( |
| 130 | + client, "_execute_service", return_value=response |
| 131 | + ) as execute_service_mock: |
| 132 | + result = client.get_branch_details() |
| 133 | + |
| 134 | + self.assertEqual( |
| 135 | + result, |
| 136 | + [ |
| 137 | + BusinessBranch( |
| 138 | + branch_number=0, |
| 139 | + address="ΑΓ. ΜΑΡΙΝΑΣ 19400 ΚΟΡΩΠΙ", |
| 140 | + sepe_service_code=None, |
| 141 | + oaed_service_code=None, |
| 142 | + business_branch_activity_code=None, |
| 143 | + kallikratis_municipal_code=None, |
| 144 | + status_description=None, |
| 145 | + raw_payload=BRANCH_RAW_PAYLOADS[0], |
| 146 | + ), |
| 147 | + BusinessBranch( |
| 148 | + branch_number=1, |
| 149 | + address="ΛΕΒΙΔΟΥ 14562 ΚΗΦΙΣΙΑ", |
| 150 | + sepe_service_code=None, |
| 151 | + oaed_service_code=None, |
| 152 | + business_branch_activity_code=None, |
| 153 | + kallikratis_municipal_code=None, |
| 154 | + status_description=None, |
| 155 | + raw_payload=BRANCH_RAW_PAYLOADS[1], |
| 156 | + ), |
| 157 | + ], |
| 158 | + ) |
| 159 | + execute_service_mock.assert_called_once_with("EX_BASE_02") |
| 160 | + |
| 161 | + def test_get_branch_details_returns_empty_list_for_no_content(self) -> None: |
| 162 | + client = ErganiClient("username", "password", "https://example.test") |
| 163 | + |
| 164 | + with patch.object( |
| 165 | + client, "_execute_service", return_value=None |
| 166 | + ) as execute_service_mock: |
| 167 | + result = client.get_branch_details() |
| 168 | + |
| 169 | + self.assertEqual(result, []) |
| 170 | + execute_service_mock.assert_called_once_with("EX_BASE_02") |
0 commit comments