Exemple #1
0
    def test_store_relationship(self):

        with self.app_context():
            store_label=StoreModel("Costco")
            item=ItemModel("cookies", 19.87, 1)
            store_label.save_to_db()
            item.save_to_db()
            self.assertEqual(item.store.name, "Costco")
Exemple #2
0
 def delete(self, name):
     store = StoreModel(name)
     if store:
         try:
             store.delete_from_db()
         except:
             return {
                 'message': '{} does not exist on the database'.format(name)
             }
     return {'message': 'Store Deleted.'}
Exemple #3
0
    def test_store_relationship(self):
        with self.app_context():
            store = StoreModel("Costco")
            item = ItemModel("ham", "20.11", 1)

            store.save_to_db()
            item.save_to_db()

            self.assertEqual(store.items.count(), 1)
            self.assertEqual(store.items.first().name, "ham")
            self.assertEqual(store.items.first().price, 20.11)
 def delete(self, name):
     store = StoreModel.find_by_name(name)
     if (store):
         store.delete_from_db()
     else:
         return {'message': 'Store not found'}
     return {'message': 'Store Delted'}
Exemple #5
0
    def test_crud(self):

        with self.app_context():
            store = StoreModel("Costco")
            self.assertIsNotNone(store.name)
            self.assertIsNone(StoreModel.find_by_name("Costco"))

            store.save_to_db()

            self.assertIsNotNone(StoreModel.find_by_name("Costco"))

            store.delete_from_db()
            self.assertIsNone(StoreModel.find_by_name("Costco"))
Exemple #6
0
 def test_delete_item(self):
     with self.app() as client:
         with self.app_context():
             # StoreModel("Costco").save_to_db()
             StoreModel("Costco").save_to_db()
             ItemModel("ham", 18.29, 1).save_to_db()
             response = client.delete("/item/ham") #dont need authorization anymore
             self.assertEqual(response.status_code, 200)
Exemple #7
0
    def get(self, name):
        store = StoreModel.find_by_name(name)

        if store:
            return store.json()
        return {
            'message': '{} does not exist on the database'.format(name)
        }, 404
Exemple #8
0
    def test_json_with_item(self):
        with self.app_context():
            store = StoreModel("Costco")
            item = ItemModel("ham", 20.11, 1)

            store.save_to_db()
            item.save_to_db()

            expected = {
                "name": "Costco",
                "item_list": [{
                    "name": "ham",
                    "price": 20.11
                }]
            }

            self.assertDictEqual(store.json(), expected)
Exemple #9
0
 def get(self):
     user_id = get_jwt_identity()
     stores =  [store.json() for store  in StoreModel.find_all()]
     if user_id:
         return {'stores':stores},200
     return {'items': [store['name'] for store in stores],
             'message': 'Login or provide authorization header to get detailed item information'
             },200
Exemple #10
0
 def test_put_update_item(self):
     with self.app() as client:
         with self.app_context():
             StoreModel("Costco").save_to_db()
             ItemModel("ham", 18.29, 1).save_to_db()
             response = client.put("/item/ham", data={"price": 17.55, "store_id": 1})
             self.assertEqual(response.status_code, 200)
             self.assertEqual(ItemModel.find_by_name("ham").price, 17.55)
Exemple #11
0
    def test_create_duplicate_item(self):
        with self.app() as client:
            with self.app_context():
                StoreModel("Costco").save_to_db()
                ItemModel("ham", 18.29, 1).save_to_db()

                response = client.post("/item/ham", data={"price": 17.55, "store_id": 1})
                self.assertEqual(response.status_code, 400)
                self.assertDictEqual({"message": "the item already exists"}, json.loads(response.data))
Exemple #12
0
    def get(self, name):
        item = StoreModel.find_by_name(name)

        if item:
            return {
                'items': item.json()
            }  #item is an object and we have to return a dictionary

        return {'message': 'item not found'}, 404
Exemple #13
0
 def delete(self,name):
     claims = get_jwt_claims()
     if not claims['is_admin']:
         return {'message':'Admin privileges required for this operation'}
     store = StoreModel.find_by_name(name)
     if(store):
         store.delete_from_db()
     else:
         return {'message': 'Store not found'}
     return {'message': 'Store Delted'}
Exemple #14
0
    def test_item_list(self):
        with self.app() as client:
            with self.app_context():
                with self.app_context():
                    StoreModel("Costco").save_to_db()
                    ItemModel("ham", 18.29, 1).save_to_db()

                    response = client.get("/items")
                    self.assertDictEqual(d1={'items': [{'name': 'ham', 'price': 18.29}]},
                                         d2=json.loads(response.data))
Exemple #15
0
    def test_delete_store(self):
        with self.app() as client:
            with self.app_context():
                #client.post("/store/costco")
                StoreModel("Costco").save_to_db()
                response = client.delete("/store/costco")

                self.assertEqual(response.status_code, 200)
                self.assertDictEqual({"message": "Store deleted"},
                                     json.loads(response.data))
