def test_order_create_with_line(self): p1 = factories.ProductFactory() p2 = factories.ProductFactory() a1 = factories.AddressFactory(user=self.user) temp_data = { "status":20, "billing_name":"test1", "billing_address1":a1.address1, "billing_address2":a1.address2, "billing_zip_code":a1.zip_code, "billing_city":a1.city, "billing_country":a1.country, "shipping_name":"hello", "shipping_address1": a1.address1, "shipping_address2": a1.address2, "shipping_zip_code": a1.zip_code, "shipping_city": a1.city, "shipping_country": a1.country, } order_line_data = { "order":temp_data, "product":p1.pk, "status":10, } rv = self.client.post( order_orderline_page,order_line_data,format='json' ) print(rv.data) order_list = models.Order.objects.all() order_line_list = models.OrderLine.objects.all() self.assertTrue(len(order_list),1) self.assertTrue(len(order_line_list),1)
def test_create_order_works(self): p1 = factories.ProductFactory() p2 = factories.ProductFactory() user1 = factories.UserFactory() billing = factories.AddressFactory(user=user1) shipping = factories.AddressFactory(user=user1) basket = models.Basket.objects.create(user=user1) models.BasketLine.objects.create(basket=basket, product=p1) models.BasketLine.objects.create(basket=basket, product=p2) with self.assertLogs("main.models", level="INFO") as cm: order = basket.create_order(billing, shipping) self.assertGreaterEqual(len(cm.output), 1) order.refresh_from_db() self.assertEqual(order.user, user1) self.assertEqual(order.billing_address1, billing.address1) self.assertEqual(order.shipping_address1, shipping.address1) self.assertEqual(order.lines.all().count(), 2) lines = order.lines.all() self.assertEqual(lines[0].product, p1) self.assertEqual(lines[1].product, p2)
def test_most_bought_products(self): user = models.User.objects.create_superuser(email="*****@*****.**", password="******") products = { "A": factories.ProductFactory(name="A", active=True), "B": factories.ProductFactory(name="B", active=True), "C": factories.ProductFactory(name="C", active=True), } orders = factories.OrderFactory.create_batch(3) # only for shortening the line XD fac_ordline = factories.OrderLineFactory fac_ordline.create_batch(2, order=orders[0], product=products["A"]) fac_ordline.create_batch(2, order=orders[0], product=products["B"]) fac_ordline.create_batch(2, order=orders[1], product=products["A"]) fac_ordline.create_batch(2, order=orders[1], product=products["C"]) fac_ordline.create_batch(2, order=orders[2], product=products["A"]) fac_ordline.create_batch(1, order=orders[2], product=products["B"]) self.client.force_login(user=user) response = self.client.post( path=reverse(viewname="admin:most_bought_products"), data={"period": "90"}, ) self.assertEqual(response.status_code, 200) data = dict(zip(response.context["labels"], response.context["values"])) self.assertEqual(data, {"B": 3, "C": 2, "A": 6})
def test_mobile_flow(self): user = factories.UserFactory(email="*****@*****.**") token = Token.objects.get(user=user) self.client.credentials(HTTP_AUTHORIZATION="Token " + token.key) orders = factories.OrderFactory.create_batch(2, user=user) a = factories.ProductFactory(name="The book of A", active=True, price=12.00) b = factories.ProductFactory(name="The B Book", active=True, price=14.00) factories.OrderLineFactory.create_batch(2, order=orders[0], product=a) factories.OrderLineFactory.create_batch(2, order=orders[1], product=b) response = self.client.get(reverse("mobile_my_orders")) self.assertEqual(response.status_code, status.HTTP_200_OK) expected = [ { "id": orders[1].id, "image": None, "price": 28.0, "summary": "2 x The B Book", }, { "id": orders[0].id, "image": None, "price": 24.0, "summary": "2 x The book of A", }, ] self.assertEqual(response.json(), expected)
def test_most_bought_products(self): products = [ factories.ProductFactory(name="A", active=True), factories.ProductFactory(name="B", active=True), factories.ProductFactory(name="C", active=True), ] orders = factories.OrderFactory.create_batch(3) factories.OrderLineFactory.create_batch( 2, order=orders[0], product=products[0] # A: 2 ) factories.OrderLineFactory.create_batch( 2, order=orders[0], product=products[1] # B: 2 ) factories.OrderLineFactory.create_batch( 2, order=orders[1], product=products[0] # A: 2+2 ) factories.OrderLineFactory.create_batch( 2, order=orders[1], product=products[2] # C: 2 ) factories.OrderLineFactory.create_batch( 2, order=orders[2], product=products[0] # A: 2+2+2 ) factories.OrderLineFactory.create_batch( 1, order=orders[2], product=products[1] # B: 2+1 ) user_one = models.User.objects.create_superuser( "user_one", "whatislove") self.client.force_login(user_one) response = self.client.post( reverse("admin:most_bought_products"), {"period": "90"}, ) self.assertEqual(response.status_code, 200) data = dict( zip( response.context["labels"], response.context["values"], )) self.assertEqual(data, {"B": 3, "C": 2, "A": 6})
def test_invoice_renders_exactly_as_expected(self): products = [ factories.ProductFactory(name="A", active=True, price=Decimal("10.00")), factories.ProductFactory(name="B", active=True, price=Decimal("12.00")), ] with patch("django.utils.timezone.now") as mock_now: mock_now.return_value = timezone.make_aware( datetime(2018, 7, 25, 12, 00, 00)) order = factories.OrderFactory( id=12, billing_name="John Smith", billing_address1="add1", billing_address2="add2", billing_zip_code="zip", billing_city="London", billing_country="UK", ) factories.OrderLineFactory.create_batch(2, order=order, product=products[0]) factories.OrderLineFactory.create_batch(2, order=order, product=products[1]) user = models.User.objects.create_superuser("user2", "pw432joij") self.client.force_login(user) response = self.client.get( reverse("admin:invoice", kwargs={"order_id": order.id})) self.assertEqual(response.status_code, 200) content = response.content.decode("utf8") with open("main/fixtures/invoice_test_order.html", "r") as fixture: expected_content = fixture.read() self.assertEqual(content, expected_content) response = self.client.get( reverse("admin:invoice", kwargs={"order_id": order.id}), {"format": "pdf"}) self.assertEqual(response.status_code, 200) content = response.content with open("main/fixtures/invoice_test_order.pdf", "rb") as fixture: expected_content = fixture.read() self.assertEqual(content[:5], expected_content[:5])
def test_orderlines_are_saved(self): p1 = factories.ProductFactory() a1 = factories.AddressFactory() user1 = factories.UserFactory() order_data = { "status":10, "billing_name":"test1", "billing_address1":a1.address1, "billing_address2":a1.address2, "billing_zip_code":a1.zip_code, "billing_city":a1.city, "billing_country":a1.country, "shipping_name":"hello", "shipping_address1": a1.address1, "shipping_address2": a1.address2, "shipping_zip_code": a1.zip_code, "shipping_city": a1.city, "shipping_country": a1.country, } order = models.Order.objects.create( user=user1, **order_data ) orderline = models.OrderLine.objects.create( product=p1, order=order, status=10 ) orderline.status=30 orderline.save() orderline.refresh_from_db() self.assertEqual(order.status,30)
def test_mobile_flow(self): user = factories.UserFactory(email='*****@*****.**') token = Token.objects.get(user=user) self.client.credentials( HTTP_AUTHORIZATION='Token ' + token.key ) orders = factories.OrderFactory.create_batch( 2, user=user ) a = factories.ProductFactory( name='Yet another book', active=True, price=18.00 ) b = factories.ProductFactory( name='Masterpiece', active=True, price=12.00 ) factories.OrderLineFactory.create_batch( 2, order=orders[0], product=a ) factories.OrderLineFactory.create_batch( 2, order=orders[1], product=b ) response = self.client.get(reverse('mobile_my_orders')) self.assertEqual( response.status_code, status.HTTP_200_OK ) expected = [ { 'id': orders[1].id, 'image': None, 'summary': '2 x Masterpiece', 'price': 24.0, }, { 'id': orders[0].id, 'image': None, 'summary': '2 x Yet another book', 'price': 36.0, }, ] self.assertEqual(response.json(), expected)
def test_invoice_renders_exactly_as_expected(self): products = [ factories.ProductFactory(name='A', active=True, price=Decimal('10.00')), factories.ProductFactory(name='B', active=True, price=Decimal('12.00')), ] with patch('django.utils.timezone.now') as mock_now: mock_now.return_value = datetime(2018, 7, 25, 12, 00, 00) order = factories.OrderFactory( id=12, billing_name='John Smith', billing_address1='add1', billing_address2='add2', billing_zip_code='zip', billing_city='London', billing_country='UK', ) factories.OrderLineFactory.create_batch(2, order=order, product=products[0]) factories.OrderLineFactory.create_batch(2, order=order, product=products[1]) user = models.User.objects.create_superuser('user2', 'pw432joij') self.client.force_login(user) response = self.client.get( reverse('admin:invoice', kwargs={'order_id': order.id})) self.assertEqual(response.status_code, 200) content = response.content.decode('utf8') with open('main/fixtures/invoice_test_order.html', 'r') as fixture: expected_content = fixture.read() self.assertEqual(compare_bodies(content, expected_content)) response = self.client.get( reverse('admin:invoice', kwargs={'order_id': order.id}), {'format': 'pdf'}) self.assertEqual(response.status_code, 200) content = response.content with open('main/fixtures/invoice_test_order.pdf', 'rb') as fixture: expected_content = fixture.read() self.assertEqual(content[:5], expected_content[:5])
def test_mobile_flow(self): user = factories.UserFactory() token = Token.objects.get(user=user) self.client.credentials(HTTP_AUTHORIZATION="Token " + token.key) a = factories.ProductFactory(title='Sausage', slug='sausage', is_active=True, price=12.00) b = factories.ProductFactory(title='meat', slug='meat', is_active=True, price=14.00) order_item_a = factories.OrderItemFactory.create_batch(1, quantity=2, item=a, user=user) print('orderitem_a:', order_item_a[0].id) order_item_b = factories.OrderItemFactory.create_batch(1, quantity=2, item=b, user=user) print('orderitem_b:', order_item_b) response = self.client.get(reverse('me2ushop:mobile_my_orders')) self.assertEqual(response.status_code, status.HTTP_200_OK) expected = [ { 'id': order_item_b[0].id, 'image': None, 'price': 28.0, 'summary': '2 x meat', }, { 'id': order_item_a[0].id, 'image': None, 'price': 24.0, 'summary': '2 x Sausage', }, ] self.assertEqual(response.json(), expected)
def test_most_bought_products(self): products = [ factories.ProductFactory(name='A', active=True), factories.ProductFactory(name='B', active=True), factories.ProductFactory(name='C', active=True), ] orders = factories.OrderFactory.create_batch(3) factories.OrderLineFactory.create_batch(2, order=orders[0], product=products[0]) factories.OrderLineFactory.create_batch(2, order=orders[0], product=products[1]) factories.OrderLineFactory.create_batch(2, order=orders[1], product=products[0]) factories.OrderLineFactory.create_batch(2, order=orders[1], product=products[2]) factories.OrderLineFactory.create_batch(2, order=orders[2], product=products[0]) factories.OrderLineFactory.create_batch(1, order=orders[2], product=products[1]) user = models.User.objects.create_superuser('user2', 'pw432joij') self.client.force_login(user) response = self.client.post( reverse("admin:most_bought_products"), {"period": "90"}, ) self.assertEqual(response.status_code, 200) data = dict( zip( response.context["labels"], response.context["values"], )) self.assertEqual(data, {"B": 3, "C": 2, "A": 6})
def test_invoice_renders_exactly_as_expected(self): products = [ factories.ProductFactory(name='Backgammon for dummies', active=True, price=Decimal('13.00')), factories.ProductFactory(name='The cathedral and the bazaar ', active=True, price=Decimal('10.00')), ] with patch('django.utils.timezone.now') as mock_now: mock_now.return_value = datetime(2019, 11, 5, 00, 00) order = factories.OrderFactory( id=4, billing_name='ForTest', billing_address1='ForTest', billing_address2='ForTest', billing_zip_code='ForTest', billing_city='ForTest', billing_country='ua', ) factories.OrderLineFactory.create_batch(2, order=order, product=products[0]) factories.OrderLineFactory.create_batch(2, order=order, product=products[1]) user = models.User.objects.create_superuser('user', 'newone') self.client.force_login(user) response = self.client.get( reverse('admin:invoice', kwargs={'order_id': order.id})) self.assertEqual(response.status_code, 200) content = response.content.decode('utf8') with open('main/fixtures/invoice_test_order.html') as fixture: expected_content = fixture.read() self.assertHTMLEqual(content, expected_content)
def test_most_bought_products(self): products = [ factories.ProductFactory(name='A', active=True), factories.ProductFactory(name='B', active=True), factories.ProductFactory(name='C', active=True), ] orders = factories.OrderFactory.create_batch(3) factories.OrderLineFactory.create_batch(2, order=orders[0], product=products[0]) factories.OrderLineFactory.create_batch(2, order=orders[0], product=products[1]) factories.OrderLineFactory.create_batch(2, order=orders[1], product=products[0]) factories.OrderLineFactory.create_batch(2, order=orders[1], product=products[2]) factories.OrderLineFactory.create_batch(2, order=orders[2], product=products[0]) factories.OrderLineFactory.create_batch(1, order=orders[2], product=products[1]) user = models.User.objects.create_superuser('user2', 'topsecret') self.client.force_login(user) response = self.client.post( reverse('admin:most_bought_products'), {'period': '90'}, ) self.assertEqual(response.status_code, 200) data = dict( zip( response.context['labels'], response.context['values'], )) self.assertEqual(data, {'B': 3, 'C': 2, 'A': 6})
def test_order_views(self): p1 = factories.ProductFactory() p2 = factories.ProductFactory() a1 = factories.AddressFactory(user=self.user) temp_data = { "status":20, "billing_name":"test1", "billing_address1":a1.address1, "billing_address2":a1.address2, "billing_zip_code":a1.zip_code, "billing_city":a1.city, "billing_country":a1.country, "shipping_name":"hello", "shipping_address1": a1.address1, "shipping_address2": a1.address2, "shipping_zip_code": a1.zip_code, "shipping_city": a1.city, "shipping_country": a1.country } rv = self.client.post( order_page,temp_data ) order_list = models.Order.objects.all() self.assertTrue(len(order_list),1)
def test_create_order_works(self): """ """ prod_one = factories.ProductFactory() prod_two = factories.ProductFactory() user_one = factories.UserFactory() billing = factories.AddressFactory(user=user_one) shipping = factories.AddressFactory(user=user_one) basket = models.Basket.objects.create(user=user_one) models.BasketLine.objects.create(basket=basket, product=prod_one) models.BasketLine.objects.create(basket=basket, product=prod_two) with self.assertLogs("main.models", level="INFO") as cm: order = basket.create_order(billing, shipping) self.assertGreaterEqual(len(cm.output), 1) order.refresh_from_db() # user_one's order self.assertEquals(order.user, user_one) # correct billing|shipping addresses self.assertEquals(order.billing_address1, billing.address1) self.assertEquals(order.shipping_address1, shipping.address1) # two products while checking out self.assertEquals(order.lines.all().count(), 2) # same as the prev line, just break it down as a list lines = order.lines.all() self.assertEquals(lines[0].product, prod_one) self.assertEquals(lines[1].product, prod_two)
def test_active_manager_works(self): """ The "manager" itself lives in the 'models.py' Example code >> class ActiveManager(models.Manager): >> def active(self): >> return self.filter(active=True) >> class Product(models.Model): >> ... >> objects = ActiveManager() """ factories.ProductFactory.create_batch(2, active=True) factories.ProductFactory(active=False) # The test objects here are only two "active" objs # so that's why the 2nd param of `assertEqual` is "2". self.assertEqual(len(models.Product.objects.active()), 2)
def test_active_manager_work(self): factories.ProductFactory.create_batch(2, active=True) factories.ProductFactory(active=False) self.assertEqual(len(models.Product.objects.active()), 2)
def test_invoice_renders_exactly_as_expected(self): """ About the HTML version of the invoice: the rows and columns aren't always aligned, but the PDF ver. is fine. According to the examples provided, the "mis-alignment" is okay :) """ user = models.User.objects.create_superuser(email="*****@*****.**", password="******") products = { "A": factories.ProductFactory(name="A", active=True, price=Decimal("11.00")), "B": factories.ProductFactory(name="B", active=True, price=Decimal("22.00")), } with patch("django.utils.timezone.now") as mock_now: mock_now.return_value = datetime(year=2020, month=12, day=20, hour=12, minute=00, second=00) order = factories.OrderFactory( id=12, billing_name="John Smiiiiith", billing_address1="addr1", billing_address2="addr2", billing_postal_code="postal", billing_city="London", billing_country="UK", ) factories.OrderLineFactory.create_batch(2, order=order, product=products["A"]) factories.OrderLineFactory.create_batch(2, order=order, product=products["B"]) self.client.force_login(user=user) response = self.client.get(path=reverse(viewname="admin:invoice", kwargs={"order_id": order.id})) self.assertEqual(response.status_code, 200) content = response.content.decode("utf8") with open(file="main/fixtures/invoice_test_order.html", mode="r") as f: expected_content = f.read() self.assertNotEqual(content, expected_content) response = self.client.get( path=reverse(viewname="admin:invoice", kwargs={"order_id": order.id}), data={"format": "pdf"}, ) self.assertEqual(response.status_code, 200) content = response.content with open(file="main/fixtures/invoice_test_order.pdf", mode="rb") as f: expected_content = f.read() self.assertNotEqual(content, expected_content)
def test_invoice_renders_exactly_as_expected(self): products = [ factories.ProductFactory(name="A", active=True, price=Decimal("1.00")), factories.ProductFactory(name="B", active=True, price=Decimal("5.00")), ] with patch("django.utils.timezone.now") as mock_now: mock_now.return_value = datetime(2019, 2, 2, 12, 00, 00) order = factories.OrderFactory( id=12, billing_name="John Smith", billing_address1="add1", billing_address2="add2", billing_zip_code="zip", billing_city="London", billing_country="UK", ) factories.OrderLineFactory.create_batch( 2, order=order, product=products[0] # A: 2 ) factories.OrderLineFactory.create_batch( 2, order=order, product=products[1] # B: 2 ) user_two = models.User.objects \ .create_superuser("user_two", "thatsgreat") self.client.force_login(user_two) # ********************-----********************** # ***************** print HTML ****************** # ********************-----********************** response = self.client.get( reverse("admin:invoice", kwargs={"order_id": order.id})) self.assertEqual(response.status_code, 200) content = response.content.decode("utf8") with open("main/fixtures/invoice_test_order.html", "r") as fixture: expected_content = fixture.read() self.assertEqual(content, expected_content) # ********************-----********************** # ***************** print PDF ****************** # ********************-----********************** response = self.client.get(reverse("admin:invoice", kwargs={"order_id": order.id}), data={"format": "pdf"}) self.assertEqual(response.status_code, 200) content = response.content with open("main/fixtures/invoice_test_order.pdf", "rb") as fixture: expected_content = fixture.read() self.assertEqual(content, expected_content)