def test_should_give_create_response_with_no_unique_id_fields(self):
        form_model = MagicMock(spec=FormModel)
        required_field_names = ['ds_id', 'ds_name', 'some_question']
        results = Response({
            "_hits": [
                Result({
                    '_type': "form_model_id",
                    '_id': 'index_id',
                    '_source': {
                        'ds_id': 'his_id',
                        'ds_name': 'his_name',
                        'some_question': 'answer'
                    }
                })
            ]
        })

        form_model.entity_questions = []
        form_model.id = 'form_model_id'
        local_time_delta = ('+', 2, 0)

        submissions = SubmissionQueryResponseCreator(
            form_model,
            local_time_delta).create_response(required_field_names, results)

        expected = [[
            'index_id', ["his_name<span class='small_grey'>  his_id</span>"],
            'answer'
        ]]
        self.assertEqual(submissions, expected)
def test_render_contributions_handles_unicode():
    hits = Response({
        'hits': {
            'hits': [
                {
                    '_type': 'hep',
                    '_source': {
                        'control_number':
                        1427573,
                        'titles': [
                            {
                                'title':
                                u'Storage Ring Based EDM Search — Achievements and Goals'
                            },
                        ],
                    },
                },
            ],
            'total':
            1,
        },
    }).hits

    expected = ([
        [
            u"<a href='/literature/1427573'>Storage Ring Based EDM Search — Achievements and Goals</a>",
            u'\n\n',
            '',
            0,
        ],
    ], 1)
    result = render_contributions(hits)

    assert expected == result
    def test_should_format_non_image_media_question(self):
        form_model = MagicMock(spec=FormModel)
        form_model.is_media_type_fields_present = True
        form_model.media_fields = [VideoField("video", "mp4", "mp4")]
        required_field_names = [
            'some_question', 'ds_id', 'ds_name', 'form_model_id_mp4'
        ]
        results = Response({
            "_hits": [
                Result({
                    '_type': "form_model_id",
                    '_id': 'index_id',
                    '_source': {
                        'ds_id': 'his_id',
                        'ds_name': 'his_name',
                        'form_model_id_q1': 'sub_last_name',
                        'form_model_id_mp4': 'vid.mp4',
                        'some_question': 'answer for it'
                    }
                })
            ]
        })
        form_model.id = 'form_model_id'
        local_time_delta = ('+', 2, 0)

        submissions = SubmissionQueryResponseCreator(
            form_model,
            local_time_delta).create_response(required_field_names, results)

        expected = [[
            'index_id', 'answer for it',
            ["his_name<span class='small_grey'>  his_id</span>"],
            '  <a href=\'/download/attachment/index_id/vid.mp4\'>vid.mp4</a>'
        ]]
        self.assertEqual(submissions, expected)
def test_render_people():
    hits = Response({
        'hits': {
            'hits': [
                {
                    '_type': 'authors',
                    '_source': {
                        'control_number': 1,
                        'name': {
                            'preferred_name': 'preferred_name',
                        },
                    },
                },
            ],
            'total': 1,
        },
    }).hits

    expected = ([
        [
            "<a href='/authors/1'>preferred_name</a>",
        ],
    ], 1)
    result = render_people(hits)

    assert expected == result
예제 #5
0
    def test__form_es(self, mock_esp_init, mock_esp_add_search, mock_esp_search):
        # __init__ methods always return None
        mock_esp_init.return_value = None

        # this allows the form.search() method to complete without
        # exceptions being raised
        mock_esp_search.return_value = [Response({})]

        # by default, the form has no internal Elasticsearch object
        self.assertEqual(self.form.es, None)

        # on search(), ElasticsearchProcessor() should be initialized with
        # a None Elasticsearch object (the form's)
        self.form.search()
        mock_esp_init.assert_called_with(None)
        mock_esp_init.reset()

        # here, we're setting the form's internal Elasticsearch
        # object, so the above tests should have the opposite
        # result
        form = BlogPostSearchForm(es=Elasticsearch(['127.0.0.2:9201']))
        self.assertIsInstance(form.es, Elasticsearch)
        self.assertEqual(form.es.transport.hosts[0]['host'], '127.0.0.2')
        self.assertEqual(form.es.transport.hosts[0]['port'], 9201)

        form.search()
        mock_esp_init.assert_called_with(form.es)
예제 #6
0
    def test__form_search(self, mock_esp_add_search, mock_esp_search):
        mock_esp_search.return_value = [Response({})]
        self.form.search()
        mock_esp_add_search.assert_called_with(self.form, 1, 20)
        mock_esp_add_search.reset()

        self.form.search(5, 50)
        mock_esp_add_search.assert_called_with(self.form, 5, 50)
def test_render_contributions():
    hits = Response({
        'hits': {
            'hits': [
                {
                    '_type': 'hep',
                    '_source': {
                        'citation_count':
                        1,
                        'control_number':
                        1,
                        'publication_info': [
                            {
                                'journal_title': 'first-journal_title'
                            },
                        ],
                        'titles': [
                            {
                                'title': 'first-title'
                            },
                        ],
                    },
                },
                {
                    '_type': 'hep',
                    '_source': {
                        'control_number': 2,
                        'titles': [
                            {
                                'title': 'second-title'
                            },
                        ],
                    },
                },
            ],
            'total':
            2,
        },
    }).hits

    expected = ([
        [
            "<a href='/literature/1'>first-title</a>",
            u'\n\n',
            'first-journal_title',
            1,
        ],
        [
            "<a href='/literature/2'>second-title</a>",
            u'\n\n',
            '',
            0,
        ],
    ], 2)
    result = render_contributions(hits)

    assert expected == result
