|
| 1 | +import pytest |
| 2 | +import responses |
| 3 | + |
| 4 | +from verda.exceptions import APIException |
| 5 | +from verda.long_term import LongTermPeriod, LongTermService |
| 6 | + |
| 7 | +INVALID_REQUEST = 'invalid_request' |
| 8 | +INVALID_REQUEST_MESSAGE = 'Bad request' |
| 9 | + |
| 10 | +PERIOD_1 = { |
| 11 | + 'code': '3_MONTHS', |
| 12 | + 'name': '3 months', |
| 13 | + 'is_enabled': True, |
| 14 | + 'unit_name': 'month', |
| 15 | + 'unit_value': 3.0, |
| 16 | + 'discount_percentage': 14.0, |
| 17 | +} |
| 18 | + |
| 19 | +PERIOD_2 = { |
| 20 | + 'code': '6_MONTHS', |
| 21 | + 'name': '6 months', |
| 22 | + 'is_enabled': True, |
| 23 | + 'unit_name': 'month', |
| 24 | + 'unit_value': 6.0, |
| 25 | + 'discount_percentage': 20.0, |
| 26 | +} |
| 27 | + |
| 28 | +PAYLOAD = [PERIOD_1, PERIOD_2] |
| 29 | + |
| 30 | + |
| 31 | +class TestLongTermService: |
| 32 | + @pytest.fixture |
| 33 | + def long_term_service(self, http_client): |
| 34 | + return LongTermService(http_client) |
| 35 | + |
| 36 | + @pytest.fixture |
| 37 | + def endpoint(self, http_client): |
| 38 | + return http_client._base_url + '/long-term/periods' |
| 39 | + |
| 40 | + def test_get_cluster_periods(self, long_term_service, endpoint): |
| 41 | + # arrange |
| 42 | + responses.add(responses.GET, endpoint + '/clusters', json=PAYLOAD, status=200) |
| 43 | + |
| 44 | + # act |
| 45 | + periods = long_term_service.get_cluster_periods() |
| 46 | + |
| 47 | + # assert |
| 48 | + assert isinstance(periods, list) |
| 49 | + assert len(periods) == 2 |
| 50 | + assert isinstance(periods[0], LongTermPeriod) |
| 51 | + assert periods[0].code == '3_MONTHS' |
| 52 | + assert periods[0].name == '3 months' |
| 53 | + assert periods[0].is_enabled is True |
| 54 | + assert periods[0].unit_name == 'month' |
| 55 | + assert periods[0].unit_value == 3.0 |
| 56 | + assert periods[0].discount_percentage == 14.0 |
| 57 | + assert periods[1].code == '6_MONTHS' |
| 58 | + assert responses.assert_call_count(endpoint + '/clusters', 1) is True |
| 59 | + |
| 60 | + def test_get_cluster_periods_failed(self, long_term_service, endpoint): |
| 61 | + # arrange |
| 62 | + url = endpoint + '/clusters' |
| 63 | + responses.add( |
| 64 | + responses.GET, |
| 65 | + url, |
| 66 | + json={'code': INVALID_REQUEST, 'message': INVALID_REQUEST_MESSAGE}, |
| 67 | + status=400, |
| 68 | + ) |
| 69 | + |
| 70 | + # act + assert |
| 71 | + with pytest.raises(APIException) as excinfo: |
| 72 | + long_term_service.get_cluster_periods() |
| 73 | + |
| 74 | + assert excinfo.value.code == INVALID_REQUEST |
| 75 | + assert excinfo.value.message == INVALID_REQUEST_MESSAGE |
| 76 | + assert responses.assert_call_count(url, 1) is True |
| 77 | + |
| 78 | + def test_get_instance_periods(self, long_term_service, endpoint): |
| 79 | + # arrange |
| 80 | + responses.add(responses.GET, endpoint + '/instances', json=PAYLOAD, status=200) |
| 81 | + |
| 82 | + # act |
| 83 | + periods = long_term_service.get_instance_periods() |
| 84 | + |
| 85 | + # assert |
| 86 | + assert isinstance(periods, list) |
| 87 | + assert len(periods) == 2 |
| 88 | + assert isinstance(periods[0], LongTermPeriod) |
| 89 | + assert periods[0].code == '3_MONTHS' |
| 90 | + assert periods[0].discount_percentage == 14.0 |
| 91 | + assert periods[1].unit_value == 6.0 |
| 92 | + assert responses.assert_call_count(endpoint + '/instances', 1) is True |
| 93 | + |
| 94 | + def test_get_instance_periods_failed(self, long_term_service, endpoint): |
| 95 | + # arrange |
| 96 | + url = endpoint + '/instances' |
| 97 | + responses.add( |
| 98 | + responses.GET, |
| 99 | + url, |
| 100 | + json={'code': INVALID_REQUEST, 'message': INVALID_REQUEST_MESSAGE}, |
| 101 | + status=400, |
| 102 | + ) |
| 103 | + |
| 104 | + # act + assert |
| 105 | + with pytest.raises(APIException) as excinfo: |
| 106 | + long_term_service.get_instance_periods() |
| 107 | + |
| 108 | + assert excinfo.value.code == INVALID_REQUEST |
| 109 | + assert excinfo.value.message == INVALID_REQUEST_MESSAGE |
| 110 | + assert responses.assert_call_count(url, 1) is True |
| 111 | + |
| 112 | + def test_get_cluster_periods_empty_list(self, long_term_service, endpoint): |
| 113 | + # arrange |
| 114 | + responses.add(responses.GET, endpoint + '/clusters', json=[], status=200) |
| 115 | + |
| 116 | + # act |
| 117 | + periods = long_term_service.get_cluster_periods() |
| 118 | + |
| 119 | + # assert |
| 120 | + assert isinstance(periods, list) |
| 121 | + assert len(periods) == 0 |
0 commit comments