def test_no_lexicon(self): with self.assertRaises(BadInput) as e: word_search([SearchDescription.length(7, 7), SearchDescription.number_anagrams(1, 2)]) self.assertEqual( str(e.exception), 'The first search description must contain a lexicon.')
def test_tag_single(self): self.create_some_tags() qs = word_search([ SearchDescription.lexicon(self.csw15), SearchDescription.length(8, 8), SearchDescription.has_tags(['D4'], self.cesar) ]) self.assertEqual(qs.size(), 1) self.assertEqual(['AELMOSTU'], qs.alphagram_string_list()) # Check that it fully populated the question logger.debug(qs.questions_array()[0].to_python_full()) self.assertEqual(qs.questions_array()[0].to_python_full(), { 'question': 'AELMOSTU', 'probability': 2477, 'answers': [{ 'word': 'SOULMATE', 'def': 'a person with whom one is perfectly suited [n -S]', 'f_hooks': '', 'b_hooks': 'S', 'symbols': '', 'f_inner': False, 'b_inner': False, }] })
def test_probability_limit_outside_of_range(self): qs = word_search([ SearchDescription.lexicon(self.america), SearchDescription.length(7, 7), SearchDescription.number_anagrams(8, 100), SearchDescription.probability_limit(10, 20), ]) self.assertEqual(qs.size(), 0)
def test_probability_limit_many(self): qs = word_search([ SearchDescription.lexicon(self.america), SearchDescription.length(7, 7), SearchDescription.point_value(40, 100), SearchDescription.probability_limit(1, 50), ]) self.assertEqual(['AVYYZZZ', 'AIPZZZZ'], qs.alphagram_string_list())
def test_tag_no_match(self): self.create_some_tags() with self.assertRaises(BadInput) as e: word_search([ SearchDescription.lexicon(self.america), SearchDescription.length(8, 8), SearchDescription.has_tags(['D4'], self.cesar) ]) self.assertEqual(str(e.exception), 'Query returns no results.')
def test_probability_limit_another(self): qs = word_search([ SearchDescription.lexicon(self.america), SearchDescription.length(7, 7), SearchDescription.number_anagrams(8, 100), SearchDescription.probability_limit(3, 4), ]) self.assertEqual(qs.size(), 2) self.assertEqual(['AEGINRS', 'EORSSTU'], qs.alphagram_string_list())
def test_probability_limit_unallowed(self): with self.assertRaises(BadInput) as e: word_search([ SearchDescription.lexicon(self.america), SearchDescription.length(7, 7), SearchDescription.probability_limit(1, 3), SearchDescription.probability_list([92, 73, 85, 61]), ]) print(str(e.exception)) self.assertTrue('Mutually exclusive' in str(e.exception))
def test_points(self): qs = word_search([ SearchDescription.lexicon(self.america), SearchDescription.length(7, 7), SearchDescription.point_value(40, 100) ]) self.assertEqual(qs.size(), 2) self.assertEqual( ['AVYYZZZ', 'AIPZZZZ'], qs.alphagram_string_list()) self.assertTrue(len(qs.questions_array()[0].answers[0].definition) > 0)
def test_probability_list(self): qs = word_search([ SearchDescription.lexicon(self.america), SearchDescription.length(7, 7), SearchDescription.probability_list([92, 73, 85, 61]) ]) self.assertEqual(qs.size(), 4) self.assertEqual( ['AINORST', 'EILNOST', 'EILORST', 'ADENOST'], qs.alphagram_string_list()) self.assertTrue(len(qs.questions_array()[0].answers[0].definition) > 0)
def test_probability_word_search(self): qs = word_search([SearchDescription.lexicon(self.america), SearchDescription.length(8, 8), SearchDescription.probability_range(200, 203)]) self.assertEqual(qs.size(), 4) self.assertEqual( ['ADEEGNOR', 'EEGILNOR', 'ADEEGORT', 'AEEGLNOT'], qs.alphagram_string_list()) self.assertTrue(len(qs.questions_array()[0].answers[0].definition) > 0)
def test_tag_search(self): self.create_some_tags() t = time.time() qs = word_search([ SearchDescription.lexicon(self.america), SearchDescription.length(8, 8), SearchDescription.probability_range(5001, 7500), SearchDescription.has_tags(['D4'], self.cesar), ]) logger.debug('Tag search completed in %s seconds', time.time() - t) self.assertEqual(qs.size(), 2500)
def test_alphagram_list(self): qs = word_search([ SearchDescription.lexicon(self.america), SearchDescription.alphagram_list(['DEGORU', 'AAAIMNORT', 'DGOS']) ]) self.assertEqual(qs.size(), 3) self.assertEqual( ['DGOS', 'DEGORU', 'AAAIMNORT'], qs.alphagram_string_list()) self.assertTrue(len(qs.questions_array()[0].answers[0].definition) > 0)
def test_more_tags(self): self.create_some_tags() qs = word_search([ SearchDescription.lexicon(self.america), SearchDescription.length(5, 5), SearchDescription.has_tags(['D2', 'D5'], self.cesar) ]) self.assertEqual(qs.size(), 2) self.assertEqual(['AEILT', 'CINOZ'], qs.alphagram_string_list()) self.assertTrue(len(qs.questions_array()[0].answers[0].definition) > 0)
def test_pts_num_anagrams(self): qs = word_search([ SearchDescription.lexicon(self.america), SearchDescription.length(7, 7), SearchDescription.number_anagrams(8, 100), SearchDescription.point_value(8, 100), ]) self.assertEqual(qs.size(), 3) self.assertEqual( ['AEGINST', 'AEGINRS', 'EIPRSST'], qs.alphagram_string_list()) self.assertTrue(len(qs.questions_array()[0].answers[0].definition) > 0)
def test_tag_list(self): self.create_some_tags() qs = word_search([ SearchDescription.lexicon(self.america), SearchDescription.length(8, 8), SearchDescription.has_tags(['D2', 'D3', 'D4', 'D5'], self.cesar) ]) logger.debug('Found qs: %s', qs) self.assertEqual(qs.size(), 2) self.assertEqual(['AEEGLNOT', 'CEILNOPR'], qs.alphagram_string_list()) self.assertTrue(len(qs.questions_array()[0].answers[0].definition) > 0)
def test_probability_and_points(self): qs = word_search([ SearchDescription.lexicon(self.america), SearchDescription.length(8, 8), SearchDescription.probability_range(3000, 3010), SearchDescription.point_value(14, 30) ]) self.assertEqual(qs.size(), 4) self.assertEqual( ['EHILORTY', 'DEHIOPRT', 'DEINORVW', 'CDEINORV'], qs.alphagram_string_list()) self.assertTrue(len(qs.questions_array()[0].answers[0].definition) > 0)
def get_questions_for_probability_range(self, p_min, p_max, length): """ Use a single query to return alphagrams and words for a probability range, fully populated. """ configs = { 'search_descriptions': [ SearchDescription.length(length, length), SearchDescription.probability_range(p_min, p_max) ] } return self.get_questions_from_configs(configs)
def get_questions_for_probability_list(self, p_list, length): for p in p_list: if type(p) is not int: raise BadInput("Every probability must be an integer. %s" % p_list) if len(p_list) > MAX_CHUNK_SIZE: raise BadInput('Too many alphagrams to interpolate') configs = { 'search_descriptions': [ SearchDescription.length(length, length), SearchDescription.probability_list(p_list) ] } return self.get_questions_from_configs(configs)
def setup_quiz(self, p_min=10, p_max=90, length=8): """ A helper function to start a quiz. """ wwg = WordwallsGame() user = User.objects.get(username='******') lex = Lexicon.objects.get(lexiconName='America') search = SearchDescription.probability_range(p_min, p_max, length, lex) table_id = wwg.initialize_by_search_params(user, search, 240) return table_id, user
def create_some_tags(self): self.america = Lexicon.objects.get(lexiconName='America') self.cesar = User.objects.get(username='******') t = time.time() qs = word_search([ SearchDescription.lexicon(self.america), SearchDescription.length(8, 8), SearchDescription.probability_range(5001, 8500), ]) logger.debug('Initial word search completed in %s seconds', time.time() - t) self.assertEqual(qs.size(), 3500) # Create hella tags. for q in qs.questions_array(): AlphagramTag.objects.create(user=self.cesar, lexicon=self.america, tag='D4', alphagram=q.alphagram.alphagram) logger.debug('And time elapsed after tag creation: %s', time.time() - t)
def get_questions_from_alphagrams(self, alphagrams): """ A helper function to return an entire structure, a list of alphagrams and words, given a list of alphagrams. param: - alphagrams - A list of Alphagram objects. """ configs = { 'search_descriptions': [ SearchDescription.alphagram_list( [a.alphagram for a in alphagrams]) ] } return self.get_questions_from_configs(configs)
def searchForAlphagrams(data, lex): """ Searches for alphagrams using form data """ length = int(data['wordLength']) return SearchDescription.probability_range(data['probabilityMin'], data['probabilityMax'], length, lex)
def test_not_enough_params(self): with self.assertRaises(BadInput) as e: word_search([SearchDescription.length(7, 7)]) self.assertEqual(str(e.exception), 'search_descriptions must have at least 2 elements')