def test_create_order(self): """ Create a new Order """ order = OrderFactory() resp = self.app.post("/orders", json=order.serialize(), content_type="application/json") # pylint: disable=maybe-no-member self.assertEqual(resp.status_code, status.HTTP_201_CREATED) # Make sure location header is set location = resp.headers.get("Location", None) self.assertIsNotNone(location) # Check the data is correct new_order = resp.get_json() self.assertEqual(new_order["name"], order.name, "Names does not match") self.assertEqual(new_order["products"], order.products, "Product does not match") # pylint: disable=maybe-no-member self.assertEqual(new_order["status"], order.status, "Status does not match") # pylint: disable=maybe-no-member # Check that the location header was correct by getting it resp = self.app.get(location, content_type="application/json") self.assertEqual(resp.status_code, status.HTTP_200_OK) new_order = resp.get_json() self.assertEqual(new_order["name"], order.name, "Names does not match") self.assertEqual(new_order["products"], order.products, "Product does not match") # pylint: disable=maybe-no-member self.assertEqual(new_order["status"], order.status, "Status does not match") # pylint: disable=maybe-no-member
def test_unsupported_media_type(self): """ Send wrong media type """ order = OrderFactory() resp = self.app.post("/orders", json=order.serialize(), content_type="test/html") # pylint: disable=maybe-no-member self.assertEqual(resp.status_code, status.HTTP_415_UNSUPPORTED_MEDIA_TYPE) # pylint: disable=maybe-no-member
def test_getting_order_list_by_regular_user(regular): user, client = regular for _ in range(3): OrderFactory() OrderFactory(user=user) response = client.get("/orders/") assert response.status_code == 200 assert len(response.data) == 1 assert response.data[0]["user"] == user.id
def _create_orders(self, count): """ Factory method to create orders in bulk """ orders = [] for _ in range(count): order = OrderFactory() resp = self.app.post("/orders", json=order.serialize(), content_type="application/json") # pylint: disable=maybe-no-member self.assertEqual(resp.status_code, status.HTTP_201_CREATED, "Could not create test Order") new_order = resp.get_json() order.id = new_order["id"] orders.append(order) return orders
def test_getting_single_order(regular): user, client = regular order = OrderFactory(user=user) response = client.get(f"/orders/{order.id}/") assert response.status_code == 200 assert response.data["user"] == user.id
def main(num_manufacturers, num_items_per_man, num_orders): mans = ManufacturerFactory.create_batch(num_manufacturers) print(f"Created {num_manufacturers} Manufacturers") items = list( chain.from_iterable([ ItemFactory.create_batch(num_items_per_man, manufacturer=man) for man in mans ])) print(f"Created {len(items)} Items") users = UserFactory.create_batch(50) for _ in range(0, num_orders): num_items = random.randint(5, 20) user = random.choice(users) OrderFactory.create(user=user, items=random.sample(items, num_items)) print(f"Created {num_orders} Orders")
def test_create_an_order(self): """ Create a Order and assert that it exists """ fake_order = OrderFactory() order = Order(name=fake_order.name, status=fake_order.status) self.assertTrue(order is not None) self.assertEqual(order.id, None) self.assertEqual(order.name, fake_order.name) self.assertEqual(order.status, fake_order.status)
def test_update_order(self): """ Update an existing Order """ # create an Order to update test_order = OrderFactory() resp = self.app.post("/orders", json=test_order.serialize(), content_type="application/json") # pylint: disable=maybe-no-member self.assertEqual(resp.status_code, status.HTTP_201_CREATED) # update the pet new_order = resp.get_json() new_order["name"] = "Happy-Happy Joy-Joy" resp = self.app.put("/orders/{}".format(new_order["id"]), json=new_order, content_type="application/json") self.assertEqual(resp.status_code, status.HTTP_200_OK) updated_order = resp.get_json() self.assertEqual(updated_order["name"], "Happy-Happy Joy-Joy")
def _create_order(self, products=[]): """ Creates an order from a Factory """ fake_order = OrderFactory() order = Order( name=fake_order.name, status=fake_order.status, products=products ) self.assertTrue(order is not None) self.assertEqual(order.id, None) return order
def test_regular_user_changing_order_properties_not_allowed(regular): user, client = regular order = OrderFactory(user=user) restaurant = RestaurantFactory() response = client.patch(f"/orders/{order.id}/", {"restaurant": restaurant.id}) assert response.status_code == 400 assert response.data["detail"] == "Only order status can be modified"
def test_getting_order_list_by_restaurant_owner(owner): user, client = owner restaurant = RestaurantFactory(owner=user) for _ in range(3): OrderFactory(restaurant=restaurant) response = client.get("/orders/") assert response.status_code == 200 assert len(response.data) == 3 assert response.data[0]["user"] != user.id
def test_regular_user_status_changes(regular, status, next_status, is_allowed): user, client = regular order = OrderFactory(status=status, user=user) response = client.patch(f"/orders/{order.id}/", {"status": next_status}) if is_allowed: assert response.status_code == 200 assert response.data["status"] == next_status else: assert response.status_code == 403 assert "do not have permission" in response.data["detail"]
def test_float_value(self): attrs = OrderFactory.attributes() attrs['value'] = 100. self.assertRaises(ValueError, Order, **attrs)
def test_good_buy(self): attrs = OrderFactory.attributes() order = Order(**attrs) self.assertTrue(order) self.assertEqual(order.type, BUY)
def create_order(pizzas, customer, **kwargs): return OrderFactory.create(pizzas=pizzas, customer=customer, **kwargs)
def test_good_sell(self): attrs = OrderFactory.attributes() attrs['type'] = SELL order = Order(**attrs) self.assertTrue(order) self.assertEqual(order.type, SELL)
def test_negative_value(self): attrs = OrderFactory.attributes() attrs['value'] = -100 self.assertRaises(ValueError, Order, **attrs)
def test_bad_type(self): attrs = OrderFactory.attributes() attrs['type'] = 'abc' self.assertRaises(ValueError, Order, **attrs)