def test_template_value_set_copy_variable_function_afterwards(self): p = Project("project") ct = ConfigTemplate(name="my template", template_content="not a real config", project=p) ct.update_template_variable("var 1") ct.update_template_variable("var 2") ct.update_template_variable("var 3") db.session.add_all([p, ct]) db.session.commit() # verify database content self.assertTrue(len(ConfigTemplate.query.all()) == 1) self.assertTrue(len(TemplateVariable.query.all()) == 3 + 1) # create a new template value set without link to a config template tvs = TemplateValueSet(hostname="Value Set") # define it afterwards (you need to trigger the copy process manually) tvs.config_template = ct tvs.copy_variables_from_config_template( ) # this method will automatically create a 'hostname' variable value self.assertTrue(len(TemplateValue.query.all()) == 4) self.assertTrue(tvs.is_value_defined("var_1")) self.assertTrue(tvs.is_value_defined("var_2")) self.assertTrue(tvs.is_value_defined("var_3")) self.assertTrue(tvs.is_value_defined("hostname"))
def test_variable_name_conversion(self): p = Project("project") ct = ConfigTemplate("config template", project=p) tvs = TemplateValueSet("My Template Value set", config_template=ct) db.session.add_all([p, ct, tvs]) tvs.update_variable_value("First Test") self.assertTrue(tvs.is_value_defined("First_Test")) tvs.update_variable_value("first_test") self.assertTrue(len(TemplateValueSet.query.all()) == 1) self.assertTrue(tvs.is_value_defined("first_test")) variable_map = ( ("Value ", "Value"), ("the variable name", "the_variable_name"), ("%&/__ASDF ", "ASDF"), ) for string, expected_result in variable_map: self.assertEqual(tvs.convert_variable_name(string), expected_result)
def test_template_value_set_update_value(self): p = Project("Project") ct1 = ConfigTemplate(name="first script", project=p) tvs1 = TemplateValueSet(hostname="tvs1", config_template=ct1) tvs2 = TemplateValueSet(hostname="tvs2", config_template=ct1) db.session.add(p) db.session.add(ct1) db.session.add(tvs1) db.session.add(tvs2) db.session.commit() # verify that the variables are not defined self.assertFalse(tvs1.is_value_defined("first variable")) self.assertFalse(tvs1.is_value_defined("second variable")) self.assertFalse(tvs2.is_value_defined("first variable")) # add variables to the first configuration template tvs1var1_value = "description for first tvs1 variable" tvs1.update_variable_value("first variable", tvs1var1_value) tvs1.update_variable_value("second variable", "value for second tvs1 variable") tvs2.update_variable_value("first variable", "value for first tvs2 variable") self.assertTrue(len(tvs1.values.all()) == 2 + 1) self.assertTrue(len(tvs2.values.all()) == 1 + 1) # update value tvs1var1_value_mod = "modified description" tvs1.update_variable_value("first variable", tvs1var1_value_mod) tvs1var1 = tvs1.get_template_value_by_name("first_variable") self.assertNotEqual(tvs1var1.value, tvs1var1_value) self.assertEqual(tvs1var1.value, tvs1var1_value_mod)
def test_template_value_set_copy_variable_function_during_creation(self): p = Project("project") ct = ConfigTemplate(name="my template", template_content="not a real config", project=p) self.assertEqual(ct.update_template_variable("var 1"), "var_1") self.assertEqual(ct.update_template_variable("var 2"), "var_2") self.assertEqual(ct.update_template_variable("var 3"), "var_3") db.session.add_all([p, ct]) db.session.commit() # verify database content self.assertTrue(len(ConfigTemplate.query.all()) == 1) self.assertTrue(len(TemplateVariable.query.all()) == 3 + 1) # create a new template value set and verify that template variables are copied to the TemplateValueSet object tvs = TemplateValueSet(hostname="Value Set", config_template=ct) self.assertTrue(len(TemplateValue.query.all()) == 4) self.assertTrue(tvs.is_value_defined("hostname")) self.assertTrue(tvs.is_value_defined("var_1")) self.assertTrue(tvs.is_value_defined("var_2")) self.assertTrue(tvs.is_value_defined("var_3")) var_1_value = "My Value" tvs.update_variable_value("var_1", var_1_value) # call it again and verify that nothing has changed tvs.copy_variables_from_config_template() self.assertTrue(len(TemplateValue.query.all()) == 4) self.assertTrue(tvs.is_value_defined("hostname")) self.assertTrue(tvs.is_value_defined("var_1")) self.assertTrue(tvs.is_value_defined("var_2")) self.assertTrue(tvs.is_value_defined("var_3")) # test that old values are preserved self.assertEqual(tvs.get_template_value_by_name_as_string("var_1"), var_1_value) self.assertEqual(tvs.get_template_value_by_name_as_string("hostname"), tvs.hostname)
def test_template_value_set_add_variable_and_lookup(self): # create test data p = Project(name="project") ct = ConfigTemplate(name="template", project=p) tvs1 = TemplateValueSet(hostname="tvs1", config_template=ct) tvs2 = TemplateValueSet(hostname="tvs2", config_template=ct) tvs3 = TemplateValueSet(hostname="tvs3", config_template=ct) tvs4 = TemplateValueSet(hostname="tvs4", config_template=ct) db.session.add_all([p, ct, tvs1, tvs2, tvs3, tvs4]) db.session.commit() # verify that the variables are not defined self.assertFalse(tvs1.is_value_defined("first variable")) self.assertFalse(tvs1.is_value_defined("second variable")) self.assertFalse(tvs1.is_value_defined("first variable")) # add variables to the first configuration template tvs1var1_value = "value for first TVS1 variable" tvs1var2_value = "value for second TVS1 variable" tvs1.update_variable_value("first variable", tvs1var1_value) tvs1.update_variable_value("second variable", tvs1var2_value) self.assertFalse(tvs2.is_value_defined("first variable")) db.session.commit() # add variables to the second configuration template tvs2var1_value = "value for first TVS2 variable" tvs2.update_variable_value("first variable", tvs2var1_value) db.session.commit() # retrieve variables self.assertTrue(len(tvs1.values.all()) == 2 + 1) self.assertTrue(len(tvs2.values.all()) == 1 + 1) self.assertTrue( tvs1.is_value_defined( tvs1.convert_variable_name("first variable"))) self.assertTrue( tvs1.is_value_defined( tvs1.convert_variable_name("second variable"))) self.assertTrue( tvs2.is_value_defined( tvs1.convert_variable_name("first variable"))) tvs1var1 = tvs1.get_template_value_by_name( tvs1.convert_variable_name("first variable")) self.assertTrue(type(tvs1var1) is TemplateValue) self.assertEqual(tvs1var1.template_value_set, tvs1) self.assertEqual(tvs1var1.value, tvs1var1_value) tvs1var2 = tvs1.get_template_value_by_name( tvs1.convert_variable_name("second variable")) self.assertTrue(type(tvs1var2) is TemplateValue) self.assertEqual(tvs1var2.template_value_set, tvs1) self.assertEqual(tvs1var2.value, tvs1var2_value) tvs2var1 = tvs2.get_template_value_by_name( tvs1.convert_variable_name("first variable")) self.assertTrue(type(tvs2var1) is TemplateValue) self.assertEqual(tvs2var1.template_value_set, tvs2) self.assertEqual(tvs2var1.value, tvs2var1_value) # invalid template lookup with self.assertRaises(TemplateValueNotFoundException): tvs1.get_template_value_by_name("unknown key")