diff --git a/src/energenie/KVS.py b/src/energenie/KVS.py index 4236a36..4d895c3 100644 --- a/src/energenie/KVS.py +++ b/src/energenie/KVS.py @@ -4,7 +4,7 @@ from lifecycle import * -class KVSFile(): +class KVS(): """A persistent key value store""" def __init__(self, filename=None): self.filename = filename @@ -71,42 +71,39 @@ pass #TODO # find key in object store, delete it - @untested def __getitem__(self, key): return self.store[key] - @untested def __setitem__(self, key, value): self.store[key] = value self.append(key, value) - @untested def __delitem__(self, key): del self.store[key] self.remove(key) + def __len__(self): + return len(self.store) + @untested def keys(self): return self.store.keys() - @untested - def size(self): - return len(self.store) - def append(self, key, values): """Append a new record to the persistent file""" - with open(self.filename, 'a') as f: - f.write("ADD %s\n" % key) - for k in values: - v = values[k] - f.write("%s=%s\n" % (k, v)) - f.write("\n") + if self.filename != None: + with open(self.filename, 'a') as f: + f.write("ADD %s\n" % key) + for k in values: + v = values[k] + f.write("%s=%s\n" % (k, v)) + f.write("\n") - - @unimplemented + @untested def remove(self, key): """Remove reference to this key in the file, and remove from in memory store""" - pass #TODO + if self.filename != None: + pass #TODO # open file for read write # search line at a time, process each command # when we find the command 'ADD key' diff --git a/src/energenie/KVS_test.py b/src/energenie/KVS_test.py index 1325561..935a7fb 100644 --- a/src/energenie/KVS_test.py +++ b/src/energenie/KVS_test.py @@ -4,43 +4,85 @@ import unittest from lifecycle import * +from KVS import KVS + +# A dummy test class + +class TV(): + def __init__(self, id): + self.id = id + + def __repr__(self): + return "TV(%s)" % self.id + class TestKVSMemory(unittest.TestCase): @test_0 def test_create_blank(self): - pass #TODO: create a blank registry not bound to a file - #init + """Create a blank kvs, not bound to any external file""" + kvs = KVS() + # it should not fall over @test_0 def test_add(self): - pass #TODO: add an object into the store - #setitem + """Add an object into the kvs store""" + kvs = KVS() + + kvs["tv1"] = TV(1) + kvs["tv2"] = TV(2) + + print(kvs.store) @test_0 def test_change(self): - pass #TODO: if we change the value associated with a key, does it get updated? - #setitem + """Change the value associated with an existing key""" + kvs = KVS() + kvs["tv1"] = TV(1) + kvs["tv1"] = TV(111) # change it + + print(kvs.store) @test_0 def test_get(self): - pass #TODO: get an object by it's key name - #getitem + """Get the object associated with a key in the store""" + kvs = KVS() + kvs["tv1"] = TV(1) + t = kvs["tv1"] + print(t) @test_0 def test_delete(self): - pass #TODO: delete an object by it's key name - #delitem + """Delete an existing key in the store, and a missing key for error""" + kvs = KVS() + kvs["tv1"] = TV(1) + del kvs["tv1"] + print(kvs.store) + + try: + del kvs["tv1"] # expect error + self.fail("Did not get expected KeyError exception") + except KeyError: + pass # expected @test_0 def test_size(self): - pass #TODO: how big is the kvs - # size + """How big is the kvs""" + kvs = KVS() + kvs["tv1"] = TV(1) + print(len(kvs)) + kvs["tv2"] = TV(2) + print(len(kvs)) @test_0 def test_keys(self): - pass #TODO: get all keys - # keys + """Get out all keys of the kvs""" + kvs = KVS() + kvs["tv1"] = TV(1) + kvs["tv2"] = TV(2) + kvs["tv3"] = TV(3) + print(kvs.keys()) + class TestKVSPersisted(unittest.TestCase): @@ -53,6 +95,9 @@ def test_load(self): pass #TODO: load a blank kvs from an external file # load + # callback for object creation needs to be passed in + # want to test that kvp's could be used to pass to kwargs to construct + # data must be passed in string format @test_0 def test_add(self): @@ -60,6 +105,10 @@ # setitem @test_0 + def test_change(self): + pass #TODO: change the value associated with an existing key + + @test_0 def test_delete(self): pass #TODO: does persistent version get an IGN update? # delitem