Ejemplo n.º 1
0
 def test_init_internal_data(self, tmpdir):
     # A bucket with same path won't overwrite existing data...
     bucket1 = Bucket(str(tmpdir))
     assert bucket1._get_internal_data() == dict(version=1,
                                                 curr_src_num=0,
                                                 curr_repr_num={})
     to_set = dict(version=1, curr_src_num=1, curr_repr_num={'1': 2})
     bucket1._set_internal_data(to_set)
     assert bucket1._get_internal_data() == to_set
     bucket2 = Bucket(str(tmpdir))
     assert bucket2._get_internal_data() == to_set
Ejemplo n.º 2
0
 def test_get_stored_repr_num(self, tmpdir):
     # we can get a representation number if the repective key is
     # stored in the bucket already.
     bucket = Bucket(str(tmpdir.join("cache")))
     key_path1 = tmpdir / "cache" / "keys" / "1" / "1.key"
     key_path2 = tmpdir / "cache" / "keys" / "1" / "2.key"
     key_path3 = tmpdir / "cache" / "keys" / "2" / "1.key"
     assert bucket.get_stored_repr_num(1, 'somekey') is None
     assert bucket.get_stored_repr_num(1, 'otherkey') is None
     assert bucket.get_stored_repr_num(2, 'somekey') is None
     assert bucket.get_stored_repr_num(2, 'otherkey') is None
     key_path1.write('otherkey', ensure=True)
     assert bucket.get_stored_repr_num(1, 'somekey') is None
     assert bucket.get_stored_repr_num(1, 'otherkey') == 1
     assert bucket.get_stored_repr_num(2, 'somekey') is None
     assert bucket.get_stored_repr_num(2, 'otherkey') is None
     key_path2.write('somekey', ensure=True)
     assert bucket.get_stored_repr_num(1, 'somekey') == 2
     assert bucket.get_stored_repr_num(1, 'otherkey') == 1
     assert bucket.get_stored_repr_num(2, 'somekey') is None
     assert bucket.get_stored_repr_num(2, 'otherkey') is None
     key_path3.write('somekey', ensure=True)
     assert bucket.get_stored_repr_num(1, 'somekey') == 2
     assert bucket.get_stored_repr_num(1, 'otherkey') == 1
     assert bucket.get_stored_repr_num(2, 'somekey') == 1
     assert bucket.get_stored_repr_num(2, 'otherkey') is None
Ejemplo n.º 3
0
 def test_curr_repr_num(self, tmpdir):
     # we can get/set current representation number
     bucket = Bucket(str(tmpdir))
     assert bucket.get_current_repr_num(1) == 0
     assert bucket.get_current_repr_num('2') == 0
     bucket.set_current_repr_num('1', 12)
     assert bucket.get_current_repr_num('1') == 12
     assert bucket.get_current_repr_num('2') == 0
Ejemplo n.º 4
0
 def test_init_sets_attributes(self, tmpdir):
     # Main attributes are set properly...
     bucket = Bucket(str(tmpdir))
     assert bucket.srcdir == tmpdir / "sources"
     assert bucket.resultdir == tmpdir / "repr"
     assert bucket.keysdir == tmpdir / "keys"
     assert bucket._data == dict(version=1,
                                 curr_src_num=0,
                                 curr_repr_num=dict())
Ejemplo n.º 5
0
 def test_get_representation_stored(self, cache_env):
     # we can get paths of representations
     bucket = Bucket(str(cache_env.join("cache")))
     res1 = bucket.store_representation(str(cache_env / "src1.txt"),
                                        str(cache_env / "result1.txt"),
                                        repr_key=b'mykey')
     res2 = bucket.get_representation(res1)
     assert res1 == "1_1"
     assert res2 == cache_env / "cache" / "repr" / "1" / "1" / "result1.txt"
Ejemplo n.º 6
0
 def test_store_representation_string_key(self, cache_env):
     #  we can store sources with their representations and a string key
     bucket = Bucket(str(cache_env.join("cache")))
     res = bucket.store_representation(str(cache_env / "src1.txt"),
                                       str(cache_env / "result1.txt"),
                                       repr_key="somekey")
     assert res == "1_1"
     assert (cache_env / "cache" / "keys" / "1" /
             "1.key").read() == 'somekey'
