Exemplo n.º 1
0
    def test_exporter(self):
        authors = AuthorFactory.create_batch(3)
        categories = CategoryFactory.create_batch(3)

        books = BookFactory.create_batch(2, author=authors[0])
        books += BookFactory.create_batch(3, author=authors[1])
        books += BookFactory.create_batch(4, author=authors[2])

        books[0].categories = categories[:2]  # 2 categories
        books[-1].categories = categories[2:]  # 1 category

        resources = [get_resource_for_model(model) for model in [Author, Book, Category]]
        exporter = Exporter(resources)
        book = exporter.export()

        sheets = book.sheets()
        self.assertEqual(len(sheets), 3)
        titles = set([sheet.title for sheet in sheets])
        expected_titles = set(['authors (app.Author)', 'books (app.Book)', 'categorys (app.Category)'])
        self.assertEqual(titles, expected_titles)

        # test that all the data is there as expected
        author_sheet, book_sheet, category_sheet = sheets

        self.assertEqual(len(author_sheet), 3)  # 3 authors
        self.assertEqual(len(author_sheet[0]), 3)  # id, name, birthday

        self.assertEqual(len(book_sheet), 9)  # 3 authors
        # id, name, author_id, author_email, imported, published, price, categories
        self.assertEqual(len(book_sheet[0]), 8)
        self.assertEqual(book_sheet[0][2], authors[0].id)
        self.assertEqual(book_sheet[0][-1], ','.join([str(cat.id) for cat in categories[:2]]))

        self.assertEqual(len(category_sheet), 3)
        self.assertEqual(len(category_sheet[0]), 2)  # id, name
Exemplo n.º 2
0
    def test_exporter_custom_title(self):
        AuthorFactory.create_batch(3)

        exporter = Exporter([get_resource_for_model(Author)])
        book = exporter.export()

        sheets = book.sheets()
        self.assertEqual(len(sheets), 1)

        sheet = sheets[0]
        self.assertEqual(sheet.title, 'Author')