def test_project_with_multiple_config_templates(self): # create test data p1 = Project(name="Project with config templates") ct1 = ConfigTemplate(name="first", project=p1) ct2 = ConfigTemplate(name="second", project=p1) ct1.project = p1 ct2.project = p1 db.session.add(p1) db.session.add(ct1) db.session.add(ct2) db.session.commit() # read the project from the database received_p1 = Project.query.filter_by( name="Project with config templates").first() received_ct1 = received_p1.configtemplates.all()[0] received_ct2 = received_p1.configtemplates.all()[1] self.assertEqual(p1, received_p1) self.assertEqual(ct1, received_ct1) self.assertEqual(ct2, received_ct2)
def test_config_template_update_variable(self): p1 = Project(name="Project 1") ct1 = ConfigTemplate(name="first script", project=p1) ct2 = ConfigTemplate(name="second script", project=p1) ct1.project = p1 ct2.project = p1 db.session.add(p1) db.session.add(ct1) db.session.add(ct2) db.session.commit() # verify that the variables are not defined self.assertFalse(ct1.is_variable_defined("first variable")) self.assertFalse(ct1.is_variable_defined("second variable")) self.assertFalse(ct2.is_variable_defined("first variable")) # add variables to the first configuration template ct1var1_desc = "description for first P1 variable" ct1.update_template_variable("first variable", ct1var1_desc) ct1.update_template_variable("second variable", "description for second P1 variable") ct2.update_template_variable("first variable", "description for first P2 variable") self.assertTrue(len(ct1.variables.all()) == 2 + 1) self.assertTrue(len(ct2.variables.all()) == 1 + 1) # update value ct1var1_desc_mod = "modified description" ct1.update_template_variable("first variable", ct1var1_desc_mod) ct1var1 = ct1.get_template_variable_by_name("first_variable") self.assertNotEqual(ct1var1.description, ct1var1_desc) self.assertEqual(ct1var1.description, ct1var1_desc_mod)
def test_config_template_add_variables_and_lookup(self): # create test data p1 = Project(name="Project 1") ct1 = ConfigTemplate(name="first script", project=p1) ct2 = ConfigTemplate(name="second script", project=p1) ct1.project = p1 ct2.project = p1 db.session.add(p1) db.session.add(ct1) db.session.add(ct2) db.session.commit() # verify that the variables are not defined self.assertFalse(ct1.is_variable_defined("first variable")) self.assertFalse(ct1.is_variable_defined("second variable")) self.assertFalse(ct2.is_variable_defined("first variable")) # add variables to the first configuration template ct1var1_desc = "description for first P1 variable" ct1var2_desc = "description for second P1 variable" ct1.update_template_variable("first variable", ct1var1_desc) ct1.update_template_variable("second variable", ct1var2_desc) self.assertFalse(ct2.is_variable_defined("first variable")) db.session.commit() # add variables to the second configuration template ct2var1_desc = "description for first P2 variable" ct2.update_template_variable("first variable", ct2var1_desc) db.session.commit() # retrieve variables (hostname is automatically created) self.assertTrue(len(ct1.variables.all()) == 2 + 1) self.assertTrue(len(ct2.variables.all()) == 1 + 1) self.assertTrue(ct1.is_variable_defined("first_variable")) self.assertTrue(ct1.is_variable_defined("second_variable")) self.assertTrue(ct2.is_variable_defined("first_variable")) ct1var1 = ct1.get_template_variable_by_name("first_variable") self.assertTrue(type(ct1var1) is TemplateVariable) self.assertEqual(ct1var1.config_template, ct1) self.assertEqual(ct1var1.description, ct1var1_desc) ct1var2 = ct1.get_template_variable_by_name("second_variable") self.assertTrue(type(ct1var2) is TemplateVariable) self.assertEqual(ct1var2.config_template, ct1) self.assertEqual(ct1var2.description, ct1var2_desc) ct2var1 = ct2.get_template_variable_by_name("first_variable") self.assertTrue(type(ct2var1) is TemplateVariable) self.assertEqual(ct2var1.config_template, ct2) self.assertEqual(ct2var1.description, ct2var1_desc) # invalid template lookup with self.assertRaises(TemplateVariableNotFoundException): ct1.get_template_variable_by_name("unknown key")
def test_config_template_delete_cascade_option(self): p1 = Project(name="Project 1") ct1 = ConfigTemplate(name="first script", project=p1) ct2 = ConfigTemplate(name="second script", project=p1) ct1.project = p1 ct2.project = p1 ct1.update_template_variable("test 1") ct1.update_template_variable("test 2") ct1.update_template_variable("test 3") ct2.update_template_variable("other test 1") ct2.update_template_variable("other test 2") ct2.update_template_variable("other test 3") tvs1 = TemplateValueSet("valueset 1", ct1) tvs2 = TemplateValueSet("valueset 2", ct1) tvs3 = TemplateValueSet("valueset 1", ct2) tvs4 = TemplateValueSet("valueset 2", ct2) tvs5 = TemplateValueSet("valueset 3", ct2) db.session.add_all([p1, ct1, ct2, tvs1, tvs2, tvs3, tvs4, tvs5]) db.session.commit() # test cascade option when deleting objects (hostname is automatically created) self.assertTrue( len(TemplateVariable.query.all()) == 6 + 2, len(TemplateVariable.query.all())) self.assertTrue( len(TemplateValueSet.query.all()) == 5, len(TemplateValueSet.query.all())) db.session.delete(ct1) self.assertTrue(len(Project.query.all()) == 1) self.assertTrue( len(TemplateVariable.query.all()) == 3 + 1, len(TemplateVariable.query.all())) self.assertTrue( len(TemplateValueSet.query.all()) == 3, len(TemplateValueSet.query.all())) db.session.delete(ct2) self.assertTrue(len(Project.query.all()) == 1) self.assertTrue( len(TemplateVariable.query.all()) == 0, len(TemplateVariable.query.all())) self.assertTrue( len(TemplateValueSet.query.all()) == 0, len(TemplateValueSet.query.all()))
def add_config_template(project_id): """add a new Config Template :param project_id: :return: """ if not session.get('logged_in'): return render_template("login.html") else: parent_project = Project.query.filter( Project.id == project_id).first_or_404() form = ConfigTemplateForm(request.form) if form.validate_on_submit(): try: config_template = ConfigTemplate(name="", project=parent_project) config_template.name = form.name.data config_template.template_content = form.template_content.data config_template.project = parent_project db.session.add(config_template) db.session.commit() flash( "Config template <strong>%s</strong> successful created" % config_template.name, "success") return redirect( url_for("view_config_template", project_id=project_id, config_template_id=config_template.id)) except IntegrityError as ex: if "UNIQUE constraint failed" in str(ex): msg = "Config Template name already in use, please use another one" else: msg = "Config template was not created (unknown error, see log for details)" logger.error(msg, exc_info=True) flash(msg, "error") db.session.rollback() except Exception: msg = "Config template was not created (unknown error, see log for details)" logger.error(msg, exc_info=True) flash(msg, "error") return render_template("config_template/add_config_template.html", project_id=project_id, project=parent_project, form=form)
def test_config_template_name_validation_function(self): p1 = Project(name="Project 1") p2 = Project(name="Project 2") p1ct1 = ConfigTemplate(name="first", project=p1) p2ct2 = ConfigTemplate(name="first", project=p2) p1ct3 = ConfigTemplate(name="second", project=p1) p1ct1.project = p1 p2ct2.project = p2 p1ct3.project = p1 db.session.add(p1) db.session.add(p1ct1) db.session.commit() # test validation of the config template name strings self.assertFalse(p1.valid_config_template_name("first")) self.assertFalse(p1.valid_config_template_name("second")) self.assertTrue(p1.valid_config_template_name("first1")) self.assertTrue(p2.valid_config_template_name("second"))
def test_config_template_name_constraint(self): # create test data p1_name = "Project with config templates" p2_name = "Another Project with config templates" p2ct1_template_content = "Code for the second project" p1 = Project(name=p1_name) p2 = Project(name=p2_name) p1ct1 = ConfigTemplate(name="first", project=p1) p1ct2 = ConfigTemplate(name="second", project=p1) p2ct1 = ConfigTemplate(name="first", template_content=p2ct1_template_content, project=p2) p1ct1.project = p1 p1ct2.project = p1 p2ct1.project = p2 db.session.add_all([p1, p2, p1ct1, p1ct2, p2ct1]) db.session.commit() # get project 1 data and verify results received_p1 = Project.query.filter_by(name=p1_name).first() self.assertEqual(received_p1, p1) # verify results query_result = received_p1.configtemplates.all() self.assertTrue(len(query_result) == 2) # get configuration templates for project 1 query_result = received_p1.configtemplates.filter_by( name="first").all() self.assertTrue(len(query_result) == 1, "Too many results received") received_p1ct1 = query_result[0] self.assertEqual(received_p1ct1, p1ct1) self.assertNotEqual(received_p1ct1.template_content, p2ct1_template_content) # get project 2 data and verify results received_p2 = Project.query.filter_by(name=p2_name).first() self.assertEqual(received_p2, p2) # verify results query_result = received_p2.configtemplates.all() self.assertTrue(len(query_result) == 1) # get configuration templates for project 2 query_result = received_p2.configtemplates.filter_by( name="first").all() self.assertTrue(len(query_result) == 1, "Too many results received") received_p2ct1 = query_result[0] self.assertEqual(received_p2ct1, p2ct1) self.assertEqual(received_p2ct1.template_content, p2ct1_template_content) # try to create another "second" config template with self.assertRaises(IntegrityError): faulty_ct3 = ConfigTemplate(name="second", template_content="doesn't matter", project=p1)