def test_update_creates_word_and_inactivates_existing_word(self): word_id = self.word.get_id() update_url = '/words/{}'.format(word_id) self.assertEqual(True, self.word.get_is_active()) lexeme_form = self.word.get_lexeme_form() lexical_class = Word.LEXICAL_CLASSES[0] word_data = { 'lexeme_form': lexeme_form, 'lexical_class': lexical_class } response = self.put(update_url, data=word_data) self.assertEqual(200, response.status_code) word = json.loads(response.data) self.assertIsNotNone(word.get('id')) self.assertIsNotNone(word.get('date_created')) self.assertIsNotNone(word.get('date_modified')) self.assertEqual(lexeme_form, word.get('lexeme_form')) self.assertEqual(lexical_class, word.get('lexical_class')) self.assertNotEqual(word_id, word.get('id')) self.assertIsNotNone(word.get('is_active')) # Make sure the word was actually updated in the database saved_word = WordsService.get_instance().get(int(word.get('id'))) self.assertEqual(saved_word.get_id(), word.get('id')) self.assertEqual(Word.dump_datetime(saved_word.get_date_created()), word.get('date_created')) self.assertEqual(Word.dump_datetime(saved_word.get_date_modified()), word.get('date_modified')) self.assertEqual(saved_word.get_lexeme_form(), word.get('lexeme_form')) self.assertEqual(saved_word.get_lexical_class(), word.get('lexical_class')) self.assertEqual(saved_word.get_is_active(), word.get('is_active')) # Ensure old lexeme_form template was marked inactive old_word = WordsService.get_instance().get(word_id) self.assertEqual(False, old_word.get_is_active())
def test_create_returns_created_status(self): create_url = '/words' lexeme_form = 'foo' lexical_class = Word.LEXICAL_CLASSES[0] word_data = { 'lexeme_form': lexeme_form, 'lexical_class': lexical_class } response = self.post(create_url, data=word_data) self.assertEqual(201, response.status_code) word = json.loads(response.data) self.assertIsNotNone(word.get('id')) self.assertIsNotNone(word.get('date_created')) self.assertIsNotNone(word.get('date_modified')) self.assertEqual(lexeme_form, word.get('lexeme_form')) self.assertEqual(lexical_class, word.get('lexical_class')) self.assertEqual(True, word.get('is_active')) # Make sure the word was actually saved to the database saved_word = WordsService.get_instance().get(int(word.get('id'))) self.assertEqual(saved_word.get_id(), word.get('id')) self.assertEqual(Word.dump_datetime(saved_word.get_date_created()), word.get('date_created')) self.assertEqual(Word.dump_datetime(saved_word.get_date_modified()), word.get('date_modified')) self.assertEqual(saved_word.get_lexeme_form(), word.get('lexeme_form')) self.assertEqual(saved_word.get_lexical_class(), word.get('lexical_class')) self.assertEqual(saved_word.get_is_active(), word.get('is_active'))
def create(): # Get the input validator inputs = CreateInputs(get_inputs()) # Verify the word creation inputs if inputs.validate_on_submit(): word = Word(inputs.lexeme_form.data, inputs.lexical_class.data) try: word.save() return render_view('words/show', 201, word=word.serialized) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=inputs.serialized()) return render_view('422', 422, errors=inputs.errors, inputs=inputs.serialized())
def update(word_id): # Get the word word = WordsService.get_instance().get(word_id) # Verify the word creation inputs if word: # Get the input validator inputs = UpdateInputs(get_inputs()) combined_inputs = dict(inputs.serialized().items() + {'id': word_id}.items()) if inputs.validate_on_submit(): # If we're only marking the word as active or inactive, pass through to the update if inputs.is_active.data and \ not any([inputs.lexeme_form.data, inputs.lexical_class.data]): try: word.update(**get_mixed_dict_from_multidict(get_inputs(), inputs)) return render_view('words/show', 200, word=word.serialized) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=combined_inputs) else: word.update(**{'is_active': False}) lexeme_form = inputs.lexeme_form.data if inputs.lexeme_form.data else word.get_lexeme_form() lexical_class = inputs.lexical_class.data if inputs.lexical_class.data else word.get_lexical_class() is_active = inputs.is_active.data if inputs.is_active.data else word.get_is_active() word = Word(lexeme_form, lexical_class) word.set_is_active(is_active) try: word.save() return render_view( 'words/show', 200, word=word.serialized ) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=combined_inputs) return render_view('422', 422, errors=inputs.errors, inputs=combined_inputs) return render_view('422', 422, errors=NOT_FOUND_ERROR, inputs={'id': word_id})
def test_delete_deletes_word(self): delete_url = '/words/{}'.format(self.word.get_id()) self.assertEqual(True, self.word.get_is_active()) response = self.delete(delete_url) self.assertEqual(200, response.status_code) word = json.loads(response.data) self.assertIsNotNone(word.get('id')) self.assertIsNotNone(word.get('date_created')) self.assertIsNotNone(word.get('date_modified')) self.assertIsNotNone(word.get('lexeme_form')) self.assertIsNotNone(word.get('lexical_class')) self.assertEqual(False, word.get('is_active')) # Make sure the word was actually updated in the database saved_word = WordsService.get_instance().get(int(word.get('id'))) self.assertEqual(saved_word.get_id(), word.get('id')) self.assertEqual(Word.dump_datetime(saved_word.get_date_created()), word.get('date_created')) self.assertEqual(Word.dump_datetime(saved_word.get_date_modified()), word.get('date_modified')) self.assertEqual(saved_word.get_lexeme_form(), word.get('lexeme_form')) self.assertEqual(saved_word.get_lexical_class(), word.get('lexical_class')) self.assertEqual(saved_word.get_is_active(), word.get('is_active'))
def insert_dummy_data(self): word = None definition_template = None definition_filler = None for word_data in WORDS: word = Word(word_data.get('lexeme_form'), word_data.get('lexical_class')) word.save() self.NUM_WORDS += 1 for template_data in word_data.get('definition_templates', []): definition_template = DefinitionTemplate( word, template_data.get('definition'), template_data.get('filler_lexical_classes') ) definition_template.save() self.NUM_DEFINITION_TEMPLATES += 1 for filler_data in template_data.get('definition_fillers', []): definition_filler = DefinitionFiller( definition_template, filler_data.get('filler'), filler_data.get('is_dictionary') ) definition_filler.save() self.NUM_DEFINITION_FILLERS += 1 self.word = word self.definition_template = definition_template self.definition_filler = definition_filler