def test_prioritizing_autocomplete_and_keyword_on_badge_and_historic_badges(self): OfficerInfoDocType( full_name='Badge-Matched Guy', badge='00123', badge_keyword='00123', ).save() OfficerInfoDocType( full_name='Historic-Badge-Matched Guy', historic_badges=['00123'], historic_badges_keyword=['00123'] ).save() OfficerInfoDocType( full_name='Partial Badge-Matched Guy', badge='100123', badge_keyword='100123' ).save() OfficerInfoDocType( full_name='Partial Historic-Badge-Matched Guy', historic_badges=['100123'], historic_badges_keyword=['100123'] ).save() OfficerInfoDocType( full_name='Unmatched Guy', badge='12345', badge_keyword='12345' ).save() self.refresh_index() response = OfficerWorker().search('00123') expect(response.hits.total).to.equal(4) expect(response.hits.hits[0]['_source']['full_name']).to.eq('Badge-Matched Guy') expect(response.hits.hits[1]['_source']['full_name']).to.eq('Historic-Badge-Matched Guy') expect(response.hits.hits[2]['_source']['full_name']).to.eq('Partial Badge-Matched Guy') expect(response.hits.hits[3]['_source']['full_name']).to.eq('Partial Historic-Badge-Matched Guy')
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_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'] }] })
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'])
def test_search_with_date(self): OfficerInfoDocType(meta={ 'id': '1' }, full_name='full name', badge='123', url='url').save() CrDocType(meta={ 'id': '1' }, crid='1234', incident_date='2017-12-27').save() self.refresh_index() workers = { 'OFFICER': OfficerWorker(), 'DATE > CR': DateCRWorker(), } response = SearchManager(workers=workers).search('fu na 2017-12-27') expect(response).to.eq({ 'OFFICER': [{ 'id': '1', 'url': 'url', 'badge': '123', 'full_name': u'full name' }], 'DATE > CR': [{ 'id': '1', 'crid': '1234', 'incident_date': '2017-12-27' }] })
def get_officers(self, names): results = [] seen_ids = [] for (source, name) in names: query = OfficerInfoDocType().search().query( 'function_score', query={ 'match': { 'full_name': { 'query': name, 'operator': 'and' } } }, script_score={ 'script': { 'lang': 'painless', 'inline': '_score + doc[\'allegation_count\'].value * 3' } }) search_result = query[:1].execute() results += [(source, OfficerSerializer(obj).data) for obj in search_result if obj['id'] not in seen_ids] seen_ids += [obj['id'] for obj in search_result] return results
def test_search_officer_badge(self): OfficerInfoDocType(full_name='John Doe', badge='100123').save() self.refresh_index() response = OfficerWorker().search('100') expect(response.hits.total).to.equal(1) expect(response.hits.hits[0]['_source']['full_name']).to.eq('John Doe')
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')
def test_search_without_spaces(self): OfficerInfoDocType(meta={ 'id': '1' }, full_name='Kevin Mc Donald', badge='123', url='url').save() OfficerInfoDocType(meta={ 'id': '2' }, full_name='John Mcdonald', badge='123', url='url').save() self.refresh_index() response = SearchManager().search('McDonald') response1 = SearchManager().search('Mc Donald') expect(response['OFFICER']).to.eq([{ 'id': '1', 'url': 'url', 'badge': '123', 'full_name': u'Kevin Mc Donald' }, { 'id': '2', 'url': 'url', 'badge': '123', 'full_name': u'John Mcdonald' }]) expect(response1['OFFICER']).to.eq([{ 'id': '1', 'url': 'url', 'badge': '123', 'full_name': u'Kevin Mc Donald' }, { 'id': '2', 'url': 'url', 'badge': '123', 'full_name': u'John Mcdonald' }])
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')
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')
def extract(self, tweets): results = [] for tweet in tweets: for url in tweet.urls: parsed = urlparse(url) if parsed.netloc == self.site_netloc: matches = re.match(r'^/officer/(\d+)', parsed.path) try: officer_id = matches.group(1) officer = OfficerInfoDocType().get(officer_id) results.append( ('cpdb-url', OfficerSerializer(officer).data)) except (AttributeError, ObjectDoesNotExist, TransportError): continue return results
def test_get_top_officers(self): OfficerInfoDocType( id='1', full_name='Daryl Smith', has_visual_token=True, complaint_percentile=99.345, ).save() OfficerInfoDocType( id='2', full_name='Officer highest percentile', has_visual_token=True, complaint_percentile=99.5, ).save() OfficerInfoDocType( id='3', full_name='Officer low percentile', has_visual_token=True, complaint_percentile=96.345, ).save() OfficerInfoDocType( id='4', full_name='Officer no visual token', has_visual_token=False, complaint_percentile=99.88, ).save() OfficerInfoDocType( id='5', full_name='Officer filter out', has_visual_token=True, complaint_percentile=99.2, ).save() OfficerInfoDocType( id='6', full_name='Officer no allegation_percentile', has_visual_token=True, complaint_percentile=None, ).save() officers_index_alias.read_index.refresh() results = OfficerInfoDocType.get_top_officers(size=2) expect(results).to.have.length(2) expect(results[0].id).to.eq('2') expect(results[1].id).to.eq('1')
def test_search(self): OfficerInfoDocType(meta={ 'id': '1' }, full_name='full name', badge='123', url='url').save() self.refresh_index() response = SearchManager().search('fu na') expect(response).to.eq({ 'UNIT': [], 'NEIGHBORHOOD': [], 'OFFICER': [{ 'id': '1', 'url': 'url', 'badge': '123', 'full_name': u'full name' }], 'COMMUNITY': [] })
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'})
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'])