-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #761 from WesternFriend/improve-orders
Improve orders
- Loading branch information
Showing
6 changed files
with
144 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import factory | ||
from factory.django import DjangoModelFactory | ||
from faker import Faker | ||
from .models import ( | ||
Order, | ||
OrderItem, | ||
) | ||
|
||
fake = Faker() | ||
|
||
|
||
class OrderFactory(DjangoModelFactory): | ||
class Meta: | ||
model = Order | ||
|
||
purchaser_given_name = factory.Faker("first_name") | ||
purchaser_family_name = factory.Faker("last_name") | ||
purchaser_meeting_or_organization = factory.Faker("company") | ||
purchaser_email = factory.Faker("email") | ||
recipient_name = factory.LazyAttribute( | ||
lambda x: f"{fake.first_name()} {fake.last_name()}" | ||
) | ||
recipient_street_address = factory.Faker("street_address") | ||
recipient_postal_code = factory.Faker("zipcode") | ||
recipient_po_box_number = factory.Faker("random_int") | ||
recipient_address_locality = factory.Faker("city") | ||
recipient_address_region = factory.Faker("state") | ||
recipient_address_country = factory.Faker("country") | ||
shipping_cost = factory.Faker( | ||
"pydecimal", | ||
left_digits=2, | ||
right_digits=2, | ||
positive=True, | ||
) | ||
paid = factory.Faker("boolean") | ||
braintree_transaction_id = factory.Faker("uuid4") | ||
|
||
|
||
class OrderItemFactory(DjangoModelFactory): | ||
class Meta: | ||
model = OrderItem | ||
|
||
order = factory.SubFactory(OrderFactory) | ||
product_title = factory.Faker( | ||
"sentence", | ||
nb_words=3, | ||
) | ||
product_id = factory.Faker( | ||
"random_int", | ||
min=1, | ||
max=99999, | ||
) | ||
price = factory.Faker( | ||
"pydecimal", | ||
left_digits=2, | ||
right_digits=2, | ||
positive=True, | ||
) | ||
quantity = factory.Faker( | ||
"random_int", | ||
min=1, | ||
max=100, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,65 @@ | ||
# Create your tests here. | ||
from django.test import TestCase | ||
from .models import Order, OrderItem | ||
|
||
|
||
class OrderModelTest(TestCase): | ||
@classmethod | ||
def setUpTestData(cls) -> None: | ||
# Create an Order instance using the factory | ||
cls.order = Order( | ||
id=1, | ||
purchaser_given_name="John", | ||
purchaser_family_name="Doe", | ||
purchaser_meeting_or_organization="Western Friend", | ||
shipping_cost=4.00, | ||
) | ||
|
||
def test_purchaser_full_name(self) -> None: | ||
# Test the method | ||
self.assertEqual(self.order.purchaser_full_name, "John Doe Western Friend") | ||
|
||
# Test with empty purchaser_family_name | ||
self.order.purchaser_family_name = "" | ||
self.order.save() | ||
self.assertEqual(self.order.purchaser_full_name, "John Western Friend") | ||
|
||
# Test with only purchaser_meeting_or_organization | ||
self.order.purchaser_given_name = "" | ||
self.order.save() | ||
self.assertEqual(self.order.purchaser_full_name, "Western Friend") | ||
|
||
def test_str_method(self) -> None: | ||
# Test the method | ||
self.assertEqual( | ||
str(self.order), | ||
"Order 1", | ||
) | ||
|
||
|
||
class OrderItemModelTest(TestCase): | ||
@classmethod | ||
def setUpTestData(cls) -> None: | ||
# Create an OrderItem instance using the factory | ||
cls.order_item = OrderItem( | ||
id=1, | ||
product_title="Quaker Faith & Practice", | ||
product_id=1, | ||
price=19.99, | ||
quantity=2, | ||
) | ||
|
||
def test_str_method(self) -> None: | ||
expected_string = "2x Quaker Faith & Practice @ 19.99/each" | ||
|
||
self.assertEqual( | ||
str(self.order_item), | ||
expected_string, | ||
) | ||
|
||
def test_get_cost_method(self) -> None: | ||
expected_cost = 39.98 | ||
|
||
self.assertEqual( | ||
self.order_item.get_cost(), | ||
expected_cost, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters