def test_param_change_class(self): """Test the ParamChange class.""" # Raise an error because the name is invalid. with self.assertRaisesRegexp(utils.ValidationError, 'Only parameter names'): param_domain.ParamChange('¡hola', 'Copier', {}) # Raise an error because no such generator type exists. with self.assertRaisesRegexp(utils.ValidationError, 'Invalid generator id'): param_domain.ParamChange('abc', 'InvalidGenerator', {}) # Raise an error because the given generator requires initialization # args. with self.assertRaisesRegexp(utils.ValidationError, 'require any initialization'): param_domain.ParamChange('abc', 'RestrictedCopier', {}) # Raise an error because customization_args is not a dict. with self.assertRaisesRegexp(utils.ValidationError, 'Expected a dict'): param_domain.ParamChange('abc', 'Copier', ['a', 'b']) param_change = param_domain.ParamChange('abc', 'Copier', {'value': '3'}) self.assertEqual(param_change.name, 'abc') self.assertEqual(param_change.generator.id, 'Copier') self.assertEqual( param_change.to_dict(), { 'name': 'abc', 'generator_id': 'Copier', 'customization_args': { 'value': '3' } }) self.assertEqual(param_change.get_normalized_value('Int', {}), 3)
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_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_param_change_validation(self): """Test validation of parameter changes.""" # Raise an error because the name is invalid. with self.assertRaisesRegexp(utils.ValidationError, 'Only parameter names'): param_domain.ParamChange('¡hola', 'Copier', {}).validate() # Raise an error because no such generator type exists. with self.assertRaisesRegexp(utils.ValidationError, 'Invalid generator id'): param_domain.ParamChange('abc', 'InvalidGenerator', {}).validate() # Raise an error because customization_args is not a dict. with self.assertRaisesRegexp(utils.ValidationError, 'Expected a dict'): param_domain.ParamChange('abc', 'Copier', ['a', 'b']).validate()
def test_param_change_class(self): """Test the ParamChange class.""" param_change = param_domain.ParamChange( 'abc', 'Copier', {'value': '3'}) self.assertEqual(param_change.name, 'abc') self.assertEqual(param_change.generator.id, 'Copier') self.assertEqual(param_change.to_dict(), { 'name': 'abc', 'generator_id': 'Copier', 'customization_args': {'value': '3'} }) self.assertEqual(param_change.get_normalized_value('Int', {}), 3)
def test_param_change_validation(self): """Test validation of parameter changes.""" # Raise an error because the name is invalid. with self.assertRaisesRegexp(utils.ValidationError, 'Only parameter names'): param_domain.ParamChange('¡hola', 'Copier', {}).validate() # Raise an error because generator ID is not string. with self.assertRaisesRegexp(utils.ValidationError, 'Expected generator ID to be a string'): param_domain.ParamChange('abc', 123, {}).validate() # Raise an error because no such generator type exists. with self.assertRaisesRegexp(utils.ValidationError, 'Invalid generator ID'): param_domain.ParamChange('abc', 'InvalidGenerator', {}).validate() # Raise an error because customization_args is not a dict. with self.assertRaisesRegexp(utils.ValidationError, 'Expected a dict'): param_domain.ParamChange('abc', 'Copier', ['a', 'b']).validate() # Raise an error because the param_change name is not a string. with self.assertRaisesRegexp( utils.ValidationError, 'Expected param_change name to be a string, received'): param_domain.ParamChange(3, 'Copier', {}).validate() # Raise an error because the arg names in customization_args are not # strings. with self.assertRaisesRegexp( Exception, 'Invalid parameter change customization_arg name:'): param_domain.ParamChange('abc', 'Copier', {1: '1'}).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_change_validation(self) -> None: """Test validation of parameter changes.""" # Raise an error because the name is invalid. with self.assertRaisesRegex( # type: ignore[no-untyped-call] utils.ValidationError, 'Only parameter names'): param_domain.ParamChange( '¡hola', 'Copier', self.sample_customization_args).validate() # Raise an error because generator ID is not string. with self.assertRaisesRegex( # type: ignore[no-untyped-call] utils.ValidationError, 'Expected generator ID to be a string'): # TODO(#13059): After we fully type the codebase we plan to get # rid of the tests that intentionally test wrong inputs that we # can normally catch by typing. param_domain.ParamChange( 'abc', 123, # type: ignore[arg-type] self.sample_customization_args).validate() # Raise an error because no such generator type exists. with self.assertRaisesRegex( # type: ignore[no-untyped-call] utils.ValidationError, 'Invalid generator ID'): param_domain.ParamChange( 'abc', 'InvalidGenerator', self.sample_customization_args).validate() # Raise an error because customization_args is not a dict. with self.assertRaisesRegex( # type: ignore[no-untyped-call] utils.ValidationError, 'Expected a dict'): # TODO(#13059): After we fully type the codebase we plan to get # rid of the tests that intentionally test wrong inputs that we # can normally catch by typing. param_domain.ParamChange( 'abc', 'Copier', ['a', 'b']).validate() # type: ignore[arg-type] # Raise an error because the param_change name is not a string. with self.assertRaisesRegex( # type: ignore[no-untyped-call] utils.ValidationError, 'Expected param_change name to be a string, received'): # TODO(#13059): After we fully type the codebase we plan to get # rid of the tests that intentionally test wrong inputs that we # can normally catch by typing. param_domain.ParamChange( 3, # type: ignore[arg-type] 'Copier', self.sample_customization_args).validate() # Raise an error because the arg names in customization_args are not # strings. with self.assertRaisesRegex( # type: ignore[no-untyped-call] Exception, 'Invalid parameter change customization_arg name:'): # TODO(#13059): After we fully type the codebase we plan to get # rid of the tests that intentionally test wrong inputs that we # can normally catch by typing. param_domain.ParamChange('abc', 'Copier', { 1: '1' }).validate() # type: ignore[misc]
def test_param_change_class(self) -> None: """Test the ParamChange class.""" # The argument `customization_args` of ParamChange() can only accept # values of type CustomizationArgsDict, but for testing purposes here # we are providing dictionary that only contain `value` key. Thus to # avoid MyPy's missing keys error, we added an ignore here. param_change = param_domain.ParamChange( 'abc', 'Copier', {'value': '3'}) # type: ignore[typeddict-item] self.assertEqual(param_change.name, 'abc') self.assertEqual(param_change.generator.id, 'Copier') self.assertEqual( param_change.to_dict(), { 'name': 'abc', 'generator_id': 'Copier', 'customization_args': { 'value': '3' } }) self.assertEqual(param_change.get_value({}), '3')