|
| 1 | +"""HDXState Utility Tests""" |
| 2 | + |
| 3 | +import json |
| 4 | +from copy import deepcopy |
| 5 | +from datetime import datetime, timezone |
| 6 | +from os import makedirs |
| 7 | +from os.path import exists, join |
| 8 | +from shutil import copyfile, rmtree |
| 9 | +from tempfile import gettempdir |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +from tests.hdx.data import MockResponse, dataset_resultdict |
| 14 | +from tests.hdx.data.test_resource import resultdict |
| 15 | + |
| 16 | +from hdx.api.configuration import Configuration |
| 17 | +from hdx.api.utilities.hdx_state import HDXState |
| 18 | +from hdx.utilities.dateparse import iso_string_from_datetime, parse_date |
| 19 | + |
| 20 | + |
| 21 | +class TestState: |
| 22 | + @pytest.fixture(scope="class") |
| 23 | + def tempfolder(self): |
| 24 | + return join(gettempdir(), "test_state") |
| 25 | + |
| 26 | + @pytest.fixture(scope="class") |
| 27 | + def statefolder(self, fixturesfolder): |
| 28 | + return join(fixturesfolder, "state") |
| 29 | + |
| 30 | + @pytest.fixture(scope="class") |
| 31 | + def statefile(self): |
| 32 | + return "last_build_date.txt" |
| 33 | + |
| 34 | + @pytest.fixture(scope="class") |
| 35 | + def multidatestatefile(self): |
| 36 | + return "analysis_dates.txt" |
| 37 | + |
| 38 | + @pytest.fixture(scope="class") |
| 39 | + def date1(self): |
| 40 | + return datetime(2020, 9, 23, 0, 0, tzinfo=timezone.utc) |
| 41 | + |
| 42 | + @pytest.fixture(scope="class") |
| 43 | + def date2(self): |
| 44 | + return datetime(2022, 5, 12, 10, 15, tzinfo=timezone.utc) |
| 45 | + |
| 46 | + @pytest.fixture(scope="function") |
| 47 | + def do_state(self, tempfolder, statefile): |
| 48 | + class MockSession: |
| 49 | + @staticmethod |
| 50 | + def post(url, data, headers, files, allow_redirects, auth=None): |
| 51 | + if "resource" in url: |
| 52 | + result = json.dumps(resultdict) |
| 53 | + return MockResponse( |
| 54 | + 200, |
| 55 | + '{"success": true, "result": %s, "help": "http://test-data.humdata.org/api/3/action/help_show?name=resource_show"}' |
| 56 | + % result, |
| 57 | + ) |
| 58 | + else: |
| 59 | + myresultdict = deepcopy(dataset_resultdict) |
| 60 | + resource = myresultdict["resources"][0] |
| 61 | + resource["name"] = statefile |
| 62 | + resource["url"] = join(tempfolder, statefile) |
| 63 | + result = json.dumps(myresultdict) |
| 64 | + return MockResponse( |
| 65 | + 200, |
| 66 | + '{"success": true, "result": %s, "help": "http://test-data.humdata.org/api/3/action/help_show?name=package_show"}' |
| 67 | + % result, |
| 68 | + ) |
| 69 | + |
| 70 | + Configuration.read().remoteckan().session = MockSession() |
| 71 | + |
| 72 | + @pytest.fixture(scope="function") |
| 73 | + def do_state_multi(self, tempfolder, multidatestatefile): |
| 74 | + class MockSession: |
| 75 | + @staticmethod |
| 76 | + def post(url, data, headers, files, allow_redirects, auth=None): |
| 77 | + if "resource" in url: |
| 78 | + result = json.dumps(resultdict) |
| 79 | + return MockResponse( |
| 80 | + 200, |
| 81 | + '{"success": true, "result": %s, "help": "http://test-data.humdata.org/api/3/action/help_show?name=resource_show"}' |
| 82 | + % result, |
| 83 | + ) |
| 84 | + else: |
| 85 | + myresultdict = deepcopy(dataset_resultdict) |
| 86 | + resource = myresultdict["resources"][0] |
| 87 | + resource["name"] = multidatestatefile |
| 88 | + resource["url"] = join(tempfolder, multidatestatefile) |
| 89 | + result = json.dumps(myresultdict) |
| 90 | + return MockResponse( |
| 91 | + 200, |
| 92 | + '{"success": true, "result": %s, "help": "http://test-data.humdata.org/api/3/action/help_show?name=package_show"}' |
| 93 | + % result, |
| 94 | + ) |
| 95 | + |
| 96 | + Configuration.read().remoteckan().session = MockSession() |
| 97 | + |
| 98 | + def test_state( |
| 99 | + self, tempfolder, statefolder, statefile, date1, date2, configuration, do_state |
| 100 | + ): |
| 101 | + if not exists(tempfolder): |
| 102 | + makedirs(tempfolder) |
| 103 | + statepath = join(tempfolder, statefile) |
| 104 | + copyfile(join(statefolder, statefile), statepath) |
| 105 | + with HDXState( |
| 106 | + "test_dataset", tempfolder, parse_date, iso_string_from_datetime |
| 107 | + ) as state: |
| 108 | + assert state.get() == date1 |
| 109 | + with HDXState( |
| 110 | + "test_dataset", tempfolder, parse_date, iso_string_from_datetime |
| 111 | + ) as state: |
| 112 | + assert state.get() == date1 |
| 113 | + state.set(date2) |
| 114 | + with HDXState( |
| 115 | + "test_dataset", tempfolder, parse_date, iso_string_from_datetime |
| 116 | + ) as state: |
| 117 | + assert state.get() == date2.replace(hour=0, minute=0) |
| 118 | + rmtree(tempfolder) |
| 119 | + |
| 120 | + def test_multi_date_state( |
| 121 | + self, |
| 122 | + tempfolder, |
| 123 | + statefolder, |
| 124 | + multidatestatefile, |
| 125 | + date1, |
| 126 | + date2, |
| 127 | + configuration, |
| 128 | + do_state_multi, |
| 129 | + ): |
| 130 | + if not exists(tempfolder): |
| 131 | + makedirs(tempfolder) |
| 132 | + statepath = join(tempfolder, multidatestatefile) |
| 133 | + copyfile(join(statefolder, multidatestatefile), statepath) |
| 134 | + with HDXState( |
| 135 | + "test_dataset", |
| 136 | + tempfolder, |
| 137 | + HDXState.dates_str_to_country_date_dict, |
| 138 | + HDXState.country_date_dict_to_dates_str, |
| 139 | + ) as state: |
| 140 | + state_dict = state.get() |
| 141 | + assert state_dict == {"DEFAULT": date1} |
| 142 | + with HDXState( |
| 143 | + "test_dataset", |
| 144 | + tempfolder, |
| 145 | + HDXState.dates_str_to_country_date_dict, |
| 146 | + HDXState.country_date_dict_to_dates_str, |
| 147 | + ) as state: |
| 148 | + state_dict = state.get() |
| 149 | + assert state_dict == {"DEFAULT": date1} |
| 150 | + state_dict["AFG"] = date2 |
| 151 | + state.set(state_dict) |
| 152 | + with HDXState( |
| 153 | + "test_dataset", |
| 154 | + tempfolder, |
| 155 | + HDXState.dates_str_to_country_date_dict, |
| 156 | + HDXState.country_date_dict_to_dates_str, |
| 157 | + ) as state: |
| 158 | + state_dict = state.get() |
| 159 | + assert state_dict == { |
| 160 | + "DEFAULT": date1, |
| 161 | + "AFG": date2.replace(hour=0, minute=0), |
| 162 | + } |
| 163 | + rmtree(tempfolder) |
0 commit comments