def setUp(self): mongoConn = MongoConnection() self.storage = Storage(mongoConn) reader = TextReader() self.calculator = TagStudy(self.storage, reader) self.detector = TagDetector(self.storage, reader)
def setUp(self): mongoConn = MongoConnection() self.storage = Storage(mongoConn)
class StorageTests(unittest.TestCase): def setUp(self): mongoConn = MongoConnection() self.storage = Storage(mongoConn) def tearDown(self): self.storage = None # ------------------------------------------------------------ def test_save_tag_words(self): words = self.add_tag_words() self.assert_tag_words_found(words) self.remove_tag_with_words() def add_tag_words(self): words = [TagWord("mark", 1), TagWord("paul", 2), TagWord("john", 2)] self.storage.save_tag_words("name", words) return words def assert_tag_words_found(self, words): found_words = self.storage.get_tag_words("name") self.assertIsNotNone(found_words, "no words") self.assertEqual(len(found_words), len(words)) def remove_tag_with_words(self): self.storage.remove_tag("name") # ------------------------------------------------------------ def test_remove_tag(self): self.add_tags() self.assert_tags_present() self.assert_one_tag_was_removed() self.assert_both_tags_were_removed() def add_tags(self): self.storage.save_tag_words("tag1", []) self.storage.save_tag_words("tag2", []) def assert_tags_present(self): self.assertIsNotNone(self.storage.get_tag_words("tag1")) self.assertIsNotNone(self.storage.get_tag_words("tag2")) def assert_one_tag_was_removed(self): self.storage.remove_tag("tag1") self.assertIsNone(self.storage.get_tag_words("tag1")) self.assertIsNotNone(self.storage.get_tag_words("tag2")) def assert_both_tags_were_removed(self): self.storage.remove_tag("tag2") self.assertIsNone(self.storage.get_tag_words("tag1")) self.assertIsNone(self.storage.get_tag_words("tag2")) # ---------------------------------------------------------------- def test_get_tags_by_words(self): self.add_tags_words() self.check_found_tags_by_words() def add_tags_words(self): words1 = (TagWord("mark", 1, 1.0 / 5), TagWord("paul", 2, 2.0 / 5), TagWord("john", 2, 2.0 / 5)) words2 = (TagWord("john", 1, 1.0 / 5), TagWord("ira", 2, 2.0 / 5), TagWord("natali", 2, 2.0 / 5)) self.storage.save_tag_words("tag1", words1) self.storage.save_tag_words("tag2", words2) def check_found_tags_by_words(self): self.check_found_tags(("tag1", "tag2"), (2.0 / 5, 1.0 / 5), "john") self.check_found_tags((), (), "mila") def check_found_tags(self, expectedTags, expectedParts, tag): tags = self.storage.get_tags_by_words([tag]) tagsList = [tag.get("name") for tag in tags] tagParts = [tag.get("part") for tag in tags] self.assertEqual(expectedTags, tuple(tagsList), 'wrong list of tags for "john"') self.assertEqual(expectedParts, tuple(tagParts), 'wrong list of tags parts for "john"')
class TagDetectorTests(unittest.TestCase): def setUp(self): mongoConn = MongoConnection() self.storage = Storage(mongoConn) reader = TextReader() self.calculator = TagStudy(self.storage, reader) self.detector = TagDetector(self.storage, reader) def tearDown(self): self.detector = None def test_no_text(self): tags = self.detector.detect('') self.assertEqual([], tags, 'Should be empty') def test_simple_text_single_tag(self): try: tags = ('tag1',) text = 'Hello my World' self.calculator.learn(tags, text) foundTags = self.detector.detect('Hello wonderful world!') foundTagsNames = [tag.name for tag in foundTags] self.assertEqual(tags, tuple(foundTagsNames)) finally: self.storage.remove_tag('tag1') def test_simple_text_two_tags(self): try: tags = ('tag1', 'tag2') text = 'Hello my World' self.calculator.learn(tags, text) foundTags = self.detector.detect('Hello wonderful world!') foundTagsNames = [tag.name for tag in foundTags] self.assertEqual(('tag1', 'tag2'), tuple(foundTagsNames)) finally: self.storage.remove_tag('tag1') self.storage.remove_tag('tag2') def test_two_texts(self): try: tags = ('tag1', 'tag2') text = 'Hello my World' self.calculator.learn(tags, text) tags = ('tag2', 'tag3') text = 'Hello John!' self.calculator.learn(tags, text) tags = ('tag2',) text = 'Hello Ruslan! How are you today?' self.calculator.learn(tags, text) foundTags = self.detector.detect('Hello Ruslan!') foundTagsNames = [tag.name for tag in foundTags] self.assertEqual(('tag2', 'tag1', 'tag3'), tuple(foundTagsNames)) finally: self.storage.remove_tag('tag1') self.storage.remove_tag('tag2') self.storage.remove_tag('tag3')
class TagStudyTests(unittest.TestCase): def setUp(self): mongoConn = MongoConnection() self.storage = Storage(mongoConn) reader = TextReader() self.calculator = TagStudy(self.storage, reader) def tearDown(self): self.calculator = None def test_learn_new_tag(self): tags = ['tag1'] text = 'Hello World!' self.calculator.learn(tags, text) self.check_tag_words('tag1', {'hello': 1, 'world': 1}) def test_learn_few_tags(self): tags = ['tag1', 'tag2'] text = 'Hello World!' self.calculator.learn(tags, text) self.check_tag_words('tag1', {'hello': 1, 'world': 1}) self.check_tag_words('tag2', {'hello': 1, 'world': 1}) def test_learn_merge_tags(self): tags = ['tag1'] text = 'Hello World!' self.calculator.learn(tags, text) self.check_tag_words('tag1', {'hello': 1, 'world': 1}, remove=False) text = 'Hello Ruslan!' self.calculator.learn(tags, text) self.check_tag_words('tag1', {'hello': 2, 'world': 1, 'ruslan': 1}) def test_learn_tags_words_parts(self): tags = ['tag1'] text = 'Hello World! World is nice, so it is a pleasure to say hello to the world' self.calculator.learn(tags, text) words = self.storage.get_tag_words('tag1') try: foundHello = False foundWorld = False foundNice = False for word in words: if word.word == 'hello': self.assertEqual(2, word.count) self.assertEqual(float(2.0 / 8), word.part) foundHello = True if word.word == 'world': self.assertEqual(3, word.count) self.assertEqual(3.0 / 8, word.part) foundWorld = True if word.word == 'nice': self.assertEqual(1, word.count) self.assertEqual(1.0 / 8, word.part) foundNice = True self.assertTrue(foundHello, 'Hello is not found') self.assertTrue(foundWorld, 'World is not found') self.assertTrue(foundNice, 'Nice is not found') finally: self.storage.remove_tag('tag1') def check_tag_words(self, tag, checkWords, remove=True): words = self.storage.get_tag_words(tag) for key, value in checkWords.items(): self.assertWordInWithCount(words, key, value) if remove: self.storage.remove_tag(tag) def assertWordInWithCount(self, words, word, count): for each in words: if each.word == word: self.assertEqual(count, each.count, "Wrong count") return self.fail("Word is not found") # ----------------------------------------------------- def test_forget_new_tag(self): try: tags = ['tag1'] text = 'Hello World!' self.calculator.learn(tags, text) self.check_tag_words('tag1', {'hello': 1, 'world': 1}, remove=False) self.calculator.forget(tags, text) words = self.storage.get_tag_words('tag1') self.assertEqual(0, len(words)) finally: self.storage.remove_tag("tag1") def test_forget_merge_tags(self): tags = ['tag1'] text = 'Hello World!' self.calculator.learn(tags, text) self.check_tag_words('tag1', {'hello': 1, 'world': 1}, remove=False) text = 'Hello Ruslan!' self.calculator.learn(tags, text) self.check_tag_words('tag1', {'hello': 2, 'world': 1, 'ruslan': 1}, remove=False) self.calculator.forget(tags, text) self.check_tag_words('tag1', {'hello': 1, 'world': 1})