00001
00002 using System;
00003 using NUnit.Framework;
00004 using Foodolini.Database;
00005
00006 namespace Foodolini.Database.Test
00007 {
00008
00012 [TestFixture]
00013 public class FoodItemRowTest : DatabaseTest
00014 {
00015 FoodItemRow food;
00016 FoodItemRow food2;
00017 FoodItemRow food3;
00018
00019
00023 private void InsertTestData(){
00024 food = new FoodItemRow();
00025 food.ConsumedBy = null;
00026 food.ConsumedDate = null;
00027 food.ExpirationDate = System.DateTime.Now.AddDays(5);
00028 food.FoodDescriptionId = 5;
00029 food.IsOpen = false;
00030 food.Quantity = 7;
00031
00032 food2 = new FoodItemRow();
00033 food2.ConsumedBy = 1;
00034 food2.ConsumedDate = System.DateTime.Now.AddDays(-1) ;
00035 food2.ExpirationDate = System.DateTime.Now.AddDays(3);
00036 food2.FoodDescriptionId = 9;
00037 food2.IsOpen = false ;
00038 food2.Quantity = 3;
00039
00040 food3 = new FoodItemRow();
00041 food3.ConsumedBy = 2;
00042 food3.ConsumedDate = System.DateTime.Now.AddDays(-1) ;
00043 food3.ExpirationDate = System.DateTime.Now.AddDays(3);
00044 food3.FoodDescriptionId = 4;
00045 food3.IsOpen = true ;
00046 food3.Quantity = 5;
00047 }
00052 [Test]
00053 public void SaveLoadTest()
00054 {
00055 InsertTestData();
00056
00057 this.repository.Add<FoodItemRow>(food);
00058 Assert.AreNotEqual(0,food.FoodItemRowId," check that rowId has been set");
00059 this.repository.Add<FoodItemRow>(food2);
00060 Assert.AreNotEqual(0,food2.FoodItemRowId," check that rowId has been set");
00061
00062 this.repository.Add<FoodItemRow>(food3);
00063 Assert.AreNotEqual(0,food3.FoodItemRowId," check that rowId has been set");
00064
00065 foreach (FoodItemRow row in this.repository.All<FoodItemRow>()){
00066 if ( row.ConsumedBy == 2) {
00067 Assert.AreEqual(true ,row.IsOpen,"check IsOpen is loaded 2");
00068 Assert.AreEqual(2 ,row.ConsumedBy,"check ConsumedBy is loaded 2");
00069 Assert.AreEqual(System.DateTime.Now.AddDays(-1).Day ,row.ConsumedDate.Value.Day,"check ConsumedDate is loaded 2");
00070 Assert.AreEqual(System.DateTime.Now.AddDays(3).Day ,row.ExpirationDate.Day,"check ExpirationDate is loaded 2");
00071 Assert.AreEqual(4 ,row.FoodDescriptionId,"check FoodDescriptionId is loaded 2");
00072 Assert.AreEqual(5 ,row.Quantity,"check Quantity is loaded 2");
00073 }else if( row.ConsumedBy == 1) {
00074 Assert.AreEqual(false ,row.IsOpen,"check IsOpen is loaded 1");
00075 Assert.AreEqual(1 ,row.ConsumedBy,"check ConsumedBy is loaded 1");
00076 Assert.AreEqual(System.DateTime.Now.AddDays(-1).Day ,row.ConsumedDate.Value.Day ,"check ConsumedDate is loaded 1");
00077 Assert.AreEqual(System.DateTime.Now.AddDays(3).Day ,row.ExpirationDate.Day,"chek ExpirationDate is loaded 1");
00078 Assert.AreEqual(9 ,row.FoodDescriptionId,"check FoodDescriptionId is loaded 1");
00079 Assert.AreEqual(3 ,row.Quantity,"chck Quantity is loaded 1");
00080 }else{
00081 Assert.AreEqual(false ,row.IsOpen,"check IsOpen is loaded null");
00082 Assert.AreEqual(null ,row.ConsumedBy,"check ConsumedBy is loaded null");
00083 Assert.AreEqual(null ,row.ConsumedDate,"check ConsumedDate is loaded null");
00084 Assert.AreEqual(System.DateTime.Now.AddDays(5).Day ,row.ExpirationDate.Day,"chek ExpirationDate is loaded null");
00085 Assert.AreEqual(5 ,row.FoodDescriptionId,"check FoodDescriptionId is loaded null");
00086 Assert.AreEqual(7 ,row.Quantity,"chck Quantity is loaded null");
00087 }
00088
00089 }
00090 }
00091
00099 [Test]
00100 public void DeleteTest()
00101 {
00102 int counter = 0;
00103
00104 InsertTestData();
00105 this.repository.Add<FoodItemRow>(food);
00106 this.repository.Add<FoodItemRow>(food2);
00107 this.repository.Add<FoodItemRow>(food3);
00108
00109 foreach (FoodItemRow i in this.repository.All<FoodItemRow>()){
00110 counter ++;
00111 }
00112 Assert.AreEqual(3,counter,"check number of rows");
00113
00114
00115 this.repository.Delete<FoodItemRow>(food2);
00116 counter = 0;
00117 foreach (FoodItemRow i in this.repository.All<FoodItemRow>()){
00118 counter ++;
00119 Assert.AreNotEqual(3,i.Quantity ,"check that the right one have been deleted");
00120 }
00121 Assert.AreEqual(2,counter,"check number of rows");
00122
00123
00124 this.repository.Delete<FoodItemRow>(food);
00125 counter = 0;
00126 foreach (FoodItemRow i in this.repository.All<FoodItemRow>()){
00127 counter ++;
00128 Assert.AreNotEqual(7,i.Quantity ,"check that the right one have been deleted");
00129 }
00130 Assert.AreEqual(1,counter,"check number of rows");
00131
00132
00133 this.repository.Delete<FoodItemRow>(food);
00134 counter = 0;
00135 foreach (FoodItemRow i in this.repository.All<FoodItemRow>()){
00136 counter ++;
00137 Assert.AreEqual(5,i.Quantity ,"check that only food3 remains");
00138 }
00139 Assert.AreEqual(1,counter,"check number of rows");
00140 }
00141
00146 [Test]
00147 public void UpdateTest()
00148 {
00149 InsertTestData();
00150 this.repository.Add<FoodItemRow>(food);
00151 this.repository.Add<FoodItemRow>(food2);
00152 this.repository.Add<FoodItemRow>(food3);
00153
00154 food.IsOpen = true;
00155 food.Quantity = 44;
00156 food3.ConsumedBy = 22;
00157
00158 this.repository.Update<FoodItemRow>(food);
00159 this.repository.Update<FoodItemRow>(food3);
00160 this.repository.Update<FoodItemRow>(food2);
00161
00162 int counter = 0;
00163 foreach (FoodItemRow i in this.repository.Where<FoodItemRow>("FoodDescriptionId = @0", 5)){
00164 counter++;
00165 Assert.AreEqual(44,i.Quantity ,"seach must match");
00166 Assert.AreEqual(true,i.IsOpen ,"isopen must match");
00167 }
00168 Assert.AreEqual(1,counter,"only one row selected");
00169
00170
00171 counter = 0;
00172 foreach (FoodItemRow i in this.repository.Where<FoodItemRow>("Quantity = @0", 3)){
00173 counter++;
00174 Assert.AreEqual(1,i.ConsumedBy ,"seach must match");
00175 }
00176 Assert.AreEqual(1,counter,"only one row selected");
00177
00178
00179 counter = 0;
00180 foreach (FoodItemRow i in this.repository.Where<FoodItemRow>("ConsumedBy = @0", 22)){
00181 counter++;
00182 Assert.AreEqual(4,i.FoodDescriptionId ,"seach must match");
00183 }
00184 Assert.AreEqual(1,counter,"only one row selected");
00185 }
00186
00187
00193 [Test]
00194 public void WereTest()
00195 {
00196 InsertTestData();
00197 this.repository.Add<FoodItemRow>(food);
00198 this.repository.Add<FoodItemRow>(food2);
00199 food2.Quantity = 4;
00200 this.repository.Add<FoodItemRow>(food2);
00201 food2.Quantity = 5;
00202 this.repository.Add<FoodItemRow>(food2);
00203 int counter = 0;
00204 foreach (FoodItemRow i in this.repository.Where<FoodItemRow>("FoodDescriptionId = @0", 9)){
00205 counter++;
00206 Assert.AreEqual(1,i.ConsumedBy,"seach must match");
00207 }
00208 Assert.AreEqual(3,counter,"found all 1 ");
00209 foreach (FoodItemRow i in this.repository.Where<FoodItemRow>("FoodDescriptionId = @0 AND Quantity = @1", 5,7)){
00210 Assert.AreEqual(null,i.ConsumedBy,"seach must match");
00211 }
00212 counter = 0;
00213 foreach (FoodItemRow i in this.repository.Where<FoodItemRow>("FoodDescriptionId = @0 AND IsOpen = @1 AND ConsumedBy = @2", 9,false,1)){
00214 counter++;
00215 Assert.AreEqual(1,i.ConsumedBy,"seach must match");
00216 }
00217 Assert.AreEqual(3,counter,"found all 2");
00218
00219 FoodItemRow testFood;
00220 testFood= this.repository.SingleWhere<FoodItemRow >("Quantity = @0",4);
00221 Assert.AreEqual(1,testFood.ConsumedBy, "single seach must match");
00222
00223
00224 }
00225 }
00226 }