summaryrefslogtreecommitdiff
path: root/service
diff options
context:
space:
mode:
authorPatrick Maia and Victor Shyba <pixelated-team+pmaia+vshyba@thoughtworks.com>2014-09-07 16:55:57 +0000
committerPatrick Maia <pmaia@thoughtworks.com>2014-09-07 16:55:57 +0000
commitff01686c1b38d26b0ef54833cf3463ab0bf73bf8 (patch)
tree464343b9b4d52e714289a4165c5f64d0a4f9fe71 /service
parent2628ad607bec6f147a4a73786976585c74b483c7 (diff)
#51 - some tests for TagIndex and the addition of method TagIndex.add
Diffstat (limited to 'service')
-rw-r--r--service/pixelated/adapter/tag_index.py4
-rw-r--r--service/test/adapter/tag_index_test.py63
2 files changed, 67 insertions, 0 deletions
diff --git a/service/pixelated/adapter/tag_index.py b/service/pixelated/adapter/tag_index.py
index 25108643..ea69be1a 100644
--- a/service/pixelated/adapter/tag_index.py
+++ b/service/pixelated/adapter/tag_index.py
@@ -34,6 +34,10 @@ class TagIndex:
self.close_db() # force flush
self.db = dbm.open(self.db_path, 'c')
+ def add(self, tag):
+ if tag.name not in self.db:
+ self.set(tag)
+
def get(self, tag_name):
if tag_name in self.db:
return Tag.from_json_string(self.db.get(tag_name))
diff --git a/service/test/adapter/tag_index_test.py b/service/test/adapter/tag_index_test.py
new file mode 100644
index 00000000..05f7ce55
--- /dev/null
+++ b/service/test/adapter/tag_index_test.py
@@ -0,0 +1,63 @@
+
+#
+# Copyright (c) 2014 ThoughtWorks, Inc.
+#
+# Pixelated is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# Pixelated is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with Pixelated. If not, see <http://www.gnu.org/licenses/>.
+import unittest
+import os
+
+from pixelated.adapter.tag_index import TagIndex
+from pixelated.adapter.tag import Tag
+
+
+class TestTagIndex(unittest.TestCase):
+
+ def setUp(self):
+ self.db_path = '/tmp/database_test_tag_index'
+ self.tag_index = TagIndex(self.db_path)
+
+ def tearDown(self):
+ self.tag_index.close_db()
+ os.remove(self.db_path + '.db')
+
+ def test_get_and_set_works(self):
+ tag = Tag('a_tag')
+ self.tag_index.set(tag)
+ self.assertEquals(tag, self.tag_index.get('a_tag'))
+
+ def test_values_returns_all_values_in_the_index(self):
+ tag_a = Tag('tag_a')
+ self.tag_index.set(tag_a)
+ tag_b = Tag('tag_b')
+ self.tag_index.set(tag_b)
+ tag_c = Tag('tag_c')
+ self.tag_index.set(tag_c)
+
+ self.assertEquals(set([tag_a, tag_b, tag_c]), self.tag_index.values())
+
+ def test_changes_are_visible_between_instances_using_same_file(self):
+ tag = Tag('some_tag')
+ self.tag_index.set(tag)
+
+ other_tag_index = TagIndex(self.db_path)
+ self.assertIn(tag, other_tag_index.values())
+
+ def test_add_does_not_replace_existent_tag_with_same_name(self):
+ tag = Tag('tag', True)
+ self.tag_index.set(tag)
+
+ same_name_tag = Tag('tag', False)
+ self.tag_index.add(same_name_tag)
+
+ self.assertEquals(True, self.tag_index.get('tag').default)