Commits

Jason R. Coombs  committed 1f6e976

Updated karma command to handle the exceptions now raised by MongoDB Karma, per issue #17.

  • Participants
  • Parent commits e468ba7
  • Tags 1101.3b1

Comments (0)

Files changed (3)

File pmxbot/karma.py

 
 from . import storage
 
+class SameName(ValueError): pass
+class AlreadyLinked(ValueError): pass
+
 class Karma(storage.SelectableStorage):
 	pass
 
 		thing1 = thing1.strip().lower()
 		thing2 = thing2.strip().lower()
 		if thing1 == thing2:
-			raise ValueError("Attempted to link two of the same")
+			raise SameName("Attempted to link two of the same name")
 		rec = self.db.find_one({'names': thing2})
 		if thing1 in rec['names']:
-			raise ValueError("Those two are already linked")
+			raise AlreadyLinked("Those two are already linked")
 		if not rec: raise KeyError(thing2)
 		try:
 			query = {'names': thing1}

File pmxbot/pmxbot.py

 			return "%s karma--" % karmee
 	elif '==' in rest:
 		t1, t2 = rest.split('==')
-		karma_mod.karma.link(t1, t2)
+		try:
+			karma_mod.karma.link(t1, t2)
+		except karma_mod.SameName:
+			karma_mod.karma.change(nick, -1)
+			return "Don't try to link a name to itself!"
+		except karma_mod.AlreadyLinked:
+			return "Those names were previously linked."
 		score = karma_mod.karma.lookup(t1)
 		return "%s and %s are now linked and have a score of %s" % (t1, t2, score)
 	else:

File tests/unit/test_karma.py

 		self.setup_karma(mongodb_uri)
 		k = self.karma
 		k.set('foo', 99)
-		with pytest.raises(ValueError):
+		with pytest.raises(karma.SameName):
 			k.link('foo', 'foo')
 		assert k.lookup('foo') == 99
 
 		k.set('bar', 50)
 		k.link('foo', 'bar')
 		assert k.lookup('foo') == k.lookup('bar') == 100
-		with pytest.raises(ValueError):
+		with pytest.raises(karma.AlreadyLinked):
 			k.link('foo', 'bar')
-		with pytest.raises(ValueError):
+		with pytest.raises(karma.AlreadyLinked):
 			k.link('bar', 'foo')
 		assert k.lookup('foo') == k.lookup('bar') == 100