Example #1
0
def mock_publisher_lazy_with_3_books(db_session):
    publisher = PublisherFactory.create(name="mock_publisher_lazy_with_3_books")

    book1 = BookFactory(publisher=publisher)
    book2 = BookFactory(publisher=publisher)
    book3 = BookFactory(publisher=publisher)

    yield publisher
Example #2
0
def mock_person_with_3_books_read(db_session):
    person = PersonFactory.create(name="mock_pers_with_3_books_read")

    book1 = BookFactory(reader_id=person.id)
    book2 = BookFactory(reader_id=person.id)
    book3 = BookFactory(reader_id=person.id)

    yield person
Example #3
0
def test_friendly_renderer():
    user = UserFactory(is_superuser=True)
    BookFactory()
    BookFactory(best_seller=True)

    client = APIClient()
    client.force_authenticate(user=user)
    response = client.get(reverse('sample:book-csv-view') + '?format=csv')
    dataset = Dataset().load(response.content.decode('utf-8'), 'csv')
    assert len(dataset._get_headers()) == 4
    assert dataset[0][2] == ''
    assert dataset[1][2] == 'Yes'
Example #4
0
def test_export_model_view():
    user = UserFactory(is_superuser=True)
    BookFactory(name='demo')
    BookFactory(name='test')

    client = APIClient()
    client.force_authenticate(user=user)

    url = reverse('sample:author-list') + '?format=csv'
    response = client.get(url, format='json')

    dataset = Dataset().load(response.content.decode('utf-8'), 'csv')
    assert len(dataset._get_headers()) == 4
    assert dataset.headers == ['ID', 'Books', 'First name', 'Last name']
Example #5
0
def test_history(admin_client):
    """
    We can render the object history page
    """
    book = BookFactory()
    url = reverse("admin:books_book_history", args=(book.pk, ))

    response = admin_client.get(url)
    templates_used = [t.name for t in response.templates]

    assert response.status_code == 200
    render_counts = {x: templates_used.count(x) for x in set(templates_used)}

    # The number of times each template was rendered
    assert render_counts == {
        "admin/object_history.html": 1,
        "admin/base.html": 1,
        "admin/base_site.html": 1,
        "jazzmin/includes/ui_builder_panel.html": 1,
    }

    # The templates that were used
    assert set(templates_used) == {
        "admin/object_history.html",
        "admin/base.html",
        "admin/base_site.html",
        "jazzmin/includes/ui_builder_panel.html",
    }
Example #6
0
def test_detail(admin_client):
    """
    We can render the detail view
    """
    book = BookFactory()
    url = reverse("admin:books_book_change", args=(book.pk, ))

    response = admin_client.get(url)
    templates_used = [t.name for t in response.templates]

    assert response.status_code == 200
    render_counts = {x: templates_used.count(x) for x in set(templates_used)}

    # The number of times each template was rendered
    assert render_counts == {
        "django/forms/widgets/text.html": 6,
        "admin/widgets/foreign_key_raw_id.html": 1,
        "django/forms/widgets/select_option.html": 19,
        "admin/edit_inline/stacked.html": 1,
        "admin/prepopulated_fields_js.html": 1,
        "jazzmin/includes/ui_builder_panel.html": 1,
        "admin/widgets/related_widget_wrapper.html": 3,
        "django/forms/widgets/textarea.html": 1,
        "admin/change_form_object_tools.html": 1,
        "django/forms/widgets/date.html": 3,
        "admin/base.html": 1,
        "admin/includes/fieldset.html": 4,
        "django/forms/widgets/hidden.html": 6,
        "django/forms/widgets/attrs.html": 41,
        "admin/change_form.html": 1,
        "admin/base_site.html": 1,
        "django/forms/widgets/select.html": 5,
        "jazzmin/includes/horizontal_tabs.html": 1,
        "django/forms/widgets/input.html": 16,
        "admin/submit_line.html": 1,
    }

    # The templates that were used
    assert set(templates_used) == {
        "django/forms/widgets/text.html",
        "admin/widgets/foreign_key_raw_id.html",
        "django/forms/widgets/select_option.html",
        "admin/edit_inline/stacked.html",
        "admin/prepopulated_fields_js.html",
        "jazzmin/includes/ui_builder_panel.html",
        "admin/widgets/related_widget_wrapper.html",
        "django/forms/widgets/textarea.html",
        "admin/change_form_object_tools.html",
        "django/forms/widgets/date.html",
        "admin/base.html",
        "admin/includes/fieldset.html",
        "django/forms/widgets/hidden.html",
        "django/forms/widgets/attrs.html",
        "admin/change_form.html",
        "admin/base_site.html",
        "django/forms/widgets/select.html",
        "jazzmin/includes/horizontal_tabs.html",
        "django/forms/widgets/input.html",
        "admin/submit_line.html",
    }
