Ejemplo n.º 1
0
    def post(self):
        data = Item.parser.parse_args()

        if ItemModel.find_item_by_name(data["name"]):
            return {"message": "Error: Item already exists"}, 409

        item = ItemModel(**data)
        item.save()

        return item.json(), 201
Ejemplo n.º 2
0
    def put(self, name):
        data = Item.parser.parse_args()

        item = ItemModel.find_by_name(name)

        if item is None:
            item = ItemModel(name, **data)
        else:
            item.price = data['price']

        item.save_to_db()

        return item.json()
Ejemplo n.º 3
0
    def post(self, name):
        self.logger.info(f'parsed args: {Item.parser.parse_args()}')

        if ItemModel.find_by_name(name):
            return {'message': "An item with name '{}' already exists.".format(
                name)}, 400
        data = Item.parser.parse_args()
        item = ItemModel(name, data['price'], data['store_id'])

        try:
            item.save_to_db()
        except:
            return {"message": "An error occurred inserting the item."}, 500
        return item.json(), 201
Ejemplo n.º 4
0
    def test_item_json(self):

        with self.app_context():
            store = StoreModel('test store')
            item = ItemModel('test item', 19.99, 1)

            store.save_to_db()
            item.save_to_db()

            item_json = item.json()

            expected = {'name': 'test item', 'price': 19.99}

            self.assertDictEqual(expected, item_json)
Ejemplo n.º 5
0
    def put(self, name):

        data = Item.parser.parse_args()

        try:
            item = ItemModel.find_item_by_name(name)
            if not item:
                item = ItemModel(**data)
            else:
                item.price = data["price"]
            item.save()

            return item.json()
        except:
            return {"message": "There was an error updating the item"}, 500
Ejemplo n.º 6
0
    def post(self, name):
        if ItemModel.find_by_name(name):
            return {
                'message':
                "An item with name '{}' already exists.".format(name)
            }, 400

        data = Item.parser.parse_args()

        item = ItemModel(name, **data)

        try:
            item.save_to_db()
        except:
            return {"message": "An error occurred inserting the item."}, 500

        return item.json(), 201
Ejemplo n.º 7
0
class ItemTest(TestCase):
    def setUp(self):
        self.item = ItemModel('Caneta', 1.5)

    def test_create_item(self):
        self.assertEqual(self.item.name, 'Caneta')
        self.assertEqual(self.item.price, 1.5)

    def test_save_item(self):
        with patch('app.models.item.db.session.add') as mocked_db_add:
            with patch(
                    'app.models.item.db.session.commit') as mocked_db_commit:
                self.item.save_to_db()

                mocked_db_add.assert_called()
                mocked_db_commit.assert_called()

    def test_delete_item(self):
        with patch('app.models.item.db.session.delete') as mocked_db_delete:
            with patch(
                    'app.models.item.db.session.commit') as mocked_db_commit:
                self.item.delete_from_db()

                mocked_db_delete.assert_called()
                mocked_db_commit.assert_called()

    def test_find_by_name(self):
        app = create_app()
        app.testing = True

        expected = self.item

        with app.app_context():
            self.item.save_to_db()
            resp = ItemModel.find_by_name('Caneta')

            self.assertEqual(resp.name, expected.name)
            self.assertEqual(resp.price, expected.price)

    def test_json(self):
        item_json = self.item.json()
        expected = {'name': 'Caneta', 'price': 1.5}

        self.assertDictEqual(item_json, expected)