def test_no_brains(self):

        self.replay()

        source = RelatedDocumentsCatalogTableSource(self.config, self.request)

        result = source.extend_query_with_textfilter([], 'filter')

        self.assertEquals(result, [])
    def test_no_brains(self):

        self.replay()

        source = RelatedDocumentsCatalogTableSource(self.config, self.request)

        result = source.extend_query_with_textfilter([], 'filter')

        self.assertEquals(result, [])
    def test_with_matches_on_author(self):

        self.replay()

        source = RelatedDocumentsCatalogTableSource(self.config, self.request)

        result = source.extend_query_with_textfilter(
            [self.brain_1, self.brain_2, self.brain_3], 'author_2')

        self.assertEquals(result, [self.brain_2])
    def test_with_matches_on_author(self):

        self.replay()

        source = RelatedDocumentsCatalogTableSource(self.config, self.request)

        result = source.extend_query_with_textfilter(
            [self.brain_1, self.brain_2, self.brain_3], 'author_2')

        self.assertEquals(result, [self.brain_2])
    def test_custom_sort_indexes(self):

        request1 = {}
        request2 = {}
        request3 = {}

        request1['sort'] = 'document_date'
        request2['sort'] = 'receipt_date'
        request3['sort'] = 'delivery_date'

        self.expect(self.config.sort_reverse).result(False).count(0, None)

        self.replay()

        source1 = RelatedDocumentsCatalogTableSource(self.config, request1)
        source2 = RelatedDocumentsCatalogTableSource(self.config, request2)
        source3 = RelatedDocumentsCatalogTableSource(self.config, request3)

        result1 = source1.extend_query_with_ordering(
            ['brain1', 'brain3', 'brain2'])
        result2 = source2.extend_query_with_ordering(
            ['brain1', 'brain3', 'brain2'])
        result3 = source3.extend_query_with_ordering(
            ['brain1', 'brain3', 'brain2'])

        self.assertEquals(result1, ['brain1', 'brain3', 'brain2'])
        self.assertEquals(result2, ['brain1', 'brain3', 'brain2'])
        self.assertEquals(result3, ['brain1', 'brain3', 'brain2'])

        self.assertEquals(source1.config._custom_sort_method,
                          default_custom_sort)
        self.assertEquals(source2.config._custom_sort_method,
                          default_custom_sort)
        self.assertEquals(source3.config._custom_sort_method,
                          default_custom_sort)
    def test_no_sort_index(self):

        self.expect(self.config.sort_reverse).result(False).count(0, None)
        self.replay()

        source = RelatedDocumentsCatalogTableSource(self.config, self.request)

        result = source.extend_query_with_ordering(
            ['brain1', 'brain3', 'brain2'])

        self.assertEquals(result, ['brain1', 'brain3', 'brain2'])
    def test_bad_item(self):

        self.expect(self.config.context.relatedItems).result([self.doc_rel_3])

        self.replay()

        source = RelatedDocumentsCatalogTableSource(self.config, self.request)

        result = source.get_related_documents()

        self.assertEquals(result, [])
    def test_no_sort_index(self):

        self.expect(self.config.sort_reverse).result(False).count(0, None)
        self.replay()

        source = RelatedDocumentsCatalogTableSource(self.config, self.request)

        result = source.extend_query_with_ordering(
            ['brain1', 'brain3', 'brain2'])

        self.assertEquals(result, ['brain1', 'brain3', 'brain2'])
    def test_no_items(self):

        self.expect(self.config.context.relatedItems).result([])

        self.replay()

        source = RelatedDocumentsCatalogTableSource(self.config, self.request)

        result = source.get_related_documents()

        self.assertEquals(result, [])
    def test_trashed_items(self):

        self.expect(self.config.context.relatedItems).result(
            [self.doc_rel_1, self.doc_rel_4])

        self.replay()

        source = RelatedDocumentsCatalogTableSource(self.config, self.request)

        result = source.get_related_documents()

        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].brain, self.doc_rel_1)
    def test_trashed_items(self):

        self.expect(self.config.context.relatedItems).result(
            [self.doc_rel_1, self.doc_rel_4])

        self.replay()

        source = RelatedDocumentsCatalogTableSource(self.config, self.request)

        result = source.get_related_documents()

        self.assertEquals(len(result), 1)
        self.assertEquals(result[0].brain, self.doc_rel_1)
    def test_no_transform(self):

        self.request['sort'] = 'document_author'

        self.expect(self.config.sort_reverse).result(False).count(0, None)
        self.expect(self.config.columns).result(self.columns)

        self.replay()

        source = RelatedDocumentsCatalogTableSource(self.config, self.request)

        result = source.extend_query_with_ordering(
            [self.brain_1, self.brain_3, self.brain_2])

        self.assertEquals(result, [self.brain_3, self.brain_1, self.brain_2])
    def test_no_transform(self):

        self.request['sort'] = 'document_author'

        self.expect(self.config.sort_reverse).result(False).count(0, None)
        self.expect(self.config.columns).result(self.columns)

        self.replay()

        source = RelatedDocumentsCatalogTableSource(self.config, self.request)

        result = source.extend_query_with_ordering(
            [self.brain_1, self.brain_3, self.brain_2])

        self.assertEquals(result, [self.brain_3, self.brain_1, self.brain_2])
    def test_no_columns_bad_sort_index(self):

        self.request['sort'] = 'bad_index'

        self.expect(self.config.sort_reverse).result(False).count(0, None)
        self.expect(self.config.columns).result({})

        self.replay()

        source = RelatedDocumentsCatalogTableSource(self.config, self.request)

        self.assertRaises(AttributeError, source.extend_query_with_ordering,
                          [self.brain_1, self.brain_3, self.brain_2])
    def setUp(self):
        super(BuildQueryTests, self).setUp()

        self.request = {}
        self.config = self.mocker.mock(count=False)
        self.expect(self.config.filter_text).result('')
        self.expect(self.config.update_config()).result(True)
        self.expect(self.config.get_base_query()).result({})

        self.source = self.mocker.patch(
            RelatedDocumentsCatalogTableSource(self.config, self.request))
        self.expect(
            self.source.extend_query_with_ordering(ANY)).call(lambda x: x)
        self.expect(self.source.extend_query_with_textfilter(
            ANY, ANY)).call(lambda x, y: x)
    def test_custom_sort_indexes(self):

        request1 = {}
        request2 = {}
        request3 = {}

        request1['sort'] = 'document_date'
        request2['sort'] = 'receipt_date'
        request3['sort'] = 'delivery_date'

        self.expect(self.config.sort_reverse).result(False).count(0, None)

        self.replay()

        source1 = RelatedDocumentsCatalogTableSource(
            self.config, request1)
        source2 = RelatedDocumentsCatalogTableSource(
            self.config, request2)
        source3 = RelatedDocumentsCatalogTableSource(
            self.config, request3)

        result1 = source1.extend_query_with_ordering(
            ['brain1', 'brain3', 'brain2'])
        result2 = source2.extend_query_with_ordering(
            ['brain1', 'brain3', 'brain2'])
        result3 = source3.extend_query_with_ordering(
            ['brain1', 'brain3', 'brain2'])

        self.assertEquals(result1, ['brain1', 'brain3', 'brain2'])
        self.assertEquals(result2, ['brain1', 'brain3', 'brain2'])
        self.assertEquals(result3, ['brain1', 'brain3', 'brain2'])

        self.assertEquals(
            source1.config._custom_sort_method, default_custom_sort)
        self.assertEquals(
            source2.config._custom_sort_method, default_custom_sort)
        self.assertEquals(
            source3.config._custom_sort_method, default_custom_sort)