Beispiel #1
0
    def test_save_creates_directory(self, tmp_path):
        path = tmp_path / "some" / "dir" / "doc.json"
        assert not path.exists()

        doc = FileMappedDocument(path)
        doc.save()
        assert path.exists()
Beispiel #2
0
    def test_save_with_directory_fails(self, tmp_path):
        path = tmp_path / "somedir"
        doc = FileMappedDocument(path)

        path.mkdir()
        with pytest.raises(IsADirectoryError):
            doc.save()
Beispiel #3
0
    def test_save_sets_mod(self, tmp_path):
        path = tmp_path / "doc.json"
        doc = FileMappedDocument(path, mode=0o600)
        doc.save()

        mode = path.stat().st_mode & 0o777
        assert oct(mode) == oct(0o600)
Beispiel #4
0
    def test_load_merges_content(self, tmp_path):
        path = tmp_path / "doc.json"
        # save state
        doc = FileMappedDocument(path)
        doc["number"] = 42
        doc["nested"] = {"foo": "bar"}
        doc.save()

        # save state from elsewhere
        doc1 = FileMappedDocument(path)
        doc1["another"] = 420
        doc1["nested"]["baz"] = "ed"
        doc1.save()

        # load state
        doc.load()
        assert doc == {"number": 42, "another": 420, "nested": {"foo": "bar", "baz": "ed"}}
Beispiel #5
0
    def test_save_and_load(self, tmp_path):
        path = tmp_path / "doc.json"
        doc = FileMappedDocument(path)

        doc["number"] = 42
        doc["nested"] = {"foo": "bar"}

        doc.save()

        doc = FileMappedDocument(path)
        doc.load()

        assert doc == {"number": 42, "nested": {"foo": "bar"}}
Beispiel #6
0
def get_machine_id() -> str:
    cache_path = os.path.join(config.dirs.cache, "machine.json")
    doc = FileMappedDocument(cache_path)

    if "machine_id" not in doc:
        # generate a machine id
        doc["machine_id"] = _generate_machine_id()
        # try to cache the machine ID
        call_safe(doc.save)

    return doc["machine_id"]
Beispiel #7
0
    def test_load_with_directory_fails(self, tmp_path):
        path = tmp_path / "somedir"
        path.mkdir()

        with pytest.raises(IsADirectoryError):
            FileMappedDocument(path)
Beispiel #8
0
 def test_load_without_file_succeeds(self, tmp_path):
     path = tmp_path / "doc.json"
     doc = FileMappedDocument(path)
     assert doc == {}