def test_store_json(self):
        store = StoreModel('test')
        expected = {'id': None, 'name': 'test', 'items': []}

        self.assertEqual(
            store.json(), expected,
            "The JSON export of the store is incorrect. Received {}, expected {}."
            .format(store.json(), expected))
    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')
Beispiel #3
0
    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')
    def test_delete_store(self):
        with self.app() as c:
            with self.app_context():
                StoreModel('test').save_to_db()
                r = c.delete('/store/test')

                self.assertEqual(r.status_code, 200)
                self.assertDictEqual(d1={'message': 'Store deleted'},
                                     d2=json.loads(r.data.decode()))
    def test_create_store(self):
        store = StoreModel('test')

        self.assertEqual(
            store.name, 'test',
            "The name of the store after creation does not equal the constructor argument."
        )
        self.assertListEqual(
            store.items.all(), [],
            "The store's items length was not 0 even though no items were added."
        )
    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_store_list(self):
        with self.app() as c:
            with self.app_context():
                StoreModel('test').save_to_db()
                r = c.get('/stores')

                self.assertDictEqual(
                    d1={'stores': [{
                        'id': 1,
                        'name': 'test',
                        'items': []
                    }]},
                    d2=json.loads(r.data.decode()))
    def test_create_store(self):
        with self.app() as c:
            with self.app_context():
                r = c.post('/store/test')

                self.assertEqual(r.status_code, 201)
                self.assertIsNotNone(StoreModel.find_by_name('test'))
                self.assertDictEqual(d1={
                    'id': 1,
                    'name': 'test',
                    'items': []
                },
                                     d2=json.loads(r.data.decode()))
Beispiel #9
0
    def post(self, name):
        if StoreModel.find_by_name(name):
            return {
                'message':
                "A store with name '{}' already exists.".format(name)
            }, 400

        store = StoreModel(name)
        try:
            store.save_to_db()
        except:
            return {"message": "An error occurred creating the store."}, 500

        return store.json(), 201
    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={
                    'id':
                    1,
                    'name':
                    'test',
                    'items': [{
                        'price': 2.99,
                        'id': 1,
                        'name': 'test'
                    }]
                },
                                     d2=json.loads(r.data.decode()))
Beispiel #11
0
 def get(self, name):
     store = StoreModel.find_by_name(name)
     if store:
         return store.json()
     return {'message': 'Store not found'}, 404
Beispiel #12
0
    def delete(self, name):
        store = StoreModel.find_by_name(name)
        if store:
            store.delete_from_db()

        return {'message': 'Store deleted'}