Skip to content

Commit

Permalink
Merge pull request #761 from WesternFriend/improve-orders
Browse files Browse the repository at this point in the history
Improve orders
  • Loading branch information
brylie authored Jul 6, 2023
2 parents bc63860 + 6bb7786 commit 48dde86
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 2 deletions.
63 changes: 63 additions & 0 deletions orders/factories.py
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,
)
3 changes: 2 additions & 1 deletion orders/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class Order(ClusterableModel):
FieldPanel("recipient_address_country"),
FieldPanel("shipping_cost"),
FieldPanel("paid"),
FieldPanel("braintree_transaction_id"),
InlinePanel("items", label="Order items"),
]

Expand Down Expand Up @@ -140,7 +141,7 @@ class OrderItem(Orderable):
]

def __str__(self) -> str:
return f"{self.quantity}x {self.product_title} @ { self.price}"
return f"{self.quantity}x {self.product_title} @ { round(self.price, 2) }/each" # noqa: E501

def get_cost(self) -> Decimal:
return self.price * self.quantity
66 changes: 65 additions & 1 deletion orders/tests.py
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,
)
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ dev = [
"coverage",
"validate-pyproject",
"pip-tools",
"factory_boy",
"types-factory-boy",
]

[tool.black]
Expand Down
10 changes: 10 additions & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,12 @@ draftjs-exporter==2.1.7
# via wagtail
et-xmlfile==1.1.0
# via openpyxl
factory-boy==3.2.1
# via
# Western-Friend-website (pyproject.toml)
# types-factory-boy
faker==18.11.2
# via factory-boy
fastjsonschema==2.17.1
# via validate-pyproject
filelock==3.12.2
Expand Down Expand Up @@ -165,6 +171,7 @@ python-dateutil==2.8.2
# via
# arrow
# botocore
# faker
python-dotenv==1.0.0
# via Western-Friend-website (pyproject.toml)
pytz==2023.3
Expand Down Expand Up @@ -200,6 +207,8 @@ types-beautifulsoup4==4.12.0.5
# via Western-Friend-website (pyproject.toml)
types-braintree==4.20.0.0
# via Western-Friend-website (pyproject.toml)
types-factory-boy==0.4.1
# via Western-Friend-website (pyproject.toml)
types-html5lib==1.1.11.14
# via types-beautifulsoup4
types-pytz==2023.3.0.0
Expand All @@ -220,6 +229,7 @@ typing-extensions==4.7.1
# django-stubs-ext
# mypy
# pydantic
# types-factory-boy
tzdata==2023.3
# via Western-Friend-website (pyproject.toml)
urllib3==1.26.16
Expand Down
2 changes: 2 additions & 0 deletions store/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class OrderModelAdmin(ModelAdmin):
inspect_view_enabled = True
inspect_view_fields = [
"id",
"paid",
"braintree_transaction_id",
"purchaser_given_name",
"purchaser_family_name",
"purchaser_meeting_or_organization",
Expand Down

0 comments on commit 48dde86

Please sign in to comment.