Ejemplo n.º 7
0
 def test_get_stored_source_num(self, cache_env):
     # we can test whether a source file is stored in a bucket already.
     bucket = Bucket(str(cache_env.join("cache")))
     src1 = cache_env / "src1.txt"
     src2 = cache_env / "src2.txt"
     assert bucket.get_stored_source_num(str(src1)) is None
     assert bucket.get_stored_source_num(str(src2)) is None
     shutil.copyfile(str(src1), os.path.join(bucket.srcdir, "source_1"))
     assert bucket.get_stored_source_num(str(src1)) == 1
     assert bucket.get_stored_source_num(str(src2)) is None
     shutil.copyfile(str(src2), os.path.join(bucket.srcdir, "source_2"))
     assert bucket.get_stored_source_num(str(src1)) == 1
     assert bucket.get_stored_source_num(str(src2)) == 2
Ejemplo n.º 8
0
 def test_store_representation_no_key(self, cache_env):
     # we can store sources with their representations
     bucket = Bucket(str(cache_env.join("cache")))
     res = bucket.store_representation(str(cache_env / "src1.txt"),
                                       str(cache_env / "result1.txt"))
     source_path = cache_env / "cache" / "sources" / "source_1"
     result_path = cache_env / "cache" / "repr" / "1" / "1" / "result1.txt"
     assert res == "1_1"
     assert source_path.isfile()
     assert source_path.read() == "source1\n"
     assert result_path.dirpath().isdir()
     assert result_path.isfile()
     assert result_path.read() == "result1\n"
     assert (cache_env / "cache" / "keys" / "1" / "1.key").isfile()
     assert (cache_env / "cache" / "keys" / "1" / "1.key").read() == ""
Ejemplo n.º 9
0
 def test_store_representation_update_result(self, cache_env):
     # if we send a different representation for the same source
     # and key, the old representation will be replaced.
     bucket = Bucket(str(cache_env / "cache"))
     res1 = bucket.store_representation(str(cache_env / "src1.txt"),
                                        str(cache_env / "result1.txt"),
                                        repr_key='mykey')
     res2 = bucket.store_representation(str(cache_env / "src1.txt"),
                                        str(cache_env / "result2.txt"),
                                        repr_key='mykey')
     assert res1 == "1_1"
     assert res2 == "1_1"
     result_dir = cache_env / "cache" / "repr" / "1" / "1"
     assert result_dir.join("result1.txt").exists() is False
     assert result_dir.join("result2.txt").exists() is True
     assert result_dir.join("result2.txt").read() == "result2\n"
Ejemplo n.º 10
0
 def test_keys(self, cache_env):
     # we can get a list of all bucket keys in a bucket.
     bucket = Bucket(str(cache_env))
     assert list(bucket.keys()) == []
     key1 = bucket.store_representation(str(cache_env / "src1.txt"),
                                        str(cache_env / "result1.txt"),
                                        repr_key='foo')
     assert list(bucket.keys()) == [
         key1,
     ]
     key2 = bucket.store_representation(str(cache_env / "src1.txt"),
                                        str(cache_env / "result2.txt"),
                                        repr_key='bar')
     assert sorted(list(bucket.keys())) == [key1, key2]
     key3 = bucket.store_representation(str(cache_env / "src1.txt"),
                                        str(cache_env / "result3.txt"),
                                        repr_key='baz')
     assert sorted(list(bucket.keys())) == [key1, key2, key3]
Ejemplo n.º 11
0
 def test_curr_src_num(self, tmpdir):
     # we can get/set current source number
     bucket = Bucket(str(tmpdir))
     assert bucket.get_current_source_num() == 0
     bucket.set_current_source_num(12)
     assert bucket.get_current_source_num() == 12
Ejemplo n.º 12
0
 def test_init_creates_subdirs(self, tmpdir):
     # a new bucket contains certain subdirs and a file
     Bucket(str(tmpdir))
     for filename in ['sources', 'repr', 'keys', 'data']:
         assert tmpdir.join(filename).exists()
Ejemplo n.º 13
0
 def test_get_representation_unstored(self, tmpdir):
     # we cannot get unstored representations
     bucket = Bucket(str(tmpdir.join("cache")))
     assert bucket.get_representation("1_1") is None