Beispiel #1
0
    def setUp(self):
        self.user = User(username='******',
                         password='******',
                         email='*****@*****.**')
        self.user.save()

        self.author = Author(first_name='Ernest', last_name='Hemingway')
        self.author.save()

        self.book = Book(title='A Farewell to Arms',
                         author=self.author,
                         publication_year=1929)
        self.book.save()

        self.book2 = Book(title='The Sun Also Rises',
                          author=self.author,
                          publication_year=1926)
        self.book2.save()

        self.book3 = Book(title='For Whom The Bell Tolls',
                          author=self.author,
                          publication_year=1940)
        self.book3.save()

        self.review = Review(user=self.user,
                             book=self.book,
                             timestamp=datetime.datetime.now(),
                             review_message='Good Book',
                             rating=5)
Beispiel #2
0
    def testAuthorNullFields(self):
        """Test whether Author with Null fields is invalidated"""
        #last name is Null
        a1 = Author(first_name='Ernest')
        self.assertRaises(ValidationError, a1.full_clean)

        #first name is Null
        a2 = Author(last_name='Hemingway')
        self.assertRaises(ValidationError, a2.full_clean)
Beispiel #3
0
def add_author(request):
    if request.method == 'POST':
        form = AuthorForm(request.POST)
        if form.is_valid():
            a = Author(first_name=form.cleaned_data["first_name"],
                       last_name=form.cleaned_data["last_name"],
                       email=form.cleaned_data["email"])
            a.save()
            return HttpResponseRedirect('/authors/')
    else:
        form = AuthorForm()
    return render(request, 'add_author.html', {'form': form})
Beispiel #4
0
    def setUp(self):
        self.user = User(username='******', password='******', email='*****@*****.**')
        self.user.save()

        self.author = Author(first_name='Ernest', last_name='Hemingway')
        self.author.save()

        self.book = Book(title='A Farewell to Arms', author=self.author,
            publication_year=1929)
        self.book.save()

        self.book2 = Book(title='The Sun Also Rises', author=self.author,
            publication_year=1926)
        self.book2.save()

        self.book3 = Book(title='For Whom The Bell Tolls', author=self.author,
            publication_year=1940)
        self.book3.save()

        self.review = Review(
            user=self.user,
            book=self.book,
            timestamp=datetime.datetime.now(),
            review_message='Good Book',
            rating=5
        )
Beispiel #5
0
    def testBookNegativePublicationYear(self):
        """Test whether a Book with a negative pub year is invalidated"""

        b = Book(title='A Farewell to Arms',
                 author=Author(first_name='Ernest', last_name='Hemingway'),
                 publication_year=-10)

        self.assertRaises(ValidationError, b.full_clean)
Beispiel #6
0
    def testBookString(self):
        """Test whether string representation of Book is correct"""

        b = Book(title='A Farewell to Arms',
                 author=Author(first_name='Ernest', last_name='Hemingway'),
                 publication_year=1929)

        self.assertEqual(str(b), 'A Farewell to Arms')
Beispiel #7
0
def database_update():
    url = f'http://fantasy-worlds.org/lib/1/'
    page = requests.get(url).content
    soup = BeautifulSoup(page, 'lxml')
    find_id = soup.find_all('a', target="_blank")
    id = re.search('id\d+', str(find_id))[0][2:]
    if not Books.objects.exists():
        start_id = 1
        id = 2
    else:
        start_id = Books.objects.latest('id').id_book
        if start_id == 1:
            start_id = 2
    try:
        Books.objects.get(id_book=id)
    except Exception:
        for id_book in range(start_id, int(id)):
            url = f'http://fantasy-worlds.org/lib/id{id_book}/'
            page = requests.get(url).content
            soup = BeautifulSoup(page, 'lxml')
            find_id = soup.find_all('a', target="_blank")
            id_book = re.search('id\d+', str(find_id))[0][2:]
            if id_book != id:
                page = requests.get(url).content
                soup = BeautifulSoup(page, 'lxml')
                title = soup.find('span', itemprop="name").get_text(strip=True)
                author = soup.find('a', itemprop="author").get_text(strip=True)
                author = author.split(' ')
                description = soup.find(
                    'span', itemprop='description').get_text(strip=True)
                isbn = soup.find('span', itemprop='isbn').get_text(strip=True)
                id_book = id_book
                image_number = re.search('\d+/\d+\.jpg', str(find_id))
                image_link = (
                    f'http://fantasy-worlds.org/img/full/{image_number[0]}')
                book_link = url
                a = Author(first_name=author[0], last_name=author[1])
                a.save()
                b = Books(author=a,
                          title=title,
                          book_link=book_link,
                          description=description,
                          id_book=id_book,
                          image_link=image_link,
                          isbn=isbn)
                b.save()
