Exemplo n.º 1
0
def test_keys_append(tmpdir):
    path = Path(tmpdir) / 'test.hdf'
    test_artifact = Artifact(path)
    test_keys = test_artifact._keys

    test_artifact.write('test.keys', 'data')
    assert 'test.keys' in test_artifact
    assert 'test.keys' in test_keys
    assert test_keys._keys == ['metadata.keyspace', 'test.keys'
                               ] == [str(k) for k in test_artifact.keys]
Exemplo n.º 2
0
def test_keys_initialization(tmpdir):
    path = Path(tmpdir) / 'test.hdf'
    test_artifact = Artifact(path)
    test_key = test_artifact._keys

    assert test_artifact._path == test_key._path
    assert test_key._keys == ['metadata.keyspace']

    test_artifact.write('new.keys', 'data')
    assert test_key._keys == ['metadata.keyspace', 'new.keys']
    assert test_key.to_list() == test_artifact.keys
Exemplo n.º 3
0
def test_keys_remove(tmpdir):
    path = Path(tmpdir) / 'test.hdf'
    test_artifact = Artifact(path)
    test_keys = test_artifact._keys

    test_artifact.write('test.keys1', 'data')
    test_artifact.write('test.keys2', 'data')
    assert 'test.keys1' in test_artifact and 'test.keys2' in test_artifact
    assert 'test.keys1' in test_keys and 'test.keys2' in test_keys

    test_artifact.remove('test.keys2')
    assert 'test.keys1' in test_artifact and 'test.keys2' not in test_artifact
    assert 'test.keys1' in test_keys and 'test.keys2' not in test_keys
Exemplo n.º 4
0
def test_create_hdf(tmpdir):
    path = Path(tmpdir) / 'test.hdf'
    assert not path.is_file()

    test_artifact = Artifact(path)
    assert path.is_file()
    assert 'metadata.keyspace' in test_artifact

    test_artifact.write('new.key', 'data')
    assert 'new.key' in test_artifact

    #  check whether the existing path was NOT wiped out
    new_artifact = Artifact(test_artifact.path)
    assert new_artifact.path == test_artifact.path
    assert 'new.key' in new_artifact
Exemplo n.º 5
0
def test_artifact_write_no_data(hdf_mock):
    path = Path('/place/with/artifact.hdf')
    filter_terms = ['location == Global', 'draw == 10']
    key = 'new.key'

    a = Artifact(path, filter_terms)
    initial_keys = a.keys

    assert key not in a.keys

    with pytest.raises(ArtifactException):
        a.write(key, None)

    assert key not in a.keys
    assert key not in a._cache
    hdf_mock.write.called_once_with(path, 'metadata.keyspace',
                                    ['metadata.keyspace'])
    hdf_mock.remove.assert_not_called()
    assert a.keys == initial_keys
Exemplo n.º 6
0
def test_artifact_write_duplicate_key(hdf_mock, keys_mock):
    path = Path('/place/with/artifact.hdf')
    filter_terms = ['location == Global', 'draw == 10']
    key = 'population.structure'

    art = Artifact(path, filter_terms)
    initial_keys = art.keys
    assert initial_keys == keys_mock

    with pytest.raises(ArtifactException) as err_info:
        art.write(key, "data")

    assert f'{key} already in artifact.' == str(err_info.value)
    assert key in art
    assert key in art.keys
    assert key not in art._cache
    hdf_mock.write.called_once_with(path, 'metadata.keyspace',
                                    ['metadata.keyspace'])
    hdf_mock.remove.assert_not_called()
    assert art.keys == initial_keys
Exemplo n.º 7
0
def test_artifact_write(hdf_mock, keys_mock):
    path = Path(Path('/place/with/artifact.hdf'))
    filter_terms = ['location == Global', 'draw == 10']
    key = 'new.key'

    a = Artifact(path, filter_terms)
    initial_keys = a.keys

    assert key not in a.keys

    a.write(key, "data")

    assert key in a.keys
    assert key not in a._cache
    expected_call = [
        call(path, 'metadata.keyspace', ['metadata.keyspace']),
        call(path, 'metadata.keyspace', keys_mock + [key]),
        call(path, key, 'data')
    ]
    assert hdf_mock.write.call_args_list == expected_call
    assert set(a.keys) == set(initial_keys + [key])
Exemplo n.º 8
0
def test_replace(hdf_mock, keys_mock):
    path = Path('/place/with/artifact.hdf')
    filter_terms = ['location == Global', 'draw == 10']
    key = 'new.key'

    a = Artifact(path, filter_terms=filter_terms)

    assert key not in a.keys

    a.write(key, "data")
    keyspace_key = 'metadata.keyspace'
    new_keyspace = [k for k in keys_mock + [key]]

    assert hdf_mock.write.call_args_list == [
        call(path, keyspace_key, [str(keyspace_key)]),
        call(path, keyspace_key, new_keyspace),
        call(path, key, 'data')
    ]

    hdf_mock.reset_mock()

    a.replace(key, "new_data")

    # keyspace will be remove first in self.remove from a.replace then self.write from a.replace
    expected_calls_remove = [
        call(path, keyspace_key),
        call(path, key),
        call(path, keyspace_key)
    ]
    assert hdf_mock.remove.call_args_list == expected_calls_remove

    expected_calls_write = [
        call(path, keyspace_key, new_keyspace),
        call(path, keyspace_key, new_keyspace),
        call(path, key, 'new_data')
    ]
    assert hdf_mock.write.call_args_list == expected_calls_write
    assert key in a.keys