def test_get_citizens(self): url = reverse('imports') last_count = Citizen.objects.count() count = 10000 data = generate_import(count) response = self.client.post(url, data, format='json') url = reverse('show-all', kwargs={"import_id": get_import_id()}) start_time = time.time() response = self.client.get(url) lead_time = time.time() - start_time self.assertTrue(lead_time < 10) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(Citizen.objects.count(), count + last_count)
def test_imports(self): url = reverse('imports') last_count = Citizen.objects.count() count = 10000 data = generate_import(count) start_time = time.time() response = self.client.post(url, data, format='json') lead_time = time.time() - start_time self.assertTrue(lead_time < 10) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertJSONEqual(response.content, { "data": { "import_id": get_import_id() } }) self.assertEqual(Citizen.objects.count(), count + last_count)
def imports(request): if request.method == 'POST': try: data = JSONParser().parse(request) except ParseError: return HttpResponse(status=400) if not is_relative_ties_valid(data): return HttpResponse(status=400) try: serializer = CitizenSerializer(data=data['citizens'], many=True) except KeyError: return HttpResponse(status=400) generate_import_id() if serializer.is_valid(): serializer.save() return JsonResponse({"data": { "import_id": get_import_id() }}, status=201) return HttpResponse(status=400) else: return HttpResponse(status=405)
def test_update(self): url = reverse('imports') last_count = Citizen.objects.count() count = 10000 data = generate_import(count) response = self.client.post(url, data, format='json') citizen_id = random.randint(0, count - 1) url = reverse('update-citizen', kwargs={'citizen_id': citizen_id, "import_id": get_import_id()}) last_data = CitizenSerializer(Citizen.objects.filter(citizen_id=citizen_id).get(import_id__exact=get_import_id())).data last_realtives = last_data['relatives'] data = generate_update(citizen_id, count) start_time = time.time() response = self.client.patch(url, data, format='json') lead_time = time.time() - start_time for field in data: last_data[field] = data[field] self.assertTrue(lead_time < 10) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertJSONEqual(response.content, '{"data":' + json.dumps(last_data) + '}') self.assertEqual(Citizen.objects.count(), count + last_count) for relative in last_data['relatives']: self.assertTrue(citizen_id in Citizen.objects.filter(citizen_id=relative).get(import_id__exact=get_import_id()).get_relatives()) for relative in last_realtives: self.assertTrue(citizen_id not in Citizen.objects.filter(citizen_id=relative).get(import_id__exact=get_import_id()).get_relatives())
def test_excess_fields_update(self): url = reverse('imports') last_count = Citizen.objects.count() count = 10000 data = generate_import(count) response = self.client.post(url, data, format='json') citizen_id = random.randint(0, count - 1) url = reverse('update-citizen', kwargs={'citizen_id': citizen_id, "import_id": get_import_id()}) last_data = CitizenSerializer(Citizen.objects.filter(citizen_id=citizen_id).get(import_id__exact=get_import_id())).data last_realtives = last_data['relatives'] data = generate_update(citizen_id, count) data['excess'] = 'value' response = self.client.patch(url, data, format='json') self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(Citizen.objects.count(), count + last_count)