Esempio n. 1
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)
Esempio n. 2
0
 def test_get(self):
     """
     Test retrieving a single disaggregation
     """
     disaggregation = DisaggregationFactory()
     num_values = 3
     for i in range(num_values):
         DisaggregationValueFactory(disaggregation=disaggregation)
     response = self.forced_auth_req('get', self._get_url(disaggregation))
     self.assertEqual(response.status_code, status.HTTP_200_OK)
     self.assertEqual(disaggregation.name, response.data['name'])
     self.assertEqual(num_values, len(response.data['disaggregation_values']))
Esempio n. 3
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)
Esempio n. 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())
Esempio n. 5
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)
Esempio n. 6
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)
Esempio n. 7
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)