import json

from aleph.tests.util import TestCase


class SearchApiTestCase(TestCase):

    def setUp(self):
        super(SearchApiTestCase, self).setUp()
        self.load_fixtures('docs.yaml')

    def test_simplest_search(self):
        # self.login(is_admin=True)
        res = self.client.get('/api/2/search?q=banana&facet=collection_id')
        assert res.status_code == 200, res
        assert res.json['total'] == 1, res.json
        # assert '<em>banana</em>' in res.data, res.json
        assert 'Public Collection' in res.data, res.json
        assert 'Secret Document' not in res.data, res.json

        _, headers = self.login(is_admin=True)
        res = self.client.get('/api/2/search?q=banana',
                              headers=headers)
        assert res.status_code == 200, res
        assert res.json['total'] == 3, res.json
        assert 'Secret Document' in res.data, res.json

    def test_facet_attribute(self):
        res = self.client.get('/api/2/search?facet=languages')
        assert res.status_code == 200, res
        lang_facet = res.json['facets']['languages']
        assert len(lang_facet['values']) == 2
        text = json.dumps(lang_facet)
        assert '"ru"' in text, lang_facet

    def test_facet_schema(self):
        res = self.client.get('/api/2/search?facet=schema')
        assert res.status_code == 200, res
        facet = res.json['facets']['schema']
        assert len(facet['values']) == 2
        assert facet['values'][0]['label'] == 'Documents', facet

        res = self.client.get('/api/2/search?facet=schema&filter:schema=Company')  # noqa
        assert res.status_code == 200, res
        facet = res.json['facets']['schema']
        assert len(facet['values']) == 2, facet['values']
        assert facet['values'][0]['label'] == 'Companies', facet

    def test_basic_filters(self):
        res = self.client.get('/api/2/search?filter:source_id=23')
        assert res.status_code == 200, res
        assert res.json['total'] == 0, res.json

        res = self.client.get('/api/2/search?filter:languages=ru')
        assert res.status_code == 200, res
        assert res.json['total'] == 1, res.json

@@ -1728,17 +1728,17 @@
 s'%5D) == 
-2
+1
 , facet%5B
@@ -1809,24 +1809,323 @@
 es', facet%0A%0A
+        res = self.client.get('/api/2/search?facet=schema&post_filter:schema=Company')%0A        assert res.status_code == 200, res%0A        facet = res.json%5B'facets'%5D%5B'schema'%5D%0A        assert len(facet%5B'values'%5D) == 2, facet%5B'values'%5D%0A        assert facet%5B'values'%5D%5B0%5D%5B'label'%5D == 'Documents', facet%0A%0A
     def test

Fix test_facet_schema to test for post_filter