def test_uninitialized_serializer_returns_correct_fields_metadata(self):
        expected_metadata = LAWMRunParameters.get_metadata()

        uninitialized_serializer = RunParametersSerializer()
        actual_metadata = uninitialized_serializer.get_metadata()

        self.assert_dicts_equal(expected_metadata, actual_metadata)
    def test_serializer_is_initialized_with_default_data_when_none_provided(
            self):
        expected_run_params_data = RunParametersSerializer.get_default_serialized_data(
        )

        actual_run_params_data = RunParametersSerializer().data

        self.assert_dicts_equal(expected_run_params_data,
                                actual_run_params_data)
Beispiel #3
0
 def get_POST_parameters_example():
     """
     Return an example of the validated data of a valid POST request to "simulate
     a model" endpoint
     """
     serialized_data = RunParametersSerializer.get_default_serialized_data()
     serializer = RunParametersSerializer(data=serialized_data)
     serializer.is_valid()
     deserialized_data = serializer.validated_data
     return deserialized_data
    def test_serializer_returns_correct_parameters(self):
        expected_params = RunParametersSerializer(self.run_params).data

        actual_params = self.data["parameters"]

        self.assert_not_empty(actual_params)
        self.assertEqual(actual_params, expected_params)
Beispiel #5
0
 def setUpTestData(cls):
     cls.default_run_parameters = RunParametersSerializer.get_default_serialized_data(
     )
     cls.url = reverse("api:simulate")
     cls.expected_fields = {"general", "regional"}
     cls.all_simus_before = list(LAWMSimulation.objects.all())
     cls.all_run_parameters_before = list(LAWMRunParameters.objects.all())
Beispiel #6
0
class DescriptiveFieldsMetadataTest(TestCase):
    def setUp(self):
        self.serializer = RunParametersSerializer()
        self.metadater = DescriptiveFieldsMetadater()

    def test_metadater_for_serializer_includes_expected_fields(self):
        expected_metadata = self.serializer.get_metadata()

        actual_metadata = self.metadater.get_serializer_info(self.serializer)

        self.assertEqual(expected_metadata, actual_metadata)
    def test_serializer_class_method_returns_correct_default_values_serialized(
            self):
        expected_run_params_data = {
            "general":
            GeneralParametersSerializer.get_default_serialized_data(),
            "regional":
            ManyRegionalParametersSerializer.get_default_serialized_data(),
        }

        actual_run_params_data = RunParametersSerializer.get_default_serialized_data(
        )

        self.assert_dicts_equal(expected_run_params_data,
                                actual_run_params_data)
Beispiel #8
0
    def test_simulate_OPTIONS_returns_correct_metadata_when_logged_in(self):
        serializer = RunParametersSerializer()

        user = User.objects.create_user('username', 'plsdonthack')
        self.client.force_login(user)

        options_response = self.client.options(self.url)
        actual_actions_post = options_response.data["actions"]["POST"]

        metadater_class_used = self.get_metadata_class_from_response(
            options_response)
        expected_actions_post = metadater_class_used().get_serializer_info(
            serializer)

        self.assert_dicts_equal(actual_actions_post, expected_actions_post)
    def test_when_instance_is_provided_the_data_returned_should_be_correct(
            self):
        value = 2323
        simu = LAWMSimulation.objects.create()
        general_parameters = LAWMGeneralParameters.objects.create(
            simulation_stop=value)
        run_parameters = LAWMRunParameters.objects.create(
            simulation=simu, general_parameters=general_parameters)

        expected_gen_params_data = GeneralParametersSerializer.get_default_serialized_data(
        )
        expected_gen_params_data["simulation_stop"] = value

        expected_run_params_data = {
            "general": expected_gen_params_data,
            "regional": {}
        }

        actual_run_params_data = RunParametersSerializer(run_parameters).data

        self.assert_dicts_equal(expected_run_params_data,
                                actual_run_params_data)
    def test_with_valid_inputs_saves_to_db_correctly(self):
        expected_simulation_stop = 2005
        expected_developed_max_calories = 2600
        expected_general_parameters = LAWMGeneralParameters(
            simulation_stop=expected_simulation_stop)
        expected_all_regional_params = LAWMRegionalParameters.new_in_memory_with_defaults_all_regions(
        )
        expected_all_regional_params["developed"].max_calories = 2600

        simu = LAWMSimulation.objects.create()
        data = RunParametersSerializer.get_default_serialized_data()
        data["general"]["simulation_stop"] = expected_simulation_stop
        data["regional"]["developed"][
            "max_calories"] = expected_developed_max_calories

        serializer = RunParametersSerializer(data=data)
        serializer.is_valid(raise_exception=True)
        serializer.save(simulation=simu)

        actual_run_parameters = simu.run_parameters
        self.assertIsNotNone(actual_run_parameters)

        actual_general_parameters = actual_run_parameters.general_parameters
        self.assertIsNotNone(actual_general_parameters)
        general_params_fields_to_ignore = ["id"]
        self.assert_equal_in_memory_django_models(
            expected_general_parameters, actual_general_parameters,
            general_params_fields_to_ignore)

        actual_all_regional_params = actual_run_parameters.regional_parameters.all(
        )
        for reg_name in expected_all_regional_params:
            expected_reg_params = expected_all_regional_params[reg_name]
            actual_reg_params_qs = actual_all_regional_params.filter(
                region__name=reg_name)
            self.assert_has_length(actual_reg_params_qs, 1)
            actual_reg_params = actual_reg_params_qs[0]
            reg_params_fields_to_ignore = ["id", "run_parameters_id"]
            self.assert_equal_in_memory_django_models(
                expected_reg_params, actual_reg_params,
                reg_params_fields_to_ignore)
Beispiel #11
0
class SimulationDetailSerializer(serializers.HyperlinkedModelSerializer):
    url        = serializers.HyperlinkedIdentityField(view_name="api:simulation-detail")
    regions    = serializers.SerializerMethodField('get_regions_urls')
    parameters = RunParametersSerializer(source="run_parameters")

    class Meta:
        model = LAWMSimulation
        fields = ["created", "url", "regions", "parameters"]

    def get_regions_urls(self, obj):
        request = self.context['request']
        regions_urls = {
            reg_res.region_name: self.get_url_from_region_result(reg_res, request)
            for reg_res in obj.region_results.all()
        }
        return regions_urls

    @staticmethod
    def get_url_from_region_result(reg_res, request):
        args = [reg_res.simulation_id, reg_res.region_name]
        relative_url = reverse("api:regionresult-detail", args=args)
        absolute_url = request.build_absolute_uri(relative_url)
        return absolute_url
Beispiel #12
0
 def retrieve(self, request, *args, **kwargs):
     """
     Get an example of the data to send to the POST request. Includes default values.
     """
     default_params = RunParametersSerializer.get_default_serialized_data()
     return Response(default_params)
 def setUpTestData(cls):
     db_tree = cls.create_full_simulation_db_tree()
     cls.simu = db_tree.simu
     cls.run_parameters = db_tree.run_parameters
     serializer = RunParametersSerializer(cls.run_parameters)
     cls.data = serializer.data
Beispiel #14
0
 def setUp(self):
     self.serializer = RunParametersSerializer()
     self.metadater = DescriptiveFieldsMetadater()