Skip to content

Commit cc7b0ba

Browse files
committed
Order creation test implemented
1 parent c134808 commit cc7b0ba

File tree

6 files changed

+182
-37
lines changed

6 files changed

+182
-37
lines changed

.vscode/settings.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"python.testing.unittestArgs": [
3+
"-v",
4+
"-s",
5+
"./nxtbn",
6+
"-p",
7+
"*test.py"
8+
],
9+
"python.testing.pytestEnabled": false,
10+
"python.testing.unittestEnabled": true
11+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import random
2+
import sys
3+
from django.conf import settings
4+
from rest_framework import status
5+
6+
from rest_framework.reverse import reverse
7+
8+
from nxtbn.core import PublishableStatus
9+
from nxtbn.core.utils import normalize_amount_currencywise
10+
from nxtbn.home.base_tests import BaseTestCase
11+
12+
from django.utils import timezone
13+
14+
from nxtbn.product.models import Product, ProductType
15+
from rest_framework.test import APIClient
16+
17+
from nxtbn.product.tests import ProductFactory, ProductTypeFactory, ProductVariantFactory
18+
from nxtbn.tax.tests import TaxClassFactory, TaxRateFactory
19+
20+
21+
22+
23+
class OrderCreateNoTrackableTaxableAPI(BaseTestCase):
24+
25+
26+
"""
27+
Test Case for Order Create API with a taxable product that has no stock tracking.
28+
This test case ensures that a product variant with no stock tracking and taxable status is created successfully.
29+
The product price is set to $20.26 USD with a tax rate of 15%, and the shipping address is in US, NY.
30+
The tax rate should be applied correctly for this location. Total items in the order are 3.
31+
Expected Output:
32+
- Subtotal: $60.78 (rounded to 2 decimal places)
33+
- Tax: $9.12 (rounded to 2 decimal places)
34+
- Total: $69.90 (rounded to 2 decimal places)
35+
The test will verify if the expected calculations are returned in the API response.
36+
"""
37+
38+
def setUp(self):
39+
super().setUp()
40+
self.client = APIClient()
41+
self.client.login(email='[email protected]', password='testpass')
42+
43+
self.country = 'US'
44+
self.state = 'NY'
45+
46+
self.tax_class = TaxClassFactory()
47+
self.tax_rate = TaxRateFactory(
48+
tax_class=self.tax_class,
49+
is_active=True,
50+
rate = 15, # 15%
51+
country = self.country,
52+
state = self.state
53+
)
54+
self.product_type = ProductTypeFactory(
55+
name="Don't track stock but taxable",
56+
track_stock=False,
57+
taxable=True,
58+
)
59+
self.product = ProductFactory(
60+
product_type=self.product_type,
61+
tax_class=self.tax_class,
62+
status=PublishableStatus.PUBLISHED,
63+
)
64+
65+
self.variants = ProductVariantFactory(
66+
product=self.product,
67+
track_inventory=False,
68+
currency='USD',
69+
price=normalize_amount_currencywise(20.26, settings.BASE_CURRENCY), # $20.26 USD
70+
stock=10,
71+
cost_per_unit=50,
72+
)
73+
74+
self.order_api_url = reverse('order-create')
75+
self.order_eastimate_api_url = reverse('order-eastimate')
76+
77+
78+
79+
def test_order_create_api(self):
80+
order_payload = {
81+
"shipping_address": {
82+
"country": self.country,
83+
"state": self.state,
84+
"street_address": "123 Main St",
85+
"city": "New York",
86+
"postal_code": "10001",
87+
"email": "[email protected]",
88+
"first_name": "John",
89+
"last_name": "Doe",
90+
"phone_number": "1234567890"
91+
},
92+
"billing_address": {
93+
"country": self.country,
94+
"state": self.state,
95+
"street_address": "123 Main St",
96+
"city": "New York",
97+
"postal_code": "10001",
98+
"email": "[email protected]",
99+
"first_name": "John",
100+
"last_name": "Doe",
101+
"phone_number": "1234567890"
102+
},
103+
"variants": [
104+
{
105+
"alias": self.variants.alias,
106+
"quantity": 3,
107+
}
108+
]
109+
}
110+
111+
# Eastimation Test
112+
order_eastimate_response = self.client.post(self.order_eastimate_api_url, order_payload, format='json')
113+
self.assertEqual(order_eastimate_response.status_code, status.HTTP_200_OK)
114+
self.assertEqual(order_eastimate_response.data['subtotal'], '$60.78')
115+
self.assertEqual(order_eastimate_response.data['total'], '$69.90')
116+
self.assertEqual(order_eastimate_response.data['estimated_tax'], '$9.12')
117+
self.assertEqual(order_eastimate_response.data['discount'], '$0.00')
118+
self.assertEqual(order_eastimate_response.data['total_items'], 3)
119+
self.assertEqual(order_eastimate_response.data['shipping_fee'], '$0.00')
120+
121+
# Order Create Test
122+
order_response = self.client.post(self.order_api_url, order_payload, format='json')
123+
self.assertEqual(order_response.status_code, status.HTTP_200_OK)
124+
self.assertEqual(order_response.data['subtotal'], '$60.78')
125+
self.assertEqual(order_response.data['total'], '$69.90')
126+
self.assertEqual(order_response.data['estimated_tax'], '$9.12')
127+
self.assertEqual(order_response.data['discount'], '$0.00')
128+
self.assertEqual(order_response.data['total_items'], 3)
129+
self.assertEqual(order_response.data['shipping_fee'], '$0.00')

