Beispiel #1
0
def test_FixedLengthTupleFilesystemStoreBackend_verify_that_key_to_filepath_operation_is_reversible(tmp_path_factory):
    path = str(tmp_path_factory.mktemp('test_FixedLengthTupleFilesystemStoreBackend_verify_that_key_to_filepath_operation_is_reversible__dir'))
    project_path = str(tmp_path_factory.mktemp('my_dir'))

    my_store = FixedLengthTupleFilesystemStoreBackend(
        root_directory=os.path.abspath(path),
        base_directory=project_path,
        key_length=3,
        filepath_template= "{0}/{1}/{2}/foo-{2}-expectations.txt",
    )
    #This should pass silently
    my_store.verify_that_key_to_filepath_operation_is_reversible()

    my_store = FixedLengthTupleFilesystemStoreBackend(
        root_directory=os.path.abspath(path),
        base_directory=project_path,
        key_length=3,
        filepath_template= "{0}/{1}/foo-{2}-expectations.txt",
    )
    #This also should pass silently
    my_store.verify_that_key_to_filepath_operation_is_reversible()


    with pytest.raises(ValueError):
        my_store = FixedLengthTupleFilesystemStoreBackend(
            root_directory=os.path.abspath(path),
            base_directory=project_path,
            key_length=3,
            filepath_template= "{0}/{1}/foo-expectations.txt",
        )
def test_FilesystemStoreBackend_two_way_string_conversion(tmp_path_factory):
    path = str(
        tmp_path_factory.mktemp(
            'test_FilesystemStoreBackend_two_way_string_conversion__dir'))
    project_path = str(tmp_path_factory.mktemp('my_dir'))

    my_store = FixedLengthTupleFilesystemStoreBackend(
        root_directory=os.path.abspath(path),
        base_directory=project_path,
        key_length=3,
        filepath_template="{0}/{1}/{2}/foo-{2}-expectations.txt",
    )

    tuple_ = ("A__a", "B-b", "C")
    converted_string = my_store._convert_key_to_filepath(tuple_)
    print(converted_string)
    assert converted_string == "A__a/B-b/C/foo-C-expectations.txt"

    recovered_key = my_store._convert_filepath_to_key(
        "A__a/B-b/C/foo-C-expectations.txt")
    print(recovered_key)
    assert recovered_key == tuple_

    with pytest.raises(ValueError):
        tuple_ = ("A/a", "B-b", "C")
        converted_string = my_store._convert_key_to_filepath(tuple_)
        print(converted_string)
Beispiel #3
0
def test_FixedLengthTupleFilesystemStoreBackend(tmp_path_factory):
    path = "dummy_str"
    project_path = str(tmp_path_factory.mktemp('test_FixedLengthTupleFilesystemStoreBackend__dir'))

    my_store = FixedLengthTupleFilesystemStoreBackend(
        root_directory=os.path.abspath(path),
        base_directory=project_path,
        key_length=1,
        filepath_template= "my_file_{0}",
    )

    #??? Should we standardize on KeyValue, or allow each BackendStore to raise its own error types?
    with pytest.raises(FileNotFoundError):
        my_store.get(("AAA",))
    
    my_store.set(("AAA",), "aaa")
    assert my_store.get(("AAA",)) == "aaa"

    my_store.set(("BBB",), "bbb")
    assert my_store.get(("BBB",)) == "bbb"

    # NOTE: variable key lengths are not supported in this class
    # I suspect the best option is to differentiate between stores meant for reading AND writing,
    # vs Stores that only need to support writing. If a store only needs to support writing,
    # we don't need to guarantee reversibility of keys, which makes the internals **much** simpler.

    # my_store.set("subdir/my_file_BBB", "bbb")
    # assert my_store.get("subdir/my_file_BBB") == "bbb"

    # my_store.set("subdir/my_file_BBB", "BBB")
    # assert my_store.get("subdir/my_file_BBB") == "BBB"

    # with pytest.raises(TypeError):
    #     my_store.set("subdir/my_file_CCC", 123)
    #     assert my_store.get("subdir/my_file_CCC") == 123

    # my_store.set("subdir/my_file_CCC", "ccc")
    # assert my_store.get("subdir/my_file_CCC") == "ccc"

    print(my_store.list_keys())
    assert set(my_store.list_keys()) == set([("AAA",), ("BBB",)])

    print(gen_directory_tree_str(project_path))
    assert gen_directory_tree_str(project_path) == """\