예제 #8
0
    def test_nonexistent_exclusion(self):
        """Test proper handling of nonexistent key in exclusion list"""

        expectation = {"include": "the good stuff", "exclude": "the bad stuff"}

        response = Response(dummy_response())
        instance = response[0]

        ser = ReadOnlyElasticSerializer(instance)
        ser.Meta.exclude = ['bad_key']

        self.assertDictEqual(ser.data, expectation)
예제 #9
0
    def test_exclusion(self):
        """Test excluding key from serialized data"""

        expectation = {'include': 'the good stuff'}

        response = Response(dummy_response())
        instance = response[0]

        ser = ReadOnlyElasticSerializer(instance)
        ser.Meta.exclude = ['exclude']

        self.assertEqual(ser.data, expectation)
def test_render_conferences():
    hits = Response({
        'hits': {
            'hits': [
                {
                    '_type': 'conferences',
                    '_source': {
                        'address': [
                            {
                                'original_address': 'original_address'
                            },
                        ],
                        'control_number': 1,
                        'titles': [
                            {
                                'title': 'title'
                            },
                        ],
                    },
                },
                {
                    '_type': 'conferences',
                    '_source': {
                        'control_number': 2,
                    },
                },
            ],
            'total':
            2,
        },
    }).hits

    expected = ([
        [
            '<a href="/conferences/1">title</a>',
            'original_address',
            '',
            u'  ',
        ],
    ], 1)
    with current_app.test_request_context():
        result = render_conferences(2, hits)

    assert expected == result
def test_formdata_to_model_normalizes_journal_title(e, ui, u):
    e.return_value = Response({
        'hits': {
            'hits': [
                {
                    '_index': 'records-hep',
                    '_type': 'hep',
                    '_id': '42',
                    '_source': {
                        'short_titles': [
                            {
                                'title': 'quux'
                            },
                        ],
                    },
                },
            ],
        },
    })

    data = {}
    extra_data = {}
    formdata = {
        'type_of_doc': 'foo',
        'title': [
            'bar',
        ],
        'journal_title': 'baz',
    }

    obj = StubObj(data, extra_data)

    expected = [
        {
            'journal_title': 'quux'
        },
    ]
    result = formdata_to_model(obj, formdata)

    assert expected == result['publication_info']
def test_render_conferences_handles_unicode():
    hits = Response({
        'hits': {
            'hits': [
                {
                    '_type': 'conference',
                    '_source': {
                        'address': [
                            {
                                'original_address': 'Paris, France'
                            },
                        ],
                        'control_number': 1351301,
                        'titles': [
                            {
                                'title': u'Théorie de Cordes en France'
                            },
                        ],
                    },
                },
            ],
            'total':
            1,
        },
    }).hits

    expected = ([
        [
            u'<a href="/conferences/1351301">Théorie de Cordes en France</a>',
            'Paris, France',
            '',
            u'  ',
        ],
    ], 1)
    with current_app.test_request_context():
        result = render_conferences(1, hits)

    assert expected == result
예제 #13
0
    def test_should_format_image_question(self):
        form_model = MagicMock(spec=FormModel)
        form_model.is_media_type_fields_present = True
        form_model.media_fields = [PhotoField("photo", "img", "img")]
        required_field_names = [
            'some_question', 'ds_id', 'ds_name', 'form_model_id_img'
        ]
        hits = AttrList([
            Result({
                '_type': "form_model_id",
                '_id': 'index_id',
                '_source': {
                    'ds_id': 'his_id',
                    'ds_name': 'his_name',
                    'form_model_id_q1': 'sub_last_name',
                    'form_model_id_img': 'img2.png',
                    'some_question': 'answer for it'
                }
            })
        ])
        hits.total = 1
        results = Response({"_hits": hits})
        form_model.id = 'form_model_id'
        local_time_delta = ('+', 2, 0)

        submissions = SubmissionQueryResponseCreator(
            form_model,
            local_time_delta).create_response(required_field_names, results,
                                              {})

        expected = ([[
            'index_id', 'answer for it',
            ["his_name<span class='small_grey'>  his_id</span>"],
            '<img src=\'/download/attachment/index_id/preview_img2.png\' alt=\'\'/>  <a href=\'/download/attachment/index_id/img2.png\'>img2.png</a>',
            0
        ]], 1)
        self.assertEqual(submissions, expected)
예제 #14
0
    def test_should_give_back_entries_according_to_header_order(self):
        form_model = MagicMock(spec=FormModel)
        required_field_names = [
            'some_question', 'ds_id', 'ds_name', 'form_model_id_q1',
            'form_model_id_q1_unique_code'
        ]
        hits = AttrList([
            Result({
                '_type': "form_model_id",
                '_id': 'index_id',
                '_source': {
                    'ds_id': 'his_id',
                    'ds_name': 'his_name',
                    'form_model_id_q1_unique_code': 'subject_id',
                    'form_model_id_q1': 'sub_last_name',
                    'some_question': 'answer for it'
                }
            })
        ])
        hits.total = 1
        results = Response({"_hits": hits})
        form_model.entity_questions = [
            UniqueIdField('Test subject', 'name', 'q1', 'which subject')
        ]
        form_model.id = 'form_model_id'
        local_time_delta = ('+', 2, 0)
        submissions = SubmissionQueryResponseCreator(
            form_model,
            local_time_delta).create_response(required_field_names, results,
                                              {})

        expected = ([[
            'index_id', 'answer for it',
            ["his_name<span class='small_grey'>  his_id</span>"],
            ["sub_last_name<span class='small_grey'>  subject_id</span>"], 0
        ]], 1)
        self.assertEqual(submissions, expected)
예제 #15
0
 def execute(self):
     return Response(self._query(self.to_dict()),
                     callbacks=self._doc_type_map)