def test_get_init_params(self): """Test the get_init_params() method.""" exploration = exp_domain.Exploration.create_default_exploration( 'eid', 'A title', 'A category') independent_pc = param_domain.ParamChange('a', 'Copier', { 'value': 'firstValue', 'parse_with_jinja': False }) dependent_pc = param_domain.ParamChange('b', 'Copier', { 'value': '{{a}}', 'parse_with_jinja': True }) exploration.param_specs = { 'a': param_domain.ParamSpec('UnicodeString'), 'b': param_domain.ParamSpec('UnicodeString'), } exploration.param_changes = [independent_pc, dependent_pc] new_params = exploration.get_init_params() self.assertEqual(new_params, {'a': 'firstValue', 'b': 'firstValue'}) exploration.param_changes = [dependent_pc, independent_pc] # Jinja string evaluation fails gracefully on dependencies that do not # exist. new_params = exploration.get_init_params() self.assertEqual(new_params, {'a': 'firstValue', 'b': ''})
def test_update_learner_params(self): """Test the update_learner_params() method.""" independent_pc = param_domain.ParamChange('a', 'Copier', { 'value': 'firstValue', 'parse_with_jinja': False }) dependent_pc = param_domain.ParamChange('b', 'Copier', { 'value': '{{a}}', 'parse_with_jinja': True }) exp_param_specs = { 'a': param_domain.ParamSpec('UnicodeString'), 'b': param_domain.ParamSpec('UnicodeString'), } old_params = {} new_params = self.get_updated_param_dict( old_params, [independent_pc, dependent_pc], exp_param_specs) self.assertEqual(new_params, {'a': 'firstValue', 'b': 'firstValue'}) self.assertEqual(old_params, {}) old_params = {'a': 'secondValue'} new_params = self.get_updated_param_dict(old_params, [dependent_pc], exp_param_specs) self.assertEqual(new_params, {'a': 'secondValue', 'b': 'secondValue'}) self.assertEqual(old_params, {'a': 'secondValue'}) # Jinja string evaluation fails gracefully on dependencies that do not # exist. old_params = {} new_params = self.get_updated_param_dict(old_params, [dependent_pc], exp_param_specs) self.assertEqual(new_params, {'b': ''}) self.assertEqual(old_params, {})
def test_param_spec_validation(self) -> None: """Test validation of param specs.""" param_spec = param_domain.ParamSpec('Real') with self.assertRaisesRegex( # type: ignore[no-untyped-call] utils.ValidationError, 'is not among the supported object types'): param_spec.validate() # Restore a valid parameter spec. param_spec.obj_type = 'UnicodeString' param_spec.validate()
def test_cannot_get_updated_param_dict_with_invalid_param_name(self): param_change_list = [ param_domain.ParamChange('a', 'Copier', { 'value': 'firstValue', 'parse_with_jinja': False }) ] exp_param_specs = { 'b': param_domain.ParamSpec('UnicodeString'), } with self.assertRaisesRegex(Exception, 'Parameter a not found'): self.get_updated_param_dict({}, param_change_list, exp_param_specs)
def test_param_spec_validation(self): """Test validation of param specs.""" param_spec = param_domain.ParamSpec('FakeType') with self.assertRaisesRegex(TypeError, 'is not a valid object class'): param_spec.validate() param_spec.obj_type = 'Real' with self.assertRaisesRegex(utils.ValidationError, 'is not among the supported object types'): param_spec.validate() # Restore a valid parameter spec. param_spec.obj_type = 'UnicodeString' param_spec.validate()
def test_param_spec_validation(self): """Test validation of param specs.""" param_spec = param_domain.ParamSpec('FakeType') with self.assertRaisesRegexp( TypeError, '\'FakeType\' is not a valid object class.'): param_spec.validate() param_spec.obj_type = 'Real' with self.assertRaisesRegexp( utils.ValidationError, 'Only \'UnicodeString\' is the ' 'supported object type for parameters, not: Real'): param_spec.validate() # Restore a valid parameter spec. param_spec.obj_type = 'UnicodeString' param_spec.validate()
def test_param_spec_to_dict(self) -> None: sample_dict = {'obj_type': 'UnicodeString'} param_spec = param_domain.ParamSpec(sample_dict['obj_type']) self.assertEqual(param_spec.to_dict(), sample_dict)