2011-10-09 18:48:03 +00:00
|
|
|
from decimal import Decimal as D
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
2011-08-31 03:46:55 +00:00
|
|
|
from django.test import TestCase
|
2011-10-08 03:11:57 +00:00
|
|
|
from django.utils import unittest
|
2011-10-09 18:48:03 +00:00
|
|
|
from django.db import IntegrityError
|
2011-10-14 14:18:38 +00:00
|
|
|
from django.contrib.auth.models import User
|
2011-08-31 03:46:55 +00:00
|
|
|
|
2011-10-09 18:48:03 +00:00
|
|
|
from regluit.payment.models import Transaction
|
2011-10-08 03:11:57 +00:00
|
|
|
from regluit.core.models import Campaign, Work, UnglueitError
|
2011-09-29 06:23:50 +00:00
|
|
|
from regluit.core import bookloader, models, search
|
2011-10-08 03:11:57 +00:00
|
|
|
from regluit.payment.parameters import PAYMENT_TYPE_AUTHORIZATION
|
2011-09-07 09:34:03 +00:00
|
|
|
|
2011-10-14 02:16:28 +00:00
|
|
|
|
2011-10-13 01:59:46 +00:00
|
|
|
class TestBookLoader(TestCase):
|
2011-08-31 03:46:55 +00:00
|
|
|
|
2011-09-09 05:38:28 +00:00
|
|
|
def test_add_book(self):
|
2011-09-10 11:36:38 +00:00
|
|
|
# edition
|
2011-10-10 16:57:10 +00:00
|
|
|
edition = bookloader.add_by_isbn('0441012035')
|
2011-09-09 05:38:28 +00:00
|
|
|
self.assertEqual(edition.title, 'Neuromancer')
|
|
|
|
self.assertEqual(edition.publication_date, '2004')
|
|
|
|
self.assertEqual(edition.publisher, 'Ace Books')
|
|
|
|
self.assertEqual(edition.isbn_10, '0441012035')
|
2011-10-10 16:57:10 +00:00
|
|
|
self.assertEqual(edition.isbn_13, '9780441012039')
|
|
|
|
self.assertEqual(edition.googlebooks_id, "2NyiPwAACAAJ")
|
2011-10-14 04:02:19 +00:00
|
|
|
self.assertEqual(edition.language, "en")
|
2011-09-09 05:38:28 +00:00
|
|
|
|
2011-09-10 11:36:38 +00:00
|
|
|
# subjects
|
2011-10-10 16:57:10 +00:00
|
|
|
subject_names = [subject.name for subject in edition.subjects.all()]
|
|
|
|
self.assertEqual(len(subject_names), 11)
|
|
|
|
self.assertTrue('Japan' in subject_names)
|
2011-09-09 05:38:28 +00:00
|
|
|
|
2011-09-10 11:36:38 +00:00
|
|
|
# authors
|
2011-10-10 16:57:10 +00:00
|
|
|
self.assertEqual(edition.authors.all().count(), 1)
|
|
|
|
self.assertEqual(edition.authors.all()[0].name, 'William Gibson')
|
|
|
|
|
|
|
|
# work
|
|
|
|
self.assertTrue(edition.work)
|
2011-09-10 11:36:38 +00:00
|
|
|
|
|
|
|
def test_double_add(self):
|
2011-10-10 16:57:10 +00:00
|
|
|
bookloader.add_by_isbn('0441012035')
|
|
|
|
bookloader.add_by_isbn('0441012035')
|
|
|
|
self.assertEqual(models.Edition.objects.all().count(), 1)
|
2011-09-10 11:36:38 +00:00
|
|
|
self.assertEqual(models.Author.objects.all().count(), 1)
|
|
|
|
self.assertEqual(models.Work.objects.all().count(), 1)
|
2011-10-10 16:57:10 +00:00
|
|
|
self.assertEqual(models.Subject.objects.all().count(), 11)
|
2011-10-10 21:26:38 +00:00
|
|
|
|
|
|
|
def test_missing_isbn(self):
|
|
|
|
e = bookloader.add_by_isbn('0139391401')
|
|
|
|
self.assertEqual(e, None)
|
2011-09-29 06:23:50 +00:00
|
|
|
|
2011-10-13 01:59:46 +00:00
|
|
|
def test_thingisbn(self):
|
|
|
|
isbns = bookloader.thingisbn('0441012035')
|
|
|
|
self.assertTrue(len(isbns) > 20)
|
|
|
|
self.assertTrue('0441012035' in isbns)
|
|
|
|
self.assertTrue('3453313895' in isbns)
|
|
|
|
|
2011-10-14 04:02:19 +00:00
|
|
|
def test_add_related(self):
|
|
|
|
# add one edition
|
|
|
|
edition = bookloader.add_by_isbn('0441012035')
|
|
|
|
self.assertEqual(models.Edition.objects.count(), 1)
|
|
|
|
self.assertEqual(models.Work.objects.count(), 1)
|
|
|
|
|
|
|
|
# ask for related editions to be added using the work we just created
|
|
|
|
bookloader.add_related('0441012035')
|
|
|
|
self.assertTrue(models.Edition.objects.count() > 20)
|
|
|
|
self.assertEqual(models.Work.objects.count(), 1)
|
|
|
|
self.assertTrue(edition.work.editions.count() > 20)
|
|
|
|
|
2011-09-29 06:23:50 +00:00
|
|
|
class SearchTests(TestCase):
|
|
|
|
|
|
|
|
def test_basic_search(self):
|
|
|
|
results = search.gluejar_search('melville')
|
|
|
|
self.assertEqual(len(results), 10)
|
|
|
|
|
|
|
|
r = results[0]
|
2011-09-29 19:07:16 +00:00
|
|
|
self.assertTrue(r.has_key('title'))
|
2011-09-29 06:23:50 +00:00
|
|
|
self.assertTrue(r.has_key('author'))
|
|
|
|
self.assertTrue(r.has_key('description'))
|
|
|
|
self.assertTrue(r.has_key('image'))
|
|
|
|
self.assertTrue(r.has_key('publisher'))
|
2011-09-29 19:07:16 +00:00
|
|
|
self.assertTrue(r.has_key('isbn_10'))
|
2011-10-10 16:57:10 +00:00
|
|
|
self.assertTrue(r.has_key('googlebooks_id'))
|
2011-09-29 06:23:50 +00:00
|
|
|
|
|
|
|
def test_googlebooks_search(self):
|
|
|
|
response = search.googlebooks_search('melville')
|
|
|
|
self.assertEqual(len(response['items']), 10)
|
2011-10-07 21:17:54 +00:00
|
|
|
|
2011-10-10 21:26:38 +00:00
|
|
|
|
2011-10-07 21:17:54 +00:00
|
|
|
class CampaignTests(TestCase):
|
|
|
|
|
2011-10-09 18:48:03 +00:00
|
|
|
def test_required_fields(self):
|
|
|
|
# a campaign must have a target, deadline and a work
|
|
|
|
|
|
|
|
c = Campaign()
|
|
|
|
self.assertRaises(IntegrityError, c.save)
|
|
|
|
|
|
|
|
c = Campaign(target=D('1000.00'))
|
|
|
|
self.assertRaises(IntegrityError, c.save)
|
|
|
|
|
|
|
|
c = Campaign(target=D('1000.00'), deadline=datetime(2012, 1, 1))
|
|
|
|
self.assertRaises(IntegrityError, c.save)
|
|
|
|
|
2011-10-14 04:02:19 +00:00
|
|
|
w = Work()
|
|
|
|
w.save()
|
|
|
|
c = Campaign(target=D('1000.00'), deadline=datetime(2012, 1, 1), work=w)
|
|
|
|
c.save()
|
2011-10-09 18:48:03 +00:00
|
|
|
|
2011-10-08 03:11:57 +00:00
|
|
|
def test_campaign_status(self):
|
|
|
|
w = Work()
|
|
|
|
w.save()
|
|
|
|
# INITIALIZED
|
2011-10-09 18:48:03 +00:00
|
|
|
c1 = Campaign(target=D('1000.00'),deadline=datetime(2012,1,1),work=w)
|
2011-10-08 03:11:57 +00:00
|
|
|
c1.save()
|
2011-10-10 20:35:22 +00:00
|
|
|
self.assertEqual(c1.status, 'INITIALIZED')
|
2011-10-08 03:11:57 +00:00
|
|
|
# ACTIVATED
|
2011-10-09 18:48:03 +00:00
|
|
|
c2 = Campaign(target=D('1000.00'),deadline=datetime(2012,1,1),work=w)
|
2011-10-08 03:11:57 +00:00
|
|
|
c2.save()
|
2011-10-10 20:35:22 +00:00
|
|
|
self.assertEqual(c2.status, 'INITIALIZED')
|
2011-10-08 03:11:57 +00:00
|
|
|
c2.activate()
|
2011-10-10 20:35:22 +00:00
|
|
|
self.assertEqual(c2.status, 'ACTIVE')
|
2011-10-08 03:11:57 +00:00
|
|
|
# SUSPENDED
|
|
|
|
c2.suspend(reason="for testing")
|
2011-10-10 20:35:22 +00:00
|
|
|
self.assertEqual(c2.status, 'SUSPENDED')
|
2011-10-08 03:11:57 +00:00
|
|
|
# RESUMING
|
|
|
|
c2.resume()
|
|
|
|
self.assertEqual(c2.suspended, None)
|
2011-10-10 20:35:22 +00:00
|
|
|
self.assertEqual(c2.status,'ACTIVE')
|
2011-10-08 03:11:57 +00:00
|
|
|
# should not let me suspend a campaign that hasn't been initialized
|
|
|
|
self.assertRaises(UnglueitError, c1.suspend, "for testing")
|
|
|
|
# UNSUCCESSFUL
|
2011-10-09 18:48:03 +00:00
|
|
|
c3 = Campaign(target=D('1000.00'),deadline=datetime.utcnow() - timedelta(days=1),work=w)
|
2011-10-08 03:11:57 +00:00
|
|
|
c3.save()
|
|
|
|
c3.activate()
|
2011-10-10 20:35:22 +00:00
|
|
|
self.assertEqual(c3.status, 'UNSUCCESSFUL')
|
2011-10-08 03:11:57 +00:00
|
|
|
# SUCCESSFUL
|
2011-10-09 18:48:03 +00:00
|
|
|
c4 = Campaign(target=D('1000.00'),deadline=datetime.utcnow() - timedelta(days=1),work=w)
|
2011-10-08 03:11:57 +00:00
|
|
|
c4.save()
|
|
|
|
c4.activate()
|
|
|
|
|
|
|
|
t = Transaction()
|
|
|
|
t.amount = D('1234.00')
|
|
|
|
t.type = PAYMENT_TYPE_AUTHORIZATION
|
|
|
|
t.status = 'ACTIVE'
|
|
|
|
t.campaign = c4
|
|
|
|
t.save()
|
2011-10-10 20:35:22 +00:00
|
|
|
self.assertEqual(c4.status, 'SUCCESSFUL')
|
2011-10-08 03:11:57 +00:00
|
|
|
|
|
|
|
# ACTIVE
|
2011-10-09 18:48:03 +00:00
|
|
|
c4.deadline = datetime.utcnow() + timedelta(days=1)
|
2011-10-08 03:11:57 +00:00
|
|
|
c4.save()
|
2011-10-10 20:35:22 +00:00
|
|
|
self.assertEqual(c4.status, 'ACTIVE')
|
2011-10-08 03:11:57 +00:00
|
|
|
|
|
|
|
# WITHDRAWN
|
2011-10-09 18:48:03 +00:00
|
|
|
c5 = Campaign(target=D('1000.00'),deadline=datetime(2012,1,1),work=w)
|
2011-10-08 03:11:57 +00:00
|
|
|
c5.save()
|
|
|
|
c5.activate().withdraw('testing')
|
2011-10-10 20:35:22 +00:00
|
|
|
self.assertEqual(c5.status, 'WITHDRAWN')
|
2011-10-14 02:16:28 +00:00
|
|
|
|
|
|
|
|
2011-10-14 14:42:33 +00:00
|
|
|
class SettingsTest(TestCase):
|
|
|
|
|
|
|
|
def test_dev_me_alignment(self):
|
|
|
|
from regluit.settings import me, dev
|
|
|
|
self.assertEqual(set(me.__dict__.keys()) ^ set(dev.__dict__.keys()), set([]))
|
|
|
|
|
|
|
|
def test_prod_me_alignment(self):
|
|
|
|
from regluit.settings import me, prod
|
|
|
|
self.assertEqual(set(me.__dict__.keys()) ^ set(prod.__dict__.keys()), set([]))
|
|
|
|
|
|
|
|
|
2011-10-14 14:18:38 +00:00
|
|
|
class WishlistTest(TestCase):
|
|
|
|
|
|
|
|
def test_add_remove(self):
|
|
|
|
# add a work to a user's wishlist
|
|
|
|
user = User.objects.create_user('test', 'test@example.com', 'testpass')
|
|
|
|
edition = bookloader.add_by_isbn('0441012035')
|
|
|
|
work = edition.work
|
|
|
|
user.wishlist.works.add(work)
|
|
|
|
self.assertEqual(user.wishlist.works.count(), 1)
|
|
|
|
user.wishlist.works.remove(work)
|
|
|
|
self.assertEqual(user.wishlist.works.count(), 0)
|
|
|
|
|
2011-10-14 02:16:28 +00:00
|
|
|
class SettingsTest(TestCase):
|
|
|
|
|
|
|
|
def test_dev_me_alignment(self):
|
|
|
|
from regluit.settings import me, dev
|
|
|
|
self.assertEqual(set(me.__dict__.keys()) ^ set(dev.__dict__.keys()), set([]))
|
|
|
|
|
|
|
|
def test_prod_me_alignment(self):
|
|
|
|
from regluit.settings import me, prod
|
|
|
|
self.assertEqual(set(me.__dict__.keys()) ^ set(prod.__dict__.keys()), set([]))
|
|
|
|
|
|
|
|
def suite():
|
|
|
|
|
2011-10-14 14:42:33 +00:00
|
|
|
testcases = [TestBookLoader, SearchTests, CampaignTests, WishlistTest]
|
2011-10-14 02:16:28 +00:00
|
|
|
suites = unittest.TestSuite([unittest.TestLoader().loadTestsFromTestCase(testcase) for testcase in testcases])
|
|
|
|
suites.addTest(SettingsTest('test_dev_me_alignment')) # leave out alignment with prod test right now
|
|
|
|
return suites
|
2011-10-14 14:42:33 +00:00
|
|
|
|