Esempio n. 1
0
    def test_suggest_sample(self):
        taglessOfficerDoc = OfficerInfoDocType(
            meta={'id': '1'},
            full_name='this should not be returned',
            badge='123',
            url='url')
        taglessOfficerDoc.save()

        officerDoc = OfficerInfoDocType(meta={'id': '2'},
                                        full_name='full name',
                                        badge='123',
                                        url='url',
                                        tags=['sample'])
        officerDoc.save()

        self.refresh_index()

        response = SearchManager(workers={
            'OFFICER': OfficerWorker(),
        }).suggest_sample()

        expect(response).to.eq({
            'OFFICER': [{
                'id': '2',
                'url': 'url',
                'badge': '123',
                'full_name': u'full name',
                'tags': ['sample']
            }]
        })
Esempio n. 2
0
    def test_search_by_officer_id(self):
        doc = OfficerInfoDocType(full_name='some dude', badge='123', meta={'_id': '456'})
        doc.save()
        doc2 = OfficerInfoDocType(full_name='another guy', badge='789', meta={'_id': '012'})
        doc2.save()

        self.refresh_index()

        response = OfficerWorker().search('456')

        expect(response.hits.total).to.be.equal(1)
        expect(response.hits.hits[0]['_source']['full_name']).to.eq('some dude')
Esempio n. 3
0
class AliasUtilsTestCase(IndexMixin, TestCase):
    allow_database_queries = True

    def setUp(self):
        super(AliasUtilsTestCase, self).setUp()
        self.officer = OfficerFactory(id=1)
        self.officer_doc = OfficerInfoDocType(meta={'id': '1'})
        self.officer_doc.save()
        self.refresh_index()

    def test_set_officer_aliases(self):
        set_aliases(OfficerInfoDocType, Officer, '1', ['alias1', 'alias2'])
        expect(OfficerInfoDocType.get('1').tags).to.eq(['alias1', 'alias2'])
        expect(list(Officer.objects.get(pk=1).tags.names())).to.eq(
            ['alias1', 'alias2'])
Esempio n. 4
0
    def test_search_prioritizing_tags(self):
        doc = OfficerInfoDocType(
            full_name='some dude', badge='123', allegation_count=10)
        doc.save()
        doc = OfficerInfoDocType(
            full_name='another guy', badge='456', allegation_count=10, tags='somersault')
        doc.save()

        self.refresh_index()

        response = OfficerWorker().search('some')

        expect(response.hits.total).to.equal(2)
        expect(response.hits.hits[0]['_source']['full_name']).to.eq('another guy')
        expect(response.hits.hits[1]['_source']['full_name']).to.eq('some dude')
Esempio n. 5
0
    def test_search_prioritizing_allegation_count(self):
        doc = OfficerInfoDocType(
            full_name='full name', badge='123', allegation_count=10)
        doc.save()
        doc = OfficerInfoDocType(
            full_name='funny naga', badge='456', allegation_count=20)
        doc.save()

        self.refresh_index()

        response = OfficerWorker().search('fu na')

        expect(response.hits.total).to.equal(2)
        expect(response.hits.hits[0]['_source']['full_name']).to.eq('funny naga')
        expect(response.hits.hits[1]['_source']['full_name']).to.eq('full name')
Esempio n. 6
0
class AliasViewSetTestCase(IndexMixin, APITestCase):
    def setUp(self):
        super(AliasViewSetTestCase, self).setUp()
        self.officer = OfficerFactory(id=1)
        self.officer_doc = OfficerInfoDocType(meta={'id': '1'})
        self.officer_doc.save()
        self.refresh_index()

        admin_user = AdminUserFactory()
        token, _ = Token.objects.get_or_create(user=admin_user)
        self.client.credentials(HTTP_AUTHORIZATION='Token ' + token.key)

    def test_update_officer_aliases(self):
        response = self.client.put(
            reverse('api-v2:alias-detail',
                    kwargs={
                        'alias_type': 'officer',
                        'pk': '1'
                    }), {'aliases': ['foo', 'bar']})

        expect(response.status_code).to.eq(status.HTTP_200_OK)
        expect(response.data).to.eq({
            'message': 'Aliases successfully updated',
            'aliases': ['foo', 'bar']
        })

    def test_update_with_invalid_aliases(self):
        response = self.client.put(
            reverse('api-v2:alias-detail',
                    kwargs={
                        'alias_type': 'officer',
                        'pk': '1'
                    }),
            {
                'aliases': [
                    'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
                ]
            })

        expect(response.status_code).to.eq(status.HTTP_400_BAD_REQUEST)
        expect(response.data).to.eq({
            'message': {
                'aliases':
                ['Ensure this field has no more than 20 characters.']
            }
        })

    def test_update_aliases_with_wrong_type(self):

        response = self.client.put(
            reverse('api-v2:alias-detail',
                    kwargs={
                        'alias_type': 'not an alias type',
                        'pk': '1'
                    }), {'aliases': ['foo', 'bar']})

        expect(response.status_code).to.eq(status.HTTP_404_NOT_FOUND)
        expect(response.data).to.eq(
            {'message': 'Cannot find type "not an alias type"'})

    def test_update_aliases_with_wrong_pk(self):

        response = self.client.put(
            reverse('api-v2:alias-detail',
                    kwargs={
                        'alias_type': 'officer',
                        'pk': '2'
                    }), {'aliases': ['foo', 'bar']})

        expect(response.status_code).to.eq(status.HTTP_404_NOT_FOUND)
        expect(response.data).to.eq(
            {'message': 'Cannot find any "officer" record with pk=2'})