2011-10-09 18:48:03 +00:00
|
|
|
from decimal import Decimal as D
|
|
|
|
from datetime import datetime, timedelta
|
2012-04-27 21:29:57 +00:00
|
|
|
from regluit.utils.localdatetime import now, date_today
|
2011-10-09 18:48:03 +00:00
|
|
|
|
2011-08-31 03:46:55 +00:00
|
|
|
from django.test import TestCase
|
2012-02-10 03:30:33 +00:00
|
|
|
from django.test.client import Client
|
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-11-18 00:45:26 +00:00
|
|
|
from django.conf import settings
|
2012-02-10 01:51:10 +00:00
|
|
|
from django.contrib.comments.models import Comment
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
from django.contrib.sites.models import Site
|
2011-08-31 03:46:55 +00:00
|
|
|
|
2011-10-09 18:48:03 +00:00
|
|
|
from regluit.payment.models import Transaction
|
2012-05-08 23:08:36 +00:00
|
|
|
from regluit.core.models import Campaign, Work, UnglueitError, Edition, RightsHolder, Claim, Key
|
2011-11-18 00:45:26 +00:00
|
|
|
from regluit.core import bookloader, models, search, goodreads, librarything
|
2011-11-18 18:01:37 +00:00
|
|
|
from regluit.core import isbn
|
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-11-10 23:14:33 +00:00
|
|
|
from regluit.core import tasks
|
|
|
|
from celery.task.sets import TaskSet
|
|
|
|
from celery.task import chord
|
|
|
|
|
|
|
|
from time import sleep
|
|
|
|
from math import factorial
|
2012-02-21 17:39:36 +00:00
|
|
|
from urlparse import parse_qs, urlparse
|
2011-11-10 23:14:33 +00:00
|
|
|
|
2011-10-14 02:16:28 +00:00
|
|
|
|
2011-12-19 06:33:13 +00:00
|
|
|
class BookLoaderTests(TestCase):
|
2012-04-27 21:29:57 +00:00
|
|
|
def setUp(self):
|
|
|
|
self.user = User.objects.create_user('core_test', 'test@example.org', 'core_test')
|
|
|
|
self.client = Client()
|
|
|
|
self.client.login(username='core_test', password='core_test')
|
2011-08-31 03:46:55 +00:00
|
|
|
|
2011-10-19 03:00:07 +00:00
|
|
|
def test_add_by_isbn(self):
|
2011-09-10 11:36:38 +00:00
|
|
|
# edition
|
2012-07-20 15:40:11 +00:00
|
|
|
edition = bookloader.add_by_isbn('0441007465')
|
2011-09-09 05:38:28 +00:00
|
|
|
self.assertEqual(edition.title, 'Neuromancer')
|
2012-07-20 15:40:11 +00:00
|
|
|
self.assertEqual(edition.publication_date, u'2000-07-01')
|
|
|
|
self.assertEqual(edition.publisher, u'Ace Trade')
|
|
|
|
self.assertEqual(edition.isbn_10, '0441007465')
|
|
|
|
self.assertEqual(edition.isbn_13, '9780441007462')
|
|
|
|
self.assertEqual(edition.googlebooks_id, 'IDFfMPW32hQC')
|
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)
|
2012-01-17 04:28:34 +00:00
|
|
|
|
2012-01-29 03:16:14 +00:00
|
|
|
# locale in language
|
|
|
|
edition = bookloader.add_by_isbn('9787500676911')
|
|
|
|
self.assertEqual(edition.work.language, 'zh')
|
2011-09-10 11:36:38 +00:00
|
|
|
|
2012-06-22 03:04:28 +00:00
|
|
|
# @unittest.expectedFailure
|
2012-02-02 14:05:08 +00:00
|
|
|
def test_update_edition(self):
|
|
|
|
w = models.Work(title='silly title', language='xx')
|
|
|
|
w.save()
|
|
|
|
e = models.Edition(title=w.title,work=w)
|
|
|
|
e.save()
|
|
|
|
models.Identifier(type='isbn', value='9780226032030', work=w, edition=e).save()
|
2012-02-21 17:39:36 +00:00
|
|
|
bookloader.update_edition(e)
|
2012-02-02 14:05:08 +00:00
|
|
|
self.assertEqual(e.work.language, 'en')
|
2012-06-18 20:55:45 +00:00
|
|
|
self.assertEqual(e.title, 'Forbidden Journeys')
|
2012-02-02 14:05:08 +00:00
|
|
|
|
2011-09-10 11:36:38 +00:00
|
|
|
def test_double_add(self):
|
2012-07-20 15:40:11 +00:00
|
|
|
bookloader.add_by_isbn('0441007465')
|
|
|
|
bookloader.add_by_isbn('0441007465')
|
2011-10-10 16:57:10 +00:00
|
|
|
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 21:26:38 +00:00
|
|
|
|
|
|
|
def test_missing_isbn(self):
|
2012-01-09 18:55:22 +00:00
|
|
|
e = bookloader.add_by_isbn_from_google('0139391401')
|
2011-10-10 21:26:38 +00:00
|
|
|
self.assertEqual(e, None)
|
2011-09-29 06:23:50 +00:00
|
|
|
|
2011-10-13 01:59:46 +00:00
|
|
|
def test_thingisbn(self):
|
2012-07-20 15:40:11 +00:00
|
|
|
isbns = bookloader.thingisbn('0441007465')
|
2011-10-13 01:59:46 +00:00
|
|
|
self.assertTrue(len(isbns) > 20)
|
2012-07-20 15:40:11 +00:00
|
|
|
self.assertTrue('0441007465' in isbns)
|
2011-10-13 01:59:46 +00:00
|
|
|
self.assertTrue('3453313895' in isbns)
|
|
|
|
|
2011-10-14 04:02:19 +00:00
|
|
|
def test_add_related(self):
|
|
|
|
# add one edition
|
2012-07-20 15:40:11 +00:00
|
|
|
edition = bookloader.add_by_isbn('0441007465')
|
2011-10-14 04:02:19 +00:00
|
|
|
self.assertEqual(models.Edition.objects.count(), 1)
|
|
|
|
self.assertEqual(models.Work.objects.count(), 1)
|
2012-01-10 20:20:02 +00:00
|
|
|
lang=edition.work.language
|
2011-10-14 04:02:19 +00:00
|
|
|
# ask for related editions to be added using the work we just created
|
2012-07-20 15:40:11 +00:00
|
|
|
bookloader.add_related('0441007465')
|
2011-12-13 14:55:26 +00:00
|
|
|
self.assertTrue(models.Edition.objects.count() > 15)
|
2012-01-10 20:20:02 +00:00
|
|
|
self.assertEqual(models.Work.objects.filter(language=lang).count(), 1)
|
2012-02-04 20:40:10 +00:00
|
|
|
self.assertTrue(edition.work.editions.count() > 9)
|
2011-12-13 14:55:26 +00:00
|
|
|
|
|
|
|
|
2011-12-19 06:33:13 +00:00
|
|
|
def test_populate_edition(self):
|
|
|
|
edition = bookloader.add_by_googlebooks_id('c_dBPgAACAAJ')
|
2012-02-16 18:19:36 +00:00
|
|
|
edition = tasks.populate_edition.run(edition.isbn_13)
|
2011-12-19 06:33:13 +00:00
|
|
|
self.assertTrue(edition.work.editions.all().count() > 20)
|
|
|
|
self.assertTrue(edition.work.subjects.all().count() > 10)
|
2012-01-17 04:28:34 +00:00
|
|
|
self.assertTrue(edition.work.publication_date)
|
|
|
|
edition.publication_date = None
|
|
|
|
self.assertTrue(edition.work.publication_date)
|
2012-05-07 05:18:11 +00:00
|
|
|
self.assertTrue(len(edition.work.description) > 20)
|
2012-05-07 02:31:38 +00:00
|
|
|
self.assertTrue(edition.work.identifiers.filter(type='oclc')[0])
|
|
|
|
|
2011-10-14 04:02:19 +00:00
|
|
|
|
2012-02-13 22:35:08 +00:00
|
|
|
def test_merge_works_mechanics(self):
|
|
|
|
"""Make sure then merge_works is still okay when we try to merge works with themselves and with deleted works"""
|
|
|
|
w1 = Work(title="Work 1")
|
|
|
|
w1.save()
|
|
|
|
|
|
|
|
w2 = Work(title="Work 2")
|
|
|
|
w2.save()
|
|
|
|
|
|
|
|
e1 = Edition(work=w1)
|
|
|
|
e1.save()
|
|
|
|
|
|
|
|
e2 = Edition(work=w2)
|
|
|
|
e2.save()
|
|
|
|
|
|
|
|
e2a = Edition(work=w2)
|
|
|
|
e2a.save()
|
|
|
|
|
|
|
|
self.assertTrue(e1)
|
|
|
|
self.assertTrue(e2)
|
|
|
|
self.assertTrue(e2a)
|
|
|
|
self.assertTrue(e1.work)
|
|
|
|
self.assertTrue(e2.work)
|
|
|
|
self.assertEqual(models.Work.objects.count(), 2)
|
|
|
|
|
|
|
|
w1_id = w1.id
|
|
|
|
w2_id = w2.id
|
|
|
|
|
|
|
|
# first try to merge work 1 into itself -- should not do anything
|
|
|
|
bookloader.merge_works(w1,w1)
|
|
|
|
self.assertEqual(models.Work.objects.count(), 2)
|
|
|
|
|
|
|
|
# merge the second work into the first
|
|
|
|
bookloader.merge_works(e1.work, e2.work)
|
|
|
|
self.assertEqual(models.Work.objects.count(),1)
|
|
|
|
self.assertEqual(models.WasWork.objects.count(),1)
|
|
|
|
|
|
|
|
# getting proper view?
|
|
|
|
anon_client = Client()
|
|
|
|
r = anon_client.get("/work/%s/" % w1_id)
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
r = anon_client.get("/work/%s/" % w2_id)
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
# try to do it twice -- nothing should happen
|
|
|
|
bookloader.merge_works(e1.work, e2a.work)
|
|
|
|
r = anon_client.get("/work/%s/" % w1_id)
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
r = anon_client.get("/work/%s/" % w2_id)
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
|
2011-10-19 03:00:07 +00:00
|
|
|
def test_merge_works(self):
|
|
|
|
# add two editions and see that there are two stub works
|
2012-02-29 13:51:13 +00:00
|
|
|
e1 = bookloader.add_by_isbn('0385722133')
|
|
|
|
e2 = bookloader.add_by_isbn('0385504187')
|
2011-11-06 22:42:09 +00:00
|
|
|
self.assertTrue(e1)
|
|
|
|
self.assertTrue(e2)
|
2011-12-19 06:33:13 +00:00
|
|
|
self.assertTrue(e1.work)
|
|
|
|
self.assertTrue(e2.work)
|
2011-10-19 03:00:07 +00:00
|
|
|
self.assertEqual(models.Work.objects.count(), 2)
|
|
|
|
|
|
|
|
# add the stub works to a wishlist
|
|
|
|
user = User.objects.create_user('test', 'test@example.com', 'testpass')
|
2011-12-08 23:22:05 +00:00
|
|
|
user.wishlist.add_work(e1.work, 'test')
|
|
|
|
user.wishlist.add_work(e2.work, 'test')
|
2012-05-25 18:52:50 +00:00
|
|
|
manager = User.objects.create_user('manager', 'manager@example.com', 'managerpass')
|
2011-10-19 03:47:48 +00:00
|
|
|
# create campaigns for the stub works
|
2011-10-19 03:00:07 +00:00
|
|
|
c1 = models.Campaign.objects.create(
|
|
|
|
name=e1.work.title,
|
2012-05-25 18:52:50 +00:00
|
|
|
work=e1.work,
|
2011-10-19 03:00:07 +00:00
|
|
|
description='Test Campaign 1',
|
2012-03-09 23:31:30 +00:00
|
|
|
deadline=now(),
|
2011-10-19 03:00:07 +00:00
|
|
|
target=D('1000.00'),
|
|
|
|
)
|
|
|
|
c2 = models.Campaign.objects.create(
|
|
|
|
name=e2.work.title,
|
|
|
|
work=e2.work,
|
|
|
|
description='Test Campaign 2',
|
2012-03-09 23:31:30 +00:00
|
|
|
deadline=now(),
|
2011-10-19 03:00:07 +00:00
|
|
|
target=D('1000.00'),
|
|
|
|
)
|
2012-05-25 18:52:50 +00:00
|
|
|
c2.managers.add(manager)
|
|
|
|
c2.save()
|
|
|
|
self.assertEqual(c2.pk, e2.work.last_campaign().pk)
|
2012-02-10 01:51:10 +00:00
|
|
|
# comment on the works
|
|
|
|
site = Site.objects.all()[0]
|
|
|
|
wct = ContentType.objects.get_for_model(models.Work)
|
|
|
|
comment1 = Comment(
|
|
|
|
content_type=wct,
|
|
|
|
object_pk=e1.work.pk,
|
|
|
|
comment="test comment1",
|
|
|
|
user=user,
|
|
|
|
site=site
|
|
|
|
)
|
|
|
|
comment1.save()
|
|
|
|
comment2 = Comment(
|
|
|
|
content_type=wct,
|
|
|
|
object_pk=e2.work.pk,
|
|
|
|
comment="test comment2",
|
|
|
|
user=user,
|
|
|
|
site=site
|
|
|
|
)
|
|
|
|
comment2.save()
|
2012-05-25 18:52:50 +00:00
|
|
|
comment3 = Comment(
|
|
|
|
content_type=wct,
|
|
|
|
object_pk=e2.work.pk,
|
|
|
|
comment="test comment3",
|
|
|
|
user=manager,
|
|
|
|
site=site
|
|
|
|
)
|
|
|
|
comment3.save()
|
|
|
|
|
2012-02-10 01:51:10 +00:00
|
|
|
|
2011-10-19 03:00:07 +00:00
|
|
|
# now add related edition to make sure Works get merged
|
2012-02-29 13:51:13 +00:00
|
|
|
bookloader.add_related('0385722133')
|
2011-10-19 03:00:07 +00:00
|
|
|
self.assertEqual(models.Work.objects.count(), 1)
|
2012-02-29 13:51:13 +00:00
|
|
|
w3 = models.Edition.get_by_isbn('0385722133').work
|
2011-10-19 03:00:07 +00:00
|
|
|
|
|
|
|
# and that relevant Campaigns and Wishlists are updated
|
2012-05-25 18:52:50 +00:00
|
|
|
c1=Campaign.objects.get(pk=c1.pk)
|
|
|
|
c2=Campaign.objects.get(pk=c2.pk)
|
2012-05-25 16:52:25 +00:00
|
|
|
|
2011-10-19 03:00:07 +00:00
|
|
|
self.assertEqual(c1.work, c2.work)
|
|
|
|
self.assertEqual(user.wishlist.works.all().count(), 1)
|
2012-05-25 18:52:50 +00:00
|
|
|
self.assertEqual(Comment.objects.for_model(w3).count(), 3)
|
2012-02-10 03:30:33 +00:00
|
|
|
|
|
|
|
anon_client = Client()
|
|
|
|
r = anon_client.get("/work/%s/" % w3.pk)
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
r = anon_client.get("/work/%s/" % e2.work.pk)
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
2011-11-06 21:33:04 +00:00
|
|
|
|
|
|
|
def test_ebook(self):
|
|
|
|
edition = bookloader.add_by_oclc('1246014')
|
2011-11-06 22:49:25 +00:00
|
|
|
self.assertEqual(edition.ebooks.count(), 2)
|
|
|
|
#ebook_epub = edition.ebooks.all()[0]
|
|
|
|
ebook_epub = edition.ebooks.filter(format='epub')[0]
|
|
|
|
self.assertEqual(ebook_epub.format, 'epub')
|
2012-02-21 17:39:36 +00:00
|
|
|
#self.assertEqual(ebook_epub.url, 'http://books.google.com/books/download/The_Latin_language.epub?id=U3FXAAAAYAAJ&ie=ISO-8859-1&output=epub&source=gbs_api')
|
|
|
|
self.assertEqual(parse_qs(urlparse(ebook_epub.url).query).get("id"), ['U3FXAAAAYAAJ'])
|
|
|
|
self.assertEqual(parse_qs(urlparse(ebook_epub.url).query).get("output"), ['epub'])
|
2012-02-28 22:28:33 +00:00
|
|
|
self.assertEqual(ebook_epub.provider, 'Google Books')
|
|
|
|
self.assertEqual(ebook_epub.set_provider(), 'Google Books')
|
2011-11-06 22:49:25 +00:00
|
|
|
ebook_pdf = edition.ebooks.filter(format='pdf')[0]
|
|
|
|
self.assertEqual(ebook_pdf.format, 'pdf')
|
2012-02-21 17:39:36 +00:00
|
|
|
#self.assertEqual(ebook_pdf.url, 'http://books.google.com/books/download/The_Latin_language.pdf?id=U3FXAAAAYAAJ&ie=ISO-8859-1&output=pdf&sig=ACfU3U2yLt3nmTncB8ozxOWUc4iHKUznCA&source=gbs_api')
|
|
|
|
self.assertEqual(parse_qs(urlparse(ebook_pdf.url).query).get("id"), ['U3FXAAAAYAAJ'])
|
|
|
|
self.assertEqual(parse_qs(urlparse(ebook_pdf.url).query).get("output"), ['pdf'])
|
2012-02-28 22:28:33 +00:00
|
|
|
self.assertEqual(ebook_pdf.provider, 'Google Books')
|
|
|
|
self.assertEqual(edition.public_domain, True)
|
2011-10-19 03:00:07 +00:00
|
|
|
|
2011-11-07 20:39:02 +00:00
|
|
|
w = edition.work
|
2012-02-21 17:39:36 +00:00
|
|
|
self.assertEqual(w.first_epub().url, ebook_epub.url)
|
|
|
|
self.assertEqual(w.first_pdf().url, ebook_pdf.url)
|
|
|
|
self.assertEqual(w.first_epub_url(), ebook_epub.url)
|
|
|
|
self.assertEqual(w.first_pdf_url(), ebook_pdf.url)
|
2011-11-07 20:39:02 +00:00
|
|
|
|
2012-02-28 22:28:33 +00:00
|
|
|
ebook_pdf.url='http://en.wikisource.org/wiki/Frankenstein'
|
|
|
|
self.assertEqual(ebook_pdf.set_provider(), 'Wikisource')
|
|
|
|
|
2012-04-27 21:29:57 +00:00
|
|
|
self.user.wishlist.add_work(w, 'test')
|
|
|
|
tasks.report_new_ebooks(date_today())
|
|
|
|
r = self.client.get("/notification/" )
|
|
|
|
self.assertEqual(r.status_code, 200)
|
|
|
|
|
|
|
|
|
2011-11-06 22:42:09 +00:00
|
|
|
def test_add_no_ebook(self):
|
|
|
|
# this edition lacks an ebook, but we should still be able to load it
|
|
|
|
e = bookloader.add_by_isbn('0465019358')
|
|
|
|
self.assertTrue(e)
|
|
|
|
|
2012-07-20 16:28:14 +00:00
|
|
|
@unittest.expectedFailure
|
2011-12-13 14:55:26 +00:00
|
|
|
def test_one_language(self):
|
|
|
|
# english edition for cat's cradle should only pull in other
|
|
|
|
# english editions
|
2012-07-20 16:28:14 +00:00
|
|
|
# expected failure right now because Google seems to have bad data about language of Cat's Cradle
|
|
|
|
# e.g., https://www.googleapis.com/books/v1/volumes?q=isbn:9789513033774
|
|
|
|
# title = "Kissan kehto" -- language according to API = English
|
2011-12-13 14:55:26 +00:00
|
|
|
work = bookloader.add_by_isbn('079530272X').work
|
|
|
|
self.assertEqual(work.language, 'en')
|
|
|
|
bookloader.add_related('079530272X')
|
|
|
|
for edition in work.editions.all():
|
|
|
|
self.assertEqual(edition.title.lower(), "cat's cradle")
|
|
|
|
|
2011-12-19 06:33:13 +00:00
|
|
|
def test_add_openlibrary(self):
|
2012-07-20 15:40:11 +00:00
|
|
|
work = bookloader.add_by_isbn('0441007465').work
|
|
|
|
bookloader.add_related('0441007465')
|
2011-12-19 06:33:13 +00:00
|
|
|
bookloader.add_openlibrary(work)
|
2011-12-19 07:34:29 +00:00
|
|
|
subjects = [s.name for s in work.subjects.all()]
|
2011-12-19 07:20:24 +00:00
|
|
|
self.assertTrue(len(subjects) > 10)
|
2011-12-19 07:27:07 +00:00
|
|
|
self.assertTrue('Science fiction' in subjects)
|
2012-05-07 05:18:11 +00:00
|
|
|
self.assertTrue('/works/OL27258W' in work.identifiers.filter(type='olwk').values_list('value',flat=True) )
|
2012-07-20 15:40:11 +00:00
|
|
|
self.assertTrue('888628' in work.identifiers.filter(type='gdrd').values_list('value',flat=True))
|
2012-05-07 05:18:11 +00:00
|
|
|
self.assertTrue('609' in work.identifiers.filter(type='ltwk').values_list('value',flat=True))
|
|
|
|
|
2012-02-15 02:01:13 +00:00
|
|
|
def test_load_gutenberg_edition(self):
|
|
|
|
"""Let's try this out for Moby Dick"""
|
|
|
|
|
|
|
|
title = "Moby Dick"
|
|
|
|
ol_work_id = "/works/OL102749W"
|
|
|
|
gutenberg_etext_id = 2701
|
|
|
|
epub_url = "http://www.gutenberg.org/cache/epub/2701/pg2701.epub"
|
|
|
|
license = 'http://www.gutenberg.org/license'
|
|
|
|
lang = 'en'
|
|
|
|
format = 'epub'
|
|
|
|
publication_date = datetime(2001,7,1)
|
|
|
|
seed_isbn = '9780142000083' # http://www.amazon.com/Moby-Dick-Whale-Penguin-Classics-Deluxe/dp/0142000086
|
|
|
|
|
|
|
|
ebook = bookloader.load_gutenberg_edition(title, gutenberg_etext_id, ol_work_id, seed_isbn, epub_url, format, license, lang, publication_date)
|
|
|
|
self.assertEqual(ebook.url, epub_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-12-19 06:33:13 +00:00
|
|
|
|
|
|
|
|
2011-09-29 06:23:50 +00:00
|
|
|
class SearchTests(TestCase):
|
|
|
|
|
|
|
|
def test_basic_search(self):
|
2011-11-20 14:28:53 +00:00
|
|
|
results = search.gluejar_search('melville')
|
2011-09-29 06:23:50 +00:00
|
|
|
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'))
|
2011-11-23 18:18:32 +00:00
|
|
|
self.assertTrue(r.has_key('cover_image_thumbnail'))
|
2012-04-03 19:27:33 +00:00
|
|
|
self.assertTrue(r['cover_image_thumbnail'].startswith('https'))
|
2011-09-29 06:23:50 +00:00
|
|
|
self.assertTrue(r.has_key('publisher'))
|
2011-12-20 04:26:55 +00:00
|
|
|
self.assertTrue(r.has_key('isbn_13'))
|
2011-10-10 16:57:10 +00:00
|
|
|
self.assertTrue(r.has_key('googlebooks_id'))
|
2011-09-29 06:23:50 +00:00
|
|
|
|
2012-02-05 00:22:04 +00:00
|
|
|
def test_pagination(self):
|
|
|
|
r1 = search.gluejar_search('melville', page=1)
|
|
|
|
r2 = search.gluejar_search('melville', page=2)
|
|
|
|
isbns1 = set([r['isbn_13'] for r in r1])
|
|
|
|
isbns2 = set([r['isbn_13'] for r in r2])
|
|
|
|
self.assertTrue(isbns1 != isbns2)
|
2012-02-05 00:06:53 +00:00
|
|
|
|
2011-09-29 06:23:50 +00:00
|
|
|
def test_googlebooks_search(self):
|
2012-02-05 00:22:04 +00:00
|
|
|
response = search.googlebooks_search('melville', '69.243.24.29', 1)
|
2011-09-29 06:23:50 +00:00
|
|
|
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)
|
|
|
|
|
2012-04-03 00:57:30 +00:00
|
|
|
c = Campaign(target=D('1000.00'), deadline=datetime(2013, 1, 1))
|
2011-10-09 18:48:03 +00:00
|
|
|
self.assertRaises(IntegrityError, c.save)
|
|
|
|
|
2011-10-14 04:02:19 +00:00
|
|
|
w = Work()
|
|
|
|
w.save()
|
2012-04-03 00:57:30 +00:00
|
|
|
c = Campaign(target=D('1000.00'), deadline=datetime(2013, 1, 1), work=w)
|
2012-05-20 04:12:16 +00:00
|
|
|
c.license = 'CC BY-NC'
|
2011-10-14 04:02:19 +00:00
|
|
|
c.save()
|
2012-05-20 04:12:16 +00:00
|
|
|
self.assertEqual(c.license_url, 'http://creativecommons.org/licenses/by-nc/3.0/')
|
|
|
|
self.assertEqual(c.license_badge, 'https://i.creativecommons.org/l/by-nc/3.0/88x31.png')
|
|
|
|
|
2011-10-09 18:48:03 +00:00
|
|
|
|
2011-10-08 03:11:57 +00:00
|
|
|
def test_campaign_status(self):
|
2012-04-03 00:57:30 +00:00
|
|
|
|
|
|
|
# need a user to associate with a transaction
|
|
|
|
user = User.objects.create_user('test', 'test@example.com', 'testpass')
|
|
|
|
|
2011-10-08 03:11:57 +00:00
|
|
|
w = Work()
|
|
|
|
w.save()
|
|
|
|
# INITIALIZED
|
2012-04-03 00:57:30 +00:00
|
|
|
c1 = Campaign(target=D('1000.00'),deadline=datetime(2013,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
|
2012-04-03 00:57:30 +00:00
|
|
|
c2 = Campaign(target=D('1000.00'),deadline=datetime(2013,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')
|
2012-04-03 14:45:48 +00:00
|
|
|
u = User.objects.create_user('claimer', 'claimer@example.com', 'claimer')
|
|
|
|
u.save()
|
|
|
|
rh = RightsHolder(owner = u, rights_holder_name = 'rights holder name')
|
|
|
|
rh.save()
|
|
|
|
cl = Claim(rights_holder = rh, work = w, user = u, status = 'active')
|
|
|
|
cl.save()
|
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
|
2011-12-13 21:24:39 +00:00
|
|
|
c2.resume(reason="for testing")
|
|
|
|
#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
|
2012-03-09 23:31:30 +00:00
|
|
|
c3 = Campaign(target=D('1000.00'),deadline=now() - timedelta(days=1),work=w)
|
2011-10-08 03:11:57 +00:00
|
|
|
c3.save()
|
|
|
|
c3.activate()
|
2012-04-03 00:57:30 +00:00
|
|
|
self.assertEqual(c3.status, 'ACTIVE')
|
|
|
|
# at this point, since the deadline has passed, the status should change and be UNSUCCESSFUL
|
2012-03-09 22:18:11 +00:00
|
|
|
self.assertTrue(c3.update_status())
|
2011-10-10 20:35:22 +00:00
|
|
|
self.assertEqual(c3.status, 'UNSUCCESSFUL')
|
2011-12-06 15:35:05 +00:00
|
|
|
|
2011-10-08 03:11:57 +00:00
|
|
|
# SUCCESSFUL
|
2012-03-09 23:31:30 +00:00
|
|
|
c4 = Campaign(target=D('1000.00'),deadline=now() - 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'
|
2012-01-11 23:31:26 +00:00
|
|
|
t.approved = True
|
2011-10-08 03:11:57 +00:00
|
|
|
t.campaign = c4
|
2012-04-03 00:57:30 +00:00
|
|
|
t.user = user
|
2011-10-08 03:11:57 +00:00
|
|
|
t.save()
|
2012-04-03 00:57:30 +00:00
|
|
|
self.assertTrue(c4.update_status())
|
2011-10-10 20:35:22 +00:00
|
|
|
self.assertEqual(c4.status, 'SUCCESSFUL')
|
2011-10-08 03:11:57 +00:00
|
|
|
|
|
|
|
# WITHDRAWN
|
2012-04-03 00:57:30 +00:00
|
|
|
c5 = Campaign(target=D('1000.00'),deadline=datetime(2013,1,1),work=w)
|
2011-10-08 03:11:57 +00:00
|
|
|
c5.save()
|
|
|
|
c5.activate().withdraw('testing')
|
2012-05-22 15:07:05 +00:00
|
|
|
self.assertEqual(c5.status, 'WITHDRAWN')
|
2011-10-14 02:16:28 +00:00
|
|
|
|
2012-05-22 15:07:05 +00:00
|
|
|
# testing percent-of-goal
|
|
|
|
w2 = Work()
|
|
|
|
w2.save()
|
|
|
|
c6 = Campaign(target=D('1000.00'),deadline=now() + timedelta(days=1),work=w2)
|
|
|
|
c6.save()
|
|
|
|
cl = Claim(rights_holder = rh, work = w2, user = u, status = 'active')
|
|
|
|
cl.save()
|
|
|
|
c6.activate()
|
|
|
|
t = Transaction()
|
|
|
|
t.amount = D('234.00')
|
|
|
|
t.type = PAYMENT_TYPE_AUTHORIZATION
|
|
|
|
t.status = 'ACTIVE'
|
|
|
|
t.approved = True
|
|
|
|
t.campaign = c6
|
|
|
|
t.user = user
|
|
|
|
t.save()
|
|
|
|
self.assertEqual(w2.percent_of_goal(), 23)
|
2011-10-14 02:16:28 +00:00
|
|
|
|
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')
|
2012-07-20 15:40:11 +00:00
|
|
|
edition = bookloader.add_by_isbn('0441007465')
|
2011-10-14 14:18:38 +00:00
|
|
|
work = edition.work
|
2012-02-11 19:15:06 +00:00
|
|
|
num_wishes=work.num_wishes
|
2011-12-08 23:22:05 +00:00
|
|
|
user.wishlist.add_work(work, 'test')
|
2011-10-14 14:18:38 +00:00
|
|
|
self.assertEqual(user.wishlist.works.count(), 1)
|
2012-02-11 19:15:06 +00:00
|
|
|
self.assertEqual(work.num_wishes, num_wishes+1)
|
2011-12-08 23:22:05 +00:00
|
|
|
user.wishlist.remove_work(work)
|
2011-10-14 14:18:38 +00:00
|
|
|
self.assertEqual(user.wishlist.works.count(), 0)
|
2012-02-11 19:15:06 +00:00
|
|
|
self.assertEqual(work.num_wishes, num_wishes)
|
|
|
|
|
2011-11-10 23:14:33 +00:00
|
|
|
class CeleryTaskTest(TestCase):
|
2011-12-03 03:16:11 +00:00
|
|
|
|
2011-11-10 23:14:33 +00:00
|
|
|
def test_single_fac(self):
|
|
|
|
n = 10
|
|
|
|
task = tasks.fac.delay(n)
|
|
|
|
result = task.get(timeout=10)
|
|
|
|
self.assertEqual(result,factorial(n))
|
2011-12-03 03:16:11 +00:00
|
|
|
|
2011-11-10 23:14:33 +00:00
|
|
|
def test_subtask(self):
|
|
|
|
n = 30
|
|
|
|
subtasks = [tasks.fac.subtask(args=(x,)) for x in range(n)]
|
|
|
|
job = TaskSet(tasks=subtasks)
|
|
|
|
result = job.apply_async()
|
|
|
|
while not result.ready():
|
|
|
|
sleep(0.2)
|
|
|
|
self.assertEqual(result.join(),[factorial(x) for x in range(n)])
|
|
|
|
|
2011-11-18 00:45:26 +00:00
|
|
|
class GoodreadsTest(TestCase):
|
2011-12-03 03:16:11 +00:00
|
|
|
|
2011-11-18 00:45:26 +00:00
|
|
|
def test_goodreads_shelves(self):
|
|
|
|
# test to see whether the core undeletable shelves are on the list
|
|
|
|
gr_uid = "767708" # for Raymond Yee
|
|
|
|
gc = goodreads.GoodreadsClient(key=settings.GOODREADS_API_KEY, secret=settings.GOODREADS_API_SECRET)
|
|
|
|
shelves = gc.shelves_list(gr_uid)
|
|
|
|
shelf_names = [s['name'] for s in shelves['user_shelves']]
|
|
|
|
self.assertTrue('currently-reading' in shelf_names)
|
|
|
|
self.assertTrue('read' in shelf_names)
|
|
|
|
self.assertTrue('to-read' in shelf_names)
|
2011-12-03 03:16:11 +00:00
|
|
|
|
2011-11-18 14:14:33 +00:00
|
|
|
def test_review_list_unauth(self):
|
|
|
|
gr_uid = "767708" # for Raymond Yee
|
|
|
|
gc = goodreads.GoodreadsClient(key=settings.GOODREADS_API_KEY, secret=settings.GOODREADS_API_SECRET)
|
|
|
|
reviews = gc.review_list_unauth(user_id=gr_uid, shelf='read')
|
|
|
|
# test to see whether there is a book field in each of the review
|
2012-04-13 21:53:18 +00:00
|
|
|
# url for test is http://www.goodreads.com/review/list.xml?id=767708&shelf=read&page=1&per_page=20&order=a&v=2&key=[key]
|
2011-11-18 14:14:33 +00:00
|
|
|
self.assertTrue(all([r.has_key("book") for r in reviews]))
|
2011-11-18 00:45:26 +00:00
|
|
|
|
|
|
|
class LibraryThingTest(TestCase):
|
2011-12-03 03:16:11 +00:00
|
|
|
|
2011-11-18 00:45:26 +00:00
|
|
|
def test_scrape_test_lib(self):
|
|
|
|
# account yujx : has one book: 0471925675
|
|
|
|
lt_username = 'yujx'
|
|
|
|
lt = librarything.LibraryThing(username=lt_username)
|
|
|
|
books = list(lt.parse_user_catalog(view_style=5))
|
|
|
|
self.assertEqual(len(books),1)
|
|
|
|
self.assertEqual(books[0]['isbn'], '0471925675')
|
2011-12-03 03:16:11 +00:00
|
|
|
self.assertEqual(books[0]['work_id'], '80826')
|
|
|
|
self.assertEqual(books[0]['book_id'], '79883733')
|
2011-11-18 18:01:37 +00:00
|
|
|
|
|
|
|
class ISBNTest(TestCase):
|
2011-12-03 03:16:11 +00:00
|
|
|
|
2011-11-18 18:01:37 +00:00
|
|
|
def test_ISBN(self):
|
|
|
|
milosz_10 = '006019667X'
|
|
|
|
milosz_13 = '9780060196677'
|
|
|
|
python_10 = '0-672-32978-6'
|
|
|
|
python_10_wrong = '0-672-32978-7'
|
|
|
|
python_13 = '978-0-672-32978-4'
|
|
|
|
|
|
|
|
isbn_python_10 = isbn.ISBN(python_10)
|
|
|
|
isbn_python_13 = isbn.ISBN(python_13)
|
2012-02-08 17:19:17 +00:00
|
|
|
# return None for invalid characters
|
|
|
|
self.assertEqual(None, isbn.ISBN("978-0-M72-32978-X").to_string('13'))
|
|
|
|
self.assertEqual(isbn.ISBN("978-0-M72-32978-X").valid, False)
|
2011-11-18 19:25:13 +00:00
|
|
|
# check that only ISBN 13 starting with 978 or 979 are accepted
|
2012-02-08 17:19:17 +00:00
|
|
|
self.assertEqual(None, isbn.ISBN("111-0-M72-32978-X").to_string())
|
2011-11-18 19:25:13 +00:00
|
|
|
|
2011-11-18 18:01:37 +00:00
|
|
|
# right type?
|
|
|
|
self.assertEqual(isbn_python_10.type, '10')
|
|
|
|
self.assertEqual(isbn_python_13.type, '13')
|
|
|
|
# valid?
|
|
|
|
self.assertEqual(isbn_python_10.valid, True)
|
|
|
|
self.assertEqual(isbn.ISBN(python_10_wrong).valid, False)
|
2011-10-14 02:16:28 +00:00
|
|
|
|
2011-11-18 18:01:37 +00:00
|
|
|
# do conversion -- first the outside methods
|
|
|
|
self.assertEqual(isbn.convert_10_to_13(isbn.strip(python_10)),isbn.strip(python_13))
|
|
|
|
self.assertEqual(isbn.convert_13_to_10(isbn.strip(python_13)),isbn.strip(python_10))
|
|
|
|
|
|
|
|
# check formatting
|
|
|
|
self.assertEqual(isbn.ISBN(python_13).to_string(type='13'), '9780672329784')
|
|
|
|
self.assertEqual(isbn.ISBN(python_13).to_string('13',True), '978-0-672-32978-4')
|
|
|
|
self.assertEqual(isbn.ISBN(python_13).to_string(type='10'), '0672329786')
|
|
|
|
self.assertEqual(isbn.ISBN(python_10).to_string(type='13'), '9780672329784')
|
|
|
|
self.assertEqual(isbn.ISBN(python_10).to_string(10,True), '0-672-32978-6')
|
|
|
|
|
2011-11-18 19:25:13 +00:00
|
|
|
# complain if one tries to get ISBN-10 for a 979 ISBN 13
|
|
|
|
# making up a 979 ISBN
|
|
|
|
isbn_979 = isbn.ISBN("979-1-234-56789-0").validate()
|
2012-02-08 17:19:17 +00:00
|
|
|
self.assertEqual(isbn_979.to_string('10'), None)
|
2011-11-18 19:25:13 +00:00
|
|
|
|
2011-11-18 18:01:37 +00:00
|
|
|
# check casting to string -- ISBN 13
|
2011-11-18 18:21:29 +00:00
|
|
|
self.assertEqual(str(isbn.ISBN(python_10)), '0672329786')
|
2011-11-18 18:01:37 +00:00
|
|
|
|
|
|
|
# test __eq__ and __ne__ and validate
|
|
|
|
self.assertTrue(isbn.ISBN(milosz_10) == isbn.ISBN(milosz_13))
|
|
|
|
self.assertTrue(isbn.ISBN(milosz_10) == milosz_13)
|
|
|
|
self.assertFalse(isbn.ISBN(milosz_10) == 'ddds')
|
|
|
|
self.assertFalse(isbn.ISBN(milosz_10) != milosz_10)
|
|
|
|
self.assertTrue(isbn.ISBN(python_10) != python_10_wrong)
|
|
|
|
self.assertEqual(isbn.ISBN(python_10_wrong).validate(), python_10)
|
|
|
|
self.assertEqual(isbn.ISBN(python_13).validate(), python_10)
|
|
|
|
|
|
|
|
# curious about set membership
|
|
|
|
self.assertEqual(len(set([isbn.ISBN(milosz_10), isbn.ISBN(milosz_13)])),2)
|
2011-11-18 18:21:29 +00:00
|
|
|
self.assertEqual(len(set([str(isbn.ISBN(milosz_10)), str(isbn.ISBN(milosz_13))])),2)
|
|
|
|
self.assertEqual(len(set([isbn.ISBN(milosz_10).to_string(), isbn.ISBN(milosz_13).to_string()])),1)
|
2011-11-18 18:01:37 +00:00
|
|
|
|
2012-05-08 23:08:36 +00:00
|
|
|
class EncryptedKeyTest(TestCase):
|
|
|
|
def test_create_read_key(self):
|
|
|
|
name = "the great answer"
|
|
|
|
value = "42"
|
|
|
|
key = Key.objects.create(name=name, value=value)
|
|
|
|
key.save()
|
|
|
|
# do we get back the value?
|
|
|
|
self.assertEqual(Key.objects.filter(name=name)[0].value, value)
|
|
|
|
# just checking that the encrypted value is not the same as the value
|
|
|
|
self.assertNotEqual(key.encrypted_value, value) # is this always true?
|
|
|
|
|