Exemple #16
0
    def test_create_item(self):
        with self.app() as client:
            with self.app_context():

                StoreModel("Costco").save_to_db()
                #ItemModel("ham", 18.29, 1).save_to_db()
                response = client.post("/item/ham", data={"price": 17.55, "store_id": 1})  # dont need authorization anymore
                self.assertEqual(response.status_code, 201)
                self.assertEqual(ItemModel.find_by_name('ham').price, 17.55)
                self.assertDictEqual({"name": "ham", "price": 17.55}, json.loads(response.data))
Exemple #17
0
    def test_store_list(self):
        with self.app() as client:
            with self.app_context():
                StoreModel("Costco").save_to_db()
                response = client.get("stores")

                self.assertDictEqual(
                    {"stores": [{
                        "name": "Costco",
                        "item_list": []
                    }]}, json.loads(response.data))
Exemple #18
0
    def test_create_store(self):
        with self.app() as client:
            with self.app_context():
                response = client.post("/store/costco")

                self.assertEqual(response.status_code, 201)
                self.assertIsNotNone(StoreModel.find_by_name("costco"))
                self.assertDictEqual({
                    "name": "costco",
                    "item_list": []
                }, json.loads(response.data))
Exemple #19
0
    def test_find_store(self):
        with self.app() as client:
            with self.app_context():
                StoreModel("Costco").save_to_db()
                response = client.get("store/Costco")

                self.assertEqual(response.status_code, 200)
                self.assertDictEqual({
                    "name": "Costco",
                    "item_list": []
                }, json.loads(response.data))
Exemple #20
0
    def get(self):
        """
        Get all Store details
        GET /stores
        """
        stores = StoreModel.find_all()

        if stores:
            return {"stores": [store.json() for store in stores]}, 200
        else:
            return {"message": "Sorry, No Store Found!"}, 404
Exemple #21
0
    def get(self, name):
        """
        Find a store by name
        GET /store/<string:name>
        """
        exist = StoreModel.find_by_name(name)

        if not exist:
            return {"message": "Sorry, No Store Found!"}, 404
        else:
            return exist.json(), 200
Exemple #22
0
    def test_crud(self):

        with self.app_context():

            StoreModel("COSTCO").save_to_db #save COSTCO to the database in order to deal with foreigner key
            item = ItemModel("cookies", 19.98, 1) #test the foreign key
            self.assertIsNone(ItemModel.find_by_name("cookies"))
            item.save_to_db()
            self.assertIsNotNone(ItemModel.find_by_name("cookies"))
            item.delete_from_db()
            self.assertIsNone(ItemModel.find_by_name("cookies"))
Exemple #23
0
 def post(self, name):
     if StoreModel.find_by_name(name):
         return {'Message': "Store '{}' already present.".format(name)}, 400
     store = StoreModel(name)
     try:
         store.save_to_db()
     except:
         return {"message": "An error occured while inserting a store"}, 50
     return store.json(), 201
Exemple #24
0
    def delete(self):
        """
        Delete a store
        DELETE /store?store_name=<string:name>
        """
        data = request.args
        store_name = data['store_name']

        exist = StoreModel.find_by_name(store_name)

        if exist:
            exist.delete()
            return {"message": "Item Removed!"}, 200

        return {"message", "Store don't exist!"}, 404
Exemple #25
0
    def test_store_found_with_items(self):
        with self.app() as client:
            with self.app_context():
                StoreModel("Costco").save_to_db()
                ItemModel("ham", 19.99, 1).save_to_db()
                response = client.get("store/Costco")

                self.assertEqual(response.status_code, 200)
                self.assertDictEqual(
                    {
                        "name": "Costco",
                        "item_list": [{
                            "name": "ham",
                            "price": 19.99
                        }]
                    }, json.loads(response.data))
Exemple #26
0
 def post(self, name):
     if (StoreModel.find_by_name(name)):
         return {
             'message': "store '{}'is already present".format(name)
         }, 400
     store = StoreModel(name)
     try:
         store.save_to_db()
     except:
         return {'message': 'an error occured while insering the item'}, 500
     return store.json(), 201
Exemple #27
0
    def post(self, name):
        if StoreModel.find_by_name(name):
            return {'message': '{} already exists'.format(name)}, 400

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

        return store.json(), 201
    def post(self, name):
        if (StoreModel.find_by_name(name)):
            return {"message": "Store {} already present".format(name)}

        store = StoreModel(name)
        try:
            store.save_to_db()
        except:
            return {"message": 'An error occured while inserting data'}, 500

        return store.json(), 201
Exemple #29
0
    def post(self, name):
        if StoreModel.find_by_name(name):
            return {
                'Message': 'Store name {} already exists.'.format(name)
            }, 400

        store = StoreModel(name)
        try:
            store.save_db()
        except:
            return {'Message': 'An Error Occuured While creating store'}
        return store.json(), 201
Exemple #30
0
 def post(self, name):
     ans = StoreModel.get_store_byname(name)
     if ans:
         return {
             'message': 'Store with the name {} already exists'.format(name)
         }, 400
     store = StoreModel(name)
     try:
         store.save_to_db()
     except:
         return {'message': 'There was a problem posting the store'}, 500
     return store.json(), 201