def test_get_content_from_template(self): snippet = spec.Snippet(base_directory=self.enviroment_directory, template='a = {{{ a }}}', variables=spec.Variables( spec.Variable(name='a', value=10))) self.assertEqual(snippet.render(), 'a = 10')
def test_iterate_input_variables(self): variable1 = spec.Variable(name='testvariable1', value='a') variable2 = spec.Variable(name='testvariable2', value='b') variable3 = spec.Variable(name='testvariable3', generator='debug') variables = spec.Variables(variable1, variable2, variable3) input_variables = list(variables.iterate_input_variables()) self.assertEqual(input_variables, [variable1, variable2])
def test_validate_spec_invalid_variable(self): variables = spec.Variables() default = spec.Default(variables=variables, variable='testvariable') with self.assertRaisesRegex( exceptions.SpecValidationError, "Invalid variable referenced by default: 'testvariable'"): default.validate_spec(path='test')
def test_validate_value_valid(self): variables = spec.Variables() variable = spec.Variable(variables=variables, name='testname', validators=spec.ValueValidators( {'one_letter': {}})) variable.validate_value(value='x')
def test_values_as_dict(self): variable1 = spec.Variable(name='testvariable1', value='a') variable2 = spec.Variable(name='testvariable2', value='b') variables = spec.Variables(variable1, variable2) self.assertEqual(variables.values_as_dict(), { 'testvariable1': 'a', 'testvariable2': 'b' })
def test_add_variables(self): variable1 = spec.Variable(name='testvariable1') variable2 = spec.Variable(name='testvariable2') variables = spec.Variables() variables.add_variables(variable1, variable2) self.assertEqual(len(variables._variables), 2) self.assertEqual(variables._variables['testvariable1'], variable1) self.assertEqual(variables._variables['testvariable2'], variable2)
def test_validate_value_invalid(self): variables = spec.Variables() variable = spec.Variable(variables=variables, name='testname', validators=spec.ValueValidators( {'one_letter': {}})) with self.assertRaisesRegex(exceptions.ValueValidationError, 'Must be exactly one letter'): variable.validate_value(value='xx')
def test_deserialize_multiple_values(self): variables = spec.Variables() variables.deserialize(data={ 'testvariable1': {}, 'testvariable2': {}, }) self.assertEqual(len(variables._variables), 2) self.assertIn('testvariable1', variables._variables) self.assertIn('testvariable2', variables._variables)
def test_validate_spec_invalid_generator(self): variables = spec.Variables() variable = spec.Variable(variables=variables, name='testname', generator='invalidgenerator') with self.assertRaisesRegex( exceptions.SpecValidationError, "Invalid generator: 'invalidgenerator'. Must be one of:"): variable.validate_spec(path='test')
def test_validate_spec_arguments_without_generator(self): variables = spec.Variables() variable = spec.Variable(variables=variables, name='testname', arguments={'a': 10}) with self.assertRaisesRegex( exceptions.SpecValidationError, 'Can not use "arguments" without a "generator".'): variable.validate_spec(path='test')
def test_deserialize_one_value(self): variables = spec.Variables() variables.deserialize(data={'testvariable': {}}) self.assertEqual(len(variables._variables), 1) self.assertIn('testvariable', variables._variables) self.assertIsInstance(variables._variables['testvariable'], spec.Variable) self.assertEqual(variables._variables['testvariable'].name, 'testvariable')
def test_validate_spec_both_generator_and_default(self): variables = spec.Variables() variable = spec.Variable( variables=variables, name='testname', generator='debug', default=spec.Default(variables=mock.MagicMock())) with self.assertRaisesRegex( exceptions.SpecValidationError, 'Can not provide both "default" and "generator".'): variable.validate_spec(path='test')
def test_set_variable_values(self): variables = spec.Variables() variables._variables['testvariable1'] = spec.Variable( name='testvariable') variables._variables['testvariable2'] = spec.Variable( name='testvariable') self.assertEqual(variables._variables['testvariable1'].value, None) self.assertEqual(variables._variables['testvariable2'].value, None) variables.set_variable_values(testvariable1='a', testvariable2='b') self.assertEqual(variables._variables['testvariable1'].value, 'a') self.assertEqual(variables._variables['testvariable2'].value, 'b')
def test_validate_value_no_validators(self): variables = spec.Variables() variable = spec.Variable(variables=variables, name='testname') variable.validate_value(value='x')
def test_get_variable_value_invalid_name(self): variables = spec.Variables() with self.assertRaisesRegex(exceptions.InvalidVariableName, 'Invalid variable name: testvariable'): variables.get_variable_value('testvariable')
def test_has_variable_true(self): variables = spec.Variables() variables._variables['testvariable'] = spec.Variable( name='testvariable', value='test') self.assertTrue(variables.has_variable('testvariable'))
def test_get_value_variable(self): variables = spec.Variables( spec.Variable(name='testvariable', value='variablevalue')) default = spec.Default(variables=variables, variable='testvariable') self.assertEqual(default.get_value(), 'variablevalue')
def test_get_variable_value(self): variables = spec.Variables() variables._variables['testvariable'] = spec.Variable( name='testvariable', value='test') self.assertEqual(variables.get_variable_value('testvariable'), 'test')
def test_add_variable(self): variable = spec.Variable(name='testvariable') variables = spec.Variables() variables.add_variable(variable) self.assertEqual(variables._variables['testvariable'], variable)
def test_deserialize_empty_data(self): variables = spec.Variables() variables.deserialize(data={}) self.assertEqual(variables._variables, OrderedDict())
def test_has_variable_false(self): variables = spec.Variables() self.assertFalse(variables.has_variable('testvariable'))