Commits

Jason R. Coombs committed d6733f5

Added tests and fix for karma linking in SQLite.

Comments (0)

Files changed (2)

 		return keysandkarma
 
 	def link(self, thing1, thing2):
-		t1 = thing1.strip().lower()
-		t2 = thing2.strip().lower()
+		if thing1 == thing2:
+			raise SameName("Attempted to link two of the same name")
 		GET_KARMAID_SQL = 'SELECT karmaid FROM karma_keys WHERE karmakey = ?'
 		try:
-			t1id = self.db.execute(GET_KARMAID_SQL, [t1]).fetchone()[0]
+			t1id = self.db.execute(GET_KARMAID_SQL, [thing1]).fetchone()[0]
 		except TypeError:
-			raise KeyError
-		t1value = self.lookup(t1)
+			raise KeyError(thing1)
+		t1value = self.lookup(thing1)
 		try:
-			t2id = self.db.execute(GET_KARMAID_SQL, [t2]).fetchone()[0]
+			t2id = self.db.execute(GET_KARMAID_SQL, [thing2]).fetchone()[0]
 		except TypeError:
-			raise KeyError
-		t2value = self.lookup(t2)
+			raise KeyError(thing2)
+		if t1id == t2id:
+			raise AlreadyLinked("Those two are already linked")
+		t2value = self.lookup(thing2)
 
 		newvalue = t1value + t2value
 		# update the keys so t2 points to t1s value

tests/unit/test_karma.py

 		for finalizer in cls.finalizers:
 			finalizer()
 
-	def test_linking_same_does_nothing(self):
+	def setup_karma(self):
 		tf = tempfile.NamedTemporaryFile(delete=False)
 		tf.close()
 		self.finalizers.append(functools.partial(os.remove, tf.name))
-		k = karma.Karma.from_URI('sqlite://{tf.name}'.format(**vars()))
+		return karma.Karma.from_URI('sqlite://{tf.name}'.format(**vars()))
+
+	def test_linking_same_does_nothing(self):
+		k = self.setup_karma()
 		k.set('foo', 99)
-		k.link('foo', 'foo')
+		with pytest.raises(karma.SameName):
+			k.link('foo', 'foo')
 		assert k.lookup('foo') == 99
+
+	def test_linking_similar(self):
+		k = self.setup_karma()
+		k.set('foo', 99)
+		k.set('foo|away', 1)
+		k.link('foo', 'foo|away')
+		assert k.lookup('foo') == 100
+		with pytest.raises(karma.AlreadyLinked):
+			k.link('foo', 'foo|away')
+		with pytest.raises(karma.AlreadyLinked):
+			k.link('foo|away', 'foo')
+
+	def test_already_linked_raises_error(self):
+		k = self.setup_karma()
+		k.set('foo', 50)
+		k.set('bar', 50)
+		k.link('foo', 'bar')
+		assert k.lookup('foo') == k.lookup('bar') == 100
+		with pytest.raises(karma.AlreadyLinked):
+			k.link('foo', 'bar')
+		with pytest.raises(karma.AlreadyLinked):
+			k.link('bar', 'foo')
+		assert k.lookup('foo') == k.lookup('bar') == 100
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.