def test_simple_search_form_quick_filters(self, admin_client):
        """
        Test quick filters result
        """
        ArticleFactory(
            title='title1',
            description='description1',
            published=True
        )
        ArticleFactory(
            title='title2',
            description='Dead rabbit is walking without leg.',
            published=False
        )

        response = admin_client.get(reverse('articles:list'),
                                    {'published': 'published'})
        list_items = response.context_data['list_items']
        assert len(list_items) == 1
        assert list_items[0]['fields'][0]['value'] == 'title1'

        response = admin_client.get(reverse('articles:list'),
                                    {'published': 'drafts'})
        list_items = response.context_data['list_items']
        assert list_items[0]['fields'][0]['value'] == 'title2'
def test_foreign_key_contains_operator():
    user = UserFactory(first_name="First Name")
    user2 = UserFactory(first_name="Name")
    a1 = ArticleFactory(user=user)
    ArticleFactory(user=user2)
    filter_obj = ContainsFKFilter(data={"author_first_name": "First"})
    result = filter_obj.apply().all()
    assert len(result) == 1
    assert result[0] == a1
    def test_field_actions(self, admin_client):
        ArticleFactory(title='title1', description='description1',
                       published=True)
        ArticleFactory(title='title2', description='description2',
                       published=False)

        response = admin_client.get(reverse('articles:list'),
                                    {'description': ''})
        list_items = response.context_data['list_items']
        assert len(list_items[0]['actions']) == 1
        assert len(list_items[1]['actions']) == 2
    def test_merge_generic_foreign_keys(self):
        primary_object = ArticleFactory()
        alias_object = ArticleFactory()
        primary_object.tags.create(content_object=primary_object, tag='django')
        alias_object.tags.create(content_object=alias_object, tag='python')

        merged_object = MergedModelInstance.create(primary_object,
                                                   [alias_object],
                                                   keep_old=False)

        assert merged_object.tags.count() == 2
    def test_field_classes(self, admin_client):
        """
        Virtual field displayed in ListView
        """
        ArticleFactory(published=True)
        ArticleFactory(published=False)
        response = self._request(admin_client)

        list_items = response.context_data['list_items']
        assert list_items[0]['fields'][2]['class'] == 'online'
        assert list_items[1]['fields'][2]['class'] == 'offline'
Example #6
0
 def test_virtual_ordering_field(self, admin_client):
     """
     ordering by 'virtual ordering field'
     """
     ArticleFactory(title='Article 0')
     ArticleFactory(title='Article 1')
     response = self._request(admin_client)
     lst_headers = response.context_data['list_header']
     lst_items = response.context_data['list_items']
     assert lst_headers[3]['order_url'] == '/articles/?order=category__name'
     assert lst_headers[3]['order_direction'] == 'desc'
     assert len(lst_items) == 2
     assert lst_items[0]['fields'][0]['value'] == 'Article 1'
     assert lst_items[1]['fields'][0]['value'] == 'Article 0'
    def test_simple_search_form(self, admin_client):
        """
        Test simple search form
        """
        ArticleFactory(title='title1')
        ArticleFactory(title='title2')

        # empty search, return all
        response = admin_client.get(reverse('articles:list'), {'search': ''})
        assert len(response.context_data['list_items']) == 2

        # search filled, filter content
        response = admin_client.get(reverse('articles:list'),
                                    {'search': 'le1'})
        assert len(response.context_data['list_items']) == 1
        assert response.context_data['list_items'][0][0]['value'] == 'title1'
    def test_advanced_search_form(self, admin_client):
        """
        Test simple search form
        """
        ArticleFactory(title='title1', description='description1')
        ArticleFactory(title='title2', description='description2')

        # empty search, return all
        response = admin_client.get(reverse('articles:list'),
                                    {'description': ''})
        assert len(response.context_data['list_items']) == 2

        # search filled, filter content
        response = admin_client.get(reverse('articles:list'),
                                    {'description': 'tion2'})
        list_items = response.context_data['list_items']
        assert len(list_items) == 1
        assert list_items[0]['fields'][0]['value'] == 'title2'
 def test_single_item(self, admin_client):
     """
     Single item in ListView
     """
     article = ArticleFactory()
     response = self._request(admin_client)
     self._assert_list_items_len(response, 1)
     field = response.context_data['list_items'][0][0]['field']
     assert response.context_data['list_items'][0][0]['value'] == \
         getattr(article, field)
