def test_sync_new_bill(self): new_data = { 'id': 'BILL0001', 'updated_at': fake_openstates_timestamp() } with mock.patch.object( utils, 'deserialize_openstates_bill') as mock_deserialize: info = services.sync_bill_data(new_data) mock_deserialize.assert_called_once_with(new_data) assert info.action == services.Action.ADDED
def test_sync_new_bill(self): new_data = { 'id': 'ocd-bill/117a7e45-11d8-4874-a01f-338b1efc2e5e', 'updated_at': fake_openstates_timestamp() } with mock.patch.object( utils, 'deserialize_openstates_bill') as mock_deserialize: info = services.sync_bill_data(new_data) mock_deserialize.assert_called_once_with(new_data) assert info.action == services.Action.ADDED
def test_sync_existing_bill_skips_older(self): instance = BillFactory.create() older_data = { 'id': instance.openstates_bill_id, 'updated_at': utils.format_datetime(instance.openstates_updated_at - ONE_DAY), } with mock.patch.object( utils, 'deserialize_openstates_bill') as mock_deserialize: info = services.sync_bill_data(older_data) mock_deserialize.assert_not_called() assert info.action == services.Action.SKIPPED
def test_sync_existing_bill_updates_given_newer_data(self): instance = BillFactory.create() newer_data = { 'id': instance.openstates_bill_id, 'updated_at': utils.format_datetime(instance.openstates_updated_at + ONE_DAY), } with mock.patch.object( utils, 'deserialize_openstates_bill') as mock_deserialize: info = services.sync_bill_data(newer_data) mock_deserialize.assert_called_once_with(newer_data, instance=instance) assert info.action == services.Action.UPDATED
def handle(self, *args, **options): total_bill_count = 0 if options['start']: start_token = options['start'] else: start_token = '' loop = 0 while loop < 100: bill_list = self._fetch_bills(start_token, options) if not bill_list: self.stdout.write(self.style.SUCCESS('No data to sync')) return bills_total = bill_list.pop() start_token = bill_list.pop() total_count = options['max'] if options['max'] > 0 else bills_total print( f'Processing {len(bill_list)} bills in loop #{loop}. Next cursor: {start_token}' ) for data in bill_list: info = services.sync_bill_data(data, options['force_update']) self._write_info(info) total_bill_count += 1 if total_count == total_bill_count: break loop += 1 if total_bill_count >= total_count: self.stdout.write( self.style.SUCCESS(f'Reached the maximum bill count of' + '{total_bill_count}/{total_count}')) loop = 100 if not start_token: self.stdout.write( self.style.SUCCESS(f'Reached the end of bill pages.')) loop = 100 self.stdout.write( self.style.SUCCESS( f'Successfully synced {total_bill_count} bills')) return
def handle(self, *args, **options): bill_list = self._fetch_bills(options) if not bill_list: self.stdout.write(self.style.SUCCESS('No data to sync')) return force_update = options['force_update'] for data in bill_list: bill_detail = fetch.bill_detail(data['session'], data['bill_id']) if hasattr(bill_detail, 'status_code'): response = bill_detail info = services.ActionInfo.fail( f'{response.status_code}: {response.reason}') self._write_info(info) continue info = services.sync_bill_data(bill_detail, force_update=force_update) self._write_info(info) self.stdout.write(self.style.SUCCESS('Successfully synced bills'))
def test_sync_bill_with_long_bill_id(self): bad_data = fake_bill_detail() bad_data['id'] = 'X' * 100 info = services.sync_bill_data(bad_data) assert info.action == services.Action.FAILED