def test_create(self, mock_url_model):
     """Test (overridden) create method."""
     serializer = GreenAssessmentPropertySerializer()
     data = self.data.copy()
     data['urls'] = self.urls
     instance = serializer.create(data)
     mock_url_model.objects.bulk_create.assert_called_with([
         mock_url_model(property_assessment=instance,
                        url=url[0],
                        description=url[1]) for url in self.urls
     ])
 def test_update(self, mock_url_model):
     """Test (overridden) update method."""
     data = self.data.copy()
     data['urls'] = self.urls[:1]
     gap = self.gap_factory.get_green_assessment_property(**data)
     serializer = GreenAssessmentPropertySerializer()
     data['urls'] = self.urls[1:]
     instance = serializer.update(gap, data)
     mock_url_model.objects.filter.assert_called_with(
         property_assessment=instance)
     mock_url_model.assert_called_with(url='http://example.org',
                                       property_assessment=gap)
    def test_validate(self):
        """Test (overriden) validate method"""
        serializer = GreenAssessmentPropertySerializer()
        # add context
        mock_request = mock.MagicMock()
        mock_request.user = self.user
        serializer.context = {'request': mock_request}

        # assert raises error if rating and metric supplied
        data = self.data.copy()
        data['rating'] = 'Gold Star'
        with self.assertRaises(ValidationError) as conm:
            serializer.validate(data)
        exception = conm.exception
        expected = "['Only one of metric or rating can be supplied.']"
        self.assertEqual(expected, str(exception))

        # assert raises error if metric is expected
        del data['metric']
        data['rating'] = '5'
        with self.assertRaises(ValidationError) as conm:
            serializer.validate(data)
        exception = conm.exception
        expected = "['{} uses a metric (numeric score).']".format(
            self.assessment.name)
        self.assertEqual(expected, str(exception))

        # assert raises error if metric is of wrong type
        del data['rating']
        data['metric'] = '5 stars'
        with self.assertRaises(ValidationError) as conm:
            serializer.validate(data)
        exception = conm.exception
        expected = "['Metric must be a number.']"
        self.assertEqual(expected, str(exception))

        # assert raises error if rating expected
        self.assessment.is_numeric_score = False
        data['metric'] = 5
        with self.assertRaises(ValidationError) as conm:
            serializer.validate(data)
        exception = conm.exception
        expected = "['{} uses a rating (non-numeric score).']".format(
            self.assessment.name)

        # assert raises error if rating is of wrong type
        del data['metric']
        data['rating'] = 5
        with self.assertRaises(ValidationError) as conm:
            serializer.validate(data)
        exception = conm.exception
        expected = "['Rating must be a string.']"
        self.assertEqual(expected, str(exception))

        # assert converts ints to floats
        self.assessment.is_numeric_score = True
        del data['rating']
        data['metric'] = 5
        result = serializer.validate(data)
        self.assertEqual(result, data)

        # assert raises error if integer expected
        data['metric'] = 3.5
        with self.assertRaises(ValidationError) as conm:
            serializer.validate(data)
        exception = conm.exception
        expected = "['Metric must be an integer.']"
        self.assertEqual(expected, str(exception))

        # assert raises error if assessment missing
        del data['assessment']
        with self.assertRaises(ValidationError) as conm:
            serializer.validate(data)
        exception = conm.exception
        expected = "['Could not find assessment.']"
        self.assertEqual(expected, str(exception))