fixes
[redakcja.git] / apps / catalogue / tests / book.py
1 # -*- coding: utf-8 -*-
2 #
3 # This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
5 #
6 """Tests for manipulating books in the catalogue."""
7
8 from django.test import TestCase
9 from django.contrib.auth.models import User
10 from catalogue.models import Book
11
12
13 class ManipulationTests(TestCase):
14
15     def setUp(self):
16         self.user = User.objects.create(username='tester')
17         self.book1 = Book.create(self.user, 'book 1', slug='book1')
18         self.book2 = Book.create(self.user, 'book 2', slug='book2')
19
20     def test_append(self):
21         self.book1.append(self.book2)
22         self.assertEqual(Book.objects.all().count(), 1)
23         self.assertEqual(len(self.book1), 2)
24
25     def test_append_to_self(self):
26         with self.assertRaises(AssertionError):
27             self.book1.append(Book.objects.get(pk=self.book1.pk))
28         self.assertEqual(Book.objects.all().count(), 2)
29         self.assertEqual(len(self.book1), 1)
30
31     def test_prepend_history(self):
32         self.book1.prepend_history(self.book2)
33         self.assertEqual(Book.objects.all().count(), 1)
34         self.assertEqual(len(self.book1), 1)
35         self.assertEqual(self.book1.materialize(), 'book 1')
36
37     def test_prepend_history_to_self(self):
38         with self.assertRaises(AssertionError):
39             self.book1.prepend_history(self.book1)
40         self.assertEqual(Book.objects.all().count(), 2)
41         self.assertEqual(self.book1.materialize(), 'book 1')
42         self.assertEqual(self.book2.materialize(), 'book 2')
43
44     def test_split_book(self):
45         self.book1.chunk_set.create(number=2, title='Second chunk',
46                 slug='book3')
47         self.book1[1].commit('I survived!')
48         self.assertEqual(len(self.book1), 2)
49         self.book1.split()
50         self.assertEqual(set([b.slug for b in Book.objects.all()]),
51                 set(['book2', '1', 'book3']))
52         self.assertEqual(
53                 Book.objects.get(slug='book3').materialize(),
54                 'I survived!')