2017-08-03 21:09:42 +00:00
# encoding: utf-8
2013-06-03 16:31:39 +00:00
"""
external library imports
"""
2011-10-09 18:48:03 +00:00
from datetime import datetime , timedelta
2013-06-03 16:31:39 +00:00
from decimal import Decimal as D
from math import factorial
from time import sleep , mktime
from urlparse import parse_qs , urlparse
2013-08-29 00:13:35 +00:00
from tempfile import NamedTemporaryFile
2013-06-03 16:31:39 +00:00
from celery . task import chord
from celery . task . sets import TaskSet
2013-08-29 00:13:35 +00:00
import requests
2016-12-30 22:24:20 +00:00
import requests_mock
2013-08-29 00:13:35 +00:00
import os
2011-10-09 18:48:03 +00:00
2013-06-03 16:31:39 +00:00
"""
django imports
"""
2011-11-18 00:45:26 +00:00
from django . conf import settings
2013-06-03 16:31:39 +00:00
from django . contrib . auth . models import User
2016-07-21 20:05:57 +00:00
from django_comments . models import Comment
2012-02-10 01:51:10 +00:00
from django . contrib . contenttypes . models import ContentType
from django . contrib . sites . models import Site
2013-08-29 00:13:35 +00:00
from django . core . files import File as DjangoFile
2013-06-03 16:31:39 +00:00
from django . db import IntegrityError
2016-04-09 17:24:44 +00:00
from django . db import transaction
2012-08-29 19:56:14 +00:00
from django . http import Http404
2013-06-03 16:31:39 +00:00
from django . test import TestCase
from django . test . client import Client
from django . test . utils import override_settings
from django . utils import unittest
2011-08-31 03:46:55 +00:00
2013-06-03 16:31:39 +00:00
"""
regluit imports
"""
from regluit . core import (
isbn ,
bookloader ,
models ,
search ,
goodreads ,
librarything ,
2013-09-18 21:36:42 +00:00
tasks ,
parameters ,
2013-06-03 16:31:39 +00:00
)
from regluit . core . models import (
Campaign ,
Work ,
UnglueitError ,
Edition ,
RightsHolder ,
Claim ,
Key ,
Ebook ,
Premium ,
Subject ,
2013-06-17 22:53:21 +00:00
Publisher ,
add test to reproduce duplicate pubname
Unglue.it error on January 11, 2016 at 5:02 AM:
Traceback (most recent call last):
File
"/opt/regluit/ENV/local/lib/python2.7/site-packages/django/core/handlers
/base.py", line 109, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/opt/regluit/frontend/views.py", line 795, in googlebooks
edition = bookloader.add_by_googlebooks_id(googlebooks_id)
File "/opt/regluit/core/bookloader.py", line 343, in
add_by_googlebooks_id
e.set_publisher(d.get('publisher'))
File "/opt/regluit/core/models.py", line 1827, in set_publisher
pub_name = PublisherName.objects.get(name=publisher_name)
File
"/opt/regluit/ENV/local/lib/python2.7/site-packages/django/db/models/man
ager.py", line 131, in get
return self.get_query_set().get(*args, **kwargs)
File
"/opt/regluit/ENV/local/lib/python2.7/site-packages/django/db/models/que
ry.py", line 368, in get
% (self.model._meta.object_name, num, kwargs))
MultipleObjectsReturned: get() returned more than one PublisherName --
it returned 2! Lookup parameters were {'name': u'North-Holland'}
2016-01-18 15:08:58 +00:00
PublisherName ,
2013-08-22 18:23:47 +00:00
Offer ,
2013-08-27 22:03:35 +00:00
EbookFile ,
Acq ,
2013-11-08 17:13:34 +00:00
Hold ,
2013-06-03 16:31:39 +00:00
)
2013-10-17 02:48:29 +00:00
from regluit . libraryauth . models import Library
from regluit . core . parameters import TESTING , LIBRARY , RESERVE
2017-09-15 19:55:37 +00:00
from regluit . core . loaders . utils import ( load_from_books , loaded_book_ok , )
from regluit . core . validation import valid_subject
2013-06-03 16:31:39 +00:00
from regluit . frontend . views import safe_get_work
2011-10-09 18:48:03 +00:00
from regluit . payment . models import Transaction
2011-10-08 03:11:57 +00:00
from regluit . payment . parameters import PAYMENT_TYPE_AUTHORIZATION
2013-06-03 16:31:39 +00:00
from regluit . utils . localdatetime import now , date_today
2013-09-16 01:43:58 +00:00
from regluit . pyepub import EPUB
2014-01-15 13:32:55 +00:00
from . epub import test_epub
2014-08-28 19:29:41 +00:00
from . pdf import ask_pdf , test_pdf
2011-10-14 02:16:28 +00:00
2016-12-30 22:24:20 +00:00
TESTDIR = os . path . join ( os . path . dirname ( __file__ ) , ' ../test/ ' )
YAML_VERSIONFILE = os . path . join ( TESTDIR , ' versiontest.yaml ' )
YAML_HUCKFILE = os . path . join ( TESTDIR , ' raw/master/metadata.yaml ' )
2015-08-18 10:57:14 +00:00
2011-12-19 06:33:13 +00:00
class BookLoaderTests ( TestCase ) :
2016-12-30 22:24:20 +00:00
fixtures = [ ' initial_data.json ' , ' bookloader.json ' ]
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 ' )
2015-07-30 03:01:43 +00:00
2015-08-18 10:57:14 +00:00
def test_add_by_local_yaml ( self ) :
noebook_id = bookloader . load_from_yaml ( YAML_VERSIONFILE )
noebook = models . Work . objects . get ( id = noebook_id )
self . assertEqual ( noebook . first_ebook ( ) , None )
2016-03-07 21:17:33 +00:00
huck_id = bookloader . load_from_yaml ( YAML_HUCKFILE , test_mode = True )
2015-08-18 18:34:52 +00:00
huck = models . Work . objects . get ( id = huck_id )
2015-09-24 21:58:34 +00:00
self . assertTrue ( huck . ebooks ( ) . count ( ) > 1 )
2015-08-18 10:57:14 +00:00
2016-05-09 22:32:38 +00:00
2015-07-30 03:01:43 +00:00
def test_add_by_yaml ( self ) :
2015-08-07 05:21:15 +00:00
space_id = bookloader . load_from_yaml ( ' https://github.com/gitenberg-dev/metadata/raw/master/samples/pandata.yaml ' )
huck_id = bookloader . load_from_yaml ( ' https://github.com/GITenberg/Adventures-of-Huckleberry-Finn_76/raw/master/metadata.yaml ' )
space = models . Work . objects . get ( id = space_id )
huck = models . Work . objects . get ( id = huck_id )
2016-05-09 22:32:38 +00:00
#test ebook archiving
num_ebf = EbookFile . objects . all ( ) . count ( )
for ebook in huck . ebooks ( ) . all ( ) :
f = ebook . get_archive ( )
self . assertTrue ( EbookFile . objects . all ( ) . count ( ) > num_ebf )
2015-01-30 16:33:22 +00:00
2016-12-30 22:24:20 +00:00
def test_add_by_isbn_mock ( self ) :
with requests_mock . Mocker ( real_http = True ) as m :
with open ( os . path . join ( TESTDIR , ' gb_hamilton.json ' ) ) as gb :
m . get ( ' https://www.googleapis.com/books/v1/volumes ' , content = gb . read ( ) )
self . test_add_by_isbn ( mocking = True )
2011-08-31 03:46:55 +00:00
2016-12-30 22:24:20 +00:00
def test_add_by_isbn ( self , mocking = False ) :
if not ( mocking or settings . TEST_INTEGRATION ) :
return
2011-09-10 11:36:38 +00:00
# edition
2017-09-22 22:31:06 +00:00
edition = bookloader . add_by_isbn ( ' 9780143034759 ' )
2016-06-28 15:48:06 +00:00
self . assertEqual ( edition . title , u ' Alexander Hamilton ' )
2017-09-22 22:31:06 +00:00
self . assertEqual ( edition . publication_date , u ' 2005 ' )
self . assertEqual ( edition . publisher , u ' Penguin ' )
self . assertEqual ( edition . isbn_10 , ' 0143034758 ' )
self . assertEqual ( edition . isbn_13 , ' 9780143034759 ' )
self . assertEqual ( edition . googlebooks_id , ' 4iafgTEhU3QC ' )
2011-09-09 05:38:28 +00:00
2011-09-10 11:36:38 +00:00
# authors
2016-06-28 15:48:06 +00:00
self . assertEqual ( edition . authors . all ( ) . count ( ) , 1 )
self . assertEqual ( edition . authors . all ( ) [ 0 ] . name , u ' Ron Chernow ' )
2011-10-10 16:57:10 +00:00
# work
self . assertTrue ( edition . work )
2017-09-22 22:31:06 +00:00
self . assertEqual ( edition . work . googlebooks_id , ' 4iafgTEhU3QC ' )
self . assertEqual ( edition . work . first_isbn_13 ( ) , ' 9780143034759 ' )
2012-01-17 04:28:34 +00:00
2016-01-25 19:08:37 +00:00
# test duplicate pubname
add test to reproduce duplicate pubname
Unglue.it error on January 11, 2016 at 5:02 AM:
Traceback (most recent call last):
File
"/opt/regluit/ENV/local/lib/python2.7/site-packages/django/core/handlers
/base.py", line 109, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/opt/regluit/frontend/views.py", line 795, in googlebooks
edition = bookloader.add_by_googlebooks_id(googlebooks_id)
File "/opt/regluit/core/bookloader.py", line 343, in
add_by_googlebooks_id
e.set_publisher(d.get('publisher'))
File "/opt/regluit/core/models.py", line 1827, in set_publisher
pub_name = PublisherName.objects.get(name=publisher_name)
File
"/opt/regluit/ENV/local/lib/python2.7/site-packages/django/db/models/man
ager.py", line 131, in get
return self.get_query_set().get(*args, **kwargs)
File
"/opt/regluit/ENV/local/lib/python2.7/site-packages/django/db/models/que
ry.py", line 368, in get
% (self.model._meta.object_name, num, kwargs))
MultipleObjectsReturned: get() returned more than one PublisherName --
it returned 2! Lookup parameters were {'name': u'North-Holland'}
2016-01-18 15:08:58 +00:00
ed2 = Edition . objects . create ( work = edition . work )
2017-09-22 22:31:06 +00:00
ed2 . set_publisher ( u ' Penguin ' )
add test to reproduce duplicate pubname
Unglue.it error on January 11, 2016 at 5:02 AM:
Traceback (most recent call last):
File
"/opt/regluit/ENV/local/lib/python2.7/site-packages/django/core/handlers
/base.py", line 109, in get_response
response = callback(request, *callback_args, **callback_kwargs)
File "/opt/regluit/frontend/views.py", line 795, in googlebooks
edition = bookloader.add_by_googlebooks_id(googlebooks_id)
File "/opt/regluit/core/bookloader.py", line 343, in
add_by_googlebooks_id
e.set_publisher(d.get('publisher'))
File "/opt/regluit/core/models.py", line 1827, in set_publisher
pub_name = PublisherName.objects.get(name=publisher_name)
File
"/opt/regluit/ENV/local/lib/python2.7/site-packages/django/db/models/man
ager.py", line 131, in get
return self.get_query_set().get(*args, **kwargs)
File
"/opt/regluit/ENV/local/lib/python2.7/site-packages/django/db/models/que
ry.py", line 368, in get
% (self.model._meta.object_name, num, kwargs))
MultipleObjectsReturned: get() returned more than one PublisherName --
it returned 2! Lookup parameters were {'name': u'North-Holland'}
2016-01-18 15:08:58 +00:00
2013-03-27 16:51:10 +00:00
# publisher names
old_pub_name = edition . publisher_name
edition . set_publisher ( ' test publisher name ' )
self . assertEqual ( edition . publisher , u ' test publisher name ' )
pub = Publisher ( name = edition . publisher_name )
pub . save ( )
self . assertEqual ( edition . work . publishers ( ) . count ( ) , 1 )
old_pub_name . publisher = pub
old_pub_name . save ( )
2017-09-22 22:31:06 +00:00
edition . set_publisher ( u ' Penguin ' )
2016-06-28 15:48:06 +00:00
self . assertEqual ( edition . publisher , u ' test publisher name ' ) # Perseus has been aliased
2016-04-29 15:55:12 +00:00
2016-12-30 22:24:20 +00:00
def test_language_locale_mock ( self ) :
with requests_mock . Mocker ( real_http = True ) as m :
2017-10-26 14:44:26 +00:00
with open ( os . path . join ( TESTDIR , ' zhCN.json ' ) ) as gb :
2016-12-30 22:24:20 +00:00
m . get ( ' https://www.googleapis.com/books/v1/volumes ' , content = gb . read ( ) )
self . test_language_locale ( mocking = True )
def test_language_locale ( self , mocking = False ) :
if not ( mocking or settings . TEST_INTEGRATION ) :
return
2017-10-26 14:44:26 +00:00
edition = bookloader . add_by_isbn ( ' 9787104030126 ' )
self . assertEqual ( edition . work . language , ' zh-CN ' )
2011-09-10 11:36:38 +00:00
2016-12-30 22:24:20 +00:00
def test_update_edition_mock ( self ) :
with requests_mock . Mocker ( real_http = True ) as m :
with open ( os . path . join ( TESTDIR , ' python4da.json ' ) ) as gb :
m . get ( ' https://www.googleapis.com/books/v1/volumes ' , content = gb . read ( ) )
self . test_update_edition ( mocking = True )
def test_update_edition ( self , mocking = False ) :
if not ( mocking or settings . TEST_INTEGRATION ) :
return
2012-02-02 14:05:08 +00:00
w = models . Work ( title = ' silly title ' , language = ' xx ' )
w . save ( )
e = models . Edition ( title = w . title , work = w )
e . save ( )
2015-05-27 19:37:47 +00:00
models . Identifier ( type = ' isbn ' , value = ' 9781449319793 ' , work = w , edition = e ) . save ( )
2013-11-15 23:47:02 +00:00
bookloader . update_edition ( e )
2012-02-02 14:05:08 +00:00
self . assertEqual ( e . work . language , ' en ' )
2015-05-27 19:37:47 +00:00
self . assertEqual ( e . title , ' Python for Data Analysis ' )
2012-02-02 14:05:08 +00:00
2011-09-10 11:36:38 +00:00
def test_double_add ( self ) :
2016-12-30 22:24:20 +00:00
edbefore = models . Edition . objects . all ( ) . count ( )
autbefore = models . Author . objects . all ( ) . count ( )
before = models . Work . objects . all ( ) . count ( )
bookloader . add_by_isbn ( ' 0441007465 ' ) # in fixture
self . assertEqual ( models . Edition . objects . all ( ) . count ( ) , edbefore )
self . assertEqual ( models . Author . objects . all ( ) . count ( ) , autbefore )
self . assertEqual ( models . Work . objects . all ( ) . count ( ) , before )
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
2016-12-30 22:24:20 +00:00
def test_thingisbn_mock ( self ) :
with requests_mock . Mocker ( real_http = True ) as m :
with open ( os . path . join ( TESTDIR , ' 9780441569595.xml ' ) ) as lt :
2017-07-27 14:33:13 +00:00
m . get ( ' https://www.librarything.com/api/thingISBN/0441007465 ' , content = lt . read ( ) )
2016-12-30 22:24:20 +00:00
self . test_thingisbn ( mocking = True )
def test_thingisbn ( self , mocking = False ) :
if not ( mocking or settings . TEST_INTEGRATION ) :
return
isbns = bookloader . thingisbn ( ' 0441007465 ' ) #Neuromancer
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
2016-12-30 22:24:20 +00:00
edition = bookloader . add_by_isbn ( ' 0441007465 ' ) #Neuromancer; editions in fixture but not joined
edbefore = models . Edition . objects . count ( )
before = models . Work . objects . count ( )
2012-01-10 20:20:02 +00:00
lang = edition . work . language
2016-12-30 22:24:20 +00:00
langbefore = models . Work . objects . filter ( language = lang ) . count ( )
2011-10-14 04:02:19 +00:00
# ask for related editions to be added using the work we just created
2016-12-30 22:24:20 +00:00
with requests_mock . Mocker ( real_http = True ) as m :
with open ( os . path . join ( TESTDIR , ' 9780441569595.xml ' ) ) as lt :
2017-07-27 14:33:13 +00:00
m . get ( ' https://www.librarything.com/api/thingISBN/0441007465 ' , content = lt . read ( ) )
2016-12-30 22:24:20 +00:00
bookloader . add_related ( ' 0441007465 ' ) # should join the editions
self . assertTrue ( models . Edition . objects . count ( ) > = edbefore )
self . assertTrue ( models . Work . objects . filter ( language = lang ) . count ( ) < langbefore )
2012-02-04 20:40:10 +00:00
self . assertTrue ( edition . work . editions . count ( ) > 9 )
2016-08-16 15:42:58 +00:00
self . assertTrue ( edition . work . reverse_related . count ( ) > 0 )
2016-09-29 22:02:27 +00:00
2016-09-29 22:15:46 +00:00
# is edition.work found among the from_work of all the to_work of edition.work?
back_point = True
to_works = [ wr . to_work for wr in edition . work . works_related_from . all ( ) ]
2016-09-29 22:02:27 +00:00
for to_work in to_works :
2017-10-28 22:33:58 +00:00
if edition . work_id not in [ wr1 . from_work . id for wr1 in to_work . works_related_to . all ( ) ] :
2016-09-29 22:15:46 +00:00
back_point = False
break
self . assertTrue ( back_point )
2011-12-13 14:55:26 +00:00
2011-12-19 06:33:13 +00:00
def test_populate_edition ( self ) :
2016-12-30 22:24:20 +00:00
edition = bookloader . add_by_isbn ( ' 9780606301121 ' ) # A People's History Of The United States
with requests_mock . Mocker ( real_http = True ) as m :
with open ( os . path . join ( TESTDIR , ' 9780061989834.xml ' ) ) as lt :
2017-07-27 14:33:13 +00:00
m . get ( ' https://www.librarything.com/api/thingISBN/9780606301121 ' , content = lt . read ( ) )
2016-12-30 22:24:20 +00:00
edition = tasks . populate_edition . run ( edition . isbn_13 )
2012-07-30 15:23:38 +00:00
self . assertTrue ( edition . work . editions . all ( ) . count ( ) > 10 )
self . assertTrue ( edition . work . subjects . all ( ) . count ( ) > 8 )
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 """
2016-12-30 22:24:20 +00:00
before = models . Work . objects . count ( )
wasbefore = models . WasWork . objects . count ( )
2013-03-01 18:13:14 +00:00
sub1 = Subject ( name = ' test1 ' )
sub1 . save ( )
sub2 = Subject ( name = ' test2 ' )
sub2 . save ( )
2012-02-13 22:35:08 +00:00
w1 = Work ( title = " Work 1 " )
w1 . save ( )
2013-03-01 18:13:14 +00:00
w1 . subjects . add ( sub1 )
2012-02-13 22:35:08 +00:00
w2 = Work ( title = " Work 2 " )
w2 . save ( )
2013-03-01 18:13:14 +00:00
w2 . subjects . add ( sub1 , sub2 )
2012-02-13 22:35:08 +00:00
e1 = Edition ( work = w1 )
e1 . save ( )
e2 = Edition ( work = w2 )
e2 . save ( )
2014-12-13 17:37:35 +00:00
eb1 = Ebook ( edition = e2 )
eb1 . save ( )
2012-02-13 22:35:08 +00:00
e2a = Edition ( work = w2 )
e2a . save ( )
self . assertTrue ( e1 )
self . assertTrue ( e2 )
self . assertTrue ( e2a )
self . assertTrue ( e1 . work )
self . assertTrue ( e2 . work )
2016-12-30 22:24:20 +00:00
self . assertEqual ( models . Work . objects . count ( ) , before + 2 )
2014-12-13 17:37:35 +00:00
self . assertTrue ( w2 . is_free )
self . assertFalse ( w1 . is_free )
2012-02-13 22:35:08 +00:00
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 )
2016-12-30 22:24:20 +00:00
self . assertEqual ( models . Work . objects . count ( ) , before + 2 )
2012-02-13 22:35:08 +00:00
# merge the second work into the first
bookloader . merge_works ( e1 . work , e2 . work )
2016-12-30 22:24:20 +00:00
self . assertEqual ( models . Work . objects . count ( ) , before + 1 )
self . assertEqual ( models . WasWork . objects . count ( ) , wasbefore + 1 )
self . assertEqual ( w1 . subjects . count ( ) , 2 )
2013-03-01 18:13:14 +00:00
2014-12-13 17:37:35 +00:00
self . assertTrue ( w1 . is_free )
2012-02-13 22:35:08 +00:00
# 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 )
2014-04-14 21:43:06 +00:00
# if the work has a selected edition, then don't touch the work.
w3 = Work ( title = ' work 3 ' )
e_pref = Edition ( work = w1 )
w1 . selected_edition = e_pref
bookloader . merge_works ( w3 , w1 )
self . assertTrue ( w1 . title == ' Work 1 ' )
2012-02-13 22:35:08 +00:00
2011-10-19 03:00:07 +00:00
def test_merge_works ( self ) :
2016-12-30 22:24:20 +00:00
before = models . Work . objects . count ( )
2011-10-19 03:00:07 +00:00
# add two editions and see that there are two stub works
2013-07-16 23:08:20 +00:00
2016-12-30 22:24:20 +00:00
# for this test, use two unjoined Neuromancer works in fixture
isbn1 = ' 9780441569595 '
isbn2 = ' 9780441012039 '
2013-07-16 23:08:20 +00:00
e1 = bookloader . add_by_isbn ( isbn1 )
e2 = bookloader . add_by_isbn ( isbn2 )
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
# add the stub works to a wishlist
2013-03-06 14:39:06 +00:00
user = User . objects . create_user ( ' test ' , ' test@example.org ' , ' testpass ' )
2011-12-08 23:22:05 +00:00
user . wishlist . add_work ( e1 . work , ' test ' )
user . wishlist . add_work ( e2 . work , ' test ' )
2013-03-06 14:39:06 +00:00
manager = User . objects . create_user ( ' manager ' , ' manager@example.org ' , ' 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
2013-07-16 23:08:20 +00:00
bookloader . add_related ( isbn1 )
# non-zero
self . assertGreater ( models . Work . objects . count ( ) , 0 )
w3 = models . Edition . get_by_isbn ( isbn1 ) . 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 ) :
2016-12-30 22:24:20 +00:00
with requests_mock . Mocker ( real_http = True ) as m :
with open ( os . path . join ( TESTDIR , ' gb_latinlanguage.json ' ) ) as gb :
m . get ( ' https://www.googleapis.com/books/v1/volumes ' , content = gb . read ( ) )
edition = bookloader . add_by_oclc ( ' 1246014 ' )
# we've seen the public domain status of this book fluctuate -- and the OCLC number can disappear. So if the ebook count is 2 then test
#if edition is not None and edition.ebooks.count() == 2:
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 ' )
#self.assertEqual(ebook_epub.url, 'http://books.google.com/books/download/The_Latin_language.epub?id=N1RfAAAAMAAJ&ie=ISO-8859-1&output=epub&source=gbs_api')
self . assertEqual ( parse_qs ( urlparse ( ebook_epub . url ) . query ) . get ( " id " ) , [ ' N1RfAAAAMAAJ ' ] )
self . assertEqual ( parse_qs ( urlparse ( ebook_epub . url ) . query ) . get ( " output " ) , [ ' epub ' ] )
self . assertEqual ( ebook_epub . provider , ' Google Books ' )
self . assertEqual ( ebook_epub . set_provider ( ) , ' Google Books ' )
ebook_pdf = edition . ebooks . filter ( format = ' pdf ' ) [ 0 ]
self . assertEqual ( ebook_pdf . format , ' pdf ' )
#self.assertEqual(ebook_pdf.url, 'http://books.google.com/books/download/The_Latin_language.pdf?id=N1RfAAAAMAAJ&ie=ISO-8859-1&output=pdf&sig=ACfU3U2yLt3nmTncB8ozxOWUc4iHKUznCA&source=gbs_api')
self . assertEqual ( parse_qs ( urlparse ( ebook_pdf . url ) . query ) . get ( " id " ) , [ ' N1RfAAAAMAAJ ' ] )
self . assertEqual ( parse_qs ( urlparse ( ebook_pdf . url ) . query ) . get ( " output " ) , [ ' pdf ' ] )
self . assertEqual ( ebook_pdf . provider , ' Google Books ' )
2013-12-11 00:17:12 +00:00
2016-12-30 22:24:20 +00:00
w = edition . work
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 )
2013-12-11 00:17:12 +00:00
2017-07-27 14:33:13 +00:00
ebook_pdf . url = ' https://en.wikisource.org/wiki/Frankenstein '
2016-12-30 22:24:20 +00:00
self . assertEqual ( ebook_pdf . set_provider ( ) , ' Wikisource ' )
2013-12-11 00:17:12 +00:00
2016-12-30 22:24:20 +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 )
2013-12-11 00:17:12 +00:00
2016-12-30 22:24:20 +00:00
ebook_pdf . increment ( )
updated_ebook = Ebook . objects . get ( pk = ebook_pdf . pk )
self . assertEqual ( int ( updated_ebook . download_count ) , 1 )
self . assertEqual ( int ( edition . work . download_count ) , 1 )
2012-04-27 21:29:57 +00:00
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
2013-02-18 17:17:15 +00:00
# http://books.google.com/books?id=D-WjL_HRbNQC&printsec=frontcover#v=onepage&q&f=false
# Social Life of Information
2016-12-30 22:24:20 +00:00
with requests_mock . Mocker ( real_http = True ) as m :
with open ( os . path . join ( TESTDIR , ' gb_sociallife.json ' ) ) as gb :
m . get ( ' https://www.googleapis.com/books/v1/volumes ' , content = gb . read ( ) )
e = bookloader . add_by_isbn ( ' 1578517087 ' )
self . assertTrue ( e )
2011-12-13 14:55:26 +00:00
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-07-26 16:06:39 +00:00
def test_unicode_openlibrary ( self ) :
2016-12-30 22:24:20 +00:00
with requests_mock . Mocker ( real_http = True ) as m :
with open ( os . path . join ( TESTDIR , ' gb_fightclub.json ' ) ) as gb :
m . get ( ' https://www.googleapis.com/books/v1/volumes ' , content = gb . read ( ) )
work = bookloader . add_by_isbn ( ' 9783894808358 ' ) . work #fight club
bookloader . add_openlibrary ( work )
2012-07-26 16:06:39 +00:00
self . assertTrue ( work . description . startswith ( ' Sie sind jung, ' ) )
2015-08-21 22:31:52 +00:00
def notest_load_gutenberg_edition ( self ) :
2012-02-15 02:01:13 +00:00
""" Let ' s try this out for Moby Dick """
title = " Moby Dick "
ol_work_id = " /works/OL102749W "
gutenberg_etext_id = 2701
2017-07-27 14:33:13 +00:00
epub_url = " https://www.gutenberg.org/cache/epub/2701/pg2701.epub "
license = ' https://www.gutenberg.org/license '
2012-02-15 02:01:13 +00:00
lang = ' en '
format = ' epub '
publication_date = datetime ( 2001 , 7 , 1 )
2017-07-27 14:33:13 +00:00
seed_isbn = ' 9780142000083 ' # https://www.amazon.com/Moby-Dick-Whale-Penguin-Classics-Deluxe/dp/0142000086
2012-02-15 02:01:13 +00:00
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 )
2016-05-09 22:32:38 +00:00
def tearDown ( self ) :
for ebf in EbookFile . objects . all ( ) :
ebf . file . delete ( )
2011-09-29 06:23:50 +00:00
class SearchTests ( TestCase ) :
2016-12-31 03:26:16 +00:00
def test_search_mock ( self ) :
with requests_mock . Mocker ( real_http = True ) as m :
with open ( os . path . join ( TESTDIR , ' gb_melville.json ' ) ) as gb , open ( os . path . join ( TESTDIR , ' gb_melville2.json ' ) ) as gb2 :
m . get ( ' https://www.googleapis.com/books/v1/volumes ' , [ { ' content ' : gb2 . read ( ) } , { ' content ' : gb . read ( ) } ] )
self . test_pagination ( mocking = True )
self . test_basic_search ( mocking = True )
self . test_googlebooks_search ( mocking = True )
2011-09-29 06:23:50 +00:00
2016-12-31 03:26:16 +00:00
def test_basic_search ( self , mocking = False ) :
if not ( mocking or settings . TEST_INTEGRATION ) :
return
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 ' ) )
2014-12-08 18:09:05 +00:00
self . assertTrue ( r [ ' cover_image_thumbnail ' ] . startswith ( ' https ' ) or r [ ' cover_image_thumbnail ' ] . startswith ( ' http ' ) )
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
2016-12-31 03:26:16 +00:00
def test_pagination ( self , mocking = False ) :
if not ( mocking or settings . TEST_INTEGRATION ) :
return
2012-02-05 00:22:04 +00:00
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
2016-12-31 03:26:16 +00:00
def test_googlebooks_search ( self , mocking = False ) :
if not ( mocking or settings . TEST_INTEGRATION ) :
return
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 ) :
2016-04-09 17:21:12 +00:00
fixtures = [ ' initial_data.json ' ]
2013-08-08 23:56:26 +00:00
def test_b2u ( self ) :
w = Work ( )
w . save ( )
2016-05-16 15:18:42 +00:00
this_year = datetime . now ( ) . year
2013-08-08 23:56:26 +00:00
c = Campaign (
target = D ( ' 12000.00 ' ) ,
2016-05-16 15:18:42 +00:00
deadline = datetime ( this_year , 1 , 1 ) ,
2013-08-08 23:56:26 +00:00
work = w , type = 2 ,
2016-05-16 15:18:42 +00:00
cc_date_initial = datetime ( this_year + 100 , 1 , 1 ) ,
2013-08-08 23:56:26 +00:00
)
self . assertTrue ( c . set_dollar_per_day ( ) < 0.34 )
self . assertTrue ( c . dollar_per_day > 0.31 )
2014-03-02 03:42:26 +00:00
t = Transaction ( type = 1 , campaign = c , approved = True , amount = D ( 6000.1 ) , status = " Complete " )
t . save ( )
2013-08-09 02:32:58 +00:00
c . status = ' ACTIVE '
c . save ( )
c . update_left ( )
2013-08-10 20:29:58 +00:00
#print(w.percent_of_goal())
2013-08-09 02:32:58 +00:00
self . assertEqual ( w . percent_unglued ( ) , 3 )
self . assertTrue ( w . percent_of_goal ( ) > 49 )
2013-08-22 18:23:47 +00:00
ofr = Offer . objects . create ( work = w , price = D ( 10 ) , active = True )
self . assertTrue ( c . days_per_copy < D ( 32.26 ) )
self . assertTrue ( c . days_per_copy > D ( 29.41 ) )
2013-08-08 23:56:26 +00:00
2011-10-09 18:48:03 +00:00
def test_required_fields ( self ) :
# a campaign must have a target, deadline and a work
2017-07-27 14:33:13 +00:00
# see https://stackoverflow.com/questions/21458387/transactionmanagementerror-you-cant-execute-queries-until-the-end-of-the-atom
2016-04-09 17:24:44 +00:00
with transaction . atomic ( ) :
c = Campaign ( )
self . assertRaises ( IntegrityError , c . save )
with transaction . atomic ( ) :
c = Campaign ( target = D ( ' 1000.00 ' ) )
self . assertRaises ( IntegrityError , c . save )
with transaction . atomic ( ) :
c = Campaign ( target = D ( ' 1000.00 ' ) , deadline = datetime ( 2013 , 1 , 1 ) )
self . assertRaises ( IntegrityError , c . save )
2011-10-09 18:48:03 +00:00
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 ( )
2017-07-27 14:33:13 +00:00
self . assertEqual ( c . license_url , ' https://creativecommons.org/licenses/by-nc/3.0/ ' )
2014-11-10 18:14:59 +00:00
self . assertEqual ( c . license_badge , ' /static/images/ccbync.png ' )
2012-05-20 04:12:16 +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
2013-03-06 14:39:06 +00:00
user = User . objects . create_user ( ' test ' , ' test@example.org ' , ' testpass ' )
2012-04-03 00:57:30 +00:00
2011-10-08 03:11:57 +00:00
w = Work ( )
w . save ( )
2012-09-25 20:43:25 +00:00
w2 = Work ( )
w2 . save ( )
2011-10-08 03:11:57 +00:00
# INITIALIZED
2013-06-17 21:12:58 +00:00
c1 = Campaign ( target = D ( ' 1000.00 ' ) , deadline = Campaign . latest_ending ( ) , 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
2014-02-21 18:16:55 +00:00
c2 = Campaign ( target = D ( ' 1000.00 ' ) , deadline = datetime ( 2013 , 1 , 1 ) , work = w , description = ' dummy description ' )
2011-10-08 03:11:57 +00:00
c2 . save ( )
2011-10-10 20:35:22 +00:00
self . assertEqual ( c2 . status , ' INITIALIZED ' )
2013-03-06 14:39:06 +00:00
u = User . objects . create_user ( ' claimer ' , ' claimer@example.org ' , ' claimer ' )
2012-04-03 14:45:48 +00:00
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 ( )
2012-09-25 20:43:25 +00:00
cl2 = Claim ( rights_holder = rh , work = w2 , user = u , status = ' active ' )
cl2 . 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
2014-02-21 18:16:55 +00:00
c3 = Campaign ( target = D ( ' 1000.00 ' ) , deadline = now ( ) - timedelta ( days = 1 ) , work = w2 , description = ' dummy description ' )
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 ' )
2012-09-25 20:43:25 +00:00
# premiums
pr1 = Premium ( type = ' CU ' , campaign = c3 , amount = 10 , description = ' botsnack ' , limit = 1 )
pr1 . save ( )
self . assertEqual ( pr1 . premium_remaining , 1 )
#cloning (note we changed c3 to w2 to make it clonable)
c7 = c3 . clone ( )
self . assertEqual ( c7 . status , ' INITIALIZED ' )
self . assertEqual ( c7 . premiums . all ( ) [ 0 ] . description , ' botsnack ' )
2011-10-08 03:11:57 +00:00
# SUCCESSFUL
2014-02-21 18:16:55 +00:00
c4 = Campaign ( target = D ( ' 1000.00 ' ) , deadline = now ( ) - timedelta ( days = 1 ) , work = w , description = ' dummy description ' )
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
2014-02-21 18:16:55 +00:00
c5 = Campaign ( target = D ( ' 1000.00 ' ) , deadline = datetime ( 2013 , 1 , 1 ) , work = w , description = ' dummy description ' )
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 ( )
2014-02-21 18:16:55 +00:00
c6 = Campaign ( target = D ( ' 1000.00 ' ) , deadline = now ( ) + timedelta ( days = 1 ) , work = w2 , description = ' dummy description ' )
2012-05-22 15:07:05 +00:00
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 )
2013-06-17 22:53:21 +00:00
2014-01-15 15:19:57 +00:00
self . assertEqual ( c1 . launchable , False )
c1 . description = " description "
2013-06-17 22:53:21 +00:00
self . assertEqual ( c1 . launchable , True )
c1 . work . create_offers ( )
self . assertEqual ( c1 . work . offers . count ( ) , 2 )
self . assertEqual ( c1 . work . offers . filter ( license = 2 ) . count ( ) , 1 )
c1 . type = 2
c1 . save ( )
self . assertEqual ( c1 . launchable , False )
2013-08-18 19:18:01 +00:00
of1 = c1 . work . offers . get ( license = 2 )
2013-08-22 18:23:47 +00:00
of1 . price = D ( 2 )
2013-08-18 19:18:01 +00:00
of1 . active = True
2013-06-17 22:53:21 +00:00
of1 . save ( )
self . assertEqual ( c1 . launchable , False )
e1 = models . Edition ( title = " title " , work = c1 . work )
e1 . save ( )
ebf1 = models . EbookFile ( edition = e1 , format = 1 )
ebf1 . save ( )
2013-08-19 20:01:32 +00:00
c1 . set_cc_date_initial ( )
self . assertEqual ( c1 . cc_date , settings . MAX_CC_DATE )
2013-08-18 19:18:01 +00:00
c1 . target = D ( settings . UNGLUEIT_MAXIMUM_TARGET )
c1 . save ( )
2013-06-17 22:53:21 +00:00
self . assertEqual ( c1 . launchable , True )
2011-10-14 02:16:28 +00:00
2011-10-14 14:18:38 +00:00
class WishlistTest ( TestCase ) :
2016-12-31 03:26:16 +00:00
fixtures = [ ' initial_data.json ' , ' neuromancer.json ' ]
2011-10-14 14:18:38 +00:00
def test_add_remove ( self ) :
# add a work to a user's wishlist
2013-03-06 14:39:06 +00:00
user = User . objects . create_user ( ' test ' , ' test@example.org ' , ' 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 )
2016-01-20 01:53:55 +00:00
self . assertEqual ( work . priority ( ) , 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
2014-05-02 21:20:37 +00:00
@unittest.skip ( " Goodreads down at the moment " )
2011-11-18 00:45:26 +00:00
def test_goodreads_shelves ( self ) :
2016-12-31 03:26:16 +00:00
if not settings . GOODREADS_API_SECRET :
return
2011-11-18 00:45:26 +00:00
# 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
2014-05-02 21:20:37 +00:00
@unittest.skip ( " Goodreads down at the moment " )
2011-11-18 14:14:33 +00:00
def test_review_list_unauth ( self ) :
2016-12-31 03:26:16 +00:00
if not settings . GOODREADS_API_SECRET :
return
2011-11-18 14:14:33 +00:00
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
2017-07-27 14:33:13 +00:00
# url for test is https://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 '
2017-08-03 21:09:42 +00:00
funky = ' 0– 672—329 78-6 ' # endash, mdash, space
2011-11-18 18:01:37 +00:00
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 )
2017-08-03 21:09:42 +00:00
self . assertEqual ( None , bookloader . valid_isbn ( " 978-0-M72-32978-X " ) )
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 ( ) )
2017-08-03 21:09:42 +00:00
self . assertEqual ( None , bookloader . valid_isbn ( " 111-0-M72-32978-X " ) )
2017-08-03 21:31:12 +00:00
self . assertEqual ( isbn_python_13 . to_string ( ) , bookloader . valid_isbn ( funky ) )
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 )
2017-08-03 21:31:12 +00:00
self . assertEqual ( 13 , len ( bookloader . valid_isbn ( python_10 ) ) )
self . assertEqual ( isbn_python_13 . to_string ( ) , bookloader . valid_isbn ( python_10_wrong ) )
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 ) )
2017-08-03 21:09:42 +00:00
self . assertEqual ( isbn . convert_10_to_13 ( isbn . strip ( python_10 ) ) , isbn . strip ( python_13 ) )
2014-11-05 23:33:30 +00:00
self . assertEqual ( isbn . convert_13_to_10 ( ' xxxxxxxxxxxxx ' ) , None )
self . assertEqual ( isbn . convert_10_to_13 ( ' xxxxxxxxxx ' ) , None )
2017-08-03 21:09:42 +00:00
self . assertEqual ( None , bookloader . valid_isbn ( ' xxxxxxxxxxxxx ' ) )
self . assertEqual ( None , bookloader . valid_isbn ( ' xxxxxxxxxx ' ) )
2011-11-18 18:01:37 +00:00
# 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?
2012-08-29 19:56:14 +00:00
class SafeGetWorkTest ( TestCase ) :
def test_good_work ( self ) :
w1 = models . Work ( )
w1 . save ( )
w2 = models . Work ( )
w2 . save ( )
w2_id = w2 . id
bookloader . merge_works ( w1 , w2 )
work = safe_get_work ( w1 . id )
self . assertEqual ( work , w1 )
work = safe_get_work ( w2_id )
self . assertEqual ( work , w1 )
self . assertRaises ( Http404 , safe_get_work , 3 )
2015-08-22 18:29:35 +00:00
class WorkTests ( TestCase ) :
2017-09-15 19:55:37 +00:00
def setUp ( self ) :
self . w1 = models . Work . objects . create ( )
self . w2 = models . Work . objects . create ( )
2015-08-22 18:29:35 +00:00
def test_preferred_edition ( self ) :
2017-09-15 19:55:37 +00:00
ww = models . WasWork . objects . create ( work = self . w1 , was = self . w2 . id )
e1 = models . Edition . objects . create ( work = self . w1 )
self . assertEqual ( e1 , self . w1 . preferred_edition )
e2 = models . Edition . objects . create ( work = self . w1 )
self . w1 . selected_edition = e2
self . w1 . save ( )
self . assertEqual ( e2 , self . w1 . preferred_edition )
self . assertEqual ( e2 , self . w2 . preferred_edition )
def test_valid_subject ( self ) :
self . assertTrue ( valid_subject ( ' A, valid, suj \xc3 t ' ) )
self . assertFalse ( valid_subject ( ' A, valid, suj \xc3 t, ' ) )
self . assertFalse ( valid_subject ( ' A valid suj \xc3 t \x01 ' ) )
Subject . set_by_name ( ' A, valid, suj \xc3 t; A, valid, suj \xc3 t, ' , work = self . w1 )
self . assertEqual ( 1 , self . w1 . subjects . count ( ) )
sub = Subject . set_by_name ( ' nyt:hardcover_advice=2011-06-18 ' , work = self . w1 )
self . assertEqual ( sub . name , ' NYT Bestseller - Hardcover advice ' )
self . assertEqual ( 2 , self . w1 . subjects . count ( ) )
sub2 = Subject . set_by_name ( ' !lcsh: Something ' , work = self . w1 )
self . assertEqual ( sub2 . name , ' Something ' )
self . assertEqual ( 3 , self . w1 . subjects . count ( ) )
sub3 = Subject . set_by_name ( ' Something ' , work = self . w1 )
self . assertEqual ( sub3 . name , ' Something ' )
self . assertEqual ( 3 , self . w1 . subjects . count ( ) )
self . assertEqual ( sub3 . authority , ' lcsh ' )
2012-08-29 19:56:14 +00:00
class DownloadPageTest ( TestCase ) :
2016-04-09 17:21:12 +00:00
fixtures = [ ' initial_data.json ' ]
2012-08-29 19:56:14 +00:00
def test_download_page ( self ) :
w = models . Work ( )
w . save ( )
e1 = models . Edition ( )
e1 . work = w
2012-09-19 16:37:37 +00:00
e1 . unglued = True
2012-08-29 19:56:14 +00:00
e2 = models . Edition ( )
e2 . work = w
e1 . save ( )
e2 . save ( )
eb1 = models . Ebook ( )
2017-07-27 14:33:13 +00:00
eb1 . url = " https://example.org "
2012-08-29 19:56:14 +00:00
eb1 . edition = e1
2012-09-19 16:37:37 +00:00
eb1 . format = ' epub '
2012-08-29 19:56:14 +00:00
eb2 = models . Ebook ( )
2017-07-27 14:33:13 +00:00
eb2 . url = " https://example2.com "
2012-08-29 19:56:14 +00:00
eb2 . edition = e2
2012-09-25 16:27:39 +00:00
eb2 . format = ' mobi '
2012-08-29 19:56:14 +00:00
eb1 . save ( )
eb2 . save ( )
anon_client = Client ( )
2013-04-24 19:41:35 +00:00
response = anon_client . get ( " /work/ %s /download/ " % w . id , follow = True )
2016-05-12 16:56:25 +00:00
self . assertContains ( response , " /download_ebook/ %s / " % eb1 . id , count = 11 )
2014-05-22 17:19:05 +00:00
self . assertContains ( response , " /download_ebook/ %s / " % eb2 . id , count = 5 )
2014-12-13 17:37:35 +00:00
self . assertTrue ( eb1 . edition . work . is_free )
eb1 . delete ( )
self . assertTrue ( eb2 . edition . work . is_free )
eb2 . delete ( )
self . assertFalse ( eb2 . edition . work . is_free )
2012-11-15 22:37:04 +00:00
class LocaldatetimeTest ( TestCase ) :
@override_settings ( LOCALDATETIME_NOW = None )
def test_LOCALDATETIME_NOW_none ( self ) :
try :
localdatetime . now
except NameError :
from regluit . utils import localdatetime
else :
reload ( localdatetime )
self . assertAlmostEqual ( mktime ( datetime . now ( ) . timetuple ( ) ) , mktime ( localdatetime . now ( ) . timetuple ( ) ) , 1.0 )
@override_settings ( LOCALDATETIME_NOW = lambda : datetime . now ( ) + timedelta ( 365 ) )
def test_LOCALDATETIME_NOW_year_ahead ( self ) :
try :
localdatetime . now
except NameError :
from regluit . utils import localdatetime
else :
reload ( localdatetime )
self . assertAlmostEqual ( mktime ( ( datetime . now ( ) + timedelta ( 365 ) ) . timetuple ( ) ) , mktime ( localdatetime . now ( ) . timetuple ( ) ) , 1.0 )
def test_no_time_override ( self ) :
from regluit . utils import localdatetime
self . assertAlmostEqual ( mktime ( datetime . now ( ) . timetuple ( ) ) , mktime ( localdatetime . now ( ) . timetuple ( ) ) , 1.0 )
def tearDown ( self ) :
# restore localdatetime.now() to what's in the settings file
try :
localdatetime . now
except NameError :
from regluit . utils import localdatetime
else :
reload ( localdatetime )
2013-03-04 22:19:39 +00:00
class MailingListTests ( TestCase ) :
#mostly to check that MailChimp account is setp correctly
2012-11-15 22:37:04 +00:00
2013-03-04 22:19:39 +00:00
def test_mailchimp ( self ) :
from postmonkey import PostMonkey
pm = PostMonkey ( settings . MAILCHIMP_API_KEY )
2016-12-31 03:26:16 +00:00
if settings . TEST_INTEGRATION :
self . assertEqual ( pm . ping ( ) , " Everything ' s Chimpy! " )
self . user = User . objects . create_user ( ' chimp_test ' , ' eric@gluejar.com ' , ' chimp_test ' )
self . assertTrue ( self . user . profile . on_ml )
2013-03-04 22:19:39 +00:00
2013-11-15 16:25:41 +00:00
@override_settings ( LOCAL_TEST = True )
2013-08-27 22:03:35 +00:00
class EbookFileTests ( TestCase ) :
2016-04-09 17:21:12 +00:00
fixtures = [ ' initial_data.json ' ]
2014-01-15 13:32:55 +00:00
def test_badepub_errors ( self ) :
textfile = NamedTemporaryFile ( delete = False )
textfile . write ( " bad text file " )
textfile . seek ( 0 )
self . assertTrue ( test_epub ( textfile ) )
2013-08-27 22:03:35 +00:00
def test_ebookfile ( self ) :
2013-08-29 00:13:35 +00:00
"""
Read the test epub file
"""
2013-08-27 22:03:35 +00:00
w = Work . objects . create ( title = " Work 1 " )
e = Edition . objects . create ( title = w . title , work = w )
u = User . objects . create_user ( ' test ' , ' test@example.org ' , ' testpass ' )
2013-11-15 16:25:41 +00:00
rh = RightsHolder . objects . create ( owner = u , rights_holder_name = ' rights holder name ' )
cl = Claim . objects . create ( rights_holder = rh , work = w , user = u , status = ' active ' )
c = Campaign . objects . create ( work = w ,
type = parameters . BUY2UNGLUE ,
cc_date_initial = datetime ( 2020 , 1 , 1 ) ,
target = 1000 ,
deadline = datetime ( 2020 , 1 , 1 ) ,
license = ' CC BY ' ,
2014-02-21 18:16:55 +00:00
description = " dummy description " ,
2013-11-15 16:25:41 +00:00
)
2013-08-29 00:13:35 +00:00
# download the test epub into a temp file
temp = NamedTemporaryFile ( delete = False )
test_file_content = requests . get ( settings . BOOXTREAM_TEST_EPUB_URL ) . content
temp . write ( test_file_content )
temp . close ( )
try :
# now we can try putting the test epub file into Django storage
temp_file = open ( temp . name )
dj_file = DjangoFile ( temp_file )
ebf = EbookFile ( format = ' epub ' , edition = e , file = dj_file )
ebf . save ( )
temp_file . close ( )
finally :
# make sure we get rid of temp file
os . remove ( temp . name )
2013-09-16 01:43:58 +00:00
test_epub = EPUB ( ebf . file , mode = ' a ' )
self . assertEqual ( len ( test_epub . opf ) , 4 )
self . assertTrue ( len ( test_epub . opf [ 2 ] ) < 30 )
2013-08-27 22:03:35 +00:00
acq = Acq . objects . create ( user = u , work = w , license = TESTING )
2013-09-06 02:54:11 +00:00
self . assertIsNot ( acq . nonce , None )
url = acq . get_watermarked ( ) . download_link_epub
2013-11-15 16:25:41 +00:00
self . assertRegexpMatches ( url , ' github.com/eshellman/42_ebook/blob/master/download/42 ' )
2013-11-18 04:24:03 +00:00
#self.assertRegexpMatches(url,'booxtream.com/')
2014-02-21 18:16:55 +00:00
with self . assertRaises ( UnglueitError ) as cm :
c . activate ( )
off = Offer ( price = 10.00 , work = w , active = True )
off . save ( )
2013-11-15 16:25:41 +00:00
c . activate ( )
#flip the campaign to success
c . cc_date_initial = datetime ( 2012 , 1 , 1 )
c . update_status ( )
self . assertEqual ( c . work . ebooks ( ) . count ( ) , 2 )
2013-12-18 17:34:53 +00:00
c . do_watermark = False
c . save ( )
url = acq . get_watermarked ( ) . download_link_epub
2013-11-15 16:25:41 +00:00
2016-03-28 18:30:05 +00:00
def test_ebookfile_thanks ( self ) :
2014-08-28 19:29:41 +00:00
w = Work . objects . create ( title = " Work 2 " )
e = Edition . objects . create ( title = w . title , work = w )
u = User . objects . create_user ( ' test2 ' , ' test@example.org ' , ' testpass ' )
rh = RightsHolder . objects . create ( owner = u , rights_holder_name = ' rights holder name 2 ' )
cl = Claim . objects . create ( rights_holder = rh , work = w , user = u , status = ' active ' )
c = Campaign . objects . create ( work = w ,
type = parameters . THANKS ,
license = ' CC BY-NC ' ,
description = " Please send me money " ,
)
# download the test epub into a temp file
temp = NamedTemporaryFile ( delete = False )
test_file_content = requests . get ( settings . TEST_PDF_URL ) . content
temp . write ( test_file_content )
temp . close ( )
try :
# now we can try putting the test pdf file into Django storage
temp_file = open ( temp . name )
dj_file = DjangoFile ( temp_file )
ebf = EbookFile ( format = ' pdf ' , edition = e , file = dj_file )
ebf . save ( )
2016-08-24 19:41:29 +00:00
eb = Ebook ( format = ' pdf ' , edition = e , url = ebf . file . url , provider = ' Unglue.it ' )
eb . save ( )
ebf . ebook = eb
ebf . save ( )
2014-08-28 19:29:41 +00:00
temp_file . close ( )
finally :
# make sure we get rid of temp file
os . remove ( temp . name )
#test the ask-appender
c . add_ask_to_ebfs ( )
2016-12-31 03:26:16 +00:00
if settings . AWS_SECRET_ACCESS_KEY :
assert test_pdf ( c . work . ebookfiles ( ) . filter ( asking = True ) [ 0 ] . file . url )
else :
assert test_pdf ( c . work . ebookfiles ( ) . filter ( asking = True ) [ 0 ] . file )
2014-08-28 19:29:41 +00:00
2016-03-28 18:30:05 +00:00
#Now do the same with epub
temp = NamedTemporaryFile ( delete = False )
test_file_content = requests . get ( settings . BOOXTREAM_TEST_EPUB_URL ) . content
temp . write ( test_file_content )
temp . close ( )
try :
# now we can try putting the test pdf file into Django storage
temp_file = open ( temp . name )
dj_file = DjangoFile ( temp_file )
ebf = EbookFile ( format = ' epub ' , edition = e , file = dj_file )
ebf . save ( )
2016-08-24 19:41:29 +00:00
eb = Ebook ( format = ' epub ' , edition = e , url = ebf . file . url , provider = ' Unglue.it ' )
eb . save ( )
ebf . ebook = eb
ebf . save ( )
2016-03-28 18:30:05 +00:00
temp_file . close ( )
2016-08-24 19:41:29 +00:00
ebf . make_mobi ( )
2016-03-28 18:30:05 +00:00
finally :
# make sure we get rid of temp file
os . remove ( temp . name )
#test the ask-appender
c . add_ask_to_ebfs ( )
2016-08-24 19:41:29 +00:00
self . assertTrue ( c . work . ebookfiles ( ) . filter ( asking = True , format = ' epub ' ) . count ( ) > 0 )
2016-12-31 03:26:16 +00:00
if settings . MOBIGEN_URL :
self . assertTrue ( c . work . ebookfiles ( ) . filter ( asking = True , format = ' mobi ' ) . count ( ) > 0 )
2016-08-24 19:41:29 +00:00
self . assertTrue ( c . work . ebookfiles ( ) . filter ( asking = True , ebook__active = True ) . count ( ) > 0 )
self . assertTrue ( c . work . ebookfiles ( ) . filter ( asking = False , ebook__active = True ) . count ( ) == 0 )
#test the unasker
c . revert_asks ( )
self . assertTrue ( c . work . ebookfiles ( ) . filter ( asking = True , ebook__active = True ) . count ( ) == 0 )
self . assertTrue ( c . work . ebookfiles ( ) . filter ( asking = False , ebook__active = True ) . count ( ) > 0 )
2014-08-28 19:29:41 +00:00
2015-01-26 17:48:16 +00:00
class MobigenTests ( TestCase ) :
def test_convert_to_mobi ( self ) :
"""
check the size of the mobi output of a Moby Dick epub
"""
from regluit . core . mobigen import convert_to_mobi
2016-12-30 22:24:20 +00:00
if settings . TEST_INTEGRATION :
output = convert_to_mobi ( " https://github.com/GITenberg/Moby-Dick--Or-The-Whale_2701/releases/download/0.2.0/Moby-Dick-Or-The-Whale.epub " )
self . assertTrue ( len ( output ) > 2207877 )
2015-01-26 17:48:16 +00:00
2013-10-17 02:48:29 +00:00
from . signals import handle_transaction_charged
2013-11-15 16:25:41 +00:00
@override_settings ( LOCAL_TEST = True )
2013-10-17 02:48:29 +00:00
class LibTests ( TestCase ) :
2016-04-09 17:21:12 +00:00
fixtures = [ ' initial_data.json ' ]
2013-10-17 02:48:29 +00:00
class transaction :
pass
def test_purchase ( self ) :
w = Work . objects . create ( title = " Work 1 " )
e = Edition . objects . create ( title = w . title , work = w )
u = User . objects . create_user ( ' test ' , ' test@example.org ' , ' testpass ' )
lu = User . objects . create_user ( ' library ' , ' testu@example.org ' , ' testpass ' )
2013-11-29 21:04:22 +00:00
lib = Library . objects . create ( user = lu , owner = u )
2013-10-17 02:48:29 +00:00
c = Campaign . objects . create ( work = w , type = parameters . BUY2UNGLUE , cc_date_initial = datetime ( 2020 , 1 , 1 ) , target = 1000 , deadline = datetime ( 2020 , 1 , 1 ) )
2013-08-27 22:03:35 +00:00
2013-10-17 02:48:29 +00:00
new_acq = Acq . objects . create ( user = lib . user , work = c . work , license = LIBRARY )
self . assertTrue ( new_acq . borrowable )
reserve_acq = Acq . objects . create ( user = u , work = c . work , license = RESERVE , lib_acq = new_acq )
self . assertTrue ( reserve_acq . borrowable )
self . assertFalse ( new_acq . borrowable )
2013-10-18 16:36:55 +00:00
2013-11-08 17:13:34 +00:00
self . assertTrue ( reserve_acq . expires < now ( ) + timedelta ( hours = 25 ) )
2013-10-18 16:36:55 +00:00
reserve_acq . borrow ( )
2013-11-08 17:13:34 +00:00
self . assertTrue ( reserve_acq . expires > now ( ) + timedelta ( hours = 25 ) )
u2 = User . objects . create_user ( ' user2 ' , ' test2@example.org ' , ' testpass ' )
Hold . objects . get_or_create ( library = lib , work = w , user = u2 )
reserve_acq . expire_in ( timedelta ( seconds = 0 ) )
tasks . refresh_acqs ( )
self . assertEqual ( reserve_acq . holds . count ( ) , 0 )
2016-03-04 20:09:30 +00:00
class GitHubTests ( TestCase ) :
def test_ebooks_in_github_release ( self ) :
( repo_owner , repo_name , repo_tag ) = ( ' GITenberg ' , ' Adventures-of-Huckleberry-Finn_76 ' , ' 0.0.50 ' )
ebooks = bookloader . ebooks_in_github_release ( repo_owner , repo_name ,
tag = repo_tag , token = settings . GITHUB_PUBLIC_TOKEN )
expected_set = set ( [
( ' epub ' , u ' Adventures-of-Huckleberry-Finn.epub ' ) ,
( ' mobi ' , u ' Adventures-of-Huckleberry-Finn.mobi ' ) ,
( ' pdf ' , u ' Adventures-of-Huckleberry-Finn.pdf ' )
] )
self . assertEqual ( set ( ebooks ) , expected_set )
2016-05-24 00:03:55 +00:00
class OnixLoaderTests ( TestCase ) :
2016-07-23 16:08:28 +00:00
fixtures = [ ' initial_data.json ' ]
2016-05-24 00:03:55 +00:00
def test_load ( self ) :
TEST_BOOKS = [ { ' ' : u ' ' ,
' Author1First ' : u ' Joseph ' ,
' Author1Last ' : u ' Necvatal ' ,
' Author1Role ' : u ' ' ,
' Author2First ' : u ' ' ,
' Author2Last ' : u ' ' ,
' Author2Role ' : u ' ' ,
' Author3First ' : u ' ' ,
' Author3Last ' : u ' ' ,
' Author3Role ' : u ' ' ,
' AuthorBio ' : u ' ' ,
' AuthorsList ' : u ' Joseph Nechvatal ' ,
' BISACCode1 ' : u ' ' ,
' BISACCode2 ' : u ' ' ,
' BISACCode3 ' : u ' ' ,
' Book-level DOI ' : u ' 10.3998/ohp.9618970.0001.001 ' ,
' ClothISBN ' : u ' N/A ' ,
' CopyrightYear ' : u ' 2011 ' ,
' DescriptionBrief ' : u ' ' ,
' DescriptionLong ' : u ' ' ,
' Excerpt ' : u ' ' ,
' FullTitle ' : u ' Immersion into Noise ' ,
' License ' : u ' CC BY-SA ' ,
' List Price in USD (paper ISBN) ' : u ' 23.99 ' ,
' ListPriceCurrencyType ' : u ' ' ,
' PaperISBN ' : u ' 9781607852414 ' ,
' Publisher ' : u ' Open Humanities Press ' ,
' SubjectListMARC ' : u ' ' ,
' Subtitle ' : u ' ' ,
' TableOfContents ' : u ' ' ,
' Title ' : u ' Immersion into Noise ' ,
2017-07-27 14:33:13 +00:00
' URL ' : u ' https://doi.org/10.3998/ohp.9618970.0001.001 ' ,
2016-05-24 00:03:55 +00:00
' eISBN ' : u ' N/A ' ,
' eListPrice ' : u ' N/A ' ,
' ePublicationDate ' : u ' ' ,
2016-06-10 22:15:53 +00:00
' eTerritoryRights ' : u ' ' } ,
{ ' ' : u ' ' ,
' CAD price eub ' : u ' 9.95 ' ,
' Title ' : u ' That Greece Might Still Be Free ' ,
' USD price epub ' : u ' 9.95 ' ,
' ISBN 2 with dashes ' : u ' 978-1-906924-01-0 ' ,
' Plain Text Blurb ' : u ' When in 1821, the Greeks rose in violent revolution against the rule of the Ottoman Turks, waves of sympathy spread across Western Europe and the United States. More than a thousand volunteers set out to fight for the cause. The Philhellenes, whether they set out to recreate the Athens of Pericles, start a new crusade, or make money out of a war, all felt that Greece had unique claim on the sympathy of the world. As Lord Byron wrote, " I dreamed that Greece might still be Free " ; and he died at Missolonghi trying to translate that dream into reality. William St Clair \' s meticulously researched and highly readable account of their aspirations and experiences was hailed as definitive when it was first published. Long out of print, it remains the standard account of the Philhellenic movement and essential reading for any students of the Greek War of Independence, Byron, and European Romanticism. Its relevance to more modern ethnic and religious conflicts is becoming increasingly appreciated by scholars worldwide. This revised edition includes a new introduction by Roderick Beaton, an updated bibliography and many new illustrations. ' ,
' Cover URL ' : u ' http://www.openbookpublishers.com/shopimages/products/cover/3 ' ,
' keywords ' : u ' Greece; Greek History; Lord Byron; War of Independence; Philhellenes; war; history; Romanticism ' ,
' Publication type ' : u ' Monograph ' , ' GBP price epub ' : u ' 5.95 ' , ' publication month ' : u ' 11 ' , ' no of tables ' : u ' ' ,
' GBP price paperback ' : u ' 15.95 ' , ' AUD price epub ' : u ' 9.95 ' , ' ISBN 4 with dashes ' : u ' 978-1-906924-02-7-epub ' ,
2017-07-27 14:33:13 +00:00
' DOI prefix ' : u ' 10.11647 ' , ' License URL (human-readable summary) ' : u ' https://creativecommons.org/licenses/by-nc-nd/2.0/ ' ,
2016-06-10 22:15:53 +00:00
' Contributor 5 surname ' : u ' ' , ' Contributor 1 first name ' : u ' William ' , ' Contributor 6 first name ' : u ' ' ,
' ONIX Role Code (List 17)6 ' : u ' ' , ' ONIX Role Code (List 17)5 ' : u ' ' , ' ONIX Role Code (List 17)4 ' : u ' ' ,
' ONIX Role Code (List 17)3 ' : u ' ' , ' ONIX Role Code (List 17)2 ' : u ' A24 ' , ' ONIX Role Code (List 17)1 ' : u ' A01 ' ,
' GBP price hardback ' : u ' 29.95 ' , ' Subtitle ' : u ' The Philhellenes in the War of Independence ' , ' ONIX tag6 ' : u ' ' ,
' ISBN 3 with dashes ' : u ' 978-1-906924-02-7 ' , ' Countries excluded ' : u ' None ' , ' first edition publication date ' : u ' 39753 ' ,
' Original Language ' : u ' English ' , ' ISBN 1 with dashes ' : u ' 978-1-906924-00-3 ' , ' Contributor 4 first name ' : u ' ' ,
' ISBN 5 with dashes ' : u ' 978-1-906924-02-7-mobi ' , ' Contributor 2 surname ' : u ' Beaton ' ,
2017-07-27 14:33:13 +00:00
' License URL (our copyright tab) ' : u ' https://www.openbookpublishers.com/isbn/9781906924003#copyright ' ,
2016-06-10 22:15:53 +00:00
' BISAC subject code 1 ' : u ' HIS042000 ' , ' BISAC subject code 3 ' : u ' HIS037060 ' ,
' BISAC subject code 2 ' : u ' HIS054000 ' , ' BISAC subject code 5 ' : u ' ' ,
' BISAC subject code 4 ' : u ' ' , ' Status ' : u ' Active ' , ' Geographic rights ' : u ' Worldwide ' ,
' Series Name ' : u ' ' , ' Contributor 5 first name ' : u ' ' , ' ISSN Print with dashes ' : u ' ' ,
' ISBN 5 ' : u ' 9781906924027mobi ' , ' Contributor 1 surname ' : u ' St Clair ' ,
' Contributor 2 first name ' : u ' Roderick ' ,
' Book-page permanent URL ' : u ' http://www.openbookpublishers.com/isbn/9781906924003 ' ,
' EUR price hardback ' : u ' 36.95 ' , ' EUR price epub ' : u ' 7.95 ' , ' Contributor 6 surname ' : u ' ' ,
' current edition number (integers only) ' : u ' 1 ' ,
' Table of Content ' : u " Introduction by Roderick Beaton \n 1. The Outbreak \n 2. The Return of the Ancient Hellenes \n 3. The Regiment \n 4. Two Kinds of War \n 5. The Cause of Greece, the Cause of Europe \n 6. The Road to Marseilles \n 7. Chios \n 8. The Battalion of Philhellenes \n 9. The Battle of Peta \n 10. The Triumph of the Captains \n 11. The Return Home \n 12. The German Legion \n 13. Knights and Crusaders \n 14. Secrets of State \n 15. Enter the British \n 16. Lord Byron joins the Cause \n 17. ' To bring Freedom and Knowledge to Greece ' \n 18. Arrivals at Missolonghi \n 19. The Byron Brigade \n 20. Essays in Regeneration \n 21. The New Apostles \n 22. The English Gold \n 23. The Coming of the Arabs \n 24. The Shade of Napoleon \n 25. ' No freedom to fight for at home ' \n 26. French Idealism and French Cynicism \n 27. Regulars Again \n 28. A New Fleet \n 29. Athens and Navarino \n 30. America to the Rescue \n 31. Later \n Appendix I: Remarks on Numbers \n Appendix II: The Principal Philhellenic Expeditions \n Notes on the Select Bibliography \n Select Bibliography \n Bibliography of Primary and Secondary Material Since 1972 \n Notes \n Index " ,
' no of illustrations ' : u ' 41 ' , ' OBP Role Name6 ' : u ' ' , ' GBP price PDF ' : u ' 5.95 ' ,
' OBP Role Name4 ' : u ' ' , ' OBP Role Name5 ' : u ' ' , ' OBP Role Name2 ' : u ' Introduction ' ,
' OBP Role Name3 ' : u ' ' , ' OBP Role Name1 ' : u ' Author ' , ' ONIX Status code ' : u ' 04 ' ,
' LLC (Library of Congress Codes) ' : u ' ' , ' publication day ' : u ' 1 ' ,
' Copyright holder 2 ' : u ' ' , ' Language ' : u ' English ' , ' Contributor 3 first name ' : u ' ' ,
' CAD price hardback ' : u ' 54.95 ' , ' USD price paperback ' : u ' 26.95 ' , ' ONIX tag1 ' : u ' By (author) ' ,
' ONIX tag3 ' : u ' ' , ' ONIX tag2 ' : u ' Introduction by ' , ' ONIX tag5 ' : u ' ' ,
' ONIX tag4 ' : u ' ' , ' no of audio/video ' : u ' 0 ' , ' EUR price mobi ' : u ' 7.95 ' ,
' Version of the license ' : u ' 2.0 ' , ' publication year ' : u ' 2008 ' ,
' CAD price paperback ' : u ' 29.95 ' , ' Full-text URL - PDF ' : u ' http://www.openbookpublishers.com/reader/3 ' ,
' Copyright holder 1 ' : u ' William St Clair ' , ' Copyright holder 3 ' : u ' ' ,
' Short Blurb (less than 100 words) ' : u ' When in 1821, the Greeks rose in violent revolution against Ottoman rule, waves of sympathy spread across western Europe and the USA. Inspired by a belief that Greece had a unique claim on the sympathy of the world, more than a thousand Philhellenes set out to fight for the cause. This meticulously researched and highly readable account of their aspirations and experiences has long been the standard account of the Philhellenic movement and essential reading for students of the Greek War of Independence, Byron and European Romanticism. Its relevance to more modern conflicts is also becoming increasingly appreciated. ' ,
' BIC subject code 3 ' : u ' 3JH ' , ' BIC subject code 2 ' : u ' 1DVG ' , ' BIC subject code 1 ' : u ' HBJD ' ,
' ISSN Digital with dashes ' : u ' ' , ' USD price mobi ' : u ' 9.95 ' , ' BIC subject code 5 ' : u ' ' ,
' BIC subject code 4 ' : u ' ' , ' ONIX Language Code ' : u ' eng ' , ' AUD price paperback ' : u ' 29.95 ' ,
' AUD price mobi ' : u ' 9.95 ' , ' No. in the Series ' : u ' ' , ' CAD price PDF ' : u ' 9.95 ' ,
' CAD price mobi ' : u ' 9.95 ' , ' DOI suffix ' : u ' OBP.0001 ' , ' USD price PDF ' : u ' 9.95 ' ,
' Book-page URL ' : u ' http://www.openbookpublishers.com/product/3 ' ,
' Academic discipline (OBP) ' : u ' History and Biography ' , ' EUR price paperback ' : u ' 19.95 ' ,
' License ' : u ' CC BY-NC-ND ' , ' AUD price PDF ' : u ' 9.95 ' , ' Contributor 3 surname ' : u ' ' ,
' AUD price hardback ' : u ' 54.95 ' , ' ISBN 4 ' : u ' 9781906924027epub ' , ' no of pages ' : u ' 440 ' ,
' ISBN 2 ' : u ' 9781906924010 ' , ' ISBN 3 ' : u ' 9781906924027 ' , ' ISBN 1 ' : u ' 9781906924003 ' ,
' pages ' : u ' xxi + 419 ' , ' Contributor 4 surname ' : u ' ' , ' USD price hardback ' : u ' 48.95 ' ,
' Full-text URL - HTML ' : u ' http://www.openbookpublishers.com/htmlreader/978-1-906924-00-3/main.html ' ,
' GBP price mobi ' : u ' 5.95 ' , ' Format 1 ' : u ' Paperback ' , ' EUR price PDF ' : u ' 7.95 ' , ' Format 3 ' : u ' pdf ' ,
' Format 2 ' : u ' Hardback ' , ' Format 5 ' : u ' mobi ' , ' Format 4 ' : u ' epub ' , ' MARC Code1 ' : u ' aut ' ,
' MARC Code2 ' : u ' aui ' , ' MARC Code3 ' : u ' ' , ' MARC Code4 ' : u ' ' , ' MARC Code5 ' : u ' ' ,
' MARC Code6 ' : u ' ' , ' ISO Language Code ' : u ' en ' }
2016-05-24 00:03:55 +00:00
]
results = load_from_books ( TEST_BOOKS )
for ( book , work , edition ) in results :
assert ( loaded_book_ok ( book , work , edition ) )