def test_http_write_single_points(self, mock_request): mock_request.return_value = dici(**{ 'status_code': 200, 'headers': {} }) ( emit(limit=3, start='1970-01-01') | put(count=count()) | write('http', method='PUT', url='http://localhost:8080/store?key=test2', array=False) ).execute() expect(mock_request.call_args_list).to.eq([ mock.call('PUT', 'http://localhost:8080/store?key=test2', headers=None, json={'time': '1970-01-01T00:00:00.000Z', 'count': 1}), mock.call('PUT', 'http://localhost:8080/store?key=test2', headers=None, json={'time': '1970-01-01T00:00:01.000Z', 'count': 2}), mock.call('PUT', 'http://localhost:8080/store?key=test2', headers=None, json={'time': '1970-01-01T00:00:02.000Z', 'count': 3}), ])
def test_warns_when_nothing_to_render(self, mock_render, mock_warn): ( emit(points=[]) | timechart('gnuplot') ).execute() expect(mock_render.call_count).to.eq(0) expect(mock_warn.call_args).to.eq(mock.call('no data to render'))
def test_http_read_a_few_historical_points(self, mock_request): mock_request.return_value = dici(**{ 'status_code': 200, 'headers': { 'content-type': 'application/json' }, 'json': lambda: [ {'time': '2010-01-01T00:00:00.000Z'}, {'time': '2010-01-01T00:00:01.000Z'}, {'time': '2010-01-01T00:00:02.000Z'} ] }) results = [] ( read('http', url='http://localhost:8080/points?count=3&start=2010-01-01') | memory(results) ).execute() expect(results).to.eq([ {'time': '2010-01-01T00:00:00.000Z'}, {'time': '2010-01-01T00:00:01.000Z'}, {'time': '2010-01-01T00:00:02.000Z'} ]) expect(mock_request.call_args_list).to.eq([ mock.call('GET', 'http://localhost:8080/points?count=3&start=2010-01-01', headers=None) ])
def test_http_read_a_few_live_points(self, mock_request): mock_request.return_value = dici(**{ 'status_code': 200, 'headers': { 'content-type': 'application/json' }, 'json': lambda: [ {'time': moment.now()}, {'time': moment.now()}, {'time': moment.now()}, {'time': moment.now()}, {'time': moment.now()} ] }) results = [] ( read('http', url='http://localhost:8080/points?count=5') | memory(results) ).execute() expect(results).to.have.length(5) expect(mock_request.call_args_list).to.eq([ mock.call('GET', 'http://localhost:8080/points?count=5', headers=None) ])
def test_executing_a_flume_program_that_reads(self): with redirect(input='{"time": "2016-01-01T00:00:00.000Z"}\n') as io: cli.main(['read("stdio") | write("stdio")']) expect(io.exit).to.eq(0) expect(json.loads(io.stdout)).to.eq({ 'time': '2016-01-01T00:00:00.000Z' })
def test_implicit_sink_can_be_changed(self): with redirect() as io: cli.main(['--implicit-sink', 'write("stdio", format="csv")', 'emit(limit=1,start="2016-01-01")']) expect(io.exit).to.eq(0) expect(io.stdout).to.eq('time\r\n' + '2016-01-01T00:00:00.000Z\r\n')
def test_get_field_value_from_dici_with_dot_notation_within_index_notation(self): dictionary = dici() dictionary.foo = { 'bar': 'baz', 'fizz': 'buzz' } expect(dictionary['foo.bar']).to.eq('baz')
def test_executing_a_flume_program_that_writes(self): with redirect() as io: cli.main(['emit(limit=1, start="2016-01-01") | write("stdio")']) expect(io.exit).to.eq(0) expect(json.loads(io.stdout)).to.eq({ 'time': '2016-01-01T00:00:00.000Z' })
def test_write_uses_specified_batch_size(self, mock_bulk, mock_elastic): mock_elastic.return_value = {} mock_bulk.return_value = (None, []) ( emit(limit=3, start='2016-01-01') | write('elastic', host='bananas', port=9999, batch=2) ).execute() expect(mock_bulk.call_args_list).to.eq([ mock.call({}, [ { 'time': '2016-01-01T00:00:00.000Z', '_type': 'metric', '_index': 'metrics' }, { 'time': '2016-01-01T00:00:01.000Z', '_type': 'metric', '_index': 'metrics' } ]), mock.call({}, [ { 'time': '2016-01-01T00:00:02.000Z', '_type': 'metric', '_index': 'metrics' } ]) ])
def test_write_can_points_with_time(self, mock_bulk, mock_elastic): mock_elastic.return_value = {} mock_bulk.return_value = (None, []) ( emit(limit=3, start='2016-01-01') | put(count=count()) | write('elastic', host='bananas', port=9999, batch=3) ).execute() expect(mock_bulk.call_args_list).to.eq([ mock.call({}, [ { '_type': 'metric', '_index': 'metrics', 'time': '2016-01-01T00:00:00.000Z', 'count': 1 }, { '_type': 'metric', '_index': 'metrics', 'time': '2016-01-01T00:00:01.000Z', 'count': 2 }, { '_type': 'metric', '_index': 'metrics', 'time': '2016-01-01T00:00:02.000Z', 'count': 3 } ]) ])
def test_read_can_read_with_filter(self, mock_scan, mock_elastic): mock_elastic.return_value = mock.MagicMock() mock_scan.return_value = [ {'_source': {'foo': 'bar'}} ] mock_elastic.return_value = elastic results = [] ( read('elastic', index='foo', filter='count > 3') | memory(results) ).execute() expect(results).to.eq([{'foo': 'bar'}]) query = { 'sort': ['time'], 'query': { 'constant_score': { 'filter': { 'range': { 'count': { 'gt': 3 } } } } } } expect(mock_scan.call_args).to.eq( mock.call(mock_elastic.return_value, index='foo', preserve_order=True, query=query))
def test_read_can_handle_a_single_point_with_time(self, mock_scan, mock_elastic): mock_elastic.return_value = mock.MagicMock() mock_scan.return_value = [ { '_source': { 'time': '2016-01-01T00:00:00.000Z', 'foo': 'bar' } } ] results = [] (read('elastic') | memory(results)).execute() expect(results).to.eq([ {'time': '2016-01-01T00:00:00.000Z', 'foo': 'bar'} ]) expect(mock_scan.call_args).to.eq( mock.call(mock_elastic.return_value, index='_all', preserve_order=True, query={ 'sort': ['time'], 'query': { 'match_all': {} } }))
def test_commutativity(self): """ A ∩ B = B ∩ A """ AnB = [] BnA = [] A = [ {'time': '2010-01-01T00:00:00.000Z', 'a': 0}, {'time': '2010-01-01T00:01:00.000Z', 'a': 1}, {'time': '2010-01-01T00:02:00.000Z', 'a': 2} ] B = [ {'time': '2010-01-01T00:01:00.000Z', 'a': 1}, {'time': '2010-01-01T00:03:00.000Z', 'a': 2}, {'time': '2010-01-01T00:05:00.000Z', 'a': 3} ] ( (emit(points=A), emit(points=B)) | intersect() | memory(AnB) ).execute() ( (emit(points=B), emit(points=A)) | intersect() | memory(BnA) ).execute() expect(AnB).to.eq(BnA)
def test_can_write_nothing(self): with redirect(input='') as io: ( read('stdio') | write('stdio') ).execute() expect(io.stdout).to.eq('')
def test_can_read_issue_comments_with_parameters(self, mock_request): mock_request.return_value = dici(**{ 'status_code': 200, 'headers': [], 'json': lambda: [{'created_at': '2016-01-01T00:00:00.000Z'}] }) results = [] ( github.issues.comments('whocares', 'otherrepo', 1, oauth='FAKETOKEN', since='2016-01-01') | memory(results) ).execute() expect(results).to.eq([{'time': '2016-01-01T00:00:00.000Z'}]) expect(mock_request.call_args_list).to.eq([ mock.call('GET', 'https://api.github.com/repos/whocares/otherrepo/issues/1/comments?since=2016-01-01', headers={ 'Authorization': 'token FAKETOKEN' }) ])
def test_and_with_nested_or_query(self): query = 'foo==1 and (fizz=="buzz" or fizz=="bar")' expect(filter_to_es_query(query)).to.eq({ 'constant_score': { 'filter': { 'bool': { 'must': [ { 'term': {'foo': 1} }, { 'bool': { 'minimum_should_match': 1, 'should': [ {'term': {'fizz': 'buzz'}}, {'term': {'fizz': 'bar'}} ] } } ] } } } })
def test_reduce_every_count_with_mixed_fields(self): results = [] ( emit(limit=10, every='1s', start='2016-01-01') | reduce(count=count(), foo='bar', someid=1, every='3s') | memory(results) ).execute() expect(results).to.eq([ { 'time': '2016-01-01T00:00:00.000Z', 'count': 3, 'foo': 'bar', 'someid': 1 }, { 'time': '2016-01-01T00:00:03.000Z', 'count': 3, 'foo': 'bar', 'someid': 1 }, { 'time': '2016-01-01T00:00:06.000Z', 'count': 3, 'foo': 'bar', 'someid': 1 }, { 'time': '2016-01-01T00:00:09.000Z', 'count': 1, 'foo': 'bar', 'someid': 1 } ])
def test_reduce_by_a_nested_field(self): results = [] ( emit(points=[ {'time': '2016-01-01T00:00:00.000Z', 'author': {'name': 'joe'}}, {'time': '2016-01-01T00:01:00.000Z', 'author': {'name': 'joe'}}, {'time': '2016-01-01T00:02:00.000Z', 'author': {'name': 'bob'}}, {'time': '2016-01-01T00:03:00.000Z', 'author': {'name': 'joe'}}, {'time': '2016-01-01T00:03:00.000Z', 'author': {'name': 'bob'}}, ]) | reduce(count=count(), by=['author.name']) | memory(results) ).execute() expect(results).to.contain({ 'time': '2016-01-01T00:00:00.000Z', 'author': { 'name': 'bob' }, 'count': 2 }) expect(results).to.contain({ 'time': '2016-01-01T00:00:00.000Z', 'author': { 'name': 'joe' }, 'count': 3 })
def test_reduce_with_multiple_empty_every_interval(self): results = [] ( emit(points=[ {'time': '2016-01-01T00:00:00.000Z'}, {'time': '2016-01-01T01:00:00.000Z'}, {'time': '2016-01-01T02:00:00.000Z'}, {'time': '2016-01-01T05:00:00.000Z'}, {'time': '2016-01-01T06:00:00.000Z'}, {'time': '2016-01-01T09:00:00.000Z'}, {'time': '2016-01-01T10:00:00.000Z'} ]) | reduce(count=count(), every='1h') | memory(results) ).execute() expect(results).to.eq([ {'time': '2016-01-01T00:00:00.000Z', 'count': 1}, {'time': '2016-01-01T01:00:00.000Z', 'count': 1}, {'time': '2016-01-01T02:00:00.000Z', 'count': 1}, {'time': '2016-01-01T03:00:00.000Z', 'count': 0}, {'time': '2016-01-01T04:00:00.000Z', 'count': 0}, {'time': '2016-01-01T05:00:00.000Z', 'count': 1}, {'time': '2016-01-01T06:00:00.000Z', 'count': 1}, {'time': '2016-01-01T07:00:00.000Z', 'count': 0}, {'time': '2016-01-01T08:00:00.000Z', 'count': 0}, {'time': '2016-01-01T09:00:00.000Z', 'count': 1}, {'time': '2016-01-01T10:00:00.000Z', 'count': 1} ])
def test_scores_multiple_frames_without_marks(): game = bowling.Game() game.roll(5) game.roll(3) game.roll(2) game.roll(1) expect(game.build_score()).to.eq((5 + 3) + (2 + 1))
def test_scores_spare_from_previous_frame(): game = bowling.Game() game.roll(9) game.roll(1) game.roll(4) game.roll(3) expect(game.build_score()).to.eq((9 + 1 + 4) + (4 + 3))
def test_emit_no_points(self): results = [] ( emit(points=[]) | memory(results) ).execute() expect(results).to.eq([])
def test_emit_with_limit(self): results = [] ( emit(limit=10, every='0.01s') | memory(results) ).execute() expect(results).to.have.length(10)
def test_explanation_message_with_two_dicts(self, mock_dict_diffs): mock_dict_diffs.return_value = 'Some diffs' d1 = {'a': 1, 'c': 2} d2 = {'a': 1, 'b': 3} equal = Equal(d1, d2) expect('Some diffs' in equal.explanation.message).to.eq(True)
def test_not_change_by_failure(self): try: expect(lambda x: x + 1).not_to.change(0).by(1) except BadExpectation: pass else: raise BadExpectation('it must fail')
def test_explanation_message_with_two_lists(self, mock_list_diffs): mock_list_diffs.return_value = 'Some diffs' l1 = [1, 2] l2 = [1, 3] equal = Equal(l1, l2) expect('Some diffs' in equal.explanation.message).to.eq(True)
def test_explanation_message_with_two_strings(self, mock_str_diffs): mock_str_diffs.return_value = 'Some diffs' l1 = 'cat' l2 = 'dog' equal = Equal(l1, l2) expect('Some diffs' in equal.explanation.message).to.eq(True)
def test_reduce_with_custom_reducer(self): class count_odds(reducer): def __init__(self, fieldname): self.count = 0 self.fieldname = fieldname def update(self, point): if self.fieldname in point: if point[self.fieldname] % 2 != 0: self.count += 1 def result(self): return self.count def reset(self): self.count = 0 results = [] ( emit(limit=10, every='0.001s') | put(count=count()) | reduce(odds=count_odds('count')) | keep('odds') | memory(results) ).execute() expect(results).to.eq([{'odds': 5}])
def test_commutativity_with_timeless_data(self): """ A ∩ B = B ∩ A """ AnB = [] BnA = [] A = [ {'foo': '0', 'a': 0}, {'foo': '1', 'a': 1}, {'foo': '2', 'a': 2} ] B = [ {'foo': '1', 'a': 1}, {'foo': '3', 'a': 2}, {'foo': '5', 'a': 3} ] ( (emit(points=A), emit(points=B)) | intersect('foo') | memory(AnB) ).execute() ( (emit(points=B), emit(points=A)) | intersect('foo') | memory(BnA) ).execute() expect(AnB).to.eq(BnA)
def test_negative_explanation_message(self): float_assertion = Float(1.1, is_negative=True) message = float_assertion.explanation.message expect(message) == """ A = {0} Expected A not to be a floating point number """.format(repr(1.1))
def test_challenge22(): """ Regression testing challenge22 """ name_path = join(dirname(abspath(__file__)), 'names.txt') with open(name_path, 'r') as name_file: raw_names = [s.strip() for s in name_file.readlines()] expect(main(raw_names)).to.eq(871198282)
def test_challenge50(): """ Regression testing challenge50 """ expect(main()).to.eq(997651)
def test_serialization(self): pinned_officer = OfficerFactory( id=1, rank='Police Officer', first_name='Jerome', last_name='Finnigan', allegation_count=10, trr_percentile='99.99', complaint_percentile='88.88', civilian_allegation_percentile='77.77', internal_allegation_percentile='66.66', ) relevant_allegation = AllegationFactory( crid='1', incident_date=datetime(2002, 2, 21, tzinfo=pytz.utc), most_common_category=AllegationCategoryFactory( category='Operation/Personnel Violations'), point=Point([0.01, 0.02]), ) AttachmentFileFactory( id=1, file_type='document', title='relevant document 1', allegation=relevant_allegation, show=True, preview_image_url= "https://assets.documentcloud.org/CRID-1-CR-p1-normal.gif", url='http://cr-1-document.com/', ) pinboard = PinboardFactory( id='66ef1560', title='Test pinboard', description='Test description', ) pinboard.officers.set([pinned_officer]) OfficerAllegationFactory(officer=pinned_officer, allegation=relevant_allegation) expect(pinboard.relevant_documents.count()).to.eq(1) expect(DocumentSerializer( pinboard.relevant_documents.first()).data).to.eq({ 'id': 1, 'preview_image_url': "https://assets.documentcloud.org/CRID-1-CR-p1-normal.gif", 'url': 'http://cr-1-document.com/', 'allegation': { 'crid': '1', 'category': 'Operation/Personnel Violations', 'incident_date': '2002-02-21', 'officers': [{ 'id': 1, 'rank': 'Police Officer', 'full_name': 'Jerome Finnigan', 'percentile_trr': '99.9900', 'percentile_allegation': '88.8800', 'percentile_allegation_civilian': '77.7700', 'percentile_allegation_internal': '66.6600', }], 'point': { 'lon': 0.01, 'lat': 0.02, }, } })
def test_search_with_apostrophe(self): allegation_category = AllegationCategoryFactory(category='Use of Force') allegation_1 = AllegationFactory( crid='C12345', incident_date=datetime(2007, 1, 1, tzinfo=pytz.utc), most_common_category=allegation_category, ) allegation_2 = AllegationFactory( crid='C12346', incident_date=datetime(2007, 1, 1, tzinfo=pytz.utc), most_common_category=allegation_category, ) allegation_3 = AllegationFactory( crid='C12347', incident_date=datetime(2007, 1, 1, tzinfo=pytz.utc), most_common_category=allegation_category, ) allegation_4 = AllegationFactory( crid='C12348', incident_date=datetime(2007, 1, 1, tzinfo=pytz.utc), most_common_category=allegation_category, ) AttachmentFileFactory(text_content='Name: OBrien', allegation=allegation_1) AttachmentFileFactory(text_content='Name: O\'Brien', allegation=allegation_2) AttachmentFileFactory(text_content='Name: O Brien', allegation=allegation_3) AttachmentFileFactory(text_content='Name: Jim', allegation=allegation_4) self.rebuild_index() self.refresh_index() response = self.client.get(reverse('api:suggestion-list'), { 'term': 'O\'Brien', }) sorted_cr_results = list(sorted(response.data['CR'], key=lambda cr: cr['crid'])) print(sorted_cr_results) expect(len(sorted_cr_results)).to.eq(3) expect(sorted_cr_results[0]['crid']).to.eq('C12345') expect(sorted_cr_results[0]['highlight']['text_content']).to.eq(['Name: O<em>Brien</em>']) expect(sorted_cr_results[1]['crid']).to.eq('C12346') expect(sorted_cr_results[1]['highlight']['text_content']).to.eq(['Name: O\'<em>Brien</em>']) expect(sorted_cr_results[2]['crid']).to.eq('C12347') expect(sorted_cr_results[2]['highlight']['text_content']).to.eq(['Name: O <em>Brien</em>'])
def test_search_terms_results(self): SearchTermItemFactory( slug='communities', name='Communities', category=SearchTermCategoryFactory(name='Geography'), description='Community description', call_to_action_type='view_all', link='/url-mediator/session-builder/?community=123456' ) SearchTermItemFactory( slug='wards', name='Wards', category=SearchTermCategoryFactory(name='Geography'), description='Ward description', call_to_action_type='view_all', link='/url-mediator/session-builder/?community=654321' ) self.rebuild_index() self.refresh_index() url = reverse('api:suggestion-list') response = self.client.get(url, { 'term': 'Geography', }) results = response.data['SEARCH-TERMS'] expect(results).to.have.length(2) expect(results[0]['id']).to.eq('communities') expect(results[0]['name']).to.eq('Communities') expect(results[0]['category_name']).to.eq('Geography') expect(results[0]['description']).to.eq('Community description') expect(results[0]['call_to_action_type']).to.eq('view_all') expect(results[0]['link']).to.eq('http://cpdb.lvh.me/url-mediator/session-builder/?community=123456')
def test_matches(self): def a(): pass expect(Callable(a).matches()).to.eq(True)
def test_get_queryset_raise_NotImplementedError(self): expect(lambda: ToastBaseViewSet().get_queryset()).to.throw(NotImplementedError)
def test_execute(self, _trr, _award, _join, _rank, _unit, _cr): sorted_timeline = OfficerTimelineMobileQuery(None).execute() sorted_timeline_ids = [item['id'] for item in sorted_timeline] expect(sorted_timeline_ids).to.eq([11, 9, 2, 6, 4, 10, 8, 5, 3, 1, 7])
def test_trr_timeline(self, trr_new_timeline_serializer_mock): officer = OfficerFactory(id=123, appointed_date=date(2001, 2, 3)) trr_1 = TRRFactory(officer=officer, trr_datetime=datetime(2002, 1, 4, tzinfo=pytz.utc)) trr_2 = TRRFactory(officer=officer, trr_datetime=datetime(2003, 1, 5, tzinfo=pytz.utc)) unit_1 = PoliceUnitFactory(unit_name='001', description='District 001') unit_2 = PoliceUnitFactory(unit_name='002', description='District 002') OfficerHistoryFactory( officer=officer, unit=unit_1, effective_date=date(2002, 1, 3), end_date=date(2003, 1, 2) ) OfficerHistoryFactory( officer=officer, unit=unit_2, effective_date=date(2003, 1, 3), end_date=date(2018, 1, 3) ) SalaryFactory( year=2001, rank='Police Officer', officer=officer, rank_changed=True, spp_date=date(2001, 5, 3) ) SalaryFactory( year=2002, rank='Senior Police Officer', officer=officer, rank_changed=True, spp_date=date(2002, 5, 3) ) expect(OfficerTimelineMobileQuery(officer)._trr_timeline).to.eq([{'id': 1}, {'id': 2}]) trr_timeline_queryset_arg = trr_new_timeline_serializer_mock.call_args[0][0] trr_1_arg, trr_2_arg = sorted(trr_timeline_queryset_arg, key=attrgetter('id')) expect(trr_1_arg.id).to.eq(trr_1.id) expect(trr_1_arg.unit_name).to.eq('001') expect(trr_1_arg.unit_description).to.eq('District 001') expect(trr_1_arg.rank_name).to.eq('Police Officer') expect(trr_2_arg.id).to.eq(trr_2.id) expect(trr_2_arg.unit_name).to.eq('002') expect(trr_2_arg.unit_description).to.eq('District 002') expect(trr_2_arg.rank_name).to.eq('Senior Police Officer')
def test_cr_timeline(self, cr_new_timeline_serializer_mock): officer = OfficerFactory(id=123) OfficerAllegationFactory(id=1, officer=officer, allegation__incident_date=datetime(2002, 2, 3, tzinfo=pytz.utc)) OfficerAllegationFactory(id=2, officer=officer, allegation__incident_date=datetime(2003, 1, 5, tzinfo=pytz.utc)) OfficerAllegationFactory(id=3, officer=officer, allegation__incident_date=None) unit_1 = PoliceUnitFactory(unit_name='001', description='District 001') unit_2 = PoliceUnitFactory(unit_name='002', description='District 002') OfficerHistoryFactory( officer=officer, unit=unit_1, effective_date=date(2002, 1, 3), end_date=date(2003, 1, 2) ) OfficerHistoryFactory( officer=officer, unit=unit_2, effective_date=date(2003, 1, 3), end_date=date(2018, 1, 3) ) SalaryFactory( year=2001, rank='Police Officer', officer=officer, rank_changed=True, spp_date=date(2001, 5, 3) ) SalaryFactory( year=2002, rank='Senior Police Officer', officer=officer, rank_changed=True, spp_date=date(2002, 5, 3) ) other_officer = OfficerFactory(id=456) OfficerAllegationFactory(id=4, officer=other_officer, start_date=date(2003, 1, 5)) expect(OfficerTimelineMobileQuery(officer)._cr_timeline).to.eq([{'id': 1}, {'id': 2}]) cr_timeline_queryset_arg = cr_new_timeline_serializer_mock.call_args[0][0] officer_allegation_1_arg, officer_allegation_2_arg = sorted(cr_timeline_queryset_arg, key=attrgetter('id')) expect(officer_allegation_1_arg.id).to.eq(1) expect(officer_allegation_1_arg.unit_name).to.eq('001') expect(officer_allegation_1_arg.unit_description).to.eq('District 001') expect(officer_allegation_1_arg.rank_name).to.eq('Police Officer') expect(officer_allegation_2_arg.id).to.eq(2) expect(officer_allegation_2_arg.unit_name).to.eq('002') expect(officer_allegation_2_arg.unit_description).to.eq('District 002') expect(officer_allegation_2_arg.rank_name).to.eq('Senior Police Officer')
def test_award_timeline(self, award_new_timeline_serializer_mock): officer = OfficerFactory(id=123, appointed_date=date(2001, 2, 3)) award_1 = AwardFactory(officer=officer, start_date=date(2002, 1, 3), award_type='Honored Police Star') award_2 = AwardFactory(officer=officer, start_date=date(2003, 1, 5), award_type='Life Saving Award') AwardFactory(officer=officer, start_date=date(2007, 2, 3), award_type='Complimentary Letter') AwardFactory(officer=officer, start_date=date(2008, 2, 3), award_type='Department Commendation') AwardFactory(officer=officer, start_date=date(2011, 2, 3), award_type='Citizen Honorable Mention') AwardFactory(officer=officer, start_date=None, award_type='Life Saving') unit_1 = PoliceUnitFactory(unit_name='001', description='District 001') unit_2 = PoliceUnitFactory(unit_name='002', description='District 002') OfficerHistoryFactory( officer=officer, unit=unit_1, effective_date=date(2002, 1, 3), end_date=date(2003, 1, 2) ) OfficerHistoryFactory( officer=officer, unit=unit_2, effective_date=date(2003, 1, 3), end_date=date(2018, 1, 3) ) SalaryFactory( year=2001, rank='Police Officer', officer=officer, rank_changed=True, spp_date=date(2001, 5, 3) ) SalaryFactory( year=2002, rank='Senior Police Officer', officer=officer, rank_changed=True, spp_date=date(2002, 5, 3) ) expect(OfficerTimelineMobileQuery(officer)._award_timeline).to.eq([{'id': 1}, {'id': 2}]) award_timeline_queryset_arg = award_new_timeline_serializer_mock.call_args[0][0] award_1_arg, award_2_arg = sorted(award_timeline_queryset_arg, key=attrgetter('id')) expect(award_1_arg.id).to.eq(award_1.id) expect(award_1_arg.unit_name).to.eq('001') expect(award_1_arg.unit_description).to.eq('District 001') expect(award_1_arg.rank_name).to.eq('Police Officer') expect(award_2_arg.id).to.eq(award_2.id) expect(award_2_arg.unit_name).to.eq('002') expect(award_2_arg.unit_description).to.eq('District 002') expect(award_2_arg.rank_name).to.eq('Senior Police Officer')
def test_join_timeline_no_officer_appointed_date(self): officer = OfficerFactory(id=123, appointed_date=None) expect(OfficerTimelineMobileQuery(officer)._join_timeline).to.eq([])
def test_challenge243(): """ Regression testing challenge243 """ expect(main()).to.eq(892371480)
def test_challenge52(): """ Regression testing challenge52 """ expect(main()).to.eq(142857)
def test_challenge13(): """ Regression testing challenge13 """ expect(main()).to.eq(5537376230)
def test_challenge79(): """ Regression testing challenge79 """ expect(main()).to.eq(73162890)
def test_update_attachment_downloads_and_views_count(self): attachment1 = AttachmentFileFactory(id=1, views_count=0, downloads_count=1) attachment2 = AttachmentFileFactory(id=2, views_count=0, downloads_count=0) attachment3 = AttachmentFileFactory(id=3, views_count=1, downloads_count=2) AttachmentTrackingFactory(attachment_file=attachment1, kind=constants.VIEW_EVENT_TYPE) AttachmentTrackingFactory(attachment_file=attachment1, kind=constants.DOWNLOAD_EVENT_TYPE) AttachmentTrackingFactory(attachment_file=attachment2, kind=constants.VIEW_EVENT_TYPE) AttachmentTrackingFactory(attachment_file=attachment2, kind=constants.VIEW_EVENT_TYPE) management.call_command('update_attachment_downloads_and_views_count') attachment1.refresh_from_db() attachment2.refresh_from_db() attachment3.refresh_from_db() expect(attachment1.views_count).to.eq(1) expect(attachment1.downloads_count).to.eq(1) expect(attachment2.views_count).to.eq(2) expect(attachment2.downloads_count).to.eq(0) expect(attachment3.views_count).to.eq(0) expect(attachment3.downloads_count).to.eq(0)
def test_get_badge(self): allegation_1 = AllegationFactory( incident_date=datetime(2002, 1, 1, tzinfo=pytz.utc)) allegation_2 = AllegationFactory( incident_date=datetime(2007, 1, 1, tzinfo=pytz.utc)) allegation_3 = AllegationFactory(incident_date=None) investigator_allegation_1 = InvestigatorAllegationFactory( allegation=allegation_1) investigator_allegation_2 = InvestigatorAllegationFactory( allegation=allegation_2, current_star='123456') investigator_allegation_3 = InvestigatorAllegationFactory( allegation=allegation_2, current_star=None, ) setattr(investigator_allegation_3, 'has_badge_number', True) investigator_allegation_4 = InvestigatorAllegationFactory( allegation=allegation_2, current_star=None) setattr(investigator_allegation_4, 'has_badge_number', False) investigator_allegation_5 = InvestigatorAllegationFactory( allegation=allegation_2, current_star=None, ) setattr(investigator_allegation_5, 'has_badge_number', False) investigator_allegation_6 = InvestigatorAllegationFactory( allegation=allegation_3, current_star=None, ) setattr(investigator_allegation_6, 'has_badge_number', False) investigator_allegation_7 = InvestigatorAllegationFactory( allegation=allegation_3, current_star='123456', ) investigator_allegation_8 = InvestigatorAllegationFactory( allegation=allegation_3, current_star=None, ) setattr(investigator_allegation_8, 'has_badge_number', True) expect( InvestigatorMobileSerializer( investigator_allegation_1).data['badge']).to.eq('CPD') expect( InvestigatorMobileSerializer( investigator_allegation_2).data['badge']).to.eq('CPD') expect( InvestigatorMobileSerializer( investigator_allegation_3).data['badge']).to.eq('CPD') expect( InvestigatorMobileSerializer( investigator_allegation_4).data['badge']).to.eq('COPA/IPRA') expect( InvestigatorMobileSerializer( investigator_allegation_5).data['badge']).to.eq('COPA/IPRA') expect( InvestigatorMobileSerializer( investigator_allegation_6).data['badge']).to.eq('COPA/IPRA') expect( InvestigatorMobileSerializer( investigator_allegation_7).data['badge']).to.eq('CPD') expect( InvestigatorMobileSerializer( investigator_allegation_8).data['badge']).to.eq('CPD')
def test_variables(self): expect(url).to.equal(test.url) expect(test.pub).to.be.a.string() expect(test.port).to.be.an.integer() expect(test.ip).not_to.be.none()
def test_rpc(self): expect(test.rpc()) == "http://99.233.0.99:9995/rpc"
def test_challenge204(): """ Regression testing challenge204 """ expect(main()).to.eq(2944730)
def test_challenge8(digits, huge_number, expected): """ Regression testing challenge8 """ expect(main(digits, huge_number)).to.eq(expected)
def test_challenge113(input, expected): """ Regression testing challenge113 """ expect(main(input)).to.eq(expected)
def test_challenge80(): """ Regression testing challenge80 """ expect(main()).to.eq(40886)
def test_summary_no_match(self): response = self.client.get( reverse('api-v2:units-summary', kwargs={'pk': '123'})) expect(response.status_code).to.eq(status.HTTP_404_NOT_FOUND)
def test_challenge230(): """ Regression testing challenge230 """ expect(main()).to.eq(850481152593119296)
def test_summary(self): unit = PoliceUnitFactory(unit_name='123', description='foo') officer = OfficerFactory(race='White', gender='F', birth_year='1980') OfficerHistoryFactory(unit=unit, officer=officer, end_date=None) allegation = AllegationFactory() allegation_category = AllegationCategoryFactory( category='Use of Force') OfficerAllegationFactory(officer=officer, allegation=allegation, allegation_category=allegation_category, final_finding='SU') ComplainantFactory(allegation=allegation, race='Black', age=25, gender='M') self.refresh_index() response = self.client.get( reverse('api-v2:units-summary', kwargs={'pk': '123'})) expect(response.data).to.be.eq({ 'unit_name': '123', 'description': 'foo', 'member_records': { 'active_members': 1, 'total': 1, 'facets': [{ 'name': 'race', 'entries': [{ 'name': 'White', 'count': 1 }] }, { 'name': 'age', 'entries': [{ 'name': '31-40', 'count': 1 }] }, { 'name': 'gender', 'entries': [{ 'name': 'Female', 'count': 1 }] }] }, 'complaint_records': { 'count': 1, 'sustained_count': 1, 'facets': [{ 'name': 'category', 'entries': [{ 'name': 'Use of Force', 'count': 1, 'sustained_count': 1 }] }, { 'name': 'race', 'entries': [{ 'name': 'Black', 'count': 1, 'sustained_count': 1 }] }, { 'name': 'age', 'entries': [{ 'name': '21-30', 'count': 1, 'sustained_count': 1 }] }, { 'name': 'gender', 'entries': [{ 'name': 'Male', 'count': 1, 'sustained_count': 1 }] }] } })
def test_retrieve_single_with_content_type(self): OfficerFactory(first_name='Kevin', last_name='Osborn', id=123) self.rebuild_index() self.refresh_index() text = 'Ke' retrieve_single_url = reverse('api:suggestion-single') response = self.client.get(retrieve_single_url, { 'term': text, 'contentType': 'OFFICER' }) expect(response.status_code).to.equal(status.HTTP_200_OK) expect(response.data['count']).to.equal(1) expect(response.data['next']).to.equal(None) expect(response.data['previous']).to.equal(None) expect(len(response.data['results'])).to.eq(1) expect(response.data['results'][0]['id']).to.eq('123')
def test_rank_change_timeline(self, rank_change_new_timeline_serializer_mock): officer = OfficerFactory(id=123, appointed_date=date(2001, 2, 3)) salary_1 = SalaryFactory( year=2001, rank='Police Officer', officer=officer, rank_changed=True, spp_date=date(2001, 5, 3) ) salary_2 = SalaryFactory( year=2002, rank='Senior Police Officer', officer=officer, rank_changed=True, spp_date=date(2002, 5, 3) ) SalaryFactory( year=2001, rank='Junior Police Officer', officer=officer, rank_changed=True, spp_date=date(2001, 2, 3) ) SalaryFactory( year=2003, rank='Senior Police Officer', officer=officer, rank_changed=False, spp_date=date(2003, 5, 3) ) unit_1 = PoliceUnitFactory(unit_name='001', description='District 001') unit_2 = PoliceUnitFactory(unit_name='002', description='District 002') OfficerHistoryFactory( officer=officer, unit=unit_1, effective_date=date(2001, 1, 3), end_date=date(2002, 1, 2) ) OfficerHistoryFactory( officer=officer, unit=unit_2, effective_date=date(2002, 1, 3), end_date=date(2018, 1, 3) ) other_officer = OfficerFactory(id=456) SalaryFactory( year=2001, rank='Police Officer', officer=other_officer, rank_changed=True, spp_date=date(2001, 5, 3) ) expect(OfficerTimelineMobileQuery(officer)._rank_change_timeline).to.eq([{'id': 1}, {'id': 2}]) rank_change_timeline_queryset_arg = rank_change_new_timeline_serializer_mock.call_args[0][0] salary_1_arg, salary_2_arg = rank_change_timeline_queryset_arg expect(salary_1_arg.id).to.eq(salary_1.id) expect(salary_1_arg.unit_name).to.eq('001') expect(salary_1_arg.unit_description).to.eq('District 001') expect(salary_2_arg.id).to.eq(salary_2.id) expect(salary_2_arg.unit_name).to.eq('002') expect(salary_2_arg.unit_description).to.eq('District 002')
def test_register(self): expect(expect.matcher('callable')) == Callable