示例#1
0
    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())
示例#2
0
    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'))
示例#3
0
def show(word_id):
    # Get the word
    word = WordsService.get_instance().get(word_id)

    if word:
        return render_view('words/show', 200, word=word.serialized)

    return render_view('422', 422, errors=NOT_FOUND_ERROR, inputs={'id': word_id})
示例#4
0
def index():
    # Get the input validator
    inputs = ListInputs(get_inputs())

    # Verify the list inputs
    if inputs.validate():

        words = WordsService.get_instance().get_list(inputs.limit.data, inputs.offset.data)

        return render_view('words/index', 200, words={word.get_id(): word.serialized for word in words})

    return render_view('422', 422, errors=inputs.errors, inputs=inputs.serialized())
示例#5
0
def delete(word_id):
    # Get the word
    word = WordsService.get_instance().get(word_id)

    # Verify the word creation inputs
    if word:
        try:
            word.update(**{'is_active': False})
            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={'id': word_id})

    return render_view('422', 422, errors=NOT_FOUND_ERROR, inputs={'id': word_id})
示例#6
0
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})
示例#7
0
    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 update(definition_template_id):
    """
    Request:
    {
        "word_id": "word_id",
        "definition": "definition",
        "filler_lexical_classes": "filler_lexical_classes",
        "is_active": "is_active"
    }

    Response [422] (definition_template with definition_template_id doesn't exist):
    {
        "errors": {
            "DefinitionTemplateNotFound": [
                "Unable to find DefinitionTemplate"
            ]
        },
        "inputs": {
            "id": "definition_template_id"
        }
    }

    Response [422] (invalid parameters):
    {
        "errors": {
            "name of parameter that failed validation": [
                "Reason for validation failure"
            ],
            "name of another parameter that failed validation": [
                "Reason for validation failure"
            ],
        },
        "inputs": {
            "id": "definition_template_id",
            "word_id": "value passed in. empty string if missing",
            "definition": "value passed in. empty string if missing",
            "filler_lexical_classes": "value passed in. empty string if missing",
            "is_active": "value passed in. empty string if missing"
        }
    }

    Response [422] (word with word_id doesn't exist):
    {
        "errors": {
            "WordNotFound": [
                "Unable to find the specified Word"
            ]
        },
        "inputs": {
            "id": "definition_template_id",
            "word_id": "value passed in. empty string if missing",
            "definition": "value passed in. empty string if missing",
            "filler_lexical_classes": "value passed in. empty string if missing",
            "is_active": "value passed in. empty string if missing"
        }
    }

    Response [422] (save failure - invalid filler_lexical_classes length):
    {
        "errors": {
            "AttributeError": [
                "There are {} filler lexical classes but {} fillers. These values must be the same."
            ]
        },
        "inputs": {
            "id": "definition_template_id",
            "word_id": "value passed in. empty string if missing",
            "definition": "value passed in. empty string if missing",
            "filler_lexical_classes": "value passed in. empty string if missing",
            "is_active": "value passed in. empty string if missing"
        }
    }

    Response [422] (save failure - invalid filler_lexical_classes value):
    {
        "errors": {
            "AttributeError": [
                "Cannot set the filler lexical classes to a value other than one of: {}"
            ]
        },
        "inputs": {
            "id": "definition_template_id",
            "word_id": "value passed in. empty string if missing",
            "definition": "value passed in. empty string if missing",
            "filler_lexical_classes": "value passed in. empty string if missing",
            "is_active": "value passed in. empty string if missing"
        }
    }

    Response [422] (save failure):
    {
        "errors": {
            "IntegrityError": [
                "Reason saving to the db failed"
            ]
        },
        "inputs": {
            "id": "definition_template_id",
            "word_id": "value passed in. empty string if missing",
            "definition": "value passed in. empty string if missing",
            "filler_lexical_classes": "value passed in. empty string if missing",
            "is_active": "value passed in. empty string if missing"
        }
    }

    Response [200] (success):
    {
        "id": "current value",
        "date_created": "current value",
        "date_modified": "current value",
        "word": "current value",
        "definition": "current value",
        "filler_lexical_classes": "current value",
        "is_active": "current value"
    }
    """
    # Get the definition_template
    definition_template = DefinitionTemplatesService.get_instance().get(definition_template_id)

    # Verify the definition_template creation inputs
    if definition_template:

        # Get the input validator
        inputs = UpdateInputs(get_inputs())
        combined_inputs = dict(inputs.serialized().items() + {'id': definition_template_id}.items())

        if inputs.validate_on_submit():
            # If we're only marking the definition as active or inactive, pass through to the update
            if inputs.is_active.data and \
                    not any([inputs.word_id.data, inputs.definition.data, inputs.filler_lexical_classes.data]):
                try:
                    definition_template.update(**get_mixed_dict_from_multidict(get_inputs(), inputs))
                    return render_view(
                        'definition_templates/show', 200, definition_template=definition_template.serialized
                    )
                except Exception as e:
                    return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=combined_inputs)

            # Otherwise, if we're trying to change the definition, mark the old
            # definition as inactive and create a new one with the new parameters
            else:
                definition_template.update(**{'is_active': False})

                word = WordsService.get_instance().get(inputs.word_id.data) \
                    if inputs.word_id.data else definition_template.get_word()
                definition = inputs.definition.data if inputs.definition.data else definition_template.get_definition()
                filler_lexical_classes = inputs.filler_lexical_classes.data \
                    if inputs.filler_lexical_classes.data else definition_template.get_filler_lexical_classes()
                is_active = inputs.is_active.data if inputs.is_active.data else definition_template.get_is_active()

                if word:
                    try:
                        definition_template = DefinitionTemplate(word, definition, filler_lexical_classes)
                        definition_template.set_is_active(is_active)
                        definition_template.save()
                        return render_view(
                            'definition_templates/show', 200, definition_template=definition_template.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=WORD_NOT_FOUND_ERROR, inputs=inputs.serialized())

        return render_view('422', 422, errors=inputs.errors, inputs=combined_inputs)

    return render_view('422', 422, errors=NOT_FOUND_ERROR, inputs={'id': definition_template_id})
