Beispiel #1
0
 def test_deserialization_valid(self):
     serializer = DisaggregationSerializer(data=self.data)
     self.assertTrue(serializer.is_valid(), serializer.errors)
     disaggregation = serializer.save()
     self.assertEqual(disaggregation.name, self.data['name'])
     self.assertEqual(disaggregation.active, False)
     self.assertEqual(disaggregation.disaggregation_values.count(),
                      len(self.data['disaggregation_values']))
Beispiel #2
0
 def test_create_update_delete_value_single_call(self):
     """
     Just test that creation/update/deletion all play nice together.
     """
     disaggregation = DisaggregationFactory()
     v1 = DisaggregationValueFactory(disaggregation=disaggregation)
     v2 = DisaggregationValueFactory(disaggregation=disaggregation)
     DisaggregationValueFactory(disaggregation=disaggregation)
     data = DisaggregationSerializer(instance=disaggregation).data
     # modify the first one
     data['disaggregation_values'][0]['value'] = 'updated'
     # remove the second one
     data['disaggregation_values'] = data['disaggregation_values'][:1]
     # add a new one
     data['disaggregation_values'].append({
         "value": "a new value",
         "active": False
     })
     response = self.forced_auth_req('put',
                                     self._get_url(disaggregation),
                                     user=self.pme_user,
                                     data=data)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     disaggregation = Disaggregation.objects.get(pk=disaggregation.pk)
     self.assertEqual(2, disaggregation.disaggregation_values.count())
     self.assertEqual(
         'updated',
         disaggregation.disaggregation_values.get(pk=v1.pk).value)
     self.assertFalse(
         disaggregation.disaggregation_values.filter(pk=v2.pk).exists())
     self.assertEqual(
         'a new value',
         disaggregation.disaggregation_values.exclude(pk=v1.pk)[0].value)
Beispiel #3
0
 def test_serialization(self):
     serializer = DisaggregationSerializer(self.disaggregation)
     expected = {
         'id': self.disaggregation.id,
         'name': self.disaggregation.name,
         'active': self.disaggregation.active,
         'disaggregation_values': [],
     }
     self.assertEqual(serializer.data, expected)
Beispiel #4
0
 def test_removing_disaggregation_deletes_it(self):
     disaggregation = DisaggregationFactory()
     value = DisaggregationValueFactory(disaggregation=disaggregation)
     data = DisaggregationSerializer(instance=disaggregation).data
     data['disaggregation_values'] = []
     response = self.forced_auth_req('put',
                                     self._get_url(disaggregation),
                                     user=self.pme_user,
                                     data=data)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     disaggregation = Disaggregation.objects.get(pk=disaggregation.pk)
     self.assertEqual(0, disaggregation.disaggregation_values.count())
     self.assertFalse(
         DisaggregationValue.objects.filter(pk=value.pk).exists())
Beispiel #5
0
 def test_disallow_modifying_referenced_disaggregations(self):
     # this bootstraps a bunch of stuff, including self.disaggregation referenced by an AppliedIndicator
     setup_intervention_test_data(self, include_results_and_indicators=True)
     data = DisaggregationSerializer(instance=self.disaggregation).data
     response = self.forced_auth_req('put',
                                     self._get_url(self.disaggregation),
                                     user=self.pme_user,
                                     data=data)
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
     # also try with patch
     response = self.forced_auth_req('patch',
                                     self._get_url(self.disaggregation),
                                     user=self.pme_user,
                                     data=data)
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Beispiel #6
0
 def test_disallow_modifying_unrelated_disaggregation_values(self):
     disaggregation = DisaggregationFactory()
     value = DisaggregationValueFactory()
     data = DisaggregationSerializer(instance=disaggregation).data
     data['disaggregation_values'].append({
         "id": value.pk,
         "value": "not allowed",
     })
     response = self.forced_auth_req('put',
                                     self._get_url(disaggregation),
                                     user=self.pme_user,
                                     data=data)
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
     # also try with patch
     response = self.forced_auth_req('patch',
                                     self._get_url(disaggregation),
                                     user=self.pme_user,
                                     data=data)
     self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Beispiel #7
0
 def test_update_values(self):
     """
     Test updating a disaggregation's values
     """
     disaggregation = DisaggregationFactory()
     value = DisaggregationValueFactory(disaggregation=disaggregation)
     new_value = 'updated value'
     data = DisaggregationSerializer(instance=disaggregation).data
     data['disaggregation_values'][0]['value'] = new_value
     response = self.forced_auth_req('put',
                                     self._get_url(disaggregation),
                                     user=self.pme_user,
                                     data=data)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     disaggregation = Disaggregation.objects.get(pk=disaggregation.pk)
     self.assertEqual(1, disaggregation.disaggregation_values.count())
     updated_value = disaggregation.disaggregation_values.all()[0]
     self.assertEqual(value.pk, updated_value.pk)
     self.assertEqual(new_value, updated_value.value)
Beispiel #8
0
 def test_create_values(self):
     """
     Test creating new disaggregation values
     """
     disaggregation = DisaggregationFactory()
     value = DisaggregationValueFactory(disaggregation=disaggregation)
     data = DisaggregationSerializer(instance=disaggregation).data
     data['disaggregation_values'].append({
         "value": "a new value",
         "active": False
     })
     response = self.forced_auth_req('put',
                                     self._get_url(disaggregation),
                                     user=self.pme_user,
                                     data=data)
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     disaggregation = Disaggregation.objects.get(pk=disaggregation.pk)
     self.assertEqual(2, disaggregation.disaggregation_values.count())
     new_value = disaggregation.disaggregation_values.exclude(
         pk=value.pk)[0]
     self.assertEqual('a new value', new_value.value)
Beispiel #9
0
 def test_serialization_with_values(self):
     value_1 = DisaggregationValueFactory(disaggregation=self.disaggregation)
     value_2 = DisaggregationValueFactory(disaggregation=self.disaggregation)
     serializer = DisaggregationSerializer(self.disaggregation)
     expected = {
         'id': self.disaggregation.id,
         'name': self.disaggregation.name,
         'active': self.disaggregation.active,
         'disaggregation_values': [
             {
                 'id': value_1.id,
                 'value': value_1.value,
                 'active': value_1.active,
             }, {
                 'id': value_2.id,
                 'value': value_2.value,
                 'active': value_2.active,
             }
         ]
     }
     self.assertEqual(serializer.data, expected)
Beispiel #10
0
 def test_deserialization_needs_values(self):
     del self.data['disaggregation_values']
     serializer = DisaggregationSerializer(data=self.data)
     self.assertFalse(serializer.is_valid())
     self.assertEqual(serializer.errors['disaggregation_values'], ['This field is required.'])