Exemple #1
0
 def __init__(self):
     self.simu    = LAWMSimulation.objects.create()
     self.region_africa = LAWMRegion.objects.get_or_create(name=Africa.name)[0]
     self.region_asia = LAWMRegion.objects.get_or_create(name=Asia.name)[0]
     self.region_developed = LAWMRegion.objects.get_or_create(name=Developed.name)[0]
     self.region_la = LAWMRegion.objects.get_or_create(name=Latinamerica.name)[0]
     self.region_result_r1 = LAWMRegionResult.objects.create(simulation=self.simu, region=self.region_africa)
     self.region_result_r2 = LAWMRegionResult.objects.create(simulation=self.simu, region=self.region_developed)
     self.year_results_reg_1 = self.create_year_results(self.region_result_r1, n_years=2)
     self.year_results_reg_2 = self.create_year_results(self.region_result_r2, n_years=2)
     self.general_parameters  = LAWMGeneralParameters.objects.create(simulation_stop=2001)
     self.run_parameters      = LAWMRunParameters.objects.create(
         general_parameters=self.general_parameters,
         simulation=self.simu
     )
     self.regional_parameters_developed = LAWMRegionalParameters.new_with_defaults_for_region(self.run_parameters, self.region_developed)
     self.regional_parameters_latinamerica = LAWMRegionalParameters.new_with_defaults_for_region(self.run_parameters, self.region_la)
     self.regional_parameters_africa = LAWMRegionalParameters.new_with_defaults_for_region(self.run_parameters, self.region_africa)
     self.regional_parameters_asia = LAWMRegionalParameters.new_with_defaults_for_region(self.run_parameters, self.region_asia)
     self.all_reg_params = [
         self.regional_parameters_developed,
         self.regional_parameters_latinamerica,
         self.regional_parameters_africa,
         self.regional_parameters_asia,
     ]
    def test_validators_are_automatically_added(self):
        expected_max_calories_error_message = 'Ensure this value is less than or equal to 3200.0. Send an OPTIONS ' \
                                              'request for more information.'

        overflowed_max_calories_creation_kwargs = self.get_creation_kwargs_with_overflowed_max_calories(
        )

        gen_params = LAWMRegionalParameters(
            **overflowed_max_calories_creation_kwargs)
        try:
            gen_params.full_clean()
            self.fail("An error should've been raised but wasn't.")
        except ValidationError as e:
            actual_error_dict = e.error_dict
            expected_error_keys = {"max_calories"}
            actual_error_keys = actual_error_dict.keys()
            self.assertEqual(expected_error_keys, actual_error_keys)
            actual_max_calories_errors = actual_error_dict["max_calories"]
            self.assert_has_length(actual_max_calories_errors, 1)
            actual_max_calories_error_messages = actual_max_calories_errors[
                0].messages
            self.assert_has_length(actual_max_calories_error_messages, 1)
            actual_error_message = actual_max_calories_error_messages[0]
            self.assertEqual(expected_max_calories_error_message,
                             actual_error_message)
    def test_get_metadata_returns_correct_info(self):
        expected_metadata = {
            "general": LAWMGeneralParameters.get_metadata(),
            "regional": LAWMRegionalParameters.get_metadata(),
        }
        actual_metadata = LAWMRunParameters.get_metadata()

        self.assert_dicts_equal(expected_metadata, actual_metadata)
 def get_default_serialized_data(cls):
     reg_params_per_region = LAWMRegionalParameters.new_in_memory_with_defaults_all_regions(
     )
     all_reg_params = reg_params_per_region.values()
     serializer = RegionalParametersSerializer(many=True)
     # I have to use the "to_representation" because when called in isolation with "many=True",
     # the data returned doesn't call "to_representation" but when used as a nested serializer,
     # it does call "to_representation"
     data = serializer.to_representation(all_reg_params)
     return data
 def get_expected_serialized_data_for_regional_params(
         db_regions, run_parameters):
     expected_reg_params_data = {}
     for reg in db_regions:
         reg_params = LAWMRegionalParameters.new_with_defaults_for_region(
             run_parameters, reg)
         reg_params_data = RegionalParametersSerializer(reg_params).data
         del reg_params_data["region"]
         region_name = reg.name
         expected_reg_params_data[region_name] = reg_params_data
     return expected_reg_params_data
    def get_creation_kwargs_with_overflowed_max_calories():
        run_parameters = LAWMRunParameters()
        # Uses the standard run region so depends on it
        region = LAWMRegion(name="developed")
        partial_creation_kwargs = LAWMRegionalParameters.get_default_values_for_region(
            region.name)
        partial_creation_kwargs["max_calories"] = 99999

        creation_kwargs = partial_creation_kwargs.copy()
        creation_kwargs["run_parameters"] = run_parameters
        creation_kwargs["region"] = region
        return creation_kwargs
    def test_new_in_memory_objects_for_all_regions_works(self):
        expected_developed_defaults, _, _ = self.get_developed_region_defaults(
        )

        actual_reg_params_per_region = LAWMRegionalParameters.new_in_memory_with_defaults_all_regions(
        )

        actual_regions = actual_reg_params_per_region.keys()
        self.assert_has_length(actual_regions, len(DEFAULT_REGIONS))
        actual_developed_defaults = actual_reg_params_per_region[
            Developed.name]
        self.assert_equal_in_memory_django_models(expected_developed_defaults,
                                                  actual_developed_defaults)
    def test_new_in_memory_objects_for_all_doesnt_modify_the_db(self):
        expected_db_reg_params_per_region = LAWMRegionalParameters.objects.all(
        )

        _ = LAWMRegionalParameters.new_in_memory_with_defaults_all_regions()

        actual_db_reg_params_per_region = LAWMRegionalParameters.objects.all()

        self.assert_have_equal_length(expected_db_reg_params_per_region,
                                      actual_db_reg_params_per_region)
        for first_obj, second_obj in zip(expected_db_reg_params_per_region,
                                         actual_db_reg_params_per_region):
            self.assertEqual(first_obj, second_obj)