Example #10
0
    def test_paginated_items(self, admin_client):
        """
        Paginated List view
        """
        [ArticleFactory() for i in range(10)]

        response = self._request(admin_client)
        self._assert_list_items_len(response, 2)

        assert response.context_data['page_obj'].paginator.num_pages == 5
def test_multiple_filters():
    user = UserFactory(first_name="First Name")
    user2 = UserFactory(last_name="Last Name")
    a1 = ArticleFactory(user=user)
    a2 = ArticleFactory(user=user2)
    filter_obj = MultipleFKFilter(
        data={"author_first_name": "Name", "author_last_name_istarts": "lasT"},
        operator=OrOperator,
    )
    result = filter_obj.apply().order_by(User.id.asc()).all()
    assert len(result) == 2
    assert result == [a1, a2]
    filter_obj = MultipleFKFilter(
        data={"author_first_name": "Name", "author_last_name_istarts": "Name"},
        operator=OrOperator,
    )
    result = filter_obj.apply().order_by(User.id.asc()).all()
    assert len(result) == 1
    assert result == [a1]
Example #12
0
    def test_export_csv_file(self, admin_client):
        """
        Test csv file export functional
        """
        for i in range(30):
            ArticleFactory(title="title{}".format(i), description='description{}'.format(i))

        response = admin_client.get(reverse('articles:list'), {'format': 'csv'})
        assert response.status_code == 200
        assert response._headers['content-type'][1] == 'text/csv'
        attach_file = 'Articles.csv'
        assert attach_file in response._headers['content-disposition'][1]
        # 2 more rows in file: titles and blank line
        assert len(response._container) == 32
Example #13
0
 def test_missing_virtual_field_execution_attribute_error(
         self, admin_client):
     """
     Error happens on wrong virtual field name
     """
     article = ArticleFactory()
     view = ArticleListView()
     view.fields = ['title', 'description', 'published', 'broken']
     view.get_broken_field = lambda obj: obj.unknown_field
     exc = pytest.raises(AttributeError, view.get_field_value, 'broken',
                         article)
     assert str(exc.value) == \
         "'Article' object has no attribute 'unknown_field'", \
         exc.value.message
    def test_readonly_fields(self, admin_client):
        article = ArticleFactory()
        view = ArticleUpdateView
        # Check with readonly field enabled
        view.readonly_fields = ['title']
        response = admin_client.get(self.get_url(article.pk))
        assert response.context_data['form'].fields['title']\
            .widget.attrs.get('readonly')

        # Check with readonly field disabled
        view.readonly_fields = []
        response = admin_client.get(self.get_url(article.pk))
        assert not response.context_data['form'].fields['title']\
            .widget.attrs.get('readonly')
Example #15
0
    def test_virtual_field(self, admin_client):
        """
        Virtual field displayed in ListView
        """
        article = ArticleFactory()
        view = ArticleListView()
        view.fields = ['title', 'description', 'published', 'category']
        response = self._request(admin_client)
        self._assert_list_items_len(response, 1)

        item = response.context_data['list_items'][0]
        assert item[0]['value'] == article.title
        assert item[1]['value'] == article.description
        assert item[3]['value'] == article.category.name
