Instantly share code, notes, and snippets.

Embed
What would you like to do?
from datetime import date
import factory
from .models import Campaign, ProductType, Product, Order, Customer
class CustomerFactory(factory.Factory):
class Meta:
model = Customer
class OrderFactory(factory.Factory):
class Meta:
model = Order
customer = factory.RelatedFactory(CustomerFactory)
class ProductTypeFactory(factory.Factory):
class Meta:
model = ProductType
medium = ProductType.PRINT
name = 'Half Page Ad'
price = 1000
run_length_period = 'ISSUES'
ad_deadline = 21
class ProductFactory(factory.Factory):
class Meta:
model = Product
campaign = factory.SubFactory('campaigns.factories.CampaignFactory', product=None)
date_created = date(2016, 5, 1)
date_updated = date(2016, 5, 1)
product_type = factory.RelatedFactory(ProductTypeFactory)
run_length = 3
class CampaignFactory(factory.Factory):
class Meta:
model = Campaign
book_title = 'Lies of Locke Lamora'
internal_design = True
order = factory.RelatedFactory(OrderFactory)
start_date = date(2016, 6, 15)
date_created = date(2016, 5, 1)
date_updated = date(2016, 5, 1)
product = factory.RelatedFactory(ProductFactory, 'campaign')
Creating test database for alias 'default' ('test_adbook')...
Operations to perform:
Synchronize unmigrated apps: messages, debug_toolbar, core, staticfiles, compressor
Apply all migrations: admin, sessions, inventory, contenttypes, campaigns, auth
Running pre-migrate handlers for application admin
Running pre-migrate handlers for application auth
Running pre-migrate handlers for application contenttypes
Running pre-migrate handlers for application sessions
Running pre-migrate handlers for application compressor
Running pre-migrate handlers for application debug_toolbar
Running pre-migrate handlers for application core
Running pre-migrate handlers for application campaigns
Running pre-migrate handlers for application inventory
Synchronizing apps without migrations:
Creating tables...
Running deferred SQL...
Running migrations:
Rendering model states... DONE (0.000s)
Applying contenttypes.0001_initial... OK (0.011s)
Applying auth.0001_initial... OK (0.056s)
Applying admin.0001_initial... OK (0.022s)
Applying admin.0002_logentry_remove_auto_add... OK (0.013s)
Applying contenttypes.0002_remove_content_type_name... OK (0.033s)
Applying auth.0002_alter_permission_name_max_length... OK (0.012s)
Applying auth.0003_alter_user_email_max_length... OK (0.013s)
Applying auth.0004_alter_user_username_opts... OK (0.012s)
Applying auth.0005_alter_user_last_login_null... OK (0.014s)
Applying auth.0006_require_contenttypes_0002... OK (0.002s)
Applying auth.0007_alter_validators_add_error_messages... OK (0.012s)
Applying campaigns.0001_initial... OK (0.045s)
Applying campaigns.0002_producttype_price... OK (0.014s)
Applying inventory.0001_initial... OK (0.007s)
Applying sessions.0001_initial... OK (0.009s)
Running post-migrate handlers for application admin
Adding permission 'admin | log entry | Can add log entry'
Adding permission 'admin | log entry | Can change log entry'
Adding permission 'admin | log entry | Can delete log entry'
Running post-migrate handlers for application auth
Adding permission 'auth | permission | Can add permission'
Adding permission 'auth | permission | Can change permission'
Adding permission 'auth | permission | Can delete permission'
Adding permission 'auth | group | Can add group'
Adding permission 'auth | group | Can change group'
Adding permission 'auth | group | Can delete group'
Adding permission 'auth | user | Can add user'
Adding permission 'auth | user | Can change user'
Adding permission 'auth | user | Can delete user'
Running post-migrate handlers for application contenttypes
Adding permission 'contenttypes | content type | Can add content type'
Adding permission 'contenttypes | content type | Can change content type'
Adding permission 'contenttypes | content type | Can delete content type'
Running post-migrate handlers for application sessions
Adding permission 'sessions | session | Can add session'
Adding permission 'sessions | session | Can change session'
Adding permission 'sessions | session | Can delete session'
Running post-migrate handlers for application compressor
Running post-migrate handlers for application debug_toolbar
Running post-migrate handlers for application core
Running post-migrate handlers for application campaigns
Adding permission 'campaigns | campaign | Can add campaign'
Adding permission 'campaigns | campaign | Can change campaign'
Adding permission 'campaigns | campaign | Can delete campaign'
Adding permission 'campaigns | product | Can add product'
Adding permission 'campaigns | product | Can change product'
Adding permission 'campaigns | product | Can delete product'
Adding permission 'campaigns | product type | Can add product type'
Adding permission 'campaigns | product type | Can change product type'
Adding permission 'campaigns | product type | Can delete product type'
Adding permission 'campaigns | customer | Can add customer'
Adding permission 'campaigns | customer | Can change customer'
Adding permission 'campaigns | customer | Can delete customer'
Adding permission 'campaigns | order | Can add order'
Adding permission 'campaigns | order | Can change order'
Adding permission 'campaigns | order | Can delete order'
Running post-migrate handlers for application inventory
Adding permission 'inventory | ad slot | Can add ad slot'
Adding permission 'inventory | ad slot | Can change ad slot'
Adding permission 'inventory | ad slot | Can delete ad slot'
test_ad_deadline (campaigns.tests.CampaignTest) ... ERROR
test_ad_design_price (campaigns.tests.CampaignTest) ... None
FAIL
test_slug (campaigns.tests.ProductTypeTest) ... None
FAIL
test_basic_addition (core.tests.SimpleTest) ... ok
======================================================================
ERROR: test_ad_deadline (campaigns.tests.CampaignTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/vagrant/adbook/campaigns/tests.py", line 15, in test_ad_deadline
self.assertEqual(campaign.ad_deadline, date(2016, 5, 8))
File "/home/vagrant/adbook/campaigns/models.py", line 28, in ad_deadline
return self.start_date - timedelta(days=delta)
TypeError: unsupported type for timedelta days component: NoneType
======================================================================
FAIL: test_ad_design_price (campaigns.tests.CampaignTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/vagrant/adbook/campaigns/tests.py", line 11, in test_ad_design_price
self.assertEqual(campaign.ad_design_price, 1000)
AssertionError: None != 1000
======================================================================
FAIL: test_slug (campaigns.tests.ProductTypeTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/vagrant/adbook/campaigns/tests.py", line 21, in test_slug
self.assertEqual(half_page.slug, 'half-page-ad')
AssertionError: '' != 'half-page-ad'
+ half-page-ad
----------------------------------------------------------------------
Ran 4 tests in 0.021s
FAILED (failures=2, errors=1)
Destroying test database for alias 'default' ('test_adbook')...
from datetime import timedelta
from django.db import models
from django.db.models import Max, Sum
from django.utils.text import slugify
from inventory.models import AdSlot
# Create your models here.
class Campaign(models.Model):
book_title = models.CharField(max_length=255)
date_created = models.DateField(auto_now_add=True)
date_updated = models.DateField(auto_now=True)
internal_design = models.BooleanField()
order = models.ForeignKey('Order')
start_date = models.DateField()
@property
def ad_design_price(self):
""" Get the sum of all ad design costs in the products for this campaign """
print(self.product_set.all())
return self.product_set.all().aggregate(total=Sum('price'))['total']
@property
def ad_deadline(self):
""" Get the distance between the start date and the earliest ad deadline. """
delta = self.product_set.aggregate(Max('ad_deadline'))['ad_deadline__max']
return self.start_date - timedelta(days=delta)
class Product(models.Model):
campaign = models.ForeignKey('Campaign')
date_created = models.DateField(auto_now_add=True)
date_updated = models.DateField(auto_now=True)
product_type = models.ForeignKey('ProductType')
run_length = models.IntegerField()
#ad_slot = models.ForeignKey('AdSlot')
class ProductType(models.Model):
UNKNOWN = 'UNKNOWN'
WEB = 'WEB'
PRINT = 'PRINT'
MOBILE = 'MOBILE'
EMAIL = 'EMAIL'
BILLBOARD = 'BILLBOARD'
MEDIUM_CHOICES = (
(UNKNOWN, 'Unknown'),
(WEB, 'Web'),
(PRINT, 'Print'),
(EMAIL, 'Email/Newsletter'),
(BILLBOARD, 'Billboard'),
)
RUNLENGTH_CHOICES = (
('ISSUES', 'Issues'),
('WEEKS', 'Weeks'),
)
medium = models.CharField(max_length=50, choices=MEDIUM_CHOICES)
name = models.CharField(max_length=255, )
price = models.DecimalField(max_digits=8, decimal_places=2)
run_length_period = models.CharField(max_length=50, choices=RUNLENGTH_CHOICES)
slug = models.SlugField(unique=True)
ad_deadline = models.IntegerField()
def __str__(self):
return self.name
def save(self, **kwargs):
if not self.slug:
self.slug = slugify(self.name)
super().save(**kwargs)
@property
def price(self):
return "${}".format(self.price)
# Three Customer Types
class Customer(models.Model):
# Add demographic information, payment information
pass
class Order(models.Model):
customer = models.ForeignKey('Customer')
paid = models.BooleanField(default=False)
from datetime import datetime, timedelta, date
from django.test import TestCase
from campaigns.factories import CampaignFactory, ProductTypeFactory
# Create your tests here.
class CampaignTest(TestCase):
def test_ad_design_price(self):
campaign = CampaignFactory.create()
self.assertEqual(campaign.ad_design_price, 1000)
def test_ad_deadline(self):
campaign = CampaignFactory.create()
self.assertEqual(campaign.ad_deadline, date(2016, 5, 8))
class ProductTypeTest(TestCase):
def test_slug(self):
half_page = ProductTypeFactory.create()
self.assertEqual(half_page.slug, 'half-page-ad')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment