def test_self_check_state_key_uses_sys_prefix(monkeypatch): key = "helloworld" monkeypatch.setattr(sys, "prefix", key) state = self_outdated_check.SelfCheckState("") assert state.key == key
def test_self_check_state_writes_expected_statefile( monkeypatch: pytest.MonkeyPatch, tmpdir: Path) -> None: cache_dir = tmpdir / "cache_dir" cache_dir.mkdir() key = "helloworld" statefile_path = _get_statefile_path(str(cache_dir), key) last_check = datetime.datetime.strptime( "1970-01-02T11:00:00Z", self_outdated_check.SELFCHECK_DATE_FMT) pypi_version = "1.0" monkeypatch.setattr(sys, "prefix", key) state = self_outdated_check.SelfCheckState(str(cache_dir)) state.save(pypi_version, last_check) with open(statefile_path) as f: saved = json.load(f) expected = { "key": key, "last_check": last_check.strftime(self_outdated_check.SELFCHECK_DATE_FMT), "pypi_version": pypi_version, } assert expected == saved
def test_self_check_state_reads_expected_statefile( monkeypatch: pytest.MonkeyPatch, tmpdir: Path) -> None: cache_dir = tmpdir / "cache_dir" cache_dir.mkdir() key = "helloworld" statefile_path = _get_statefile_path(str(cache_dir), key) last_check = "1970-01-02T11:00:00Z" pypi_version = "1.0" content = { "key": key, "last_check": last_check, "pypi_version": pypi_version, } Path(statefile_path).parent.mkdir() with open(statefile_path, "w") as f: json.dump(content, f) monkeypatch.setattr(sys, "prefix", key) state = self_outdated_check.SelfCheckState(str(cache_dir)) assert state.state["last_check"] == last_check assert state.state["pypi_version"] == pypi_version
def test_reads_expected_statefile(self, tmpdir: Path) -> None: # GIVEN cache_dir = tmpdir / "cache_dir" expected_path = (cache_dir / "selfcheck" / self_outdated_check._get_statefile_name(sys.prefix)) cache_dir.mkdir() (cache_dir / "selfcheck").mkdir() expected_path.write_text('{"foo": "bar"}') # WHEN state = self_outdated_check.SelfCheckState(cache_dir=str(cache_dir)) # THEN assert state._statefile_path == expected_path assert state._state == {"foo": "bar"}
def test_writes_expected_statefile(self, tmpdir: Path) -> None: # GIVEN cache_dir = tmpdir / "cache_dir" cache_dir.mkdir() expected_path = (cache_dir / "selfcheck" / self_outdated_check._get_statefile_name(sys.prefix)) # WHEN state = self_outdated_check.SelfCheckState(cache_dir=str(cache_dir)) state.set("1.0.0", datetime.datetime(2000, 1, 1, 0, 0, 0)) # THEN assert state._statefile_path == expected_path contents = expected_path.read_text() assert json.loads(contents) == { "key": sys.prefix, "last_check": "2000-01-01T00:00:00Z", "pypi_version": "1.0.0", }
def test_no_cache(self) -> None: # GIVEN / WHEN state = self_outdated_check.SelfCheckState(cache_dir="") assert state._statefile_path is None