Beispiel #8
0
    def testBookNullFields(self):
        """Test whether Book with Null fields is invalidated"""
        #title is Null
        b1 = Book(author=Author(first_name='Ernest', last_name='Hemingway'))
        self.assertRaises(ValidationError, b1.full_clean)

        #author is Null
        b2 = Book(title='A Farewell to Arms')
        self.assertRaises(ValidationError, b2.full_clean)
from bookstore.models import Author, Book

some_author = Author(name='Chris Conlan')
some_author.save()

some_book = Book(author=some_author,
                 title='Fast Python',
                 subtitle='Re-learn the basics')
some_book.save()
Beispiel #10
0
 def test_add_few_books_authors(self):
     uncle_bob = Author(name='Robert', surname='Marting')
     uncle_bob.save()
     self.book.authors.add(uncle_bob)
     levi_metthew = self.book.authors.create(name='Levi Matthew')
     self.assertEqual(list(levi_metthew.books.all()), [self.book])
Beispiel #11
0
 def test_string_representation(self):
     author = Author(name='Robert', surname='Martin')
     self.assertEqual(str(author), 'Robert Martin')
Beispiel #12
0
    def testAuthorString(self):
        """Test whether string representation of Author is correct"""

        a = Author(first_name='Ernest', last_name='Hemingway')
        self.assertEqual(str(a), 'Ernest Hemingway')
Beispiel #13
0
class BookstoreViewsTest(TestCase):
    """Test suite for Bookstore views"""
    def setUp(self):
        self.user = User(username='******',
                         password='******',
                         email='*****@*****.**')
        self.user.save()

        self.author = Author(first_name='Ernest', last_name='Hemingway')
        self.author.save()

        self.book = Book(title='A Farewell to Arms',
                         author=self.author,
                         publication_year=1929)
        self.book.save()

        self.book2 = Book(title='The Sun Also Rises',
                          author=self.author,
                          publication_year=1926)
        self.book2.save()

        self.book3 = Book(title='For Whom The Bell Tolls',
                          author=self.author,
                          publication_year=1940)
        self.book3.save()

        self.review = Review(user=self.user,
                             book=self.book,
                             timestamp=datetime.datetime.now(),
                             review_message='Good Book',
                             rating=5)

    def testIndex(self):
        """Test bookstore index view"""
        response = self.client.get(reverse('bookstore:index'))
        #Just make sure the page loads, that's all
        self.assertEqual(response.status_code, 200)

    def testBookReviewList(self):
        """Test books review list view"""
        #if no book id was specified, it should return an error
        self.assertRaises(
            NoReverseMatch,
            lambda: self.client.get(reverse('bookstore:book_review_list')))

        #if the book id is invalid, it should return an error
        response = self.client.get(
            reverse('bookstore:book_review_list', kwargs={'book_id': 0}))
        self.assertEqual(response.status_code, 404)

        #if the book id is valid, it should give the right book
        response = self.client.get(
            reverse('bookstore:book_review_list', kwargs={'book_id': 1}))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context['book'].title, 'A Farewell to Arms')

    def testUserReviewList(self):
        """Test user review list view"""
        #if no user id was specified, it should return an error
        self.assertRaises(
            NoReverseMatch,
            lambda: self.client.get(reverse('bookstore:user_review_list')))

        #if the user id is invalid, it should return an error
        response = self.client.get(
            reverse('bookstore:user_review_list', kwargs={'user_id': 0}))
        self.assertEqual(response.status_code, 404)

        #if the username is invalid, it should return an error
        response = self.client.get(
            reverse('bookstore:user_review_list',
                    kwargs={'username': '******'}))
        self.assertEqual(response.status_code, 404)

        #if the user id is valid, it should give the right book
        response = self.client.get(
            reverse('bookstore:user_review_list', kwargs={'user_id': 1}))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context['queried_user'].username, 'user')
        self.assertEqual(response.context['queried_user'].pk, 1)

        #if the username is valid, it should give the right book
        response = self.client.get(
            reverse('bookstore:user_review_list', kwargs={'username': '******'}))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context['queried_user'].username, 'user')
        self.assertEqual(response.context['queried_user'].pk, 1)

    def testBookList(self):
        """Test book list view"""
        #make sure the context has a list of books, in alphabetical order
        response = self.client.get((reverse('bookstore:book_list')))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context['book_list'][0].title,
                         'A Farewell to Arms')
        self.assertEqual(response.context['book_list'][1].title,
                         'For Whom The Bell Tolls')
        self.assertEqual(response.context['book_list'][2].title,
                         'The Sun Also Rises')
