コード例 #1
0
def test_database_get_photos_to_collect_same_checksum_same_priority(
        caplog, tmpdir):
    """
    Photos with the same priority and checksum will not be recollected
    """
    caplog.set_level(logging.DEBUG)
    example_database = {
        "version": 1,
        "hash_algorithm": "sha256",
        "photo_db": {
            "uid1": [
                {
                    "checksum": "deadbeef",
                    "source_path": str(tmpdir / "source1" / "a.jpg"),
                    "datetime": "2015:08:27 04:09:36.50",
                    "timestamp": 1440662976.5,
                    "file_size": 1024,
                    "store_path": "a.jpg",
                    "priority": 11,
                },
                {
                    "checksum": "deadbeef",
                    "source_path": str(tmpdir / "source2" / "a.jpg"),
                    "datetime": "2015:08:27 04:09:36.50",
                    "timestamp": 1440662976.5,
                    "file_size": 1024,
                    "store_path": "",
                    "priority": 11,
                },
            ]
        },
        "command_history": {
            "2021-03-08_23-56-00Z": "photomanager create --db test.json"
        },
    }
    os.makedirs(tmpdir / "source1")
    os.makedirs(tmpdir / "source2")
    os.makedirs(tmpdir / "store")
    Path(tmpdir / "source1" / "a.jpg").touch()
    Path(tmpdir / "source2" / "a.jpg").touch()
    Path(tmpdir / "store" / "a.jpg").touch()
    db = Database.from_dict(example_database)
    (
        photos_to_copy,
        (num_copied_photos, num_added_photos, num_missed_photos,
         num_stored_photos),
    ) = db.get_photos_to_collect(tmpdir / "store")
    print(photos_to_copy)
    print(num_copied_photos, num_added_photos, num_missed_photos,
          num_stored_photos)
    assert len(photos_to_copy) == 0
    assert num_copied_photos == 0
    assert num_added_photos == 0
    assert num_missed_photos == 0
    assert num_stored_photos == 2
コード例 #2
0
def test_verify_random_sample(tmpdir, caplog):
    """
    The random_fraction parameter in actions.verify will verify
    the specified fraction of the stored photos
    (rounded to the nearest integer)
    """
    caplog.set_level(logging.DEBUG)
    example_database = {
        "version": 1,
        "hash_algorithm": "sha256",
        "photo_db": {
            "uid1": [
                {
                    "checksum": "deadbeef",
                    "source_path": str(tmpdir / "source1" / "a.jpg"),
                    "datetime": "2015:08:27 04:09:36.50",
                    "timestamp": 1440662976.5,
                    "file_size": 1024,
                    "store_path": "a.jpg",
                    "priority": 11,
                },
            ],
            "uid2": [
                {
                    "checksum": "asdf",
                    "source_path": str(tmpdir / "source2" / "b.jpg"),
                    "datetime": "2015:08:27 04:09:36.50",
                    "timestamp": 1440662976.5,
                    "file_size": 1024,
                    "store_path": "b.jpg",
                    "priority": 11,
                },
            ],
            "uid3": [
                {
                    "checksum": "ffff",
                    "source_path": str(tmpdir / "source1" / "c.jpg"),
                    "datetime": "2015:08:27 04:09:36.50",
                    "timestamp": 1440662976.5,
                    "file_size": 1024,
                    "store_path": "c.jpg",
                    "priority": 11,
                },
            ],
            "uid4": [
                {
                    "checksum": "beef",
                    "source_path": str(tmpdir / "source2" / "d.jpg"),
                    "datetime": "2015:08:27 04:09:36.50",
                    "timestamp": 1440662976.5,
                    "file_size": 1024,
                    "store_path": "d.jpg",
                    "priority": 11,
                },
            ],
        },
        "command_history": {
            "2021-03-08_23-56-00Z": "photomanager create --db test.json"
        },
    }
    os.makedirs(tmpdir / "store")
    db = Database.from_dict(example_database)
    assert len(db.get_stored_photos()) == 4

    result = actions.verify(
        database=db,
        directory=tmpdir / "store",
        random_fraction=0.33,
    )
    print("\nVERIFY 33% (missing photos)")
    print(result)
    assert result["num_correct_photos"] == 0
    assert result["num_incorrect_photos"] == 0
    assert result["num_missing_photos"] == 1

    Path(tmpdir / "store" / "a.jpg").touch()
    Path(tmpdir / "store" / "b.jpg").touch()
    Path(tmpdir / "store" / "c.jpg").touch()
    Path(tmpdir / "store" / "d.jpg").touch()
    result = actions.verify(
        database=db,
        directory=tmpdir / "store",
        random_fraction=0.5,
    )
    print("\nVERIFY 50% (incorrect photos)")
    print(result)
    assert result["num_correct_photos"] == 0
    assert result["num_incorrect_photos"] == 2
    assert result["num_missing_photos"] == 0
