def test_percentile_with_custom_key(self): object1 = create_object({ 'id': 1, 'value': 0.1, 'metric_custom_value': 0.1 }) object2 = create_object({ 'id': 2, 'value': 0.2, 'metric_custom_value': 0.1 }) object3 = create_object({ 'id': 3, 'value': 0.4, 'metric_custom_value': 0.3 }) object4 = create_object({ 'id': 4, 'value': 0.4, 'metric_custom_value': 0.4 }) data = [object1, object2, object3, object4] results = percentile(data, percentile_type='custom_value') expect(results).to.have.length(4) expect(object1.percentile_custom_value).to.eq(0) expect(object2.percentile_custom_value).to.eq(0) expect(object3.percentile_custom_value).to.eq(50) expect(object4.percentile_custom_value).to.eq(75)
def test_percentile_with_missing_field(self): object1 = create_object({'id': 1, 'value': 0.1}) object2 = create_object({'id': 2, 'value': 0.2}) object3 = create_object({'id': 3, 'value': 0.4}) data = [object1, object2, object3] results = percentile(data, percentile_type='custom_value') expect(results).to.have.length(3) expect(hasattr(object1, 'percentile_custom_value')).to.be.false() expect(hasattr(object2, 'percentile_custom_value')).to.be.false() expect(hasattr(object3, 'percentile_custom_value')).to.be.false()
def test_percentile(self): object1 = create_object({'id': 1, 'metric_value': 0.1}) object2 = create_object({'id': 2, 'metric_value': 0.2}) object3 = create_object({'id': 3, 'metric_value': 0.4}) object4 = create_object({'id': 4, 'metric_value': 0.5}) data = [object2, object4, object3, object1] results = percentile(data, percentile_type='value') expect(results).to.have.length(4) expect(object1.percentile_value).to.eq(0) expect(object2.percentile_value).to.eq(25) expect(object3.percentile_value).to.eq(50) expect(object4.percentile_value).to.eq(75)
def test_serialization_remove_None_attr(self): class ObjectSerializer(NoNullSerializer): id = serializers.IntegerField() name = serializers.CharField() objects = [ create_object({ 'id': 1, 'name': 'Alex', 'value': 3 }), create_object({ 'id': 2, 'name': None, 'value': 4 }) ] data = ObjectSerializer(objects, many=True).data expect(data).to.eq([{'id': 1, 'name': 'Alex'}, {'id': 2}])
def test_doc_format(self): cr = Mock(crid='123', incident_date='2013-04-13', category='Domestic') expect(CRFormatter().doc_format(cr)).to.be.eq({ 'crid': '123', 'incident_date': '2013-04-13', 'category': 'Domestic' }) no_category_cr = create_object({ 'crid': '123', 'incident_date': '2013-04-13' }) expect(CRFormatter().doc_format(no_category_cr)).to.be.eq({ 'crid': '123', 'incident_date': '2013-04-13', 'category': 'Unknown' })
class OfficerCacheManagerTestCase(TestCase): def test_allegation_count(self): officer_1 = OfficerFactory() officer_2 = OfficerFactory() OfficerAllegationFactory.create_batch(2, final_finding='NS', officer=officer_1) OfficerAllegationFactory(final_finding='SU', officer=officer_1) officer_cache_manager.build_cached_columns() officer_1.refresh_from_db() officer_2.refresh_from_db() expect(officer_1.allegation_count).to.eq(3) expect(officer_2.allegation_count).to.eq(0) def test_sustained_count(self): officer_1 = OfficerFactory() officer_2 = OfficerFactory() OfficerAllegationFactory.create_batch(2, final_finding='SU', officer=officer_1) OfficerAllegationFactory(final_finding='NS', officer=officer_1) OfficerAllegationFactory(final_finding='NS', officer=officer_2) officer_cache_manager.build_cached_columns() officer_1.refresh_from_db() officer_2.refresh_from_db() expect(officer_1.sustained_count).to.eq(2) expect(officer_2.sustained_count).to.eq(0) def test_unsustained_count(self): officer_1 = OfficerFactory() officer_2 = OfficerFactory() OfficerAllegationFactory.create_batch(2, final_finding='NS', officer=officer_1) OfficerAllegationFactory(final_finding='SU', officer=officer_1) OfficerAllegationFactory(final_finding='SU', officer=officer_2) officer_cache_manager.build_cached_columns() officer_1.refresh_from_db() officer_2.refresh_from_db() expect(officer_1.unsustained_count).to.eq(2) expect(officer_2.unsustained_count).to.eq(0) def test_discipline_count(self): officer_1 = OfficerFactory() officer_2 = OfficerFactory() OfficerAllegationFactory.create_batch(2, officer=officer_1, disciplined=True) OfficerAllegationFactory(officer=officer_1, disciplined=False) OfficerAllegationFactory(officer=officer_2, disciplined=False) officer_cache_manager.build_cached_columns() officer_1.refresh_from_db() officer_2.refresh_from_db() expect(officer_1.discipline_count).to.eq(2) expect(officer_2.discipline_count).to.eq(0) def test_honorable_mention_count(self): officer_1 = OfficerFactory() officer_2 = OfficerFactory() AwardFactory(officer=officer_1, award_type='Other') AwardFactory(officer=officer_1, award_type='Complimentary Letter') AwardFactory(officer=officer_1, award_type='Complimentary Letter') AwardFactory(officer=officer_1, award_type='Honorable Mention') AwardFactory(officer=officer_1, award_type='ABC Honorable Mention') AwardFactory(officer=officer_2, award_type='Complimentary Letter') officer_cache_manager.build_cached_columns() officer_1.refresh_from_db() officer_2.refresh_from_db() expect(officer_1.honorable_mention_count).to.eq(2) expect(officer_2.honorable_mention_count).to.eq(0) def test_civilian_compliment_count(self): officer_1 = OfficerFactory() officer_2 = OfficerFactory() AwardFactory(officer=officer_1, award_type='Other') AwardFactory(officer=officer_1, award_type='Complimentary Letter') AwardFactory(officer=officer_1, award_type='Complimentary Letter') AwardFactory(officer=officer_1, award_type='Honorable Mention') AwardFactory(officer=officer_1, award_type='ABC Honorable Mention') AwardFactory(officer=officer_2, award_type='Honorable Mention') officer_cache_manager.build_cached_columns() officer_1.refresh_from_db() officer_2.refresh_from_db() expect(officer_1.civilian_compliment_count).to.eq(2) expect(officer_2.civilian_compliment_count).to.eq(0) def test_major_award_count(self): officer_1 = OfficerFactory() officer_2 = OfficerFactory() AwardFactory(officer=officer_1, award_type='HONORED POLICE STAR') AwardFactory(officer=officer_1, award_type='POLICE MEDAL') AwardFactory(officer=officer_1, award_type='PIPE BAND AWARD') AwardFactory(officer=officer_1, award_type='LIFE SAVING AWARD') AwardFactory(officer=officer_2, award_type='LIFE SAVING AWARD') officer_cache_manager.build_cached_columns() officer_1.refresh_from_db() officer_2.refresh_from_db() expect(officer_1.major_award_count).to.eq(2) expect(officer_2.major_award_count).to.eq(0) def test_trr_count(self): officer_1 = OfficerFactory() officer_2 = OfficerFactory() TRRFactory.create_batch(2, officer=officer_1) officer_cache_manager.build_cached_columns() officer_1.refresh_from_db() officer_2.refresh_from_db() expect(officer_1.trr_count).to.eq(2) expect(officer_2.trr_count).to.eq(0) def test_current_badge(self): officer_1 = OfficerFactory() officer_2 = OfficerFactory() OfficerBadgeNumberFactory(officer=officer_1, star='123', current=True) OfficerBadgeNumberFactory(officer=officer_2, current=False) officer_cache_manager.build_cached_columns() officer_1.refresh_from_db() expect(officer_1.current_badge).to.eq('123') expect(officer_2.current_badge).to.eq(None) def test_last_unit(self): officer = OfficerFactory() expect(officer.last_unit).to.equal(None) last_unit = PoliceUnitFactory(unit_name='BDCH') OfficerHistoryFactory(officer=officer, unit=PoliceUnitFactory(unit_name='CAND'), end_date=date(2000, 1, 1)) OfficerHistoryFactory(officer=officer, unit=last_unit, end_date=date(2002, 1, 1)) officer_cache_manager.build_cached_columns() officer.refresh_from_db() expect(officer.last_unit).to.eq(last_unit) def test_current_salary(self): officer_1 = OfficerFactory() officer_2 = OfficerFactory() expect(officer_1.current_salary).to.be.none() SalaryFactory(officer=officer_1, year=2010, salary=5000) SalaryFactory(officer=officer_1, year=2012, salary=10000) SalaryFactory(officer=officer_1, year=2015, salary=15000) SalaryFactory(officer=officer_1, year=2017, salary=20000) officer_cache_manager.build_cached_columns() officer_1.refresh_from_db() officer_2.refresh_from_db() expect(officer_1.current_salary).to.eq(20000) expect(officer_2.current_salary).to.be.none() def test_has_unique_name(self): officer_1 = OfficerFactory(first_name='Jerome', last_name='Finnigan') officer_2 = OfficerFactory(first_name='Jerome', last_name='Finnigan') officer_3 = OfficerFactory(first_name='German', last_name='Finnigan') officer_4 = OfficerFactory(first_name='Jerome', last_name='Piwinicki') officer_5 = OfficerFactory(first_name='German', last_name='Piwinicki') expect(officer_1.has_unique_name).to.be.false() expect(officer_2.has_unique_name).to.be.false() expect(officer_3.has_unique_name).to.be.false() expect(officer_4.has_unique_name).to.be.false() expect(officer_5.has_unique_name).to.be.false() officer_cache_manager.build_cached_columns() officer_1.refresh_from_db() officer_2.refresh_from_db() officer_3.refresh_from_db() officer_4.refresh_from_db() officer_5.refresh_from_db() expect(officer_1.has_unique_name).to.be.false() expect(officer_2.has_unique_name).to.be.false() expect(officer_3.has_unique_name).to.be.true() expect(officer_4.has_unique_name).to.be.true() expect(officer_5.has_unique_name).to.be.true() @patch( 'data.cache_managers.officer_cache_manager.officer_percentile.latest_year_percentile', Mock(return_value=[ create_object({ 'officer_id': 2, 'year': 2014, 'percentile_allegation': '66.6667', 'percentile_trr': '0.0000', 'percentile_honorable_mention': 66.6667 }), create_object({ 'officer_id': 1, 'year': 2017, 'percentile_allegation_civilian': '66.6667', 'percentile_allegation_internal': '66.6667', 'percentile_trr': '33.3333', 'percentile_honorable_mention': 33.3333, }), create_object({ 'officer_id': 3, 'year': 2017, 'percentile_allegation': '0.0000', 'percentile_allegation_civilian': '0.0000', 'percentile_allegation_internal': '0.0000', 'percentile_honorable_mention': 0.0 }), create_object({ 'officer_id': 4, 'year': 2017, 'percentile_allegation': '0.0000', 'percentile_allegation_civilian': '0.0000', 'percentile_allegation_internal': '0.0000', 'percentile_trr': '66.6667', }) ]) ) def test_build_cached_percentiles(self): OfficerFactory(id=1, appointed_date=date.today() - timedelta(days=60), resignation_date=None) OfficerFactory(id=2, appointed_date=date(1980, 1, 1), resignation_date=None) OfficerFactory(id=3, appointed_date=date(1980, 1, 1), resignation_date=None) OfficerFactory(id=4, appointed_date=date(1980, 1, 1), resignation_date=None) officer_cache_manager.build_cached_percentiles() officer_1 = Officer.objects.get(id=1) officer_2 = Officer.objects.get(id=2) officer_3 = Officer.objects.get(id=3) officer_4 = Officer.objects.get(id=4) expect(officer_1.complaint_percentile).to.be.none() expect(officer_1.civilian_allegation_percentile).to.eq(Decimal('66.6667')) expect(officer_1.internal_allegation_percentile).to.eq(Decimal('66.6667')) expect(officer_1.trr_percentile).to.eq(Decimal('33.3333')) expect(officer_1.honorable_mention_percentile).to.eq(Decimal('33.3333')) expect(officer_2.complaint_percentile).to.eq(Decimal('66.6667')) expect(officer_2.civilian_allegation_percentile).to.be.none() expect(officer_2.internal_allegation_percentile).to.be.none() expect(officer_2.trr_percentile).to.eq(Decimal('0.0')) expect(officer_2.honorable_mention_percentile).to.eq(Decimal('66.6667')) expect(officer_3.complaint_percentile).to.eq(Decimal('0.0000')) expect(officer_3.civilian_allegation_percentile).to.eq(Decimal('0.0000')) expect(officer_3.internal_allegation_percentile).to.eq(Decimal('0.0000')) expect(officer_3.trr_percentile).to.be.none() expect(officer_3.honorable_mention_percentile).to.eq(Decimal('0.0000')) expect(officer_4.complaint_percentile).to.eq(Decimal('0.0000')) expect(officer_4.civilian_allegation_percentile).to.eq(Decimal('0.0000')) expect(officer_4.internal_allegation_percentile).to.eq(Decimal('0.0000')) expect(officer_4.trr_percentile).to.eq(Decimal('66.6667')) expect(officer_4.honorable_mention_percentile).to.be.none() @override_settings( ALLEGATION_MIN='1988-01-01', ALLEGATION_MAX='2016-07-01', INTERNAL_CIVILIAN_ALLEGATION_MIN='2000-01-01', INTERNAL_CIVILIAN_ALLEGATION_MAX='2016-07-01', TRR_MIN='2004-01-08', TRR_MAX='2016-04-12') def test_build_cached_yearly_percentiles(self): officer_1 = OfficerFactory(id=1, appointed_date=date(2013, 1, 1)) officer_2 = OfficerFactory(id=2, appointed_date=date(2015, 3, 14)) officer_3 = OfficerFactory(id=3, appointed_date=date(2014, 3, 1), resignation_date=date(2015, 4, 14)) OfficerAllegationFactory( officer=officer_1, allegation__incident_date=datetime(2015, 1, 1, tzinfo=pytz.utc), start_date=datetime(2015, 1, 1), allegation__is_officer_complaint=False) OfficerAllegationFactory( officer=officer_1, start_date=date(2015, 1, 1), allegation__incident_date=datetime(2015, 1, 1, tzinfo=pytz.utc), allegation__is_officer_complaint=False) OfficerAllegationFactory( officer=officer_1, start_date=date(2016, 1, 22), allegation__incident_date=datetime(2016, 1, 1, tzinfo=pytz.utc), allegation__is_officer_complaint=False) OfficerAllegationFactory.create_batch( 2, officer=officer_2, start_date=date(2017, 10, 19), allegation__incident_date=datetime(2016, 1, 16, tzinfo=pytz.utc), allegation__is_officer_complaint=False ) OfficerAllegationFactory( officer=officer_2, start_date=date(2017, 10, 19), allegation__incident_date=datetime(2016, 3, 15, tzinfo=pytz.utc), allegation__is_officer_complaint=True ) OfficerAllegationFactory( officer=officer_2, start_date=date(2017, 10, 19), allegation__incident_date=datetime(2017, 3, 15, tzinfo=pytz.utc), allegation__is_officer_complaint=True ) TRRFactory( officer=officer_1, trr_datetime=datetime(2017, 3, 15, tzinfo=pytz.utc), ) TRRFactory( officer=officer_1, trr_datetime=datetime(2016, 3, 15, tzinfo=pytz.utc), ) officer_cache_manager.build_cached_yearly_percentiles() expected_officer_yearly_percentiles = { officer_1.id: { 2014: { 'percentile_allegation': Decimal(0.0), 'percentile_allegation_civilian': Decimal(0.0), 'percentile_allegation_internal': Decimal(0.0), 'percentile_trr': Decimal(0.0), }, 2015: { 'percentile_allegation': Decimal(50.0), 'percentile_allegation_civilian': Decimal(50.0), 'percentile_allegation_internal': Decimal(0.0), 'percentile_trr': Decimal(0.0), }, 2016: { 'percentile_allegation': Decimal(33.3333), 'percentile_allegation_civilian': Decimal(33.3333), 'percentile_allegation_internal': Decimal(0.0), 'percentile_trr': Decimal(66.6667), } }, officer_2.id: { 2016: { 'percentile_allegation': Decimal(66.6667), 'percentile_allegation_civilian': Decimal(66.6667), 'percentile_allegation_internal': Decimal(66.6667), 'percentile_trr': Decimal(0.0), } }, officer_3.id: { 2015: { 'percentile_allegation': Decimal(0.0), 'percentile_allegation_civilian': Decimal(0.0), 'percentile_allegation_internal': Decimal(0.0), 'percentile_trr': Decimal(0.0), } } } for officer_id, expected_yearly_percentiles in expected_officer_yearly_percentiles.items(): yearly_percentiles = OfficerYearlyPercentile.objects.filter(officer_id=officer_id) expect(yearly_percentiles.count()).to.eq(len(expected_yearly_percentiles.keys())) for year, expected_percentile in expected_yearly_percentiles.items(): percentile = yearly_percentiles.get(year=year) for attr, value in expected_percentile.items(): expect(f'{getattr(percentile, attr):.2f}').to.eq(f'{value:.2f}')
def test_lastmod(self): dummy_obj = create_object({'id': 1, 'updated_at': datetime(2018, 4, 4, 12, 0, 1, tzinfo=pytz.utc)}) expect(DummySitemap().lastmod(dummy_obj)).to.eq(datetime(2018, 4, 4, 12, 0, 1, tzinfo=pytz.utc))
def test_search_all_with_custom_syntaxes(self, DocumentCloudMock): DocumentCloudSearchQueryFactory(types=['CR', 'CPB', 'DOCUMENT'], query='CRID !AR') DocumentCloudSearchQueryFactory(types=['TRR'], query='') allegation = AllegationFactory(crid='100000') document_1 = create_object({ 'id': '1-CRID-100000-CR', 'title': 'CRID-100000-CR', 'description': AttachmentSourceType.DOCUMENTCLOUD, 'canonical_url': 'https://www.documentcloud.org/documents/1-CRID-100000-CR.html', }) document_2 = create_object({ 'id': '2-CRID-100000-CR', 'title': 'CRID-100000 CR', 'description': AttachmentSourceType.DOCUMENTCLOUD, 'canonical_url': 'https://www.documentcloud.org/documents/2-CRID-100000-CR.html', }) search_mock = DocumentCloudMock().documents.search search_mock.return_value = [document_1, document_2] custom_search_syntaxes = [ (['CR'], 'title:"CRID 1062978 CR Summary"'), ] documents = search_all(custom_search_syntaxes=custom_search_syntaxes) expect(search_mock.call_args[0][0]).to.eq( 'title:"CRID 1062978 CR Summary"') expectation_dict = { '1-CRID-100000-CR': { 'source_type': AttachmentSourceType.DOCUMENTCLOUD, 'documentcloud_id': '1', 'url': 'https://www.documentcloud.org/documents/1-CRID-100000-CR.html', 'document_type': 'CR', 'allegation': allegation }, '2-CRID-100000-CR': { 'source_type': AttachmentSourceType.DOCUMENTCLOUD, 'documentcloud_id': '2', 'url': 'https://www.documentcloud.org/documents/2-CRID-100000-CR.html', 'document_type': 'CR', 'allegation': allegation }, } expect(documents).to.have.length(2) print([document.id for document in documents]) for document in documents: expect(document.id in expectation_dict).to.be.true() expectation = expectation_dict[document.id] for field, value in expectation.items(): expect(getattr(document, field, None)).to.eq(value)
def test_search_all(self, DocumentCloudMock): DocumentCloudSearchQueryFactory(types=['CR', 'CPB', 'DOCUMENT'], query='CRID !AR') DocumentCloudSearchQueryFactory(types=['TRR'], query='') allegation = AllegationFactory(crid='100000') c_prefix_allegation = AllegationFactory(crid='C200000') AttachmentFileFactory( source_type=AttachmentSourceType.PORTAL_COPA_DOCUMENTCLOUD, external_id='1', allegation=allegation) AttachmentFileFactory( source_type=AttachmentSourceType.PORTAL_COPA_DOCUMENTCLOUD, external_id='2', allegation=allegation) AttachmentFileFactory(source_type=AttachmentSourceType. SUMMARY_REPORTS_COPA_DOCUMENTCLOUD, external_id='7', allegation=allegation) AttachmentFileFactory(source_type=AttachmentSourceType.DOCUMENTCLOUD, external_id='4', allegation=allegation) AttachmentFileFactory(source_type=AttachmentSourceType.PORTAL_COPA, external_id='5', allegation=allegation) AttachmentFileFactory( source_type=AttachmentSourceType.PORTAL_COPA_DOCUMENTCLOUD, external_id='6', allegation=allegation) AttachmentFileFactory( source_type=AttachmentSourceType.PORTAL_COPA, external_id='8', allegation=allegation, pending_documentcloud_id='123456', ) AttachmentFileFactory( source_type=AttachmentSourceType.SUMMARY_REPORTS_COPA, external_id='9', allegation=allegation, pending_documentcloud_id='456789', ) copa_document_no_crid = create_object({ 'id': '1-CRID-CR', 'description': AttachmentSourceType.PORTAL_COPA_DOCUMENTCLOUD, 'canonical_url': 'https://www.documentcloud.org/documents/1-CRID-CR.html', 'title': 'no crid' }) copa_document = create_object({ 'id': '2-CRID-100000-CR', 'title': 'CRID-100000 CR', 'description': AttachmentSourceType.PORTAL_COPA_DOCUMENTCLOUD, 'canonical_url': 'https://www.documentcloud.org/documents/2-CRID-100000-CR.html', }) should_be_filtered_copa_document = create_object({ 'id': '3-CRID-100000-CR', 'title': 'CRID-100000 CR', 'description': AttachmentSourceType.PORTAL_COPA_DOCUMENTCLOUD, 'canonical_url': 'https://www.documentcloud.org/documents/3-CRID-100000-CR.html', }) cloud_document = create_object({ 'id': '4-CRID-100000-CR', 'title': 'CRID-100000 CR', 'description': 'some description', 'canonical_url': 'https://www.documentcloud.org/documents/4-CRID-100000-CR.html', }) new_cloud_document = create_object({ 'id': '5-CRID-100000-CR', 'title': 'CRID-100000 CR 2', 'canonical_url': 'https://www.documentcloud.org/documents/5-CRID-100000-CR.html', }) duplicated_cloud_document = create_object({ 'id': '9999-CRID-456-CR', 'title': 'CRID-100000 CR', 'canonical_url': 'https://www.documentcloud.org/documents/999-CRID-100000-CR.html', }) summary_reports_copa_document = create_object({ 'id': '7-CRID-100000-CR', 'title': 'CRID-100000 CR 7', 'description': AttachmentSourceType.SUMMARY_REPORTS_COPA_DOCUMENTCLOUD, 'canonical_url': 'https://www.documentcloud.org/documents/7-CRID-100000-CR.html', }) summary_reports_copa_document_pending = create_object({ 'id': '456789-CRID-100000-CR', 'title': 'CRID-100000 CR 456789', 'description': AttachmentSourceType.SUMMARY_REPORTS_COPA_DOCUMENTCLOUD, 'canonical_url': 'https://www.documentcloud.org/documents/456789-CRID-100000-CR.html', }) new_cloud_document_without_c_prefix = create_object({ 'id': '8-CRID-200000-CR', 'title': 'CRID-200000 CR 2', 'canonical_url': 'https://www.documentcloud.org/documents/8-CRID-200000-CR.html', }) new_cloud_document_with_c_prefix = create_object({ 'id': '9-CRID-C100000-CR', 'title': 'CRID-C100000 CR', 'canonical_url': 'https://www.documentcloud.org/documents/9-CRID-C100000-CR.html', }) new_document_type_cloud_document = create_object({ 'id': '10-CRID-100000-DOCUMENT', 'title': 'CRID-100000 DOCUMENT', 'description': AttachmentSourceType.DOCUMENTCLOUD, 'canonical_url': 'https://www.documentcloud.org/documents/10-CRID-100000-DOCUMENT.html', }) search_mock = DocumentCloudMock().documents.search search_mock.return_value = [ copa_document_no_crid, copa_document, should_be_filtered_copa_document, duplicated_cloud_document, cloud_document, new_cloud_document, summary_reports_copa_document, summary_reports_copa_document_pending, new_cloud_document_without_c_prefix, new_cloud_document_with_c_prefix, new_document_type_cloud_document ] documents = search_all() expect(search_mock.call_args[0][0]).to.eq('CRID !AR') expectation_dict = { '1-CRID-CR': { 'source_type': AttachmentSourceType.PORTAL_COPA_DOCUMENTCLOUD, 'documentcloud_id': '1', 'url': 'https://www.documentcloud.org/documents/1-CRID-CR.html', 'document_type': None, 'allegation': None }, '2-CRID-100000-CR': { 'source_type': AttachmentSourceType.PORTAL_COPA_DOCUMENTCLOUD, 'documentcloud_id': '2', 'url': 'https://www.documentcloud.org/documents/2-CRID-100000-CR.html', 'document_type': 'CR', 'allegation': allegation }, '7-CRID-100000-CR': { 'source_type': AttachmentSourceType.SUMMARY_REPORTS_COPA_DOCUMENTCLOUD, 'documentcloud_id': '7', 'url': 'https://www.documentcloud.org/documents/7-CRID-100000-CR.html', 'document_type': 'CR', 'allegation': allegation }, '4-CRID-100000-CR': { 'source_type': AttachmentSourceType.DOCUMENTCLOUD, 'documentcloud_id': '4', 'url': 'https://www.documentcloud.org/documents/4-CRID-100000-CR.html', 'document_type': 'CR', 'allegation': allegation }, '5-CRID-100000-CR': { 'source_type': AttachmentSourceType.DOCUMENTCLOUD, 'documentcloud_id': '5', 'url': 'https://www.documentcloud.org/documents/5-CRID-100000-CR.html', 'document_type': 'CR', 'allegation': allegation }, '8-CRID-200000-CR': { 'source_type': AttachmentSourceType.DOCUMENTCLOUD, 'documentcloud_id': '8', 'url': 'https://www.documentcloud.org/documents/8-CRID-200000-CR.html', 'document_type': 'CR', 'allegation': c_prefix_allegation }, '9-CRID-C100000-CR': { 'source_type': AttachmentSourceType.DOCUMENTCLOUD, 'documentcloud_id': '9', 'url': 'https://www.documentcloud.org/documents/9-CRID-C100000-CR.html', 'document_type': 'CR', 'allegation': allegation }, '456789-CRID-100000-CR': { 'source_type': AttachmentSourceType.SUMMARY_REPORTS_COPA_DOCUMENTCLOUD, 'documentcloud_id': '456789', 'url': 'https://www.documentcloud.org/documents/456789-CRID-100000-CR.html', 'document_type': 'CR', 'allegation': allegation }, '10-CRID-100000-DOCUMENT': { 'source_type': AttachmentSourceType.DOCUMENTCLOUD, 'documentcloud_id': '10', 'url': 'https://www.documentcloud.org/documents/10-CRID-100000-DOCUMENT.html', 'document_type': 'DOCUMENT', 'allegation': allegation }, } expect(documents).to.have.length(9) for document in documents: expect(document.id in expectation_dict).to.be.true() expectation = expectation_dict[document.id] for field, value in expectation.items(): expect(getattr(document, field, None)).to.eq(value)
def test_merge_percentile(self): objects = [ create_object({ 'id': 1, 'value_a': 0.1, 'metric_value_a': 0.1 }), create_object({ 'id': 2, 'value_a': 0.2, 'metric_value_a': 0.1 }), create_object({ 'id': 3, 'value_a': 0.4, 'metric_value_a': 0.3 }), ] new_metric_query_set = Mock(exclude=Mock(return_value=[ create_object({ 'id': 4, 'value_b': 0.3, 'value_c': 0.4, 'metric_value_b': 0.3, 'metric_value_c': 0.4 }), ]), filter=Mock(return_value=Mock(in_bulk=Mock( return_value={ 2: create_object({ 'id': 2, 'metric_value_b': 0.3, 'metric_value_c': 0.4 }), 3: create_object({ 'id': 3, 'metric_value_b': 0.6, 'metric_value_c': 0.8 }), })))) results = merge_metric(objects, new_metric_query_set, ['value_b', 'value_c']) validate_object(results[0], { 'id': 1, 'value_a': 0.1, 'metric_value_a': 0.1, }) validate_object( results[1], { 'id': 2, 'value_a': 0.2, 'metric_value_a': 0.1, 'metric_value_b': 0.3, 'metric_value_c': 0.4 }) validate_object( results[2], { 'id': 3, 'value_a': 0.4, 'metric_value_a': 0.3, 'metric_value_b': 0.6, 'metric_value_c': 0.8 }) validate_object( results[3], { 'id': 4, 'value_b': 0.3, 'value_c': 0.4, 'metric_value_b': 0.3, 'metric_value_c': 0.4 })
def test_extract_datum_with_no_id_datum_dict(self): datum = create_object({'foo': 'bar'}) indexer = BaseIndexer() indexer.extract_datum = lambda a: {'foo': a.foo} expect(indexer.extract_datum_with_id(datum)).to.eq({'foo': 'bar'})