Commits

Jason R. Coombs  committed e468ba7

Fixed issue with karma doubling on MongoDB per issue #17

  • Participants
  • Parent commits 684ad73

Comments (0)

Files changed (2)

File pmxbot/karma.py

 	def link(self, thing1, thing2):
 		thing1 = thing1.strip().lower()
 		thing2 = thing2.strip().lower()
+		if thing1 == thing2:
+			raise ValueError("Attempted to link two of the same")
 		rec = self.db.find_one({'names': thing2})
+		if thing1 in rec['names']:
+			raise ValueError("Those two are already linked")
 		if not rec: raise KeyError(thing2)
 		try:
 			query = {'names': thing1}

File tests/unit/test_karma.py

 import tempfile
 import functools
 
+import pytest
+
 from pmxbot import karma
 
 class TestMongoDBKarma(object):
 		self.setup_karma(mongodb_uri)
 		k = self.karma
 		k.set('foo', 99)
-		k.link('foo', 'foo')
+		with pytest.raises(ValueError):
+			k.link('foo', 'foo')
 		assert k.lookup('foo') == 99
 
+	def test_already_linked_raises_error(self, mongodb_uri):
+		self.setup_karma(mongodb_uri)
+		k = self.karma
+		k.set('foo', 50)
+		k.set('bar', 50)
+		k.link('foo', 'bar')
+		assert k.lookup('foo') == k.lookup('bar') == 100
+		with pytest.raises(ValueError):
+			k.link('foo', 'bar')
+		with pytest.raises(ValueError):
+			k.link('bar', 'foo')
+		assert k.lookup('foo') == k.lookup('bar') == 100
+
 class TestSQLiteKarma(object):
 	finalizers = []