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
+
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
+
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' )
0 commit comments