<commit_before>import os
import tempfile
import functools

import pytest

from pmxbot import karma

class TestMongoDBKarma(object):
	def setup_karma(self, mongodb_uri):
		k = karma.Karma.from_URI(mongodb_uri)
		k.db = k.db.database.connection[
			k.db.database.name+'_test'
			][k.db.name]
		self.karma = k

	def teardown_method(self, method):
		if hasattr(self, 'karma'):
			self.karma.db.drop()

	def test_basic_usage(self, mongodb_uri):
		self.setup_karma(mongodb_uri)
		k = self.karma
		k.change('foo', 1)
		k.change('bar', 1)
		k.set('baz', 3)
		k.set('baz', 2)
		k.link('foo', 'bar')
		assert k.lookup('foo') == 2
		k.link('foo', 'baz')
		assert k.lookup('baz') == k.lookup('foo') == 4
		k.change('foo', 1)
		assert k.lookup('foo') == k.lookup('bar') == 5

	def test_linking_same_does_nothing(self, mongodb_uri):
		self.setup_karma(mongodb_uri)
		k = self.karma
		k.set('foo', 99)
		with pytest.raises(karma.SameName):
			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(karma.AlreadyLinked):
			k.link('foo', 'bar')
		with pytest.raises(karma.AlreadyLinked):
			k.link('bar', 'foo')
		assert k.lookup('foo') == k.lookup('bar') == 100

class TestSQLiteKarma(object):
	finalizers = []

	@classmethod
	def teardown_class(cls):
		for finalizer in cls.finalizers:
			finalizer()

	def test_linking_same_does_nothing(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()))
		k.set('foo', 99)
		k.link('foo', 'foo')
		assert k.lookup('foo') == 99
<commit_msg>Added a couple more tests to make sure that similar names don't have different behaviors with .link<commit_after>import os
import tempfile
import functools

import pytest

from pmxbot import karma

class TestMongoDBKarma(object):
	def setup_karma(self, mongodb_uri):
		k = karma.Karma.from_URI(mongodb_uri)
		k.db = k.db.database.connection[
			k.db.database.name+'_test'
			][k.db.name]
		self.karma = k

	def teardown_method(self, method):
		if hasattr(self, 'karma'):
			self.karma.db.drop()

	def test_basic_usage(self, mongodb_uri):
		self.setup_karma(mongodb_uri)
		k = self.karma
		k.change('foo', 1)
		k.change('bar', 1)
		k.set('baz', 3)
		k.set('baz', 2)
		k.link('foo', 'bar')
		assert k.lookup('foo') == 2
		k.link('foo', 'baz')
		assert k.lookup('baz') == k.lookup('foo') == 4
		k.change('foo', 1)
		assert k.lookup('foo') == k.lookup('bar') == 5

	def test_linking_same_does_nothing(self, mongodb_uri):
		self.setup_karma(mongodb_uri)
		k = self.karma
		k.set('foo', 99)
		with pytest.raises(karma.SameName):
			k.link('foo', 'foo')
		assert k.lookup('foo') == 99

	def test_linking_similar(self, mongodb_uri):
		self.setup_karma(mongodb_uri)
		k = self.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, 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(karma.AlreadyLinked):
			k.link('foo', 'bar')
		with pytest.raises(karma.AlreadyLinked):
			k.link('bar', 'foo')
		assert k.lookup('foo') == k.lookup('bar') == 100

class TestSQLiteKarma(object):
	finalizers = []

	@classmethod
	def teardown_class(cls):
		for finalizer in cls.finalizers:
			finalizer()

	def test_linking_same_does_nothing(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()))
		k.set('foo', 99)
		k.link('foo', 'foo')
		assert k.lookup('foo') == 99

{'fraction_non_alphanumeric': 0.17173607861488066, 'fraction_numerical': 0.01357042583060365, 'mean_word_length': 3.702970297029703, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4520295', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1647, 'n_words': 305}