def create_region_parameters(LAWMRegionalParameters, objects_to_save, region,
                             region_name, run_parameters):
    # CAREFUL!
    # We are using the models.py class to use its classmethod, but only because this
    # method has no side effect and doesn't modify the database
    partial_kwargs = VOLATILE_LAWMRegionalParameters.get_default_values_for_region(
        region_name)
    reg_params_kwargs = partial_kwargs | {
        "run_parameters": run_parameters,
        "region": region
    }
    reg_params = create_new_object(LAWMRegionalParameters, reg_params_kwargs,
                                   objects_to_save)
 def assert_regional_parameters_were_initialized_correctly(
         self, reg_parameters):
     self.assertIsNotNone(reg_parameters)
     reg_params_per_region = LAWMRegionalParameters.new_in_memory_with_defaults_all_regions(
     )
     for reg in DEFAULT_REGIONS:
         region_name = reg.name
         potential_regions = reg_parameters.filter(region__name=region_name)
         self.assert_has_length(potential_regions, 1)
         db_region_params = potential_regions[0]
         expected_region_params = reg_params_per_region[region_name]
         fields_to_ignore = ["id", "run_parameters_id"]
         self.assert_equal_in_memory_django_models(expected_region_params,
                                                   db_region_params,
                                                   fields_to_ignore)
    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)
    def test_get_metadata_returns_correct_information(self):
        expected_metadata = {
            "max_calories":
            MaxCalories.info_as_dict(),
            "max_build_cost":
            MaxBuildCost.info_as_dict(),
            "tech_prog_coeff_1":
            TechProgressCoefficient1.info_as_dict(),
            "tech_prog_coeff_2":
            TechProgressCoefficient2.info_as_dict(),
            "tech_prog_coeff_3":
            TechProgressCoefficient3.info_as_dict(),
            "tech_prog_coeff_4":
            TechProgressCoefficient4.info_as_dict(),
            "tech_prog_coeff_5":
            TechProgressCoefficient5.info_as_dict(),
            "tech_prog_stop":
            TechProgressStop.info_as_dict(),
            "years_building_cost_eq":
            YearsForBuildingCostEquality.info_as_dict(),
            "years_housing_level_eq":
            YearsForHousingLevelEquality.info_as_dict(),
            "desired_food_stock":
            DesiredFoodStock.info_as_dict(),
            "years_space_p_person_eq":
            YearsForSpacePerPersonEquality.info_as_dict(),
            "max_space_p_person":
            MaxSpacePerPerson.info_as_dict(),
            "desired_space_p_person":
            DesiredSpacePerPerson.info_as_dict(),
            "max_sec_5_gnp_propor":
            MaxCapitalGoodsGNPProportion.info_as_dict(),
        }
        actual_metadata = LAWMRegionalParameters.get_metadata()

        self.assertEqual(expected_metadata, actual_metadata)
 def get_developed_region_defaults(self):
     run_parameters = LAWMRunParameters()
     region = LAWMRegion(name="developed")
     dev_defaults = LAWMRegionalParameters.new_with_defaults_for_region(
         run_parameters, region)
     return dev_defaults, region, run_parameters