コード例 #3
0
def test_database_load_version_1():
    json_data = b"""{
"version": 1,
"hash_algorithm": "sha256",
"photo_db": {
    "d239210f00534b76a2b215e073f75832": [
        {
            "checksum": "deadbeef",
            "source_path": "/a/b/c.jpg",
            "datetime": "2015:08:27 04:09:36.50",
            "timestamp": 1440662976.5,
            "file_size": 1024,
            "store_path": "/d/e/f.jpg",
            "priority": 11
        },
        {
            "checksum": "deadbeef",
            "source_path": "/g/b/c.jpg",
            "datetime": "2015:08:27 04:09:36.50",
            "timestamp": 1440662976.5,
            "file_size": 1024,
            "store_path": "",
            "priority": 20,
            "tz_offset": -14400
        }
    ]
},
"command_history": {
    "2021-03-08_23-56-00Z": "photomanager create --db test.json",
    "2021-03-08_23-57-00Z": "photomanager import --db test.json test.jpg"
}
}"""
    db = Database.from_json(json_data)
    print(db.db)
    assert db.version == Database.VERSION
    assert db.hash_algorithm == HashAlgorithm.SHA256
    assert db.db["timezone_default"] == "local"
    assert db.timezone_default is None
    photo_db_expected = {
        "d239210f00534b76a2b215e073f75832": [
            PhotoFile.from_dict({
                "chk": "deadbeef",
                "src": "/a/b/c.jpg",
                "dt": "2015:08:27 04:09:36.50",
                "ts": 1440662976.5,
                "fsz": 1024,
                "sto": "/d/e/f.jpg",
                "prio": 11,
            }),
            PhotoFile.from_dict({
                "chk": "deadbeef",
                "src": "/g/b/c.jpg",
                "dt": "2015:08:27 04:09:36.50",
                "ts": 1440662976.5,
                "fsz": 1024,
                "sto": "",
                "prio": 20,
                "tzo": -14400,
            }),
        ]
    }
    command_history_expected = {
        "2021-03-08_23-56-00Z": "photomanager create --db test.json",
        "2021-03-08_23-57-00Z": "photomanager import --db test.json test.jpg",
    }
    db_expected = {
        "version": Database.VERSION,
        "hash_algorithm": HashAlgorithm.SHA256,
        "timezone_default": "local",
        "photo_db": photo_db_expected,
        "command_history": command_history_expected,
    }
    assert db.photo_db == photo_db_expected
    assert db.command_history == command_history_expected
    assert orjson.loads(db.json) != orjson.loads(json_data)
    assert db.db == db_expected
    assert db == Database.from_dict(orjson.loads(json_data))
    assert db.get_stats() == (1, 2, 1, 1024)
コード例 #4
0
def test_database_load_version_3():
    json_data = b"""{
"version": 3,
"hash_algorithm": "sha256",
"photo_db": {
    "QKEsTn2X": [
        {
            "chk": "deadbeef",
            "src": "/a/b/c.jpg",
            "dt": "2015:08:27 04:09:36.50",
            "ts": 1440662976.5,
            "fsz": 1024,
            "sto": "/d/e/f.jpg",
            "prio": 11,
            "tzo": null
        },
        {
            "chk": "deadbeef",
            "src": "/g/b/c.jpg",
            "dt": "2015:08:27 04:09:36.50",
            "ts": 1440662976.5,
            "fsz": 1024,
            "sto": "",
            "prio": 20,
            "tzo": -14400
        }
    ]
},
"command_history": {
    "2021-03-08_23-56-00Z": "photomanager create --db test.json",
    "2021-03-08_23-57-00Z": "photomanager import --db test.json test.jpg"
}
}""".replace(b"VERSION", f"{Database.VERSION}".encode())
    db = Database.from_json(json_data)
    print(db.db)
    assert db.version == Database.VERSION
    assert db.hash_algorithm == HashAlgorithm.SHA256
    assert db.db["timezone_default"] == "local"
    assert db.timezone_default is None
    photo_db_expected = {
        "QKEsTn2X": [
            PhotoFile.from_dict({
                "chk": "deadbeef",
                "src": "/a/b/c.jpg",
                "dt": "2015:08:27 04:09:36.50",
                "ts": 1440662976.5,
                "fsz": 1024,
                "sto": "/d/e/f.jpg",
                "prio": 11,
            }),
            PhotoFile.from_dict({
                "chk": "deadbeef",
                "src": "/g/b/c.jpg",
                "dt": "2015:08:27 04:09:36.50",
                "ts": 1440662976.5,
                "fsz": 1024,
                "sto": "",
                "prio": 20,
                "tzo": -14400,
            }),
        ]
    }
    command_history_expected = {
        "2021-03-08_23-56-00Z": "photomanager create --db test.json",
        "2021-03-08_23-57-00Z": "photomanager import --db test.json test.jpg",
    }
    db_expected = {
        "version": Database.VERSION,
        "hash_algorithm": HashAlgorithm.SHA256,
        "timezone_default": "local",
        "photo_db": photo_db_expected,
        "command_history": command_history_expected,
    }
    assert db.photo_db == photo_db_expected
    assert db.command_history == command_history_expected
    assert orjson.loads(db.json) != orjson.loads(json_data)
    assert db.db == db_expected
    assert db == Database.from_dict(orjson.loads(json_data))
    assert db.get_stats() == (1, 2, 1, 1024)