예제 #1
0
        public static InventoryItem Find(int id)
        {
            SqlConnection conn = DB.Connection();
              SqlDataReader rdr = null;
              conn.Open();

              SqlCommand cmd = new SqlCommand("SELECT * FROM inventoryItems WHERE id= @InventoryItemID;", conn);
              SqlParameter inventoryItemIdParameter = new SqlParameter();
              inventoryItemIdParameter.ParameterName = "@InventoryItemId";
              inventoryItemIdParameter.Value = id.ToString();
              cmd.Parameters.Add(inventoryItemIdParameter);
              rdr = cmd.ExecuteReader();

              int foundInventoryItemId = 0;
              string foundInventoryItemName = null;
              string foundInventoryItemDescription = null;
              while(rdr.Read())
              {
            foundInventoryItemId = rdr.GetInt32(0);
            foundInventoryItemName = rdr.GetString(1);
              }
              InventoryItem foundInventoryItem = new InventoryItem(foundInventoryItemName, foundInventoryItemDescription, foundInventoryItemId);
              if (rdr != null) rdr.Close();
              if (conn != null) conn.Close();
              return foundInventoryItem;
        }
예제 #2
0
        public void Test_Save_AssignsIdToObject()
        {
            InventoryItem testInventoryItem = new InventoryItem("cat", "it's a cat");
              testInventoryItem.Save();

              InventoryItem foundInventoryItem = InventoryItem.Find(testInventoryItem.GetId());

              Assert.Equal(testInventoryItem, foundInventoryItem);
        }
예제 #3
0
        public void Test_Equal_ReturnsTrueIfNamesAreTheSame()
        {
            //Arrange, Act
             InventoryItem firstInventoryItem = new InventoryItem("cat", "it's a cat");
             InventoryItem secondInventoryItem = new InventoryItem("cat", "it's a cat");

             //Assert
             Assert.Equal(firstInventoryItem, secondInventoryItem);
        }
예제 #4
0
        public void Test_DatabaseHasStuff()
        {
            //Arrange, Act
             InventoryItem newInventoryItem = new InventoryItem("cat", "it's a cat");
             newInventoryItem.Save();
             int result = InventoryItem.GetAll().Count;

             //Assert
             Assert.Equal(1, result);
        }
예제 #5
0
        public void Test_DeleteOne_DeletesGivenOne()
        {
            InventoryItem firstInventoryItem = new InventoryItem("cat", "it's a cat");
              firstInventoryItem.Save();
              InventoryItem secondInventoryItem = new InventoryItem("dog", "it's a dog");
              secondInventoryItem.Save();
              InventoryItem thirdInventoryItem = new InventoryItem("fish", "it's a fish");
              thirdInventoryItem.Save();

              InventoryItem.DeleteOne("dog");
              int newInventory=InventoryItem.GetAll().Count;

              Assert.Equal(2, newInventory);
        }
예제 #6
0
        public void Test_Save_SavesToDatabase()
        {
            //Arrange
              InventoryItem newInventoryItem = new InventoryItem("cat", "it's a cat");

              //Act
              newInventoryItem.Save();
              List<InventoryItem> result = InventoryItem.GetAll();
              List<InventoryItem> testList = new List<InventoryItem>{newInventoryItem};

              //Assert
              Assert.Equal(testList, result);
        }
예제 #7
0
        public void Test_Search_FindsIdByName()
        {
            InventoryItem testInventoryItem = new InventoryItem("cat", "it's a cat");
              testInventoryItem.Save();

              int result = InventoryItem.Search(testInventoryItem.GetName());

              Assert.Equal(testInventoryItem.GetId(), result);
        }
예제 #8
0
        public void Test_Search_DoesntFindIdForUnused()
        {
            InventoryItem testInventoryItem = new InventoryItem("cat", "it's a cat");
              testInventoryItem.Save();

              int result = InventoryItem.Search("dog");

              Assert.Equal(-1, result);
        }
예제 #9
0
        public static List<InventoryItem> GetAll()
        {
            List<InventoryItem> allInventoryItems = new List<InventoryItem>{};

              SqlConnection conn = DB.Connection();
              SqlDataReader rdr = null;
              conn.Open();

              SqlCommand cmd = new SqlCommand("Select * FROM InventoryItems;", conn);
              rdr = cmd.ExecuteReader();

              while(rdr.Read())
              {
            int inventoryItemId = rdr.GetInt32(0);
            string inventoryItemName = rdr.GetString(1);
            string inventoryItemDescription = rdr.GetString(2);
            InventoryItem newInventoryItem = new InventoryItem(inventoryItemName, inventoryItemDescription, inventoryItemId);
            allInventoryItems.Add(newInventoryItem);
              }

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