Пример #1
0
    def test_item_json(self):
        item = ItemModel('test', 19.99, 1)
        expected = {'name': 'test', 'price': 19.99}

        self.assertEqual(
            item.json(), expected,
            "The JSON export of the item is incorrect. Received {}, expected {}."
            .format(item.json(), expected))
Пример #2
0
    def test_store_relationship(self):
        with self.app_context():
            store = StoreModel('test_store')
            item = ItemModel('test', 19.99, 1)

            store.save_to_db()
            item.save_to_db()

            self.assertEqual(item.store.name, 'test_store')
    def test_store_relationship(self):
        with self.app_context():
            store = StoreModel('test')
            item = ItemModel('test_item', 19.99, 1)

            store.save_to_db()
            item.save_to_db()

            self.assertEqual(store.items.count(), 1)
            self.assertEqual(store.items.first().name, 'test_item')
Пример #4
0
    def test_store_with_items_list(self):
        with self.app() as c:
            with self.app_context():
                StoreModel('test').save_to_db()
                ItemModel('test', 17.99, 1).save_to_db()
                r = c.get('/stores')

                self.assertDictEqual(d1={'stores': [{'name': 'test', 'items': [{'name': 'test', 'price': 17.99}]}]},
                                     d2=json.loads(r.data))
    def test_put_update_item(self):
        with self.app() as c:
            with self.app_context():
                StoreModel('test').save_to_db()
                c.put('/item/test', data={'price': 17.99, 'store_id': 1})
                r = c.put('/item/test', data={'price': 18.99, 'store_id': 1})

                self.assertEqual(r.status_code, 200)
                self.assertEqual(ItemModel.find_by_name('test').price, 18.99)
Пример #6
0
    def test_store_with_items_found(self):
        with self.app() as c:
            with self.app_context():
                StoreModel('test').save_to_db()
                ItemModel('test', 2.99, 1).save_to_db()
                r = c.get('/store/test')

                self.assertEqual(r.status_code, 200)
                self.assertDictEqual(d1={'name': 'test', 'items': [{'name': 'test', 'price': 2.99}]},
                                     d2=json.loads(r.data))
    def test_delete_item(self):
        with self.app() as c:
            with self.app_context():
                StoreModel('test').save_to_db()
                ItemModel('test', 17.99, 1).save_to_db()
                r = c.delete('/item/test')

                self.assertEqual(r.status_code, 200)
                self.assertDictEqual(d1={'message': 'Item deleted'},
                                     d2=json.loads(r.data))
Пример #8
0
    def test_crud(self):
        with self.app_context():
            store = StoreModel('test')
            store.save_to_db()
            item = ItemModel('test', 19.99, 1)

            self.assertIsNone(ItemModel.find_by_name('test'), "Found an item with name 'test' before save_to_db")

            item.save_to_db()

            self.assertIsNotNone(ItemModel.find_by_name('test'),
                                 "Did not find an item with name 'test' after save_to_db")

            item.delete_from_db()

            self.assertIsNone(ItemModel.find_by_name('test'), "Found an item with name 'test' after delete_from_db")
    def test_put_item(self):
        with self.app() as c:
            with self.app_context():
                StoreModel('test').save_to_db()
                r = c.put('/item/test', data={'price': 17.99, 'store_id': 1})

                self.assertEqual(r.status_code, 200)
                self.assertEqual(ItemModel.find_by_name('test').price, 17.99)
                self.assertDictEqual(d1={
                    'name': 'test',
                    'price': 17.99
                },
                                     d2=json.loads(r.data))
    def test_item_found(self):
        with self.app() as c:
            with self.app_context():
                StoreModel('test').save_to_db()
                ItemModel('test', 17.99, 1).save_to_db()
                r = c.get('/item/test',
                          headers={'Authorization': self.auth_header})

                self.assertEqual(r.status_code, 200)
                self.assertDictEqual(d1={
                    'name': 'test',
                    'price': 17.99
                },
                                     d2=json.loads(r.data))
    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()
Пример #12
0
    def test_create_item(self):
        item = ItemModel('test', 19.99, 1)

        self.assertEqual(
            item.name, 'test',
            "The name of the item after creation does not equal the constructor argument."
        )
        self.assertEqual(
            item.price, 19.99,
            "The price of the item after creation does not equal the constructor argument."
        )
        self.assertEqual(
            item.store_id, 1,
            "The store_id of the item after creation does not equal the constructor argument."
        )
        self.assertIsNone(
            item.store,
            "The item's store was not None even though the store was not created."
        )
    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
    def delete(self, name):
        item = ItemModel.find_by_name(name)
        if item:
            item.delete_from_db()

        return {'message': 'Item deleted'}
 def get(self, name):
     item = ItemModel.find_by_name(name)
     if item:
         return item.json()
     return {'message': 'Item not found'}, 404