def test_get_by_logged_at(self): fake_disbursement(_quantity=20, prison=self.prison) minimum_created_date = date.today() - timedelta(days=1) response = self.api_request( log__action='created', logged_at__gte=minimum_created_date ) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual( Disbursement.objects.filter( log__action='created', log__created__utcdate__gte=minimum_created_date ).count(), response.data['count'] ) pks = [item['id'] for item in response.data['results']] for disbursement in Disbursement.objects.filter(pk__in=pks): for log in disbursement.log_set.all(): if log.action == 'created': self.assertGreaterEqual(log.created.date(), minimum_created_date) for disbursement in Disbursement.objects.exclude(pk__in=pks): for log in disbursement.log_set.all(): if log.action == 'created': self.assertLess(log.created.date(), minimum_created_date)
def test_list_disbursements(self): fake_disbursement(prison=self.prison, recipient_first_name='Sam') fake_disbursement(prison=Prison.objects.get(pk='INP'), recipient_first_name='James') response = self.api_request() self.assertEqual(response.status_code, status.HTTP_200_OK) data = response.data self.assertEqual(data['count'], 1) self.assertEqual(data['results'][0]['recipient_first_name'], 'Sam')
def test_filter_companies(self): fake_disbursement(_quantity=10, prison=self.prison, recipient_is_company=False) disbursement = Disbursement.objects.last() disbursement.recipient_is_company = True disbursement.save() response = self.api_request() self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['count'], 10) response = self.api_request(recipient_is_company=True) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['count'], 1) response = self.api_request(recipient_is_company=False) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['count'], 9)
def test_searching(self): fake_disbursement(_quantity=20, prison=self.prison, recipient_last_name=itertools.cycle( ['Smith', 'Willis'])) response = self.api_request(amount__lte=1000) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['count'], Disbursement.objects.filter(amount__lte=1000).count()) response = self.api_request(recipient_name='smith') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['count'], 10)
def test_ordering(self): fake_disbursement(_quantity=20, prison=self.prison) response = self.api_request(ordering='created', offset=0, limit=10) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertSequenceEqual( [item['id'] for item in response.data['results']], Disbursement.objects.order_by('created').values_list('id', flat=True)[0:10] ) response = self.api_request(ordering='-amount', offset=0, limit=100) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertSequenceEqual( [item['amount'] for item in response.data['results']], sorted(Disbursement.objects.values_list('amount', flat=True), reverse=True) )
def test_simple_search(self): """ Test for when the search param `simple_search` is used. Checks that the API return the disbursements with the supplied search value in the name of the recipient OR the prisoner number """ # prepare the data to match exactly 2 records term_part1 = get_random_string(10) term_part2 = get_random_string(10) term = f'{term_part1} {term_part2}' disbursement1 = fake_disbursement( prison=self.prison, recipient_first_name=term_part1, recipient_last_name=f'{term_part2}Junior'.upper(), ) disbursement2 = fake_disbursement( prison=self.prison, prisoner_number=term_part1, recipient_first_name=f'Mr{term_part2}', ) # these should be non-matches fake_disbursement( _quantity=5, prison=self.prison, ) response = self.api_request(simple_search=term) self.assertEqual(response.status_code, status.HTTP_200_OK) response_data = response.data['results'] self.assertEqual(len(response_data), 2) self.assertEqual( {item['id'] for item in response_data}, { disbursement1.id, disbursement2.id, }, )
def test_searching(self): fake_disbursement( # non-matches _quantity=20, prison=self.prison, recipient_last_name='Some last name', postcode='WC1B 3DG', amount=itertools.cycle([900, 1100]), ) expected_disbursement = fake_disbursement( prison=self.prison, recipient_last_name='Smith', postcode='SW1A 1AA', amount=1001, ) scenarios = ( {'amount': 1001}, {'exclude_amount__endswith': '00'}, {'exclude_amount__regex': '(900|1100)$'}, {'amount__endswith': '01'}, {'amount__regex': '.01$'}, { 'amount__lte': 1002, 'amount__gte': 1001, }, {'recipient_name': 'smith'}, {'postcode': 'SW1A1aa'}, {'postcode': 'SW1A 1aa'}, {'postcode': 'SW1A'}, ) for scenario in scenarios: response = self.api_request(**scenario) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data['count'], 1) self.assertEqual( response.data['results'][0]['id'], expected_disbursement.pk, )