Beispiel #14
0
class BookstoreViewsTest(TestCase):
    """Test suite for Bookstore views"""

    def setUp(self):
        self.user = User(username='******', password='******', email='*****@*****.**')
        self.user.save()

        self.author = Author(first_name='Ernest', last_name='Hemingway')
        self.author.save()

        self.book = Book(title='A Farewell to Arms', author=self.author,
            publication_year=1929)
        self.book.save()

        self.book2 = Book(title='The Sun Also Rises', author=self.author,
            publication_year=1926)
        self.book2.save()

        self.book3 = Book(title='For Whom The Bell Tolls', author=self.author,
            publication_year=1940)
        self.book3.save()

        self.review = Review(
            user=self.user,
            book=self.book,
            timestamp=datetime.datetime.now(),
            review_message='Good Book',
            rating=5
        )

    def testIndex(self):
        """Test bookstore index view"""
        response = self.client.get(reverse('bookstore:index'))
        #Just make sure the page loads, that's all
        self.assertEqual(response.status_code, 200)

    def testBookReviewList(self):
        """Test books review list view"""
        #if no book id was specified, it should return an error
        self.assertRaises(
            NoReverseMatch,
            lambda: self.client.get(reverse('bookstore:book_review_list'))
        )

        #if the book id is invalid, it should return an error
        response = self.client.get(reverse(
            'bookstore:book_review_list',
            kwargs = {'book_id':0}
        ))
        self.assertEqual(response.status_code, 404)

        #if the book id is valid, it should give the right book
        response = self.client.get(reverse(
            'bookstore:book_review_list',
            kwargs = {'book_id':1}
        ))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context['book'].title, 'A Farewell to Arms')

    def testUserReviewList(self):
        """Test user review list view"""
        #if no user id was specified, it should return an error
        self.assertRaises(
            NoReverseMatch,
            lambda: self.client.get(reverse('bookstore:user_review_list'))
        )

        #if the user id is invalid, it should return an error
        response = self.client.get(reverse(
            'bookstore:user_review_list',
            kwargs = {'user_id':0}
        ))
        self.assertEqual(response.status_code, 404)

        #if the username is invalid, it should return an error
        response = self.client.get(reverse(
            'bookstore:user_review_list',
            kwargs = {'username':'******'}
        ))
        self.assertEqual(response.status_code, 404)

        #if the user id is valid, it should give the right book
        response = self.client.get(reverse(
            'bookstore:user_review_list',
            kwargs = {'user_id':1}
        ))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context['queried_user'].username, 'user')
        self.assertEqual(response.context['queried_user'].pk, 1)

        #if the username is valid, it should give the right book
        response = self.client.get(reverse(
            'bookstore:user_review_list',
            kwargs = {'username':'******'}
        ))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context['queried_user'].username, 'user')
        self.assertEqual(response.context['queried_user'].pk, 1)

    def testBookList(self):
        """Test book list view"""
        #make sure the context has a list of books, in alphabetical order
        response = self.client.get((reverse('bookstore:book_list')))
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context['book_list'][0].title,
            'A Farewell to Arms')
        self.assertEqual(response.context['book_list'][1].title,
            'For Whom The Bell Tolls')
        self.assertEqual(response.context['book_list'][2].title,
            'The Sun Also Rises')
    if isClean:

        print('Data is clean.')

        myprofile = "{}\nName: {} {} {} \n".format(cnt, title, first_name,
                                                   last_name)
        myprofile += "Gender: {}\n".format(gender)
        myprofile += "Birth Date: {}\n".format(birth_date)
        myprofile += "Phone: {}\n".format(phone_number)
        myprofile += "Email: {}\n".format(email)
        myprofile += "SSN: {}\n".format(ssn)
        myprofile += "Address: {}, {}, {}, {} {}\n".format(
            address, city, state, postal_code, country)

        author = Author()
        author.title = title
        author.first_name = first_name
        author.last_name = last_name
        author.gender = gender
        author.birth_date = birth_date
        author.phone_number = phone_number
        author.email = email
        author.ssn = ssn
        author.address = address
        author.city = city
        author.state = state
        author.postal_code = postal_code
        author.country = country
        author.save()
    def setUp(self):
        uncle_bob = Author(name='Robert', surname='Martin')
        uncle_bob.save()
        mcconnell = Author(name='Steve', surname='McConnell')
        mcconnell.save()
        myers = Author(name='Glenford', surname='Myers')
        myers.save()
        levi_metthew = Author(name='Levi Metthew')
        levi_metthew.save()

        clean_code = Book(
            name='Clean code',
            description='How to write <i>code</i> well',
            price=Decimal('0.01'),
            link='http://amazons3.com/books/clean_code.pdf',
        )
        clean_code.save()
        clean_code.authors.add(uncle_bob)

        code_complete = Book(
            name='Code complete',
            description='How to write <i>code</i> well',
            price=Decimal('0.02'),
            link='http://amazons3.com/books/code_complete.pdf',
        )
        code_complete.save()
        code_complete.authors.add(mcconnell)
        code_complete.authors.add(levi_metthew)

        the_art_of_software_testing = Book(
            name='The art of software testing',
            description='The first book about software testing',
            price=Decimal('0.003'),
            link='http://amazons3.com/books/the_art_of_software_testing.pdf')
        the_art_of_software_testing.save()
        the_art_of_software_testing.authors.add(myers)
        the_art_of_software_testing.authors.add(levi_metthew)

        alobachev = User(username='******', email='*****@*****.**')
        alobachev.set_password('password')
        alobachev.save()
        alobachev.profile.bought_books.add(code_complete)
        achistyakov = User(username='******',
                           email='*****@*****.**')
        achistyakov.set_password('password')
        achistyakov.save()
        achistyakov.profile.bought_books.add(code_complete,
                                             the_art_of_software_testing)
        aukupnik = User(username='******', email='*****@*****.**')
        aukupnik.set_password('password')
        aukupnik.save()
        aukupnik.profile.bought_books.add(code_complete,
                                          the_art_of_software_testing,
                                          clean_code)
        aapostle = User(username='******', email='*****@*****.**')
        aapostle.set_password('password')
        aapostle.save()

        achistyakov.profile.cards.create(name='VISA***1111',
                                         payment_number='1111-1111-1111-1111')