def create():
    """
    Request:
    {
        "word_id": "word_id",
        "definition": "definition",
        "filler_lexical_classes": "filler_lexical_classes"
    }

    Response [422] (invalid parameters):
    {
        "errors": {
            "name of parameter that failed validation": [
                "Reason for validation failure"
            ],
            "name of another parameter that failed validation": [
                "Reason for validation failure"
            ],
        },
        "inputs": {
            "word_id": "value passed in. empty string if missing",
            "definition": "value passed in. empty string if missing",
            "filler_lexical_classes": "value passed in. empty string if missing"
        }
    }

    Response [422] (word with word_id doesn't exist):
    {
        "errors": {
            "WordNotFound": [
                "Unable to find the specified Word"
            ]
        },
        "inputs": {
            "word_id": "value passed in. empty string if missing",
            "definition": "value passed in. empty string if missing",
            "filler_lexical_classes": "value passed in. empty string if missing"
        }
    }

    Response [422] (save failure - invalid filler_lexical_classes length):
    {
        "errors": {
            "AttributeError": [
                "There are {} filler lexical classes but {} fillers. These values must be the same."
            ]
        },
        "inputs": {
            "word_id": "value passed in. empty string if missing",
            "definition": "value passed in. empty string if missing",
            "filler_lexical_classes": "value passed in. empty string if missing"
        }
    }

    Response [422] (save failure - invalid filler_lexical_classes value):
    {
        "errors": {
            "AttributeError": [
                "Cannot set the filler lexical classes to a value other than one of: {}"
            ]
        },
        "inputs": {
            "word_id": "value passed in. empty string if missing",
            "definition": "value passed in. empty string if missing",
            "filler_lexical_classes": "value passed in. empty string if missing"
        }
    }

    Response [422] (save failure):
    {
        "errors": {
            "IntegrityError": [
                "Reason saving to the db failed"
            ]
        },
        "inputs": {
            "word_id": "value passed in. empty string if missing",
            "definition": "value passed in. empty string if missing",
            "filler_lexical_classes": "value passed in. empty string if missing"
        }
    }

    Response [200] (success):
    {
        "id": "current value",
        "date_created": "current value",
        "date_modified": "current value",
        "word": "current value",
        "definition": "current value",
        "filler_lexical_classes": "current value",
        "is_active": "current value"
    }
    """
    # Get the input validator
    inputs = CreateInputs(get_inputs())

    # Verify the definition_template creation inputs
    if inputs.validate_on_submit():

        word = WordsService.get_instance().get(inputs.word_id.data)
        if word:
            try:
                definition_template = DefinitionTemplate(word, inputs.definition.data, inputs.filler_lexical_classes.data)
                definition_template.save()
                return render_view('definition_templates/show', 201, definition_template=definition_template.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=WORD_NOT_FOUND_ERROR, inputs=inputs.serialized())

    return render_view('422', 422, errors=inputs.errors, inputs=inputs.serialized())