nxtbn/order/tests/test_order_with_non_tracking_variant.py

Lines changed: 0 additions & 30 deletions
This file was deleted.

nxtbn/product/tests/__init__.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import random
2+
from django.conf import settings
13
import factory
24
from factory.django import DjangoModelFactory
35
from faker import Faker
46
from decimal import Decimal
7+
from nxtbn.core.utils import normalize_amount_currencywise
58
from nxtbn.filemanager.tests import ImageFactory
69
from nxtbn.product.models import (
710
Supplier, Color, Category, Collection, ProductTag, ProductType, Product, ProductVariant
@@ -41,8 +44,11 @@ class Meta:
4144

4245
name = factory.Faker("word")
4346
description = factory.Faker("sentence")
44-
parent = factory.SubFactory("self", nullable=True)
45-
47+
# parent = factory.Maybe(
48+
# factory.Faker("boolean"),
49+
# yes_declaration=factory.SubFactory('self'),
50+
# no_declaration=None
51+
# )
4652

4753
# Collection Factory
4854
class CollectionFactory(DjangoModelFactory):
@@ -54,7 +60,7 @@ class Meta:
5460
is_active = True
5561
created_by = factory.SubFactory(UserFactory)
5662
last_modified_by = factory.SubFactory(UserFactory)
57-
image = factory.SubFactory("ImageFactory", nullable=True)
63+
image = factory.SubFactory(ImageFactory, nullable=True)
5864

5965

6066
# Product Tag Factory
@@ -124,7 +130,7 @@ class Meta:
124130
product = factory.SubFactory(ProductFactory)
125131
name = factory.Faker("word")
126132
sku = factory.Faker("ean13")
127-
price = factory.LazyFunction(lambda: Decimal(fake.random_int(min=10, max=1000)))
133+
price = normalize_amount_currencywise(random.uniform(10, 1000), settings.BASE_CURRENCY),
128134
currency = "USD"
129135
stock = factory.Faker("random_int", min=0, max=100)
130136
track_inventory = fake.boolean()

nxtbn/tax/tests.py

Lines changed: 0 additions & 3 deletions
This file was deleted.

nxtbn/tax/tests/__init__.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import factory
2+
from factory.django import DjangoModelFactory
3+
from faker import Faker
4+
from decimal import Decimal
5+
from django_countries.fields import CountryField
6+
7+
from nxtbn.tax.models import TaxClass, TaxRate
8+
9+
faker = Faker()
10+
11+
class TaxClassFactory(DjangoModelFactory):
12+
"""
13+
Factory for the TaxClass model.
14+
"""
15+
class Meta:
16+
model = TaxClass
17+
18+
name = factory.LazyAttribute(lambda _: faker.word().capitalize())
19+
20+
21+
class TaxRateFactory(DjangoModelFactory):
22+
"""
23+
Factory for the TaxRate model.
24+
"""
25+
class Meta:
26+
model = TaxRate
27+
28+
country = factory.LazyAttribute(lambda _: faker.country_code())
29+
state = factory.LazyAttribute(lambda _: faker.state_abbr() if faker.boolean() else None)
30+
rate = factory.LazyAttribute(lambda _: Decimal(faker.random_int(min=1, max=25)))
31+
tax_class = factory.SubFactory(TaxClassFactory)
32+
is_active = factory.LazyAttribute(lambda _: faker.boolean(chance_of_getting_true=80))

0 commit comments

Comments
 (0)