Ejemplo n.º 1
0
    def test_use_custom_resources(self):
        user_resource = get_resource_for_model(User)
        self.assertTrue(issubclass(user_resource.__class__, UserResource))

        group_resource = get_resource_for_model(Group)
        self.assertTrue(issubclass(group_resource.__class__, GroupResource))

        permission_resource = get_resource_for_model(Permission)
        self.assertTrue(issubclass(permission_resource.__class__, ExportModelResource))
Ejemplo n.º 2
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
Ejemplo n.º 3
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')
Ejemplo n.º 4
0
 def test_get_resource_for_model(self):
     resource = get_resource_for_model(Author)
     self.assertIsInstance(resource, ModelResource)
     self.assertEqual(resource.Meta.model, Author)
Ejemplo n.º 5
0
    def test_custom_dataset_title(self):
        user_resource = get_resource_for_model(User)
        self.assertEqual(user_resource.title, 'Custom title')

        group_resource = get_resource_for_model(Group)
        self.assertIsNone(group_resource.title)
Ejemplo n.º 6
0
 def test_pass_kwargs(self):
     kwargs = {'foo': 'bar'}
     user_resource = get_resource_for_model(User, **kwargs)
     self.assertEqual(user_resource.foo, 'bar')