Example #16
0
    def test_field_actions_allowing(self, admin_client):
        def get_field_actions(self, obj):
            action_links = [
                ('detail', 'articles:detail', 'fa-edit'),
                ('categories', 'articles:category-list', 'fa-edit'),
            ]
            if not obj.published:
                action_links.append(
                    ('delete', 'articles:delete', 'fa-trash'), )
            return action_links

        ArticleListView.get_field_actions = get_field_actions

        ArticleFactory(title='title1', description='description1',
                       published=True)
        ArticleFactory(title='title2', description='description2',
                       published=False)

        response = admin_client.get(reverse('articles:list'),
                                    {'description': ''})
        list_items = response.context_data['list_items']
        assert len(list_items[0]['actions']) == 1
        assert len(list_items[1]['actions']) == 2
def test_2_foreign_keys():
    user = UserFactory(first_name="First Name", last_name="some last name")
    user2 = UserFactory(first_name="Name", last_name="test name")
    a1 = ArticleFactory(user=user)
    a2 = ArticleFactory(user=user2)
    filter_obj = Contains2FKFilter(data={"author_first_name": "Name"})
    result = filter_obj.apply().all()
    assert len(result) == 2
    assert {result[0], result[1]} == {a1, a2}

    filter_obj = Contains2FKFilter(
        data={"author_first_name": "Name", "author_last_name": "some"}
    )
    result = filter_obj.apply().all()
    assert len(result) == 1
    assert result[0] == a1

    filter_obj = Contains2FKFilter(
        data={"author_first_name": "Name", "author_last_name": "test"},
        operator=OrOperator,
    )
    result = filter_obj.apply().all()
    assert len(result) == 2
    assert {result[0], result[1]} == {a1, a2}
Example #18
0
    def test_missing_virtual_field(self, admin_client):
        """
        Error happens on wrong virtual field name
        """
        article = ArticleFactory()  # noqa
        view = ArticleListView()
        view.fields = ['title', 'description', 'published', 'virtual_field']
        response = self._request(admin_client)

        search_virtual_field = False
        for field in response.context_data['list_items'][0]:
            if field['type'] == 'field' and 'virtual_field' in field['field']:
                search_virtual_field = True

        assert search_virtual_field is False
Example #19
0
def test_merge_authors_action(admin_client):
    authors = AuthorFactory.create_batch(5)
    article = ArticleFactory(authors=authors)
    selected = choice(authors)

    url = reverse("admin:lynx_author_changelist")
    data = {
        "post": "yes",
        "_selected_action": [author.pk for author in authors],
        "action": "merge_authors",
        "selected": selected.pk,
    }

    admin_client.post(url, data, follow=True)

    # Merged authors are deleted
    actual = [author.pk for author in Author.objects.all()]
    assert actual == [selected.pk]

    # Merged author are replaced
    actual = [author.pk for author in article.authors.all()]
    assert actual == [selected.pk]
Example #20
0
def test_merge_topics_action(admin_client):
    topics = TopicFactory.create_batch(5)
    article = ArticleFactory(topics=topics)
    selected = choice(topics)

    url = reverse("admin:lynx_topic_changelist")
    data = {
        "post": "yes",
        "_selected_action": [topic.pk for topic in topics],
        "action": "merge_topics",
        "selected": selected.pk,
    }

    admin_client.post(url, data, follow=True)

    # Merged topics are deleted
    actual = [topic.pk for topic in Topic.objects.all()]
    assert actual == [selected.pk]

    # Merged topics are replaced
    actual = [topic.pk for topic in article.topics.all()]
    assert actual == [selected.pk]
def article():
    return ArticleFactory()
 def test_page_out_of_list(self, admin_client):
     [ArticleFactory() for i in range(20)]
     response = admin_client.get("%s?page=20" % reverse('articles:list'))
     assert response.status_code == 404
Example #23
0
def article(tag):
    article = ArticleFactory()
    article.tags.add(tag)
    return article
Example #24
0
def article_fixt(db):
    article = ArticleFactory()
    db.session.commit()
    return article
 def __init__(self):
     self.object = ArticleFactory()
     self.form = ArticleForm(instance=self.object)
     self.get_form = get_form(self.form)
    def test_update_page(self, admin_client):
        article = ArticleFactory()

        response = admin_client.get(self.get_url(article.pk))
        assert response.status_code == 200