public void Test_GetObject1s_ReturnsAllObject2Object1s() { //Arrange Object2 testObject2 = new Object2("Name"); testObject2.Save(); Object1 testObject11 = new Object1("Other object name"); testObject11.Save(); Object1 testObject12 = new Object1("Another object name"); testObject12.Save(); //Act testObject2.AddObject1(testObject11); List <Object1> result = testObject2.GetObject1s(); List <Object1> testList = new List <Object1> { testObject11 }; //Assert Assert.Equal(testList, result); }
public static List <Object2> GetAll() { List <Object2> AllObject2s = new List <Object2> { }; SqlConnection conn = DB.Connection(); conn.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM object2s;", conn); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { int object2Id = rdr.GetInt32(0); string object2Name = rdr.GetString(1); Object2 newObject2 = new Object2(object2Name, object2Id); AllObject2s.Add(newObject2); } if (rdr != null) { rdr.Close(); } if (conn != null) { conn.Close(); } return(AllObject2s); }
public List <Object2> GetObject2s() { SqlConnection conn = DB.Connection(); conn.Open(); SqlCommand cmd = new SqlCommand("SELECT object2s.* FROM object1s JOIN object1s_object2s ON (object1s.id = object1s_object2s.object1_id) JOIN object2s ON (object1s_object2s.object2_id = object2s.id) WHERE object1s.id = @Object1Id;", conn); SqlParameter object1IdParameter = new SqlParameter(); object1IdParameter.ParameterName = "@Object1Id"; object1IdParameter.Value = this.GetId(); cmd.Parameters.Add(object1IdParameter); SqlDataReader rdr = cmd.ExecuteReader(); List <int> object2Ids = new List <int> { }; while (rdr.Read()) { int object2Id = rdr.GetInt32(0); object2Ids.Add(object2Id); } if (rdr != null) { rdr.Close(); } List <Object2> object2s = new List <Object2> { }; foreach (int object2Id in object2Ids) { SqlCommand object2Query = new SqlCommand("SELECT * FROM object2s WHERE id = @Object2Id;", conn); SqlParameter object2IdParameter = new SqlParameter(); object2IdParameter.ParameterName = "@Object2Id"; object2IdParameter.Value = object2Id; object2Query.Parameters.Add(object2IdParameter); SqlDataReader queryReader = object2Query.ExecuteReader(); while (queryReader.Read()) { int thisObject2Id = queryReader.GetInt32(0); string object2Description = queryReader.GetString(1); Object2 foundObject2 = new Object2(object2Description, thisObject2Id); object2s.Add(foundObject2); } if (queryReader != null) { queryReader.Close(); } } if (conn != null) { conn.Close(); } return(object2s); }
public static Object2 Find(int id) { SqlConnection conn = DB.Connection(); conn.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM object2s WHERE id = @Object2Id", conn); SqlParameter object2IdParameter = new SqlParameter(); object2IdParameter.ParameterName = "@Object2Id"; object2IdParameter.Value = id.ToString(); cmd.Parameters.Add(object2IdParameter); SqlDataReader rdr = cmd.ExecuteReader(); int foundObject2Id = 0; string foundObject2Name = null; while (rdr.Read()) { foundObject2Id = rdr.GetInt32(0); foundObject2Name = rdr.GetString(1); } Object2 foundObject2 = new Object2(foundObject2Name, foundObject2Id); if (rdr != null) { rdr.Close(); } if (conn != null) { conn.Close(); } return(foundObject2); }
public void AddObject2(Object2 newObject2) { SqlConnection conn = DB.Connection(); conn.Open(); SqlCommand cmd = new SqlCommand("INSERT INTO object1s_object2s (object1_id, object2_id) VALUES (@Object1Id, @Object2Id);", conn); SqlParameter object1IdParameter = new SqlParameter(); object1IdParameter.ParameterName = "@Object1Id"; object1IdParameter.Value = this.GetId(); cmd.Parameters.Add(object1IdParameter); SqlParameter object2IdParameter = new SqlParameter(); object2IdParameter.ParameterName = "@Object2Id"; object2IdParameter.Value = newObject2.GetId(); cmd.Parameters.Add(object2IdParameter); cmd.ExecuteNonQuery(); if (conn != null) { conn.Close(); } }
public void Test_AddObject2_AddsObject2ToObject1() { //Arrange Object1 testObject1 = new Object1("Name"); testObject1.Save(); Object2 testObject2 = new Object2("Object name"); testObject2.Save(); Object2 testObject22 = new Object2("Other object name"); testObject22.Save(); //Act testObject1.AddObject2(testObject2); testObject1.AddObject2(testObject22); List <Object2> result = testObject1.GetObject2s(); List <Object2> testList = new List <Object2> { testObject2, testObject22 }; //Assert Assert.Equal(testList, result); }
public void Test_GetObject2s_ReturnsAllObject1Object2s() { //Arrange Object1 testObject1 = new Object1("Name"); testObject1.Save(); Object2 testObject21 = new Object2("Object name"); testObject21.Save(); Object2 testObject22 = new Object2("Other object name"); testObject22.Save(); //Act testObject1.AddObject2(testObject21); List <Object2> savedObject2s = testObject1.GetObject2s(); List <Object2> testList = new List <Object2> { testObject21 }; //Assert Assert.Equal(testList, savedObject2s); }
public void Test_EmptyAtFirst() { //Arrange, Act int result = Object2.GetAll().Count; //Assert Assert.Equal(0, result); }
public void Test_EqualOverrideTrueForSameName() { //Arrange, Act Object2 firstObject2 = new Object2("Name"); Object2 secondObject2 = new Object2("Name"); //Assert Assert.Equal(firstObject2, secondObject2); }
public void Test_Update_UpdatesInDb() { Object2 testObject2 = new Object2("Name"); testObject2.Save(); testObject2.Update("Other name"); Object2 newObject2 = new Object2("Other name", testObject2.GetId()); Assert.Equal(testObject2, newObject2); }
public void Test_FindFindsObject2InDatabase() { //Arrange Object2 testObject2 = new Object2("Name"); testObject2.Save(); //Act Object2 result = Object2.Find(testObject2.GetId()); //Assert Assert.Equal(testObject2, result); }
public override bool Equals(System.Object otherObject2) { if (!(otherObject2 is Object2)) { return(false); } else { Object2 newObject2 = (Object2)otherObject2; bool idEquality = this.GetId() == newObject2.GetId(); bool nameEquality = this.GetName() == newObject2.GetName(); return(idEquality && nameEquality); } }
public void Test_Save() { //Arrange Object2 testObject2 = new Object2("Name"); testObject2.Save(); //Act List <Object2> result = Object2.GetAll(); List <Object2> testList = new List <Object2> { testObject2 }; //Assert Assert.Equal(testList, result); }
public void Test_SaveAssignsIdToObject() { //Arrange Object2 testObject2 = new Object2("Name"); testObject2.Save(); //Act Object2 savedObject2 = Object2.GetAll()[0]; Console.WriteLine(testObject2.GetName()); int result = savedObject2.GetId(); int testId = testObject2.GetId(); //Assert Assert.Equal(testId, result); }
public void Test_AddObject1_AddsObject1ToObject2() { //Arrange Object2 testObject2 = new Object2("Name"); testObject2.Save(); Object1 testObject1 = new Object1("Other object name"); testObject1.Save(); //Act testObject2.AddObject1(testObject1); List <Object1> result = testObject2.GetObject1s(); List <Object1> testList = new List <Object1> { testObject1 }; //Assert Assert.Equal(testList, result); }
public void Test_Delete_DeletesObject1AssociationsFromDatabase() { //Arrange Object2 testObject2 = new Object2("Object name"); testObject2.Save(); string testName = "Name"; Object1 testObject1 = new Object1(testName); testObject1.Save(); //Act testObject1.AddObject2(testObject2); testObject1.Delete(); List <Object1> resultObject2Object1s = testObject2.GetObject1s(); List <Object1> testObject2Object1s = new List <Object1> { }; //Assert Assert.Equal(testObject2Object1s, resultObject2Object1s); }
public void Test_Delete_DeletesObject2AssociationsFromDatabase() { //Arrange Object1 testObject1 = new Object1("Other object name"); testObject1.Save(); string testName = "Name"; Object2 testObject2 = new Object2(testName); testObject2.Save(); //Act testObject2.AddObject1(testObject1); testObject2.Delete(); List <Object2> resultObject1Object2s = testObject1.GetObject2s(); List <Object2> testObject1Object2s = new List <Object2> { }; //Assert Assert.Equal(testObject1Object2s, resultObject1Object2s); }
public void Dispose() { Object2.DeleteAll(); Object1.DeleteAll(); }
public HomeModule() { Get["/"] = _ => { return(View["index.cshtml"]); }; Get["/object2s"] = _ => { List <Object2> AllObject2s = Object2.GetAll(); return(View["object2s.cshtml", AllObject2s]); }; Get["/object1s"] = _ => { List <Object1> AllObject1s = Object1.GetAll(); return(View["object1s.cshtml", AllObject1s]); }; Get["/object2s/new"] = _ => { return(View["object2s_form.cshtml"]); }; Post["/object2s/new"] = _ => { Object2 newObject2 = new Object2(Request.Form["object2-description"]); newObject2.Save(); return(View["success.cshtml"]); }; Get["/object1s/new"] = _ => { return(View["object1s_form.cshtml"]); }; Post["/object1s/new"] = _ => { Object1 newObject1 = new Object1(Request.Form["object1-name"]); newObject1.Save(); return(View["success.cshtml"]); }; Get["object2s/{id}"] = parameters => { Dictionary <string, object> model = new Dictionary <string, object>(); Object2 SelectedObject2 = Object2.Find(parameters.id); List <Object1> Object2Object1s = SelectedObject2.GetObject1s(); List <Object1> AllObject1s = Object1.GetAll(); model.Add("object2", SelectedObject2); model.Add("object2Object1s", Object2Object1s); model.Add("allObject1s", AllObject1s); return(View["object2.cshtml", model]); }; Get["object1s/{id}"] = parameters => { Dictionary <string, object> model = new Dictionary <string, object>(); Object1 SelectedObject1 = Object1.Find(parameters.id); List <Object2> Object1Object2s = SelectedObject1.GetObject2s(); List <Object2> AllObject2s = Object2.GetAll(); model.Add("object1", SelectedObject1); model.Add("object1Object2s", Object1Object2s); model.Add("allObject2s", AllObject2s); return(View["object1.cshtml", model]); }; Post["object2/add_object1"] = _ => { Object1 object1 = Object1.Find(Request.Form["object1-id"]); Object2 object2 = Object2.Find(Request.Form["object2-id"]); object2.AddObject1(object1); return(View["success.cshtml"]); }; Post["object1/add_object2"] = _ => { Object1 object1 = Object1.Find(Request.Form["object1-id"]); Object2 object2 = Object2.Find(Request.Form["object2-id"]); object1.AddObject2(object2); return(View["success.cshtml"]); }; Get["object2s/update/{id}"] = parameters => { Object2 foundObject2 = Object2.Find(parameters.id); return(View["object2_update.cshtml", foundObject2]); }; Patch["object2s/update/{id}"] = parameters => { Object2 foundObject2 = Object2.Find(parameters.id); foundObject2.Update(Request.Form["new-description"]); return(View["success.cshtml"]); }; Get["object1s/update/{id}"] = parameters => { Object1 foundObject1 = Object1.Find(parameters.id); return(View["object1_update.cshtml", foundObject1]); }; Patch["object1s/update/{id}"] = parameters => { Object1 foundObject1 = Object1.Find(parameters.id); foundObject1.Update(Request.Form["new-description"]); return(View["success.cshtml"]); }; Delete["object1/delete/{id}"] = parameters => { Object1 foundObject1 = Object1.Find(parameters.id); foundObject1.Delete(); return(View["success.cshtml"]); }; Delete["object2/delete/{id}"] = parameters => { Object2 foundObject2 = Object2.Find(parameters.id); foundObject2.Delete(); return(View["success.cshtml"]); }; }