Example #7
0
def test_get_read_book_from_reader_person_by_id(client,
                                                mock_person_with_3_books_read):
    newly_read_book = BookFactory(reader_id=mock_person_with_3_books_read.id)

    res = client.get(
        f"/People/{mock_person_with_3_books_read.id}/books_read/{newly_read_book.id}"
    )
    assert res.status_code == 200

    response_data = res.get_json()
    assert response_data["data"]["id"] == newly_read_book.id
Example #8
0
def test_changeform_template_default(admin_client):
    """
    The horizontal_tabs template is used by default
    """
    book = BookFactory()

    books_url = reverse("admin:books_book_change", args=(book.pk, ))

    response = admin_client.get(books_url)
    templates_used = [t.name for t in response.templates]

    assert CHANGEFORM_TEMPLATES["horizontal_tabs"] in templates_used
Example #9
0
def test_changeform_single(admin_client):
    """
    The single template is used when the modeladmin has no fieldsets, or inlines
    """
    book = BookFactory()

    books_url = reverse("admin:books_book_change", args=(book.pk, ))

    response = admin_client.get(books_url)

    templates_used = [t.name for t in response.templates]

    assert CHANGEFORM_TEMPLATES["single"] in templates_used
Example #10
0
def test_no_delete_permission(client):
    """
    When our user has no delete permission, they dont see things they are not supposed to
    """
    user = UserFactory(permissions=["books.view_book"])
    book = BookFactory()

    url = reverse("admin:books_book_change", args=(book.pk, ))
    delete_url = reverse("admin:books_book_delete", args=(book.pk, ))
    client.force_login(user)

    response = client.get(url)
    assert delete_url not in response.content.decode()
Example #11
0
def test_add_invalid_book_to_reader_person_books_read_list(
        client, db_session, mock_person_with_3_books_read):
    newly_read_book = BookFactory.create(name="mock_Read_book")

    data = [{"id": newly_read_book.id}]  # no type

    res = client.post(f"/People/{mock_person_with_3_books_read.id}/books_read",
                      json={"data": data})
    assert res.status_code == 403

    data = [{"id": "invalid id"}]  # invalid id

    res = client.post(f"/People/{mock_person_with_3_books_read.id}/books_read",
                      json={"data": data})
    assert res.status_code == 404
Example #12
0
def test_add_book_to_reader_person_books_read_list(
        client, db_session, mock_person_with_3_books_read):
    newly_read_book = BookFactory.create(name="mock_Read_book")

    data = [{"id": newly_read_book.id, "type": newly_read_book._s_type}]

    res = client.post(f"/People/{mock_person_with_3_books_read.id}/books_read",
                      json={"data": data})
    assert res.status_code == 204

    person_books_read_list = (db_session.query(models.Book).filter(
        models.Book.reader_id == mock_person_with_3_books_read.id).all())

    assert len(person_books_read_list) == 4
    assert person_books_read_list[3].id == newly_read_book.id
Example #13
0
def test_changeform_templates(admin_client, settings, config_value, template):
    """
    All changeform config values use the correct templates
    """
    book = BookFactory()

    url = reverse("admin:books_book_change", args=(book.pk, ))

    settings.JAZZMIN_SETTINGS = override_jazzmin_settings(
        changeform_format=config_value)

    response = admin_client.get(url)
    templates_used = [t.name for t in response.templates]

    assert template in templates_used
Example #14
0
def test_get_admin_url(admin_user):
    """
    We can get admin urls for Model classes, instances, or app.model strings
    """
    book = BookFactory()

    assert get_admin_url(book) == reverse("admin:books_book_change",
                                          args=(book.pk, ))
    assert get_admin_url(Book) == reverse("admin:books_book_changelist")
    assert get_admin_url(
        Book, q="test") == reverse("admin:books_book_changelist") + "?q=test"
    assert get_admin_url("books.Book") == reverse(
        "admin:books_book_changelist")
    assert get_admin_url("cheese:bad_pattern") == "#"
    assert get_admin_url("fake_app.fake_model") == "#"
    assert get_admin_url(1) == "#"
Example #15
0
def test_update():
    user = UserFactory()
    author = AuthorFactory()
    obj_dict = utils.create_dict_with_relations(author)
    book = BookFactory(author=author)
    activity = utils.create_snapshot(author, obj_dict, user)
    assert activity.target == author
    assert activity.action == activity.UPDATE
    assert activity.by_user == user
    assert activity.data["name"] == author.name
    assert activity.change == {
        "books": {
            "before": [],
            "after": [book.pk]
        }
    }
    assert activity.get_display() == "Changed books"
Example #16
0
def test_patch_publishers_books_list(client, db_session,
                                     mock_publisher_with_3_books):
    book = BookFactory.create(name="mock_book")

    res = client.patch(f"/Publishers/{mock_publisher_with_3_books.id}/books",
                       json={"data": [{
                           "id": book.id,
                           "type": book._s_type
                       }]})
    assert res.status_code == 200

    publishers_books_list = (db_session.query(models.Book).filter(
        models.Book.publisher_id == mock_publisher_with_3_books.id).all())

    response_data = res.get_json()
    assert len(publishers_books_list) == 1
    assert mock_publisher_with_3_books.books[0].id == response_data["data"][0][
        "id"]
Example #17
0
    def handle(self, *args, **options):
        call_command("reset_db", "--noinput")
        call_command("migrate")

        library = LibraryFactory()
        UserFactory(username="******",
                    email="*****@*****.**",
                    password="******",
                    is_superuser=True)

        users = UserFactory.create_batch(2, is_staff=False)

        GroupFactory.create_batch(5)

        for author in AuthorFactory.create_batch(10):
            for book in BookFactory.create_batch(5,
                                                 author=author,
                                                 library=library):
                BookLoanFactory(book=book, borrower=choice(users))

        self.stdout.write("All Data reset")
Example #18
0
def test_changeform_template_override(admin_client, settings):
    """
    We can set a global template, and override it per model
    """
    user = UserFactory()
    book = BookFactory()

    books_url = reverse("admin:books_book_change", args=(book.pk, ))
    users_url = reverse("admin:auth_user_change", args=(user.pk, ))

    settings.JAZZMIN_SETTINGS = override_jazzmin_settings(
        changeform_format="vertical_tabs",
        changeform_format_overrides={"books.book": "carousel"})

    response = admin_client.get(books_url)
    templates_used = [t.name for t in response.templates]

    assert CHANGEFORM_TEMPLATES["carousel"] in templates_used

    response = admin_client.get(users_url)
    templates_used = [t.name for t in response.templates]

    assert CHANGEFORM_TEMPLATES["vertical_tabs"] in templates_used
Example #19
0
def test_delete(admin_client):
    """
    We can load the confirm delete page, and POST it, and it deletes our object
    """
    book = BookFactory()
    url = reverse("admin:books_book_delete", args=(book.pk, ))

    response = admin_client.get(url)
    templates_used = [t.name for t in response.templates]

    assert response.status_code == 200
    render_counts = {x: templates_used.count(x) for x in set(templates_used)}

    # The number of times each template was rendered
    assert render_counts == {
        "admin/delete_confirmation.html": 1,
        "admin/base_site.html": 1,
        "admin/base.html": 1,
        "admin/includes/object_delete_summary.html": 1,
        "jazzmin/includes/ui_builder_panel.html": 1,
    }

    # The templates that were used
    assert set(templates_used) == {
        "admin/delete_confirmation.html",
        "admin/base_site.html",
        "admin/base.html",
        "admin/includes/object_delete_summary.html",
        "jazzmin/includes/ui_builder_panel.html",
    }

    response = admin_client.post(url, data={"post": "yes"}, follow=True)

    # We deleted our object, and are now back on the changelist
    assert not Book.objects.all().exists()
    assert response.resolver_match.url_name == "books_book_changelist"
Example #20
0
def generate_objects():
    print("Creating books...")
    BookFactory.create_batch(size=10)
    print("Done.")
Example #21
0
def test_relation():
    author = AuthorFactory()
    book = BookFactory(author=author)
    obj_dict = utils.create_dict_with_relations(author)
    assert obj_dict["books"] == [book.pk]
Example #22
0
def test_many_to_many_fields():
    book = BookFactory()
    fields = utils.get_to_many_field_names(book.__class__)
    # check many_to_many field
    assert "tags" in fields
Example #23
0
def test_export_view_list_docx_table_text_blob(api_client):
    BookFactory(description=factory.Faker("sentence", nb_words=800))
    url = "{}?format=docx_table".format(reverse("sample:book-view"))
    response = api_client.get(url)
    assert response.status_code == 200
Example #24
0
def test_list(admin_client):
    """
    We can render the list view
    """
    BookFactory.create_batch(5)

    url = reverse("admin:books_book_changelist")

    response = admin_client.get(url)
    templates_used = [t.name for t in response.templates]

    assert response.status_code == 200
    render_counts = {x: templates_used.count(x) for x in set(templates_used)}

    # The number of times each template was rendered
    assert render_counts == {
        "admin/filter.html": 2,
        "admin/base.html": 1,
        "admin/base_site.html": 1,
        "admin/date_hierarchy.html": 1,
        "admin/change_list_object_tools.html": 1,
        "admin/change_list.html": 1,
        "admin/change_list_results.html": 1,
        "admin/pagination.html": 1,
        "admin/search_form.html": 1,
        "admin/actions.html": 2,
        "admin/import_export/change_list.html": 1,
        "admin/import_export/change_list_export_item.html": 1,
        "admin/import_export/change_list_import_export.html": 1,
        "admin/import_export/change_list_import_item.html": 1,
        "django/forms/widgets/checkbox.html": 5,
        "django/forms/widgets/text.html": 5,
        "django/forms/widgets/select_option.html": 4,
        "django/forms/widgets/select.html": 2,
        "django/forms/widgets/input.html": 21,
        "django/forms/widgets/hidden.html": 11,
        "django/forms/widgets/attrs.html": 27,
        "jazzmin/includes/ui_builder_panel.html": 1,
    }

    # The templates that were used
    assert set(templates_used) == {
        "admin/filter.html",
        "admin/base.html",
        "admin/base_site.html",
        "admin/date_hierarchy.html",
        "admin/change_list_object_tools.html",
        "admin/change_list.html",
        "admin/change_list_results.html",
        "admin/pagination.html",
        "admin/search_form.html",
        "admin/actions.html",
        "admin/import_export/change_list.html",
        "admin/import_export/change_list_export_item.html",
        "admin/import_export/change_list_import_export.html",
        "admin/import_export/change_list_import_item.html",
        "django/forms/widgets/checkbox.html",
        "django/forms/widgets/text.html",
        "django/forms/widgets/select_option.html",
        "django/forms/widgets/select.html",
        "django/forms/widgets/input.html",
        "django/forms/widgets/hidden.html",
        "django/forms/widgets/attrs.html",
        "jazzmin/includes/ui_builder_panel.html",
    }