def index(): try: model = Words() words = model.get_words() return Success.response(words) except Exception as err: message = f'Endpoint </words/> method <GET> failure|description: {err}' abort(500, message)
def update(word: str): try: body = request.get_json(force=True) body.update({'word': word}) schema = WordSchema() data = schema.load(body) model = Words() internal_process = model.update_word(data.word, data.position) if not internal_process.operation: abort(400, internal_process.message) return Success.response({'word': data.word, 'position': data.position}) except ValidationError as err: abort(400, str(err))
def insert(): try: body = request.get_json(force=True) schema = WordSchema() data = schema.load(body) word = data.word position = data.position model = Words() WordRepeated.exit(model, data.word) model.add_word(word, position) return Success.response({ 'word': data.word, 'position': data.position }, 201) except ValidationError as err: abort(400, str(err)) except Exception as err: status, message, *extra = str(err).split(':') status, *others = status.split(' ') abort(int(status), message)
def anagram(word: str): try: container = Words().get_anagrams(word) if not container: return Success.response(container) anagrams = Anagrams.get_coincidences(word, container) if bool(int(getenv('WILL_HAVE_WORDS_REPETEAD', 0))): anagrams = Anagrams.unique_list_same_order(anagrams) return Success.response(data=anagrams) except Exception as err: message = f'Endpoint </word/anagrams> method <GET> failure|description: {err}' abort(500, message)
class TestWords(TestCase): @mock.patch('app.models.words.Transactions') @mock.patch('app.models.words.BaseLogger') @mock.patch('app.models.words.mongo_db.cx') @mock.patch('app.models.words.mongo_db.db') def setUp(self, mock_mongo_db, mock_mongo_cx, mock_transactions, mock_logger) -> None: self.words = Words() def tearDown(self) -> None: del self.words def test_get_words(self): collection = self.words.collection item = {'word': 'fake', 'position': 1} collection.find.return_value.sort.return_value = [item] output = self.words.get_words() expected = [item.get('word')] self.assertListEqual(expected, output) @mock.patch('app.models.words.Anagrams.remove_item_in_list') def test_get_anagrams(self, mock_remove): collection = self.words.collection item = {'word': 'fake', 'position': 1} collection.find.return_value.sort.return_value = [item] anagram = 'fake' self.words.get_anagrams(anagram) self.assertTrue(mock_remove.called) @mock.patch('app.models.words.InternalProcess') @mock.patch('app.models.words.UpdateOne') @mock.patch('app.models.words.InsertOne') @mock.patch('app.models.words.BaseQueries.find_one') def test_add_word(self, mock_find_one, mock_insert_one, mock_update_one, mock_internal): mock_find_one.return_value = [{'word': 'fake', 'position': 0}] collection = self.words.collection item = {'word': 'dummy', 'position': 111} collection.find.return_value.sort.return_value = [item] word = 'example' position = 1 self.words.add_word(word, position) self.assertTrue(mock_update_one.called) self.assertTrue(mock_insert_one.called_once) self.assertTrue(mock_internal.called_once) @mock.patch('app.models.words.BaseQueries.find_one') def test_update_word_not_found_word(self, mock_find_one): mock_find_one.return_value = None word = 'dummy' position = 1 output = self.words.update_word(word, position) self.assertTrue(mock_find_one.called) self.assertIsInstance(output, InternalProcess) @mock.patch('app.models.words.InternalProcess') @mock.patch('app.models.words.UpdateOne') @mock.patch('app.models.words.InsertOne') @mock.patch('app.models.words.BaseQueries.find_one') def test_update_word(self, mock_find_one, mock_insert_one, mock_update_one, mock_internal): mock_find_one.return_value = { '_id': 1000, 'word': 'fake', 'position': 0 } collection = self.words.collection item = {'word': 'dummy', 'position': 111} collection.find.return_value.sort.return_value = [item] word = 'example' position = 1 self.words.update_word(word, position) self.assertTrue(mock_update_one.called) self.assertTrue(mock_insert_one.called_once) self.assertTrue(mock_internal.called_once) def test_reset_collection(self): collection = self.words.collection collection.remove = mock.Mock() total_expected = 0 collection.remove.return_value.get.return_value = total_expected output = self.words.reset_collection_data() self.assertEqual(output, total_expected) @mock.patch('app.models.words.BaseQueries.delete_one') @mock.patch('app.models.words.BaseQueries.find_one') def test_delete_word_failure(self, mock_fine_one, mock_delete_one): mock_fine_one.return_value = None word = 'dummy' output = self.words.delete_word(word) self.assertIsInstance(output, InternalProcess) self.assertFalse(output.operation) @mock.patch('app.models.words.BaseQueries.delete_one') @mock.patch('app.models.words.BaseQueries.find_one') def test_delete_word_success(self, mock_fine_one, mock_delete_one): word = 'dummy' mock_fine_one.return_value = {'_id': 1111, 'word': word, 'position': 1} output = self.words.delete_word(word) self.assertIsInstance(output, InternalProcess) self.assertTrue(output.operation)
def setUp(self, mock_mongo_db, mock_mongo_cx, mock_transactions, mock_logger) -> None: self.words = Words()
def reset(): registers = Words().reset_collection_data() if registers is None: registers = 0 return Success.response(['Operation reset successfully', registers], 205)
def delete(tag: str): Words().delete_word(tag) return Success.response(code=204)