예제 #1
0
        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 <Object1> GetAll()
        {
            List <Object1> allObject1s = new List <Object1> {
            };

            SqlConnection conn = DB.Connection();

            conn.Open();

            SqlCommand    cmd = new SqlCommand("SELECT * FROM object1s;", conn);
            SqlDataReader rdr = cmd.ExecuteReader();

            while (rdr.Read())
            {
                int     object1Id   = rdr.GetInt32(0);
                string  object1Name = rdr.GetString(1);
                Object1 newObject1  = new Object1(object1Name, object1Id);
                allObject1s.Add(newObject1);
            }

            if (rdr != null)
            {
                rdr.Close();
            }
            if (conn != null)
            {
                conn.Close();
            }

            return(allObject1s);
        }
        public void AddObject1(Object1 newObject1)
        {
            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         = newObject1.GetId();
            cmd.Parameters.Add(object1IdParameter);

            SqlParameter object2IdParameter = new SqlParameter();

            object2IdParameter.ParameterName = "@Object2Id";
            object2IdParameter.Value         = this.GetId();
            cmd.Parameters.Add(object2IdParameter);

            cmd.ExecuteNonQuery();

            if (conn != null)
            {
                conn.Close();
            }
        }
        public static Object1 Find(int id)
        {
            SqlConnection conn = DB.Connection();

            conn.Open();

            SqlCommand   cmd = new SqlCommand("SELECT * FROM object1s WHERE id = @Object1Id;", conn);
            SqlParameter object1IdParameter = new SqlParameter();

            object1IdParameter.ParameterName = "@Object1Id";
            object1IdParameter.Value         = id.ToString();
            cmd.Parameters.Add(object1IdParameter);
            SqlDataReader rdr = cmd.ExecuteReader();

            int    foundObject1Id          = 0;
            string foundObject1Description = null;

            while (rdr.Read())
            {
                foundObject1Id          = rdr.GetInt32(0);
                foundObject1Description = rdr.GetString(1);
            }
            Object1 foundObject1 = new Object1(foundObject1Description, foundObject1Id);

            if (rdr != null)
            {
                rdr.Close();
            }
            if (conn != null)
            {
                conn.Close();
            }
            return(foundObject1);
        }
예제 #5
0
        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 List <Object1> GetObject1s()
        {
            SqlConnection conn = DB.Connection();

            conn.Open();

            SqlCommand cmd = new SqlCommand("SELECT object1_id FROM object1s_object2s WHERE object2_id = @Object2Id;", conn);

            SqlParameter object2IdParameter = new SqlParameter();

            object2IdParameter.ParameterName = "@Object2Id";
            object2IdParameter.Value         = this.GetId();
            cmd.Parameters.Add(object2IdParameter);

            SqlDataReader rdr = cmd.ExecuteReader();

            List <int> object1Ids = new List <int> {
            };

            while (rdr.Read())
            {
                int object1Id = rdr.GetInt32(0);
                object1Ids.Add(object1Id);
            }
            if (rdr != null)
            {
                rdr.Close();
            }

            List <Object1> object1s = new List <Object1> {
            };

            foreach (int object1Id in object1Ids)
            {
                SqlCommand object1Query = new SqlCommand("SELECT * FROM object1s WHERE id = @Object1Id;", conn);

                SqlParameter object1IdParameter = new SqlParameter();
                object1IdParameter.ParameterName = "@Object1Id";
                object1IdParameter.Value         = object1Id;
                object1Query.Parameters.Add(object1IdParameter);

                SqlDataReader queryReader = object1Query.ExecuteReader();
                while (queryReader.Read())
                {
                    int     thisObject1Id = queryReader.GetInt32(0);
                    string  object1Name   = queryReader.GetString(1);
                    Object1 foundObject1  = new Object1(object1Name, thisObject1Id);
                    object1s.Add(foundObject1);
                }
                if (queryReader != null)
                {
                    queryReader.Close();
                }
            }
            if (conn != null)
            {
                conn.Close();
            }
            return(object1s);
        }
예제 #7
0
        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);
        }
예제 #8
0
        public void Test_Object1sEmptyAtFirst()
        {
            //Arrange, Act
            int result = Object1.GetAll().Count;

            //Assert
            Assert.Equal(0, result);
        }
예제 #9
0
        public void Test_Equal_ReturnsTrueForSameName()
        {
            //Arrange, Act
            Object1 firstObject1  = new Object1("Name");
            Object1 secondObject1 = new Object1("Name");

            //Assert
            Assert.Equal(firstObject1, secondObject1);
        }
예제 #10
0
        public void Test_Find_FindsObject1InDatabase()
        {
            //Arrange
            Object1 testObject1 = new Object1("Name");

            testObject1.Save();

            //Act
            Object1 foundObject1 = Object1.Find(testObject1.GetId());

            //Assert
            Assert.Equal(testObject1, foundObject1);
        }
 public override bool Equals(System.Object otherObject1)
 {
     if (!(otherObject1 is Object1))
     {
         return(false);
     }
     else
     {
         Object1 newObject1   = (Object1)otherObject1;
         bool    idEquality   = this.GetId() == newObject1.GetId();
         bool    nameEquality = this.GetName() == newObject1.GetName();
         return(idEquality && nameEquality);
     }
 }
예제 #12
0
        public void Test_Save_AssignsIdToObject1Object()
        {
            //Arrange
            Object1 testObject1 = new Object1("Name");

            testObject1.Save();

            //Act
            Object1 savedObject1 = Object1.GetAll()[0];

            int result = savedObject1.GetId();
            int testId = testObject1.GetId();

            //Assert
            Assert.Equal(testId, result);
        }
예제 #13
0
        public void Test_Save_SavesObject1ToDatabase()
        {
            //Arrange
            Object1 testObject1 = new Object1("Name");

            testObject1.Save();

            //Act
            List <Object1> result   = Object1.GetAll();
            List <Object1> testList = new List <Object1> {
                testObject1
            };

            //Assert
            Assert.Equal(testList, result);
        }
예제 #14
0
        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);
        }
예제 #15
0
        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);
        }
예제 #16
0
        public void Test_Delete_DeletesObject1FromDatabase()
        {
            //Arrange
            string  name1        = "Name";
            Object1 testObject11 = new Object1(name1);

            testObject11.Save();

            string  name2        = "Other name";
            Object1 testObject12 = new Object1(name2);

            testObject12.Save();

            //Act
            testObject11.Delete();
            List <Object1> resultObject1s  = Object1.GetAll();
            List <Object1> testObject1List = new List <Object1> {
                testObject12
            };

            //Assert
            Assert.Equal(testObject1List, resultObject1s);
        }
예제 #17
0
        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);
        }
예제 #18
0
 public void Dispose()
 {
     Object2.DeleteAll();
     Object1.DeleteAll();
 }
예제 #19
0
        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"]);
            };
        }