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_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")