示例#1
0
    def test_jason(self):
        store = StoreModel('costco')
        item = ItemModel('pc', 1500, 1)

        expected = []

        self.assertEqual(store.json(), expected)
示例#2
0
 def test_delete_store(self):
     with self.app() as client:
         with self.app_context():
             StoreModel('costco').save_to_db()
             request = client.delete('/store/costco')
             self.assertIsNone(StoreModel.find_by_name('costco'))
             self.assertEqual(request.status_code, 200)
             self.assertDictEqual({'message': 'Store deleted'},
                                  json.loads(request.data))
示例#3
0
    def test_relationship_items(self):
        with self.app_contex():
            store = StoreModel('costco')
            item = ItemModel('pc', 1500, 1)

            store.save_to_db()
            item.save_to_db()

            self.assertEqual(store.items.count(), 1)
            self.assertEqual(store.items.first().name, 'pc')
示例#4
0
    def test_jason(self):
        store = StoreModel('costco')
        item = ItemModel('pc', 1500, 1)

        store.save_to_db()
        item.save_to_db()

        expected = {'name': 'costco', 'items': [{'name': 'pc', 'price': 1500}]}

        self.assertEqual(store.json(), expected)
示例#5
0
    def test_store_relashionship(self):

        with self.app_context:
            store = StoreModel('costco')
            item = ItemModel('pc', 1500, 1)

            store.save_to_db()
            item.save_to_db()

            self.assertEqual(item.store.name, 'costco')
示例#6
0
    def test_crud(self):

        with self.app_contex():
            store = StoreModel('costco')
            self.assertIsNone(StoreModel.find_by_name('costco'))

            store.save_to_db()

            self.assertIsNotNone(StoreModel.find_by_name('test'))

            store.delete_from_db()
            self.assertIsNone(StoreModel.find_by_name('costco'))
示例#7
0
 def test_get_item(self):
     with self.app() as client:
         with self.app_context():
             StoreModel('test').save_to_db()
             ItemModel('test', 19.99, 1).save_to_db()
             resp = client.get('/item/test',
                               headers={'Authorization': self.access_token})
             self.assertEqual(resp.status_code, 200)
示例#8
0
    def test_delete_store(self):
        with self.app() as client:
            with self.app_context():
                StoreModel('test').save_to_db()
                resp = client.delete('/store/test')

                self.assertEqual(resp.status_code, 200)
                self.assertDictEqual({'message': 'Store deleted'},
                                     json.loads(resp.data))
示例#9
0
 def test_delete_item(self):
     with self.app() as client:
         with self.app_context():
             StoreModel('costco').save_to_db()
             ItemModel('pc', 1500).save_to_db()
             request = client.delete('/item/pc')
             self.assertEqual(request.status_code, 200)
             self.assertDictEqual({'message': 'Item deleted'},
                                  json.loads(request.data))
示例#10
0
 def test_create_store(self):
     with self.app() as client:
         with self.app_context():
             request = client.post('/store/costco')
             self.assertEqual(request.satus_code, 201)
             self.assertIsNotNone(StoreModel.find_by_name('costco'))
             self.assertDictEqual({
                 'name': 'test',
                 'item': []
             }, json.loads(request.data))
示例#11
0
    def test_delete_item(self):
        with self.app() as client:
            with self.app_context():
                StoreModel('test').save_to_db()
                ItemModel('test', 19.99, 1).save_to_db()

                resp = client.delete('/item/test')
                self.assertEqual(resp.status_code, 200)
                self.assertDictEqual({'message': 'Item deleted'},
                                     json.loads(resp.data))
示例#12
0
    def test_store_list(self):
        with self.app() as client:
            with self.app_context():
                StoreModel('test').save_to_db()

                resp = client.get('/stores')
                self.assertDictEqual(
                    {'stores': [{
                        'name': 'test',
                        'items': []
                    }]}, json.loads(resp.data))
示例#13
0
    def test_delete_store(self):
        with self.app as client:
            # to access the db
            with self.app_context():
                # create the store and then delete it
                StoreModel('Superstore').save_to_db()
                response = client.delete('/store/Superstore')

                self.assertEqual(response.status_code, 200)
                self.assertEqual({'message': 'Store deleted'},
                                 json.loads(response.data.decode('utf-8')))
示例#14
0
    def test_put_update_item(self):
        with self.app() as client:
            with self.app_context():
                StoreModel('costco').save_to_db()
                ItemModel('pc', 1450, 1)
                request = client.get('/item/pc')

                self.assertDictEqual({'item': [{
                    'name': 'pc',
                    'price': 1500
                }]}, json.loads(request.data))
示例#15
0
 def test_find_store(self):
     with self.app() as client:
         with self.app_context():
             StoreModel('costco').save_to_db()
             request = client.get('/store/costco')
             self.assertEqual(request.status_code, 200)
             # self.assertEqual(StoreModel.find_by_name('costco'))
             self.assertDictEqual({
                 'name': 'costco',
                 'item': []
             }, json.loads(request.data))
示例#16
0
 def test_store_list(self):
     with self.app() as client:
         with self.app_context():
             StoreModel('costco').save_to_db()
             request = client.get('/stores')
             self.assertEqual(request.status_code, 200)
             self.assertDictEqual(
                 {'stores': [{
                     'name': 'costco',
                     'item': []
                 }]}, json.loads(request.data))
示例#17
0
    def test_find_store(self):
        with self.app() as client:
            with self.app_context():
                StoreModel('test').save_to_db()
                resp = client.get('/store/test')

                self.assertEqual(resp.status_code, 200)
                self.assertDictEqual({
                    'name': 'test',
                    'items': []
                }, json.loads(resp.data))
示例#18
0
    def test_find_store(self):
        with self.app as client:
            # to access the db
            with self.app_context():
                StoreModel('Superstore').save_to_db()
                response = client.get('/store/Superstore')

                self.assertEqual(response.status_code, 200)
                self.assertDictEqual({
                    'items': [],
                    'name': 'Superstore'
                }, json.loads(response.data.decode('utf-8')))
示例#19
0
    def test_item_list(self):
        with self.app() as client:
            with self.app_context():
                StoreModel('test').save_to_db()
                ItemModel('test', 5.99, 1).save_to_db()

                resp = client.get('/items')

                self.assertDictEqual(
                    {'items': [{
                        'name': 'test',
                        'price': 5.99
                    }]}, json.loads(resp.data))
示例#20
0
    def test_create_store(self):
        with self.app as client:
            # to access the db
            with self.app_context():
                # client.delete('Superstore')
                response = client.post('/store/' + StoreTest.store)

                self.assertEqual(response.status_code, 201)
                self.assertIsNotNone(StoreModel.find_by_name(StoreTest.store))
                self.assertDictEqual({
                    'items': [],
                    'name': 'Superstore'
                }, json.loads(response.data.decode('utf-8')))
示例#21
0
 def test_create_duplicate_item(self):
     with self.app() as client:
         with self.app_context():
             StoreModel('costco').save_to_db()
             ItemModel('pc', 1500, 1).save_to_db()
             request = client.post('/item/pc',
                                   data={
                                       'name': 'pc',
                                       'store_id': 1
                                   })
             self.assertEqual(request.status_code, 400)
             self.assertDictEqual(
                 {'message': 'n item with name pc already exists.'},
                 json.loads(request.data))
示例#22
0
    def test_put_item(self):
        with self.app() as client:
            with self.app_context():
                StoreModel('costco').save_to_db()

                request = client.put('/item/pc',
                                     data={
                                         'name': 'pc',
                                         'store_id': 1
                                     })
                self.assertEqual(request.status_code, 200)
                self.assertDictEqual({
                    'name': 'pc',
                    'price': 1500
                }, json.loads(request.data))
示例#23
0
 def test_store_found_with_items(self):
     with self.app() as client:
         with self.app_context():
             StoreModel('costco').save_to_db()
             ItemModel('pc', 1500, 1).save_to_db()
             request = client.get('/store/costco')
             self.assertEqual(request.status_code, 200)
             self.assertDictEqual(
                 {
                     'message': 'costco',
                     'item': [{
                         'name': 'pc',
                         'price': 1500
                     }]
                 }, json.loads(request.data))
示例#24
0
    def test_put_item(self):
        with self.app() as client:
            with self.app_context():
                StoreModel('test').save_to_db()
                resp = client.put('/item/test',
                                  data={
                                      'price': 17.99,
                                      'store_id': 1
                                  })

                self.assertEqual(resp.status_code, 200)
                self.assertEqual(ItemModel.find_by_name('test').price, 17.99)
                self.assertDictEqual({
                    'name': 'test',
                    'price': 17.99
                }, json.loads(resp.data))
示例#25
0
    def test_create_duplicate_item(self):
        with self.app() as client:
            with self.app_context():
                StoreModel('test').save_to_db()
                ItemModel('test', 17.99, 1).save_to_db()

                resp = client.post('/item/test',
                                   data={
                                       'price': 17.99,
                                       'store_id': 1
                                   })

                self.assertEqual(resp.status_code, 400)
                self.assertDictEqual(
                    {'message': 'An item with name \'test\' already exists.'},
                    json.loads(resp.data))
示例#26
0
    def test_store_found_with_items(self):
        with self.app() as client:
            with self.app_context():
                StoreModel('test').save_to_db()
                ItemModel('test', 19.99, 1).save_to_db()

                resp = client.get('/store/test')
                self.assertEqual(resp.status_code, 200)
                self.assertDictEqual(
                    {
                        'name': 'test',
                        'items': [{
                            'name': 'test',
                            'price': 19.99
                        }]
                    }, json.loads(resp.data))
示例#27
0
    def test_create_item(self):
        with self.app() as client:
            with self.app_context():
                StoreModel('test').save_to_db()

                resp = client.post('/item/test',
                                   data={
                                       'price': 17.99,
                                       'store_id': 1
                                   })

                self.assertEqual(resp.status_code, 201)
                self.assertDictEqual({
                    'name': 'test',
                    'price': 17.99
                }, json.loads(resp.data))
示例#28
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
示例#29
0
    def test_store_found_with_items(self):
        with self.app as client:
            # to access the db
            with self.app_context():
                StoreModel('test').save_to_db()
                ItemModel('testItem', 19.99, 1).save_to_db()

                response = client.get('/store/test')
                self.assertEqual(response.status_code, 200)
                self.assertDictEqual(
                    {
                        'name': 'test',
                        'items': [{
                            'name': 'testItem',
                            'price': 19.99
                        }]
                    }, json.loads(response.data.decode('utf-8')))
示例#30
0
    def test_crud(self):

        with self.app_context():
            StoreModel('costco').save_to_db()
            item = ItemModel('costco', 19.99, 1)

            self.assertIsNone(
                ItemModel.find_by_name('test'),
                "Found an item with name {}, but expected not to.".format(
                    item.name))

            item.save_to_db()

            self.assertIsNotNone(ItemModel.find_by_name('test'))

            item.delete_from_db()

            self.assertIsNone(ItemModel.find_by_name('test'))