def test_create_stamp_product_occupied(self): """ Test that tries to create a normal stamp with an unkown owner This tests the POST from endpoint /loyal/customer/${id}/stamps. """ # Create customer in DB c = Customer(**self.new_customer) c.save() # Create product in DB p = Product(**self.new_product) p.save() # Stamp product in DB st = Stamp(owned_by=c, obtained_with=p, grouped_in=None) st.save() # Create stamp through API stamp_data = {"obtained_with": p.pk, "grouped_in": None} url = self.get_url(self.STAMP_LIST_ENDP, args=[c.pk]) response = self.client.post(url, stamp_data) # Confirm it's not OK self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_get_list_20_customers(self): """ Verify that the customers list endpoint returns customers from the DB. This is a more advanced test with 20 different customers. This tests the GET endpoint /loyal/customer """ # Create customers customers = [] for _ in xrange(20): name = gen_names.get_first_name() surname = gen_names.get_last_name() customer = Customer(first_name=name, last_name=surname, email=name + "@" + surname + ".com") customer.save() customers.append(customer) # Transform customers expected_response = CustomerSerializerList(customers, many=True).data for customer in expected_response: customer['details'] = self.URL_TEST_PREFIX + customer['details'] # Get list from endpoint url = self.get_url(self.CUST_LIST_ENDP) response = self.client.get(url) # Confirm it's OK self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(20, len(response.data)) self.assertEqual(expected_response, response.data)
def test_voucher_autogeneration(self): """ Test that for every 10 stamps that we create for a customer a voucher is generated. This tests the POST from endpoint /loyal/customer/${id}/stamps. """ # Create customer c = Customer(**self.new_customer) c.save() # Create 10 stamps url = self.get_url(self.STAMP_LIST_ENDP, args=[c.pk]) for _ in xrange(10): response = self.client.post(url) self.assertEqual(response.status_code, status.HTTP_201_CREATED) # Retrieve stamps from DB stamps = Stamp.objects.all() self.assertEqual(10, len(stamps)) # Retrieve vouchers from DB vouchers = Voucher.objects.all() self.assertEqual(1, len(vouchers)) # Confirm that there are 10 stamps linked to the voucher self.assertEqual(10, vouchers[0].stamp_set.count())
def test_create_voucher(self): """ Test that creates a redeemed voucher for a given customer. This tests the POST from endpoint /loyal/customer/${id}/vouchers """ # Create customer in DB c = Customer(**self.new_customer) c.save() # Create product in DB p = Product(**self.new_product) p.save() # Create voucher voucher_data = {"redeemed_with": p.pk} url = self.get_url(self.VOUCH_LIST_ENDP, args=[c.pk]) response = self.client.post(url, voucher_data) # Confirm it's OK self.assertEqual(response.status_code, status.HTTP_201_CREATED) voucher_data.update({ 'date':response.data.get("date", None), 'link': self.get_non_customer_url(self.VOUCHER_ENDP, args=[1]), }) self.assertEqual(response.data, voucher_data)
def test_create_stamp(self): """ Test that creates a normal stamp for a given customer. This tests the POST from endpoint /loyal/customer/${id}/stamps. """ # Create customer in DB c = Customer(**self.new_customer) c.save() # Create product in DB p = Product(**self.new_product) p.save() # Create stamp stamp_data = { "obtained_with": p.pk, "grouped_in": None, } url = self.get_url(self.STAMP_LIST_ENDP, args=[c.pk]) response = self.client.post(url, stamp_data) stamp_data['link'] = self.get_non_customer_url(self.STAMP_ENDP, args=[1]) # Confirm it's OK self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data, stamp_data)
def test_create_voucher_product_occupied(self): """ Test that tries to create a redeemed voucher for a product that has already been redeemed. This tests the POST from endpoint /loyal/customer/${id}/vouchers """ # Create customer in DB c = Customer(**self.new_customer) c.save() # Create product in DB p = Product(**self.new_product) p.save() # Create voucher in DB v = Voucher(owned_by=c, redeemed_with=p) v.save() # Create voucher through API voucher_data = {"redeemed_with": p.pk} url = self.get_url(self.VOUCH_LIST_ENDP, args=[c.pk]) response = self.client.post(url, voucher_data) # Confirm it's not OK self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_get_list_one_customer(self): """ Verify that the customers list endpoint returns customers from the DB. This is a basic test with only 1 customer. This tests the GET endpoint /loyal/customer """ # Create customer c = Customer(**self.new_customer) c.save() # Get list url = self.get_url(self.CUST_LIST_ENDP) response = self.client.get(url) # Create expected customer, now includes the id expected_customer = dict(self.new_customer) expected_customer.update({ 'id': c.pk, 'details': self.get_test_url(self.CUST_DET_ENDP, args=[c.pk]) }) # Confirm it's OK self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(1, len(response.data)) self.assertEqual(expected_customer, response.data[0])
def test_get_detail_one_customer_with_counters(self): """ Verify that the customers detail endpoint returns customer info. This is a basic test with only 1 customer but wih data on the counters. This tests the GET endpoint /loyal/customer/${id} """ num_sales = 3 num_vouchers = 2 # Create customer c = Customer(**self.new_customer) c.save() for _ in xrange(num_sales): s = Sale(customer=c, date=timezone.now()) s.save() p = Product(**self.new_product) p.save() st = Stamp(owned_by=c, obtained_with=p, grouped_in=None) st.save() for _ in xrange(num_vouchers): v = Voucher(owned_by=c, redeemed_with=None) v.save() # Get details url = self.get_url(self.CUST_DET_ENDP, args=[c.pk]) response = self.client.get(url) expected_customer = dict(self.new_customer) expected_customer = dict(self.new_customer) expected_customer.update({ 'available_stamps': 1, 'total_stamps': 1, 'stamps': self.get_test_url(self.STAMP_LIST_ENDP, args=[c.pk]), 'available_vouchers': 2, 'total_vouchers': 2, 'vouchers': self.get_test_url(self.VOUCH_LIST_ENDP, args=[c.pk]), 'num_purchases': 3, 'purchases': self.get_test_url(self.SALE_LIST_ENDP, args=[c.pk]), }) # Confirm it's OK self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(expected_customer, response.data)
def _create_customers(self): print "Adding {0} customers ".format(self.num_customers), for _ in xrange(self.num_customers): name = gen_names.get_first_name() surname = gen_names.get_last_name() user = Customer(first_name=name, last_name=surname, email=name+"@"+surname+".com") user.save() sys.stdout.write('.') print "OK"
def _create_customers(self): print "Adding {0} customers ".format(self.num_customers), for _ in xrange(self.num_customers): name = gen_names.get_first_name() surname = gen_names.get_last_name() user = Customer(first_name=name, last_name=surname, email=name + "@" + surname + ".com") user.save() sys.stdout.write('.') print "OK"
def test_create_sale_non_existan_owner(self): """ Test that tries to create a sale with an unkown owner This tests the POST from endpoint /loyal/customer/${id}/sales """ # Create customer in DB c = Customer(**self.new_customer) c.save() # Create through API url = self.get_url(self.SALE_LIST_ENDP, args=[c.pk+1]) response = self.client.post(url, {'date':timezone.now()}) # Confirm it's not OK self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_create_sale_with_no_date(self): """ Test that creates a sale without a date This tests the POST from endpoint /loyal/customer/${id}/sales """ # Create customer in DB c = Customer(**self.new_customer) c.save() # Create through API url = self.get_url(self.SALE_LIST_ENDP, args=[c.pk]) response = self.client.post(url) # Confirm it's not OK self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_create_sale_non_existan_owner(self): """ Test that tries to create a sale with an unkown owner This tests the POST from endpoint /loyal/customer/${id}/sales """ # Create customer in DB c = Customer(**self.new_customer) c.save() # Create through API url = self.get_url(self.SALE_LIST_ENDP, args=[c.pk + 1]) response = self.client.post(url, {'date': timezone.now()}) # Confirm it's not OK self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_create_voucher_non_existan_owner(self): """ Test that tries to create a normal voucher with an unkown owner This tests the POST from endpoint /loyal/customer/${id}/vouchers. """ # Create customer in DB c = Customer(**self.new_customer) c.save() # Create stamp through API url = self.get_url(self.VOUCH_LIST_ENDP, args=[c.pk+1]) response = self.client.post(url, {}) # Confirm it's not OK self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_sales_list_populated(self): """ Test that for a given customer we can see it's purchases. We create 2 sales directly in the DB, one without products the other with 1 product This tests the GET from endpoint /loyal/customer/${id}/sales """ # Create customer c = Customer(**self.new_customer) c.save() # Create sales s = Sale(customer=c, date=timezone.now()) s.save() s = Sale(customer=c, date=timezone.now()) s.save() # Create product new_product = dict(self.new_product) new_product['sale'] = s p = Product(**new_product) p.save() # Get list url = self.get_url(self.SALE_LIST_ENDP, args=[c.pk]) response = self.client.get(url) # Confirm it's OK self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(2, len(response.data)) sale_data = { "date": response.data[0].get("date", None), 'products': [], 'products_links': [] } self.assertEqual(response.data[0], sale_data) sale_data = { "date": response.data[1].get("date", None), 'products': [str(p)], 'products_links': [self.get_non_customer_url(self.PRODUCT_ENDP, args=[1])], } self.assertEqual(response.data[1], sale_data)
def test_create_voucher_non_existan_redeem_product(self): """ Test that tries to create a normal voucher to redeem a non existant product This tests the POST from endpoint /loyal/customer/${id}/vouchers. """ # Create customer in DB c = Customer(**self.new_customer) c.save() # Create stamp through API voucher_data = {"redeemed_with": 10} url = self.get_url(self.VOUCH_LIST_ENDP, args=[c.pk+1]) response = self.client.post(url, voucher_data) # Confirm it's not OK self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_create_stamp_impossible_product(self): """ Test that tries to create a normal stamp for a given customer with a non existant product. This tests the POST from endpoint /loyal/customer/${id}/stamps. """ # Create customer in DB c = Customer(**self.new_customer) c.save() # Create stamp stamp_data = {"obtained_with": 10, "grouped_in": None} url = self.get_url(self.STAMP_LIST_ENDP, args=[c.pk]) response = self.client.post(url, stamp_data) # Confirm it's not OK self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_stamp_list_empty(self): """ Test that for a given customer initially the stamp list is empty. This tests the GET from endpoint /loyal/customer/${id}/stamps. """ # Create customer c = Customer(**self.new_customer) c.save() # Get list url = self.get_url(self.STAMP_LIST_ENDP, args=[c.pk]) response = self.client.get(url) # Confirm it's OK self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, [])
def test_get_detail_unkown_customer(self): """ Verify that the customers detail endpoint returns customer info. This is a basic test with only 1 customer. This tests the GET endpoint /loyal/customer/${id} """ # Create customer c = Customer(**self.new_customer) c.save() # Get details url = self.get_url(self.CUST_DET_ENDP, args=[c.pk + 1]) response = self.client.get(url) # Confirm it's an ERROR self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_voucher_list_empty(self): """ Test that for a given customer initially the voucher list is empty. This tests the GET endpoint /loyal/customer/${id}/vouchers """ # Create customer c = Customer(**self.new_customer) c.save() # Get list url = self.get_url(self.VOUCH_LIST_ENDP, args=[c.pk]) response = self.client.get(url) # Confirm it's OK self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, [])
def test_stamp_list_populated(self): """ Test that for a given customer we can see it's stamps. We create 2 stamps directly in the DB, one free and one from purchasing a product. This tests the GET from endpoint /loyal/customer/${id}/stamps. """ # Create customer c = Customer(**self.new_customer) c.save() # Create product p = Product(**self.new_product) p.save() # Create stamp s = Stamp(owned_by=c, obtained_with=p, grouped_in=None) s.save() # Create free stamp s = Stamp(owned_by=c) s.save() # Get list url = self.get_url(self.STAMP_LIST_ENDP, args=[c.pk]) response = self.client.get(url) # Confirm it's OK self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(2, len(response.data)) stamp_data = { 'obtained_with': p.pk, 'grouped_in': None, 'link': self.get_non_customer_url(self.STAMP_ENDP, args=[1]), } self.assertEqual(response.data[0], stamp_data) stamp_data.update({ 'obtained_with': None, 'link': self.get_non_customer_url(self.STAMP_ENDP, args=[2]), }) self.assertEqual(response.data[1], stamp_data)
def test_create_sale(self): """ Test that creates a single sale with no product This tests the POST from endpoint /loyal/customer/${id}/sales """ # Create customer in DB c = Customer(**self.new_customer) c.save() # Create through API my_time = timezone.now() url = self.get_url(self.SALE_LIST_ENDP, args=[c.pk]) response = self.client.post(url, {'date':str(my_time)}) # Confirm it's OK self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data, {'date':my_time, 'products':[], 'products_links':[]})
def test_sales_list_populated(self): """ Test that for a given customer we can see it's purchases. We create 2 sales directly in the DB, one without products the other with 1 product This tests the GET from endpoint /loyal/customer/${id}/sales """ # Create customer c = Customer(**self.new_customer) c.save() # Create sales s = Sale(customer=c, date=timezone.now()) s.save() s = Sale(customer=c, date=timezone.now()) s.save() # Create product new_product = dict(self.new_product) new_product['sale'] = s p = Product(**new_product) p.save() # Get list url = self.get_url(self.SALE_LIST_ENDP, args=[c.pk]) response = self.client.get(url) # Confirm it's OK self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(2, len(response.data)) sale_data = {"date": response.data[0].get("date", None), 'products':[], 'products_links':[] } self.assertEqual(response.data[0], sale_data) sale_data = {"date": response.data[1].get("date", None), 'products':[str(p)], 'products_links': [self.get_non_customer_url(self.PRODUCT_ENDP, args=[1])], } self.assertEqual(response.data[1], sale_data)
def _creation(self, customer=False, product=False, sale=False): c = None; p = None; s = None # Create customer if customer: c = Customer(**self.new_customer) c.save() # Create sale if sale: s = Sale(customer=c, date=timezone.now()) s.save() # Create product if product: p = Product(**self.new_product) p.sale = s p.save() return (c, p, s)
def test_create_stamp_non_existan_owner(self): """ Test that tries to create a normal stamp with an unkown owner This tests the POST from endpoint /loyal/customer/${id}/stamps. """ # Create customer in DB c = Customer(**self.new_customer) c.save() # Create product in DB p = Product(**self.new_product) p.save() # Create stamp through API stamp_data = {"obtained_with": p.pk, "grouped_in": None} url = self.get_url(self.STAMP_LIST_ENDP, args=[c.pk+1]) response = self.client.post(url, stamp_data) # Confirm it's not OK self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_create_stamp_non_existan_owner(self): """ Test that tries to create a normal stamp with an unkown owner This tests the POST from endpoint /loyal/customer/${id}/stamps. """ # Create customer in DB c = Customer(**self.new_customer) c.save() # Create product in DB p = Product(**self.new_product) p.save() # Create stamp through API stamp_data = {"obtained_with": p.pk, "grouped_in": None} url = self.get_url(self.STAMP_LIST_ENDP, args=[c.pk + 1]) response = self.client.post(url, stamp_data) # Confirm it's not OK self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_voucher_list_populated(self): """ Test that for a given customer we can see it's vouchers. We create 2 vouchers directly in the DB, one available and one redeemed. This tests the GET from endpoint /loyal/customer/${id}/vouchers """ # Create customer c = Customer(**self.new_customer) c.save() # Create product p = Product(**self.new_product) p.save() # Create voucher v = Voucher(owned_by=c, redeemed_with=p) v.save() # Create free voucher v = Voucher(owned_by=c) v.save() # Get list url = self.get_url(self.VOUCH_LIST_ENDP, args=[c.pk]) response = self.client.get(url) # Confirm it's OK self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(2, len(response.data)) voucher_data = {"redeemed_with": p.pk, "date": response.data[0].get("date", None), 'link': self.get_non_customer_url(self.VOUCHER_ENDP, args=[1])} self.assertEqual(response.data[0], voucher_data) voucher_data = {"redeemed_with": None, "date": response.data[1].get("date", None), 'link': self.get_non_customer_url(self.VOUCHER_ENDP, args=[2])} self.assertEqual(response.data[1], voucher_data)
def test_create_free_stamp(self): """ Test that creates a free stamp for a given customer. This tests the POST from endpoint /loyal/customer/${id}/stamps. """ # Create customer in DB c = Customer(**self.new_customer) c.save() # Create stamp url = self.get_url(self.STAMP_LIST_ENDP, args=[c.pk]) response = self.client.post(url) # Confirm it's OK free_stamp = { "obtained_with": None, "grouped_in": None, 'link': self.get_non_customer_url(self.STAMP_ENDP, args=[1]), } self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data, free_stamp)
def test_get_detail_one_customer(self): """ Verify that the customers detail endpoint returns customer info. This is a basic test with only 1 customer. This tests the GET endpoint /loyal/customer/${id} """ # Create customer c = Customer(**self.new_customer) c.save() # Get details url = self.get_url(self.CUST_DET_ENDP, args=[c.pk]) response = self.client.get(url) expected_customer = dict(self.new_customer) expected_customer.update({ 'available_stamps': 0, 'total_stamps': 0, 'stamps': self.get_test_url(self.STAMP_LIST_ENDP, args=[c.pk]), 'available_vouchers': 0, 'total_vouchers': 0, 'vouchers': self.get_test_url(self.VOUCH_LIST_ENDP, args=[c.pk]), 'num_purchases': 0, 'purchases': self.get_test_url(self.SALE_LIST_ENDP, args=[c.pk]), }) # Confirm it's OK self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(expected_customer, response.data)
def test_create_sale(self): """ Test that creates a single sale with no product This tests the POST from endpoint /loyal/customer/${id}/sales """ # Create customer in DB c = Customer(**self.new_customer) c.save() # Create through API my_time = timezone.now() url = self.get_url(self.SALE_LIST_ENDP, args=[c.pk]) response = self.client.post(url, {'date': str(my_time)}) # Confirm it's OK self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data, { 'date': my_time, 'products': [], 'products_links': [] })
def test_create_free_voucher(self): """ Test that creates a free voucher for a given customer. This tests the POST from endpoint /loyal/customer/${id}/vouchers """ # Create customer in DB c = Customer(**self.new_customer) c.save() # Create voucher url = self.get_url(self.VOUCH_LIST_ENDP, args=[c.pk]) response = self.client.post(url) # Confirm it's OK self.assertEqual(response.status_code, status.HTTP_201_CREATED) free_vouch = { "redeemed_with": None, "date": response.data.get("date", None), 'link': self.get_non_customer_url(self.VOUCHER_ENDP, args=[1]), } self.assertEqual(response.data, free_vouch)