public async Task TestCase_GetHomeDefineByUser(string user) { var context = this.fixture.GetCurrentDataContext(); HomeDefinesController control = new HomeDefinesController(context); try { control.Get(); } catch (Exception exp) { Assert.IsType <UnauthorizedAccessException>(exp); } var userclaim = DataSetupUtility.GetClaimForUser(user); control.ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = userclaim } }; var result = control.Get(); var returnValue = Assert.IsAssignableFrom <IQueryable <HomeDefine> >(result); var cnt = returnValue.Count(); Assert.Equal(context.HomeMembers.Where(p => p.User == user).Count(), cnt); await context.DisposeAsync(); }
public async Task TestCase2_POST(int nversion) { // Arrange var dbcontext = this.fixture.GetCurrentDataContext(); await dbcontext.Database.ExecuteSqlRawAsync("DELETE FROM t_dbversion WHERE VERSIONID > " + nversion.ToString()); await dbcontext.DisposeAsync(); // Test dbcontext = this.fixture.GetCurrentDataContext(); DBVersionsController control = new DBVersionsController(dbcontext); await control.Post(); var vers = dbcontext.DBVersions.ToList(); for (var i = nversion + 1; i <= DBVersionsController.CurrentVersion; i++) { Assert.True(vers.Exists(p => p.VersionID == i)); } await dbcontext.DisposeAsync(); // Reset the db version table dbcontext = this.fixture.GetCurrentDataContext(); await dbcontext.Database.ExecuteSqlRawAsync("DELETE FROM t_dbversion WHERE VERSIONID > 0"); DataSetupUtility.InitialTable_DBVersion(dbcontext); await dbcontext.SaveChangesAsync(); var lastestVersion = await dbcontext.DBVersions.MaxAsync(p => p.VersionID); Assert.True(lastestVersion == DBVersionsController.CurrentVersion); await dbcontext.DisposeAsync(); }
public CustomWebApplicationFactory() : base() { // Open connections DBConnection = new SqliteConnection("DataSource=:memory:"); DBConnection.Open(); try { var options = new DbContextOptionsBuilder <hihDataContext>() .UseSqlite(DBConnection) .Options; // Create the schema in the database CurrentDataContext = new hihDataContext(options, true); CurrentDataContext.Database.EnsureCreated(); // Setup the tables DataSetupUtility.InitializeSystemTables(CurrentDataContext); DataSetupUtility.InitializeHomeDefineAndMemberTables(CurrentDataContext); } catch { // Error occurred } finally { } }
public SqliteDatabaseFixture() { // Open connections DBConnection = new SqliteConnection("DataSource=:memory:"); DBConnection.Open(); try { // Create the schema in the database var context = GetCurrentDataContext(); if (!context.Database.IsSqlite() || context.Database.IsSqlServer()) { throw new Exception("Faield!"); } // Create tables and views DataSetupUtility.CreateDatabaseTables(context.Database); DataSetupUtility.CreateDatabaseViews(context.Database); context.Database.EnsureCreated(); context.Dispose(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); // Error occurred } finally { } }
public void InitTestData(GalleryContext context) { if (!this.IsTestDataInitialized) { DataSetupUtility.CreateTestingData(context); this.IsTestDataInitialized = true; } }
public void InitHome3TestData(hihDataContext context) { if (!this.IsHome3DataInitialized) { DataSetupUtility.CreateTestingData_Home3(context); this.IsHome3DataInitialized = true; } }
public void InitBlogTestData(hihDataContext context) { if (!this.IsBlogDataInitialized) { DataSetupUtility.CreateTestingData_Blog(context); this.IsBlogDataInitialized = true; } }
public void InitializeTestData() { if (IsTestDataIntialized) { return; } var context = GetCurrentDataContext(); DataSetupUtility.InitalizeTestData(context); IsTestDataIntialized = true; }
public void TestController1(int hid, string user) { var context = this.fixture.GetCurrentDataContext(); if (hid == DataSetupUtility.Home1ID) { fixture.InitHome1TestData(context); } if (hid == DataSetupUtility.Home2ID) { fixture.InitHome2TestData(context); } if (hid == DataSetupUtility.Home3ID) { fixture.InitHome3TestData(context); } if (hid == DataSetupUtility.Home4ID) { fixture.InitHome4TestData(context); } if (hid == DataSetupUtility.Home5ID) { fixture.InitHome5TestData(context); } // 1. Prepare dta var userclaim = DataSetupUtility.GetClaimForUser(user); var httpctx = UnitTestUtility.GetDefaultHttpContext(provider, userclaim); // 2. Test it var controller = new FinanceReportByAccountsController(context); Assert.NotNull(controller); controller.ControllerContext = new ControllerContext() { HttpContext = httpctx }; var rsts = controller.Get(); var curhmemquery = (from homemem in context.HomeMembers where homemem.HomeID == hid && homemem.User == user select homemem).FirstOrDefault(); var curhmem = Assert.IsType <HomeMember>(curhmemquery); var expamt = context.FinanceAccount.Where(p => p.HomeID == hid).Count(); var actamt = rsts.ToList().Where(p => p.HomeID == hid).Count(); if (curhmem.IsChild.HasValue && curhmem.IsChild == true) { expamt = context.FinanceAccount.Where(p => p.HomeID == hid && p.Owner == user).Count(); Assert.Equal(expamt, actamt); } else { Assert.Equal(expamt, actamt); } }
public async Task TestCase_Account(int hid, string user) { var context = this.fixture.GetCurrentDataContext(); if (hid == DataSetupUtility.Home1ID) { fixture.InitHome1TestData(context); } var control = new FinanceDocumentItemViewsController(context); var userclaim = DataSetupUtility.GetClaimForUser(user); var httpctx = UnitTestUtility.GetDefaultHttpContext(provider, userclaim); control.ControllerContext = new ControllerContext() { HttpContext = httpctx }; // Accounts var acntids = (from acnts in context.FinanceAccount where acnts.HomeID == hid select acnts.ID).ToList(); var odatacontext = UnitTestUtility.GetODataQueryContext <FinanceDocumentItemView>(this.model); foreach (var accid in acntids) { // Perform the selection var queryUrl = "http://localhost/api/FinanceDocumentItemViews?$filter=HomeID eq " + hid.ToString() + " and AccountID eq " + accid.ToString(); var req = UnitTestUtility.GetHttpRequest(httpctx, "GET", queryUrl); var options = UnitTestUtility.GetODataQueryOptions <FinanceDocumentItemView>(odatacontext, req); var rst = control.Get(options); var expacntamt = (from docitem in context.FinanceDocumentItem join docheader in context.FinanceDocument on new { docitem.DocID, HomeID = hid } equals new { DocID = docheader.ID, docheader.HomeID } where docitem.AccountID == accid select docitem.ItemID ).ToList().Count(); if (expacntamt > 0) { Assert.NotNull(rst); Assert.Equal(expacntamt, rst.Cast <FinanceDocumentItemView>().Count()); } else { Assert.Empty(rst); } } await context.DisposeAsync(); }
private void CleanupCreatedEntries() { if (objectsCreated.Count > 0) { var context = this.fixture.GetCurrentDataContext(); foreach (var kid in objectsCreated) { DataSetupUtility.DeleteUserHabit(context, kid); } objectsCreated.Clear(); context.SaveChanges(); } }
public void Dispose() { if (objectsCreated.Count > 0) { var context = this.fixture.GetCurrentDataContext(); foreach (var kid in objectsCreated) { DataSetupUtility.DeleteUserCollection(context, kid); } objectsCreated.Clear(); context.SaveChanges(); } }
public void TestController1(int hid, string user) { var context = this.fixture.GetCurrentDataContext(); if (hid == DataSetupUtility.Home1ID) { fixture.InitHome1TestData(context); } if (hid == DataSetupUtility.Home2ID) { fixture.InitHome2TestData(context); } if (hid == DataSetupUtility.Home3ID) { fixture.InitHome3TestData(context); } if (hid == DataSetupUtility.Home4ID) { fixture.InitHome4TestData(context); } if (hid == DataSetupUtility.Home5ID) { fixture.InitHome5TestData(context); } // 1. Prepare dta var userclaim = DataSetupUtility.GetClaimForUser(user); var httpctx = UnitTestUtility.GetDefaultHttpContext(provider, userclaim); // 2. Test it var controller = new FinanceReportByControlCentersController(context); Assert.NotNull(controller); controller.ControllerContext = new ControllerContext() { HttpContext = httpctx }; var rsts = controller.Get(); var expamt = context.FinanceControlCenter.Where(p => p.HomeID == hid).Count(); var actamt = rsts.ToList().Where(p => p.HomeID == hid).Count(); Assert.Equal(expamt, actamt); }
public async Task TestCase_GetList(String usr) { var context = fixture.GetCurrentDataContext(); fixture.InitializeTestData(); var control = new AwardUsersController(context); try { control.Get(); } catch (Exception ex) { Assert.NotNull(ex); } if (!String.IsNullOrEmpty(usr)) { var userclaim = DataSetupUtility.GetClaimForUser(usr); control.ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = userclaim } }; var getrst = control.Get(); Assert.NotNull(getrst); if (usr == DataSetupUtility.UserA) { Assert.Equal(1, getrst.Count()); } else if (usr == DataSetupUtility.UserB) { Assert.Equal(0, getrst.Count()); } } await context.DisposeAsync(); }
public async Task TestCase_PatchWithInvalidID() { var context = fixture.GetCurrentDataContext(); ExerciseItemsController control = new ExerciseItemsController(context); var userclaim = DataSetupUtility.GetClaimForUser(DataSetupUtility.UserA); control.ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = userclaim } }; var putrst = control.Patch(999, new Delta <ExerciseItem> { }); Assert.NotNull(putrst); Assert.IsType <NotFoundResult>(putrst.Result); await context.DisposeAsync(); }
public async Task TestCase_PutWithNonExistID() { var context = fixture.GetCurrentDataContext(); KnowledgeItemsController control = new KnowledgeItemsController(context); var userclaim = DataSetupUtility.GetClaimForUser(DataSetupUtility.UserA); control.ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = userclaim } }; var putrst = control.Put(999, new KnowledgeItem { ID = 999 }); Assert.NotNull(putrst); Assert.IsType <NotFoundResult>(putrst.Result); await context.DisposeAsync(); }
public async Task TestCase_GetList(String usr) { var context = fixture.GetCurrentDataContext(); fixture.InitializeTestData(); var control = new InvitedUsersController(context); try { control.Get(); } catch (Exception ex) { Assert.IsType <UnauthorizedAccessException>(ex); } if (!String.IsNullOrEmpty(usr)) { var userclaim = DataSetupUtility.GetClaimForUser(usr); control.ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = userclaim } }; var getrst = control.Get(); Assert.NotNull(getrst); // Why just retun the useritself? Assert.Equal(1, getrst.Count()); } await context.DisposeAsync(); }
public async Task CalculatePoints_Daily_NumberOfTimes(UserHabitRecordsControllerTestData_DailyNoOfTimes testData) { var context = this.fixture.GetCurrentDataContext(); UserHabitRecordsController control = new(context); // Add Habit, Habit Rule UserHabit habit = new UserHabit(); habit.TargetUser = DataSetupUtility.UserA; habit.ValidFrom = new DateTime(2021, 1, 1); habit.ValidTo = new DateTime(2022, 12, 31); habit.Name = "Habit_Daily_1"; habit.Category = HabitCategory.Positive; habit.Comment = habit.Name; habit.Frequency = HabitFrequency.Daily; habit.CompleteCategory = HabitCompleteCategory.NumberOfTimes; context.UserHabits.Add(habit); context.SaveChanges(); Int32 nNewHabitID = habit.ID; foreach (var rule in testData.RuleList) { rule.HabitID = habit.ID; context.UserHabitRules.Add(rule); } context.SaveChanges(); // Add user record. foreach (DateTime dt in testData.RecordDatesList) { UserHabitRecord record = new UserHabitRecord(); record.HabitID = habit.ID; record.RecordDate = dt; record.Comment = "Test1"; var rst = control.Post(record); Assert.NotNull(rst); if (rst != null) { CreatedODataResult <UserHabitRecord> rstrecord = (CreatedODataResult <UserHabitRecord>)rst.Result; Assert.NotNull(rstrecord); } } // Check on DB directly var dbrecords = (from dbrecord in context.UserHabitRecords where dbrecord.HabitID == habit.ID orderby dbrecord.RecordDate ascending select dbrecord).ToList(); Assert.Equal(testData.ExpectedRecordList.Count, dbrecords.Count); // Ensure rule is assigned correctly if (testData.ExpectedRecordList.Count > 0) { foreach (var dbrecord in dbrecords) { var ridx = testData.ExpectedRecordList.FindIndex(rd => rd.RecordDate == dbrecord.RecordDate); Assert.NotEqual(-1, ridx); Assert.Equal(testData.ExpectedRecordList[ridx].RuleID, dbrecord.RuleID); Assert.Equal(testData.ExpectedRecordList[ridx].ContinuousCount, dbrecord.ContinuousCount); } } DataSetupUtility.ClearUserHabitData(context, nNewHabitID); context.SaveChanges(); await context.DisposeAsync(); }
public async Task TestCase1_Home1() { var ctgyCount = DataSetupUtility.FinanceTransactionTypes.Count(); var context = this.fixture.GetCurrentDataContext(); FinanceTransactionTypesController control = new FinanceTransactionTypesController(context); var user = DataSetupUtility.GetClaimForUser(DataSetupUtility.UserA); control.ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = user } }; // 1. Read all categories var items = control.Get(); var itemcnt = items.Count(); Assert.Equal(ctgyCount, itemcnt); // 2. Insert new category var ctgy = new FinanceTransactionType() { HomeID = DataSetupUtility.Home1ID, Name = "Test 1", Comment = "Test 1" }; var rst1 = await control.Post(ctgy); Assert.NotNull(rst1); var rst2 = Assert.IsType <CreatedODataResult <FinanceTransactionType> >(rst1); Assert.Equal(ctgy.Name, rst2.Entity.Name); var firstctg = rst2.Entity.ID; Assert.True(firstctg > 0); Assert.Equal(ctgy.Comment, rst2.Entity.Comment); // 3. Read all categories items = control.Get(); itemcnt = items.Count(); Assert.Equal(ctgyCount + 1, itemcnt); // 4. Change it ctgy.Name = "Test 2"; rst1 = await control.Put(firstctg, ctgy); var rst3 = Assert.IsType <UpdatedODataResult <FinanceTransactionType> >(rst1); Assert.Equal(ctgy.Name, rst3.Entity.Name); // 5. Delete it var rst4 = await control.Delete(firstctg); Assert.NotNull(rst4); var rst6 = Assert.IsType <StatusCodeResult>(rst4); Assert.Equal(204, rst6.StatusCode); // 6. Read all categories again items = control.Get(); itemcnt = items.Count(); Assert.Equal(ctgyCount, itemcnt); await context.DisposeAsync(); }
public async Task TestCase1_NormalAccount(int hid, string user, int ctgyid) { var context = this.fixture.GetCurrentDataContext(); var secondhid = hid; // 0. Initialize data if (hid == DataSetupUtility.Home1ID) { if (user == DataSetupUtility.UserA || user == DataSetupUtility.UserB) { secondhid = DataSetupUtility.Home3ID; } else if (user == DataSetupUtility.UserC) { secondhid = DataSetupUtility.Home4ID; } else if (user == DataSetupUtility.UserD) { secondhid = DataSetupUtility.Home5ID; } } else if (hid == DataSetupUtility.Home2ID) { secondhid = DataSetupUtility.Home3ID; } if (hid == DataSetupUtility.Home1ID || secondhid == DataSetupUtility.Home1ID) { fixture.InitHome1TestData(context); } if (hid == DataSetupUtility.Home2ID || secondhid == DataSetupUtility.Home2ID) { fixture.InitHome2TestData(context); } if (hid == DataSetupUtility.Home3ID || secondhid == DataSetupUtility.Home3ID) { fixture.InitHome3TestData(context); } if (hid == DataSetupUtility.Home4ID || secondhid == DataSetupUtility.Home4ID) { fixture.InitHome4TestData(context); } if (hid == DataSetupUtility.Home5ID || secondhid == DataSetupUtility.Home5ID) { fixture.InitHome5TestData(context); } // 0a. Prepare the context var control = new FinanceAccountsController(context); var userclaim = DataSetupUtility.GetClaimForUser(user); var httpctx = UnitTestUtility.GetDefaultHttpContext(provider, userclaim); control.ControllerContext = new ControllerContext() { HttpContext = httpctx }; var curhmemquery = (from homemem in context.HomeMembers where homemem.HomeID == hid && homemem.User == user select homemem).FirstOrDefault(); var curhmem = Assert.IsType <HomeMember>(curhmemquery); var acntamt = (from homemem in context.HomeMembers join finacnt in context.FinanceAccount on new { homemem.HomeID, homemem.User } equals new { finacnt.HomeID, User = user } select finacnt.ID).ToList().Count(); // 1. Create first account var acnt = new FinanceAccount() { HomeID = hid, Name = "Account_" + ctgyid.ToString() + ".1", CategoryID = ctgyid, Owner = user }; var rst = await control.Post(acnt); Assert.NotNull(rst); var rst2 = Assert.IsType <CreatedODataResult <FinanceAccount> >(rst); Assert.Equal(rst2.Entity.Name, acnt.Name); Assert.Equal(rst2.Entity.HomeID, acnt.HomeID); Assert.Equal(rst2.Entity.CategoryID, acnt.CategoryID); Assert.Equal(rst2.Entity.Owner, user); var firstacntid = rst2.Entity.ID; Assert.True(firstacntid > 0); accountsCreated.Add(firstacntid); // 2. Now read the whole accounts (no home ID applied) var queryUrl = "http://localhost/api/FinanceAccounts"; var req = UnitTestUtility.GetHttpRequest(httpctx, "GET", queryUrl); var odatacontext = UnitTestUtility.GetODataQueryContext <FinanceAccount>(this.model); var options = UnitTestUtility.GetODataQueryOptions <FinanceAccount>(odatacontext, req); var rst3 = control.Get(options); Assert.NotNull(rst3); if (curhmem.IsChild.HasValue && curhmem.IsChild == true) { acntamt = context.FinanceAccount.Where(p => p.Owner == curhmem.User).Count(); Assert.Equal(acntamt, rst3.Cast <FinanceAccount>().Count()); } else { Assert.Equal(acntamt + 1, rst3.Cast <FinanceAccount>().Count()); } // 2a. Read the whole accounts (with home ID applied) queryUrl = "http://localhost/api/FinanceAccounts?$filter=HomeID eq " + hid.ToString(); req = UnitTestUtility.GetHttpRequest(httpctx, "GET", queryUrl); //var odatacontext = UnitTestUtility.GetODataQueryContext<FinanceAccount>(this.model); options = UnitTestUtility.GetODataQueryOptions <FinanceAccount>(odatacontext, req); rst3 = control.Get(options); Assert.NotNull(rst3); if (curhmem.IsChild.HasValue && curhmem.IsChild == true) { acntamt = context.FinanceAccount.Where(p => p.HomeID == hid && p.Owner == curhmem.User).Count(); Assert.Equal(acntamt, rst3.Cast <FinanceAccount>().Count()); } else { acntamt = context.FinanceAccount.Where(p => p.HomeID == hid).Count(); Assert.Equal(acntamt, rst3.Cast <FinanceAccount>().Count()); } // 3. Now create another one! acnt = new FinanceAccount() { HomeID = hid, Name = "Account_" + ctgyid.ToString() + ".2", Comment = "Comment 2", CategoryID = ctgyid, Owner = user }; rst = await control.Post(acnt); Assert.NotNull(rst); rst2 = Assert.IsType <CreatedODataResult <FinanceAccount> >(rst); Assert.Equal(rst2.Entity.Name, acnt.Name); Assert.Equal(rst2.Entity.HomeID, acnt.HomeID); Assert.Equal(rst2.Entity.CategoryID, acnt.CategoryID); Assert.Equal(rst2.Entity.Owner, acnt.Owner); var secondacntid = rst2.Entity.ID; Assert.True(secondacntid > 0); accountsCreated.Add(secondacntid); // 4. Change one account acnt.Comment = "Comment 2 Updated"; rst = await control.Put(secondacntid, acnt); Assert.NotNull(rst); var rst4 = Assert.IsType <UpdatedODataResult <FinanceAccount> >(rst); Assert.Equal(rst4.Entity.Name, acnt.Name); Assert.Equal(rst4.Entity.HomeID, acnt.HomeID); Assert.Equal(rst4.Entity.Comment, acnt.Comment); // 5. Delete the second account var rst5 = await control.Delete(secondacntid); Assert.NotNull(rst5); var rst6 = Assert.IsType <StatusCodeResult>(rst5); Assert.Equal(204, rst6.StatusCode); // 6. Now read the whole accounts rst3 = control.Get(options); Assert.NotNull(rst3); if (curhmem.IsChild.HasValue && curhmem.IsChild == true) { acntamt = context.FinanceAccount.Where(p => p.HomeID == hid && p.Owner == user).Count(); Assert.Equal(acntamt, rst3.Cast <FinanceAccount>().Count()); } else { acntamt = context.FinanceAccount.Where(p => p.HomeID == hid).Count(); Assert.Equal(acntamt, rst3.Cast <FinanceAccount>().Count()); } // 7. Delete the first account rst5 = await control.Delete(firstacntid); Assert.NotNull(rst5); rst6 = Assert.IsType <StatusCodeResult>(rst5); Assert.Equal(204, rst6.StatusCode); // 8. Now read the whole accounts rst3 = control.Get(options); Assert.NotNull(rst3); if (curhmem.IsChild.HasValue && curhmem.IsChild == true) { acntamt = context.FinanceAccount.Where(p => p.HomeID == hid && p.Owner == user).Count(); Assert.Equal(acntamt, rst3.Cast <FinanceAccount>().Count()); } else { acntamt = context.FinanceAccount.Where(p => p.HomeID == hid).Count(); Assert.Equal(acntamt, rst3.Cast <FinanceAccount>().Count()); } accountsCreated.Clear(); await context.DisposeAsync(); }
public async Task TestCase_CRUD() { var context = fixture.GetCurrentDataContext(); ExerciseItemsController control = new ExerciseItemsController(context); var userclaim = DataSetupUtility.GetClaimForUser(DataSetupUtility.UserA); control.ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = userclaim } }; // Step 1. Read all var rsts = control.Get(); var rstscnt = await rsts.CountAsync(); var existcnt = context.ExerciseItems.Count(); Assert.Equal(existcnt, rstscnt); // Step 2. Create one know ledge item var ki = new ExerciseItem() { ExerciseType = ExerciseItemType.Question, Content = "New Test 1 Content", }; ki.Answer = new ExerciseItemAnswer { Content = "New Answer" }; ki.Tags.Add(new ExerciseTag() { TagTerm = DataSetupUtility.Tag1, }); var rst = await control.Post(ki); Assert.NotNull(rst); var rst2 = Assert.IsType <CreatedODataResult <ExerciseItem> >(rst); Assert.Equal(rst2.Entity.Content, ki.Content); var firstid = rst2.Entity.ID; Assert.True(firstid > 0); objectsCreated.Add(firstid); // Check the answer var answerctrl = new ExerciseItemAnswersController(context); var answergetrst = answerctrl.Get(firstid); Assert.NotNull(answergetrst); var answergetrstresult = Assert.IsType <SingleResult <ExerciseItemAnswer> >(answergetrst); var answerobj = answergetrstresult.Queryable.ToList().ElementAtOrDefault(0); Assert.Equal(ki.Answer.Content, answerobj.Content); // Check the tag var exertagctrl = new ExerciseTagsController(context); var exertaggetrst = exertagctrl.Get(); Assert.NotNull(exertaggetrst); var exertagobj = exertaggetrst.ToList().Find(p => p.RefID == firstid); Assert.NotNull(exertagobj); Assert.Equal(DataSetupUtility.Tag1, exertagobj.TagTerm); // Step 3. Read all - 1 rsts = control.Get(); rstscnt = await rsts.CountAsync(); Assert.Equal(existcnt + 1, rstscnt); // Step 3a. Read single var singlerst = control.Get(firstid); var singleresult = Assert.IsType <SingleResult <ExerciseItem> >(singlerst); var readitem = singleresult.Queryable.ToList().ElementAtOrDefault(0); Assert.NotNull(readitem); Assert.Equal(firstid, readitem.ID); Assert.Equal(ki.ExerciseType, readitem.ExerciseType); Assert.Equal(ki.Content, readitem.Content); // Step 4. Change the exist one by Put readitem.Content += "Updated by PUT"; if (readitem.Tags == null) { readitem.Tags = new List <ExerciseTag>(); } readitem.Tags.Add(new ExerciseTag { RefID = firstid, TagTerm = DataSetupUtility.Tag2 }); var rstPut = await control.Put(firstid, readitem); Assert.NotNull(rstPut); // Due to the fact that updated result is empty // Check the result in database directly var dbkis = context.ExerciseItems.Where(p => p.ID == firstid).ToList <ExerciseItem>(); Assert.NotEmpty(dbkis); Assert.Equal(readitem.Content, dbkis[0].Content); // Check content only! var dbtags = context.ExerciseTags.Where(p => p.RefID == firstid).ToList <ExerciseTag>(); Assert.NotEmpty(dbtags); Assert.Equal(2, dbtags.Count); Assert.True(dbtags.Find(p => p.TagTerm == DataSetupUtility.Tag1) != null); Assert.True(dbtags.Find(p => p.TagTerm == DataSetupUtility.Tag2) != null); // Step 4a. Change the tags again readitem.Tags.Clear(); readitem.Tags.Add(new ExerciseTag { RefID = firstid, TagTerm = DataSetupUtility.Tag2 }); rstPut = await control.Put(firstid, readitem); Assert.NotNull(rstPut); // Due to the fact that updated result is empty // Check the result in database directly dbtags = context.ExerciseTags.Where(p => p.RefID == firstid).ToList <ExerciseTag>(); Assert.NotEmpty(dbtags); Assert.Single(dbtags); Assert.True(dbtags.Find(p => p.TagTerm == DataSetupUtility.Tag1) == null); Assert.True(dbtags.Find(p => p.TagTerm == DataSetupUtility.Tag2) != null); var tagviewcontrol = new ExerciseItemWithTagViewsController(context); var tagviewgetrst = tagviewcontrol.Get(); Assert.NotNull(tagviewgetrst); // Step 5. Change the object via PATCH var delta = new Delta <ExerciseItem>(); delta.UpdatableProperties.Clear(); delta.UpdatableProperties.Add("Content"); readitem.Content += "Changed for PATCH"; delta.TrySetPropertyValue("Content", readitem.Content); var patchrst = control.Patch(firstid, delta); dbkis = context.ExerciseItems.Where(p => p.ID == firstid).ToList <ExerciseItem>(); Assert.NotEmpty(dbkis); Assert.Equal(readitem.Content, dbkis[0].Content); // Check content only! // Step 6. Delete the object var delrst = control.Delete(firstid); Assert.NotNull(delrst); dbkis = context.ExerciseItems.Where(p => p.ID == firstid).ToList <ExerciseItem>(); Assert.Empty(dbkis); await context.DisposeAsync(); }
public async Task TestCase1(int hid, string currency, string user, short doctype) { var context = this.fixture.GetCurrentDataContext(); // 0. Prepare the context for current home if (hid == DataSetupUtility.Home1ID) { fixture.InitHome1TestData(context); } else if (hid == DataSetupUtility.Home2ID) { fixture.InitHome2TestData(context); } else if (hid == DataSetupUtility.Home3ID) { fixture.InitHome3TestData(context); } else if (hid == DataSetupUtility.Home4ID) { fixture.InitHome4TestData(context); } else if (hid == DataSetupUtility.Home5ID) { fixture.InitHome5TestData(context); } var account = context.FinanceAccount.Where(p => p.HomeID == hid && p.Status != (Byte)FinanceAccountStatus.Closed).FirstOrDefault(); var cc = context.FinanceControlCenter.Where(p => p.HomeID == hid).FirstOrDefault(); // 1. Create first Loan docs. var control = new FinanceDocumentsController(context); var userclaim = DataSetupUtility.GetClaimForUser(user); var httpctx = UnitTestUtility.GetDefaultHttpContext(provider, userclaim); control.ControllerContext = new ControllerContext() { HttpContext = httpctx }; // 1a. Prepare data var dpcontext = new FinanceLoanDocumentCreateContext(); dpcontext.DocumentInfo = new FinanceDocument() { HomeID = hid, DocType = doctype, TranCurr = currency, Desp = "Test 1" }; var item = new FinanceDocumentItem() { DocumentHeader = dpcontext.DocumentInfo, ItemID = 1, Desp = "Item 1.1", TranType = doctype == FinanceDocumentType.DocType_BorrowFrom ? FinanceTransactionType.TranType_BorrowFrom : FinanceTransactionType.TranType_LendTo, TranAmount = 1200, AccountID = account.ID, ControlCenterID = cc.ID, }; dpcontext.DocumentInfo.Items.Add(item); dpcontext.AccountInfo = new FinanceAccount() { HomeID = hid, Name = "Account_8" + ".1", CategoryID = doctype == FinanceDocumentType.DocType_BorrowFrom ? FinanceAccountCategory.AccountCategory_BorrowFrom : FinanceAccountCategory.AccountCategory_LendTo, Owner = user, Status = (Byte)FinanceAccountStatus.Normal, }; var startdate = new DateTime(2020, 1, 10); var enddate = new DateTime(2021, 1, 10); dpcontext.AccountInfo.ExtraLoan = new FinanceAccountExtraLoan() { StartDate = startdate, EndDate = enddate, TotalMonths = 12, RepaymentMethod = LoanRepaymentMethod.EqualPrincipal, InterestFree = false, }; var rsts = CommonUtility.WorkoutRepeatedDatesWithAmountAndInterest(new RepeatDatesWithAmountAndInterestCalInput { RepaymentMethod = dpcontext.AccountInfo.ExtraLoan.RepaymentMethod.Value, InterestFreeLoan = dpcontext.AccountInfo.ExtraLoan.InterestFree.Value, StartDate = dpcontext.AccountInfo.ExtraLoan.StartDate, TotalAmount = 12000, EndDate = dpcontext.AccountInfo.ExtraLoan.EndDate, TotalMonths = dpcontext.AccountInfo.ExtraLoan.TotalMonths.Value, FirstRepayDate = new DateTime(2020, 2, 15) }); var tmpdocid = 1; foreach (var rst in rsts) { var tmpdoc = new FinanceTmpLoanDocument { DocumentID = tmpdocid++, TransactionAmount = rst.TranAmount, InterestAmount = rst.InterestAmount, TransactionDate = rst.TranDate, HomeID = hid, ControlCenterID = item.ControlCenterID, OrderID = item.OrderID, Description = item.Desp, }; dpcontext.AccountInfo.ExtraLoan.LoanTmpDocs.Add(tmpdoc); } var resp = await control.PostLoanDocument(dpcontext); var doc = Assert.IsType <CreatedODataResult <FinanceDocument> >(resp).Entity; documentsCreated.Add(doc.ID); Assert.True(doc.Items.Count == 2); // Now check in the databse foreach (var docitem in doc.Items) { if (docitem.AccountID != account.ID) { accountsCreated.Add(docitem.AccountID); var acnt = context.FinanceAccount.Find(docitem.AccountID); Assert.NotNull(acnt); if (doctype == FinanceDocumentType.DocType_BorrowFrom) { Assert.True(acnt.CategoryID == FinanceAccountCategory.AccountCategory_BorrowFrom); } else if (doctype == FinanceDocumentType.DocType_LendTo) { Assert.True(acnt.CategoryID == FinanceAccountCategory.AccountCategory_LendTo); } var acntExtraLoan = context.FinanceAccountExtraLoan.Find(docitem.AccountID); Assert.NotNull(acntExtraLoan); Assert.True(acntExtraLoan.RefDocID == doc.ID); var tmpdocs = context.FinanceTmpLoanDocument.Where(p => p.AccountID == docitem.AccountID).OrderBy(p => p.TransactionDate).ToList(); Assert.True(rsts.Count == tmpdocs.Count); foreach (var rst in rsts) { DateTime dat = rst.TranDate; var tdoc = tmpdocs.Find(p => p.TransactionDate.Date == dat); Assert.NotNull(tdoc); Assert.True(tdoc.AccountID == acntExtraLoan.AccountID); } } } // Last, clear all created objects CleanupCreatedEntries(); await context.DisposeAsync(); }
public async Task TestCase1(int hid, string user) { var context = this.fixture.GetCurrentDataContext(); if (hid == DataSetupUtility.Home1ID) { fixture.InitHome1TestData(context); } if (hid == DataSetupUtility.Home2ID) { fixture.InitHome2TestData(context); } if (hid == DataSetupUtility.Home3ID) { fixture.InitHome3TestData(context); } if (hid == DataSetupUtility.Home4ID) { fixture.InitHome4TestData(context); } if (hid == DataSetupUtility.Home5ID) { fixture.InitHome5TestData(context); } // 1. Prepare dta var userclaim = DataSetupUtility.GetClaimForUser(user); var httpctx = UnitTestUtility.GetDefaultHttpContext(provider, userclaim); var listCCs = context.FinanceControlCenter.Where(p => p.HomeID == hid).ToList <FinanceControlCenter>(); var curhmemquery = (from homemem in context.HomeMembers where homemem.HomeID == hid && homemem.User == user select homemem).FirstOrDefault(); var curhmem = Assert.IsType <HomeMember>(curhmemquery); var existamt = (from homemem in context.HomeMembers join finord in context.FinanceOrder on new { homemem.HomeID, homemem.User } equals new { finord.HomeID, User = user } select finord.ID).ToList().Count(); var existamt_curhome = context.FinanceOrder.Where(p => p.HomeID == hid).Count(); // 2. Create order var control = new FinanceOrdersController(context); control.ControllerContext = new ControllerContext() { HttpContext = httpctx }; var ord = new FinanceOrder() { HomeID = hid, Name = "Order 1", Comment = "Comment 1" }; var srule = new FinanceOrderSRule() { Order = ord, RuleID = 1, ControlCenterID = listCCs[0].ID, Precent = 100 }; ord.SRule.Add(srule); var rst = await control.Post(ord); Assert.NotNull(rst); var rst2 = Assert.IsType <CreatedODataResult <FinanceOrder> >(rst); Assert.Equal(rst2.Entity.Name, ord.Name); var oid = rst2.Entity.ID; Assert.True(oid > 0); ordersCreated.Add(oid); // 3. Read the order out (without Home ID) var queryUrl = "http://localhost/api/FinanceOrders"; var req = UnitTestUtility.GetHttpRequest(httpctx, "GET", queryUrl); var odatacontext = UnitTestUtility.GetODataQueryContext <FinanceOrder>(this.model); var options = UnitTestUtility.GetODataQueryOptions <FinanceOrder>(odatacontext, req); var rst3 = control.Get(options); Assert.NotNull(rst3); if (curhmem.IsChild.HasValue && curhmem.IsChild == true) { existamt = context.FinanceOrder.Where(p => p.HomeID == hid).Count(); Assert.Equal(existamt + 1, rst3.Cast <FinanceOrder>().Count()); } else { Assert.Equal(existamt + 1, rst3.Cast <FinanceOrder>().Count()); } // 3a. Read the order out (with Home ID) queryUrl = "http://localhost/api/FinanceOrders?$filter=HomeID eq " + hid.ToString(); req = UnitTestUtility.GetHttpRequest(httpctx, "GET", queryUrl); //var odatacontext = UnitTestUtility.GetODataQueryContext<FinanceOrder>(this.model); options = UnitTestUtility.GetODataQueryOptions <FinanceOrder>(odatacontext, req); rst3 = control.Get(options); Assert.NotNull(rst3); Assert.Equal(existamt_curhome + 1, rst3.Cast <FinanceOrder>().Count()); // 4a. Change one order - Add new rule var norder = rst2.Entity; norder.Name = "New Order"; norder.SRule.ElementAt(0).Precent = 80; norder.SRule.Add(new FinanceOrderSRule { Order = norder, RuleID = 2, ControlCenterID = listCCs.Count > 1 ? listCCs[1].ID : listCCs[0].ID, Precent = 20 }); rst = await control.Put(norder.ID, norder); Assert.NotNull(rst); var rst4 = Assert.IsType <UpdatedODataResult <FinanceOrder> >(rst); Assert.Equal(norder.Name, rst4.Entity.Name); // Re-read the order //queryUrl = "http://localhost/api/FinanceOrders?$filter=HomeID eq " + hid.ToString() + " and ID eq " + norder.ID.ToString() + "&$expand=SRule"; //req = UnitTestUtility.GetHttpRequest(httpctx, "GET", queryUrl); //options = UnitTestUtility.GetODataQueryOptions<FinanceOrder>(odatacontext, req); //rst3 = control.Get(options); //var rst3a = rst3.Cast<FinanceOrder>(); //Assert.True(rst3a.Count() == 1); //var rst3a_elem = rst3a.ElementAt(0); //Assert.True(rst3a_elem.SRule.Count == 2); ////var rst3a = Assert.IsType<FinanceOrder>(rst3); ////Assert.True(rst3a.SRule.Count == 2); // 5. Delete an order var rst5 = await control.Delete(oid); Assert.NotNull(rst4); var rst6 = Assert.IsType <StatusCodeResult>(rst5); Assert.Equal(204, rst6.StatusCode); Assert.Equal(0, context.FinanceOrderSRule.Where(p => p.OrderID == oid).Count()); ordersCreated.Clear(); // 6. Read the order again queryUrl = "http://localhost/api/FinanceOrders?$filter=HomeID eq " + hid.ToString(); req = UnitTestUtility.GetHttpRequest(httpctx, "GET", queryUrl); options = UnitTestUtility.GetODataQueryOptions <FinanceOrder>(odatacontext, req); rst3 = control.Get(options); Assert.NotNull(rst3); Assert.Equal(existamt_curhome, rst3.Cast <FinanceOrder>().Count()); await context.DisposeAsync(); }
public async Task TestCase1(string user) { var context = this.fixture.GetCurrentDataContext(); fixture.InitBlogTestData(context); var control = new BlogUserSettingsController(context); var userclaim = DataSetupUtility.GetClaimForUser(user); var httpctx = UnitTestUtility.GetDefaultHttpContext(provider, userclaim); control.ControllerContext = new ControllerContext() { HttpContext = httpctx }; var existedamt = (from coll in context.BlogUserSettings where coll.Owner == user select coll).ToList().Count(); // Step 1. Read all var rsts = control.Get(); var rstscnt = await rsts.CountAsync(); Assert.Equal(existedamt, rstscnt); // Step 2. Create one new collection var nset = new BlogUserSetting() { Owner = user, Name = "Test Setting", Comment = "Test Comment", DeployFolder = "Test" }; var rst = await control.Post(nset); Assert.NotNull(rst); Assert.IsType <ForbidResult>(rst); // Step 3. Update one if (rstscnt > 0) { var existsett = control.Get(user); Assert.NotNull(existsett); var existsett2 = Assert.IsType <SingleResult <BlogUserSetting> >(existsett); Assert.NotNull(existsett2); var existsett2rst = existsett2.Queryable.FirstOrDefault(); existsett2rst.Comment = "Tobe Delteed"; existsett2rst.Author = "Author"; existsett2rst.AuthorDesp = "Author Desp"; existsett2rst.AuthorImage = "Author Image"; var rst3 = await control.Put(existsett2rst.Owner, existsett2rst); Assert.NotNull(rst3); var rst3a = Assert.IsType <OkObjectResult>(rst3); var rst3b = rst3a.Value as BlogUserSetting; Assert.Equal(existsett2rst.Comment, rst3b.Comment); Assert.Equal(existsett2rst.Author, rst3b.Author); Assert.Equal(existsett2rst.AuthorDesp, rst3b.AuthorDesp); Assert.Equal(existsett2rst.AuthorImage, rst3b.AuthorImage); } await context.DisposeAsync(); }
public async Task TestController(int hid, string user) { var context = this.fixture.GetCurrentDataContext(); var control = new LearnCategoriesController(context); var userclaim = DataSetupUtility.GetClaimForUser(user); control.ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = userclaim } }; // 0. Initialize data if (hid == DataSetupUtility.Home1ID) { fixture.InitHome1TestData(context); } else if (hid == DataSetupUtility.Home2ID) { fixture.InitHome2TestData(context); } var existamt = context.LearnCategories.Where(p => p.HomeID == hid || p.HomeID == null).Count(); // 1. Read all categories var items = control.Get(); var itemcnt = items.Count(); Assert.Equal(existamt, itemcnt); // 2. Insert new category var ctgy = new LearnCategory() { HomeID = hid, Name = "Test 1_UT_" + hid.ToString(), Comment = "Test 1" }; var rst1 = await control.Post(ctgy); Assert.NotNull(rst1); var rst2 = Assert.IsType <CreatedODataResult <LearnCategory> >(rst1); Assert.Equal(ctgy.Name, rst2.Entity.Name); var firstctg = rst2.Entity.ID; Assert.True(firstctg > 0); ctgiesCreated.Add(firstctg); Assert.Equal(ctgy.Comment, rst2.Entity.Comment); // 3. Read all categories items = control.Get(); itemcnt = items.Count(); Assert.Equal(existamt + 1, itemcnt); // 4. Change the category's name ctgy.Name = "Test 2"; rst1 = await control.Put(firstctg, ctgy); var rst3 = Assert.IsType <UpdatedODataResult <LearnCategory> >(rst1); Assert.Equal(ctgy.Name, rst3.Entity.Name); // 5. Delete it var rst4 = await control.Delete(firstctg); Assert.NotNull(rst4); var rst6 = Assert.IsType <StatusCodeResult>(rst4); Assert.Equal(204, rst6.StatusCode); ctgiesCreated.Clear(); // 6. Read all categories again items = control.Get(); itemcnt = items.Count(); Assert.Equal(existamt, itemcnt); await context.DisposeAsync(); }
public async Task TestCase_CURD(string usr) { var context = fixture.GetCurrentDataContext(); UserCollectionsController control = new UserCollectionsController(context); var userclaim = DataSetupUtility.GetClaimForUser(usr); control.ControllerContext = new ControllerContext() { HttpContext = new DefaultHttpContext() { User = userclaim } }; // Step 1. Get all var getallrst = control.Get(); Assert.NotNull(getallrst); var collindb = context.UserCollections.AsNoTracking().Where(p => p.User == usr).ToList(); Assert.Equal(collindb.Count, getallrst.Count()); // Step 2. Create one var createdone = new UserCollection() { Name = "Test 1" + usr, Comment = "Test", User = usr, }; var createditem = new UserCollectionItem() { RefType = TagRefType.KnowledgeItem, RefID = DataSetupUtility.Knowledge1ID }; createdone.Items.Add(createditem); var postrst = await control.Post(createdone); Assert.NotNull(postrst); var postcreatedrst = Assert.IsType <CreatedODataResult <UserCollection> >(postrst); var crtid = postcreatedrst.Entity.ID; objectsCreated.Add(crtid); Assert.Equal(createdone.User, postcreatedrst.Entity.User); Assert.Equal(createdone.Name, postcreatedrst.Entity.Name); Assert.Equal(createdone.Comment, postcreatedrst.Entity.Comment); var collitemindb = context.UserCollectionItems.AsNoTracking().Where(p => p.ID == crtid).ToList(); Assert.Single(collitemindb); Assert.Equal(createditem.RefType, collitemindb[0].RefType); Assert.Equal(createditem.RefID, collitemindb[0].RefID); // Step 3. Get single var getsinglerst = control.Get(crtid); Assert.NotNull(getsinglerst); var getrstresult = Assert.IsType <SingleResult <UserCollection> >(getsinglerst); var readitem = getrstresult.Queryable.ToList().ElementAtOrDefault(0); Assert.NotNull(readitem); Assert.Equal(crtid, readitem.ID); Assert.Equal(createdone.User, readitem.User); Assert.Equal(createdone.Name, readitem.Name); Assert.Equal(createdone.Comment, readitem.Comment); // Step 4. Change via PUT readitem.Comment += "Changed"; readitem.Items.Clear(); createditem = new UserCollectionItem() { RefID = DataSetupUtility.Exercise1ID, RefType = TagRefType.ExerciseItem, ID = crtid, Collection = readitem, }; readitem.Items.Add(createditem); var putrst = await control.Put(crtid, readitem); Assert.NotNull(putrst); var putokrst = Assert.IsType <OkObjectResult>(putrst); Assert.NotNull(putokrst); var putokrstobj = Assert.IsType <UserCollection>(putokrst.Value); Assert.Equal(readitem.ID, putokrstobj.ID); Assert.Equal(readitem.User, putokrstobj.User); Assert.Equal(readitem.Name, putokrstobj.Name); Assert.Equal(readitem.Comment, putokrstobj.Comment); collindb = context.UserCollections.AsNoTracking().Where(p => p.User == usr).ToList(); collitemindb = context.UserCollectionItems.AsNoTracking().Where(p => p.ID == crtid).ToList(); Assert.Single(collindb); Assert.Single(collitemindb); Assert.Equal(readitem.Comment, collindb[0].Comment); Assert.Equal(createditem.RefID, collitemindb[0].RefID); Assert.Equal(createditem.RefType, collitemindb[0].RefType); // Step 5. Delete var delrst = await control.Delete(crtid); Assert.NotNull(delrst); var delrtn = Assert.IsType <StatusCodeResult>(delrst); objectsCreated.Remove(crtid); await context.DisposeAsync(); }
public async Task TestCase1(string user) { var context = this.fixture.GetCurrentDataContext(); fixture.InitBlogTestData(context); var control = new BlogCollectionsController(context); var userclaim = DataSetupUtility.GetClaimForUser(user); var httpctx = UnitTestUtility.GetDefaultHttpContext(provider, userclaim); control.ControllerContext = new ControllerContext() { HttpContext = httpctx }; var existedamt = (from coll in context.BlogCollections where coll.Owner == user select coll).ToList().Count(); // Step 1. Read all var rsts = control.Get(); var rstscnt = await rsts.CountAsync(); Assert.Equal(existedamt, rstscnt); // Step 2. Create one new collection var newcoll = new BlogCollection() { Owner = user, Name = "TestCase1_Add_" + user, Comment = "TestCase1_Add_" + user, }; var rst = await control.Post(newcoll); Assert.NotNull(rst); var rst2 = Assert.IsType <CreatedODataResult <BlogCollection> >(rst); objectsCreated.Add(rst2.Entity.ID); newcoll.ID = rst2.Entity.ID; Assert.Equal(rst2.Entity.Name, newcoll.Name); Assert.Equal(rst2.Entity.Comment, newcoll.Comment); Assert.Equal(rst2.Entity.Owner, user); // Step 3. Read all rsts = control.Get(); rstscnt = await rsts.CountAsync(); Assert.Equal(existedamt + 1, rstscnt); // Step 4. Change it newcoll.Name = "Tobe Delteed"; var rst3 = await control.Put(newcoll.ID, newcoll); Assert.NotNull(rst3); var rst3a = Assert.IsType <UpdatedODataResult <BlogCollection> >(rst3); Assert.Equal(newcoll.Name, rst3a.Entity.Name); // Step 5. Delete it var rst5 = await control.Delete(newcoll.ID); Assert.NotNull(rst5); objectsCreated.Remove(newcoll.ID); var rst5a = Assert.IsType <StatusCodeResult>(rst5); Assert.Equal(204, rst5a.StatusCode); // Step 6. Read it again rsts = control.Get(); rstscnt = await rsts.CountAsync(); Assert.Equal(existedamt, rstscnt); await context.DisposeAsync(); }
public async Task CalculatePoints_GoToBedTimeRule() { var context = this.fixture.GetCurrentDataContext(); DailyTracesController control = new(context); // RuleMatrix // Days [20 - 21] [21 - 22] [22 - 24] // [1,1] 1 -1 -5 // [2,2] 2 -2 -10 // [3,3] 5 -5 -15 // [4,4] 10 -10 -20 // [5,INFIN] 20 -20 -25 // // [20-21] #region [20-21] AwardRule r1 = new() { RuleType = AwardRuleType.GoToBedTime, TargetUser = "******", TimeStart = 20, TimeEnd = 21, DaysFrom = 1, DaysTo = 1, Point = 1, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_1_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.GoToBedTime, TargetUser = "******", TimeStart = 20, TimeEnd = 21, DaysFrom = 2, DaysTo = 2, Point = 2, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_2_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.GoToBedTime, TargetUser = "******", TimeStart = 20, TimeEnd = 21, DaysFrom = 3, DaysTo = 3, Point = 5, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_3_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.GoToBedTime, TargetUser = "******", TimeStart = 20, TimeEnd = 21, DaysFrom = 4, DaysTo = 4, Point = 10, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_4_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.GoToBedTime, TargetUser = "******", TimeStart = 20, TimeEnd = 21, DaysFrom = 5, DaysTo = 9999, Point = 20, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_5_1" }; context.Add(r1); #endregion // [21-22] #region [21-22] r1 = new() { RuleType = AwardRuleType.GoToBedTime, TargetUser = "******", TimeStart = 21, TimeEnd = 22, DaysFrom = 1, DaysTo = 1, Point = -1, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_1_2" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.GoToBedTime, TargetUser = "******", TimeStart = 21, TimeEnd = 22, DaysFrom = 2, DaysTo = 2, Point = -2, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_2_2" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.GoToBedTime, TargetUser = "******", TimeStart = 21, TimeEnd = 22, DaysFrom = 3, DaysTo = 3, Point = -5, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_3_2" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.GoToBedTime, TargetUser = "******", TimeStart = 21, TimeEnd = 22, DaysFrom = 4, DaysTo = 4, Point = -10, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_4_2" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.GoToBedTime, TargetUser = "******", TimeStart = 21, TimeEnd = 22, DaysFrom = 5, DaysTo = 9999, Point = -20, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_5_2" }; context.Add(r1); #endregion // [22-24] #region [22-24] r1 = new() { RuleType = AwardRuleType.GoToBedTime, TargetUser = "******", TimeStart = 22, TimeEnd = 24, DaysFrom = 1, DaysTo = 1, Point = -5, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_1_3" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.GoToBedTime, TargetUser = "******", TimeStart = 22, TimeEnd = 24, DaysFrom = 2, DaysTo = 2, Point = -10, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_2_3" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.GoToBedTime, TargetUser = "******", TimeStart = 22, TimeEnd = 24, DaysFrom = 3, DaysTo = 3, Point = -15, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_3_3" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.GoToBedTime, TargetUser = "******", TimeStart = 22, TimeEnd = 24, DaysFrom = 4, DaysTo = 4, Point = -20, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_4_3" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.GoToBedTime, TargetUser = "******", TimeStart = 22, TimeEnd = 24, DaysFrom = 5, DaysTo = 9999, Point = -25, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_5_3" }; context.Add(r1); #endregion await context.SaveChangesAsync(); // Add first daily trace on 2021-05-01 DailyTrace dt = new() { RecordDate = new DateTime(2021, 5, 1), TargetUser = "******", GoToBedTime = (decimal)20.5 }; var points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(1, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Add second daily trace on 2021-05-02 dt = new() { RecordDate = new DateTime(2021, 5, 2), TargetUser = "******", GoToBedTime = (decimal)20.6 }; points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(2, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Add third daily trace on 2021-05-03 dt = new() { RecordDate = new DateTime(2021, 5, 3), TargetUser = "******", GoToBedTime = (decimal)20.7 }; points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(5, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Add fourth daily trace on 2021-05-05 dt = new() { RecordDate = new DateTime(2021, 5, 5), TargetUser = "******", GoToBedTime = (decimal)20.7 }; points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(1, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Clean award data DataSetupUtility.DeleteAwardData(context); await context.DisposeAsync(); } [Fact] public async Task CalculatePoints_SchoolWorkTimeRule() { var context = this.fixture.GetCurrentDataContext(); DailyTracesController control = new(context); // RuleMatrix // Days [13 - 18] [18 - 20] [20 - 24] // [1,1] 1 -1 -5 // [2,2] 2 -2 -10 // [3,3] 5 -5 -15 // [4,4] 10 -10 -20 // [5,INFIN] 20 -20 -25 // // [13-18] #region [13-18] AwardRule r1 = new() { RuleType = AwardRuleType.SchoolWorkTime, TargetUser = "******", TimeStart = 13, TimeEnd = 18, DaysFrom = 1, DaysTo = 1, Point = 1, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_1_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.SchoolWorkTime, TargetUser = "******", TimeStart = 13, TimeEnd = 18, DaysFrom = 2, DaysTo = 2, Point = 2, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_2_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.SchoolWorkTime, TargetUser = "******", TimeStart = 13, TimeEnd = 18, DaysFrom = 3, DaysTo = 3, Point = 5, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_3_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.SchoolWorkTime, TargetUser = "******", TimeStart = 13, TimeEnd = 18, DaysFrom = 4, DaysTo = 4, Point = 10, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_4_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.SchoolWorkTime, TargetUser = "******", TimeStart = 13, TimeEnd = 18, DaysFrom = 5, DaysTo = 9999, Point = 20, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_5_1" }; context.Add(r1); #endregion // [18 - 20] #region [18 - 20] r1 = new() { RuleType = AwardRuleType.SchoolWorkTime, TargetUser = "******", TimeStart = 18, TimeEnd = 20, DaysFrom = 1, DaysTo = 1, Point = -1, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_1_2" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.SchoolWorkTime, TargetUser = "******", TimeStart = 18, TimeEnd = 20, DaysFrom = 2, DaysTo = 2, Point = -2, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_2_2" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.SchoolWorkTime, TargetUser = "******", TimeStart = 18, TimeEnd = 20, DaysFrom = 3, DaysTo = 3, Point = -5, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_3_2" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.SchoolWorkTime, TargetUser = "******", TimeStart = 18, TimeEnd = 20, DaysFrom = 4, DaysTo = 4, Point = -10, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_4_2" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.SchoolWorkTime, TargetUser = "******", TimeStart = 18, TimeEnd = 20, DaysFrom = 5, DaysTo = 9999, Point = -20, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_5_2" }; context.Add(r1); #endregion // [20-24] #region [20-24] r1 = new() { RuleType = AwardRuleType.SchoolWorkTime, TargetUser = "******", TimeStart = 20, TimeEnd = 24, DaysFrom = 1, DaysTo = 1, Point = -5, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_1_3" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.SchoolWorkTime, TargetUser = "******", TimeStart = 20, TimeEnd = 24, DaysFrom = 2, DaysTo = 2, Point = -10, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_2_3" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.SchoolWorkTime, TargetUser = "******", TimeStart = 20, TimeEnd = 24, DaysFrom = 3, DaysTo = 3, Point = -15, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_3_3" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.SchoolWorkTime, TargetUser = "******", TimeStart = 20, TimeEnd = 24, DaysFrom = 4, DaysTo = 4, Point = -20, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_4_3" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.SchoolWorkTime, TargetUser = "******", TimeStart = 20, TimeEnd = 24, DaysFrom = 5, DaysTo = 9999, Point = -25, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Gotobed_5_3" }; context.Add(r1); #endregion await context.SaveChangesAsync(); // Add first daily trace on 2021-05-01 DailyTrace dt = new() { RecordDate = new DateTime(2021, 5, 1), TargetUser = "******", SchoolWorkTime = (decimal)18.5 }; var points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(-1, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Add second daily trace on 2021-05-02 dt = new() { RecordDate = new DateTime(2021, 5, 2), TargetUser = "******", SchoolWorkTime = (decimal)19.6 }; points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(-2, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Add third daily trace on 2021-05-03 dt = new() { RecordDate = new DateTime(2021, 5, 3), TargetUser = "******", SchoolWorkTime = (decimal)16.7 }; points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(1, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Add fourth daily trace on 2021-05-05 dt = new() { RecordDate = new DateTime(2021, 5, 5), TargetUser = "******", SchoolWorkTime = (decimal)20.7 }; points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(-5, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Clean award data DataSetupUtility.DeleteAwardData(context); await context.DisposeAsync(); } [Fact] public async Task CalculatePoints_ErrorCollectionRule() { var context = this.fixture.GetCurrentDataContext(); DailyTracesController control = new(context); // RuleMatrix // Days [Yes] [No] // [1,1] 2 -5 // [2,2] 5 -10 // [3,3] 10 -20 // [4,4] 15 -30 // [5,INFIN] 20 -40 // // [Yes] #region [Yes] AwardRule r1 = new() { RuleType = AwardRuleType.ErrorCollectionHabit, TargetUser = "******", DoneOfFact = true, DaysFrom = 1, DaysTo = 1, Point = 2, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Rule_1_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.ErrorCollectionHabit, TargetUser = "******", DoneOfFact = true, DaysFrom = 2, DaysTo = 2, Point = 5, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Rule_2_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.ErrorCollectionHabit, TargetUser = "******", DoneOfFact = true, DaysFrom = 3, DaysTo = 3, Point = 10, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Rule_3_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.ErrorCollectionHabit, TargetUser = "******", DoneOfFact = true, DaysFrom = 4, DaysTo = 4, Point = 15, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Rule_4_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.ErrorCollectionHabit, TargetUser = "******", DoneOfFact = true, DaysFrom = 5, DaysTo = 9999, Point = 20, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Rule_5_1" }; context.Add(r1); #endregion #region [No] r1 = new() { RuleType = AwardRuleType.ErrorCollectionHabit, TargetUser = "******", DoneOfFact = false, DaysFrom = 1, DaysTo = 1, Point = -5, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Rule_1_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.ErrorCollectionHabit, TargetUser = "******", DoneOfFact = false, DaysFrom = 2, DaysTo = 2, Point = -10, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Rule_2_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.ErrorCollectionHabit, TargetUser = "******", DoneOfFact = false, DaysFrom = 3, DaysTo = 3, Point = -20, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Rule_3_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.ErrorCollectionHabit, TargetUser = "******", DoneOfFact = false, DaysFrom = 4, DaysTo = 4, Point = -30, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Rule_4_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.ErrorCollectionHabit, TargetUser = "******", DoneOfFact = false, DaysFrom = 5, DaysTo = 9999, Point = -40, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Rule_5_1" }; context.Add(r1); #endregion await context.SaveChangesAsync(); // Add first daily trace on 2021-05-01 DailyTrace dt = new() { RecordDate = new DateTime(2021, 5, 1), TargetUser = "******", ErrorsCollection = true }; var points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(2, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Add second daily trace on 2021-05-02 dt = new() { RecordDate = new DateTime(2021, 5, 2), TargetUser = "******", ErrorsCollection = true }; points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(5, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Add third daily trace on 2021-05-03 dt = new() { RecordDate = new DateTime(2021, 5, 3), TargetUser = "******", ErrorsCollection = false }; points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(-5, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Add fourth daily trace on 2021-05-05 dt = new() { RecordDate = new DateTime(2021, 5, 5), TargetUser = "******", ErrorsCollection = false }; points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(-5, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Clean award data DataSetupUtility.DeleteAwardData(context); await context.DisposeAsync(); } [Fact] public async Task CalculatePoints_HandwritingRule() { var context = this.fixture.GetCurrentDataContext(); DailyTracesController control = new(context); // RuleMatrix // Days [Yes] [No] // [1,1] 2 -2 // [2,2] 4 -5 // [3,3] 6 -10 // [4,4] 8 -20 // [5,INFIN] 10 -25 // // [Yes] #region [Yes] AwardRule r1 = new() { RuleType = AwardRuleType.HandWritingHabit, TargetUser = "******", DoneOfFact = true, DaysFrom = 1, DaysTo = 1, Point = 2, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Rule_1_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.HandWritingHabit, TargetUser = "******", DoneOfFact = true, DaysFrom = 2, DaysTo = 2, Point = 4, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Rule_2_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.HandWritingHabit, TargetUser = "******", DoneOfFact = true, DaysFrom = 3, DaysTo = 3, Point = 6, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Rule_3_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.HandWritingHabit, TargetUser = "******", DoneOfFact = true, DaysFrom = 4, DaysTo = 4, Point = 8, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Rule_4_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.HandWritingHabit, TargetUser = "******", DoneOfFact = true, DaysFrom = 5, DaysTo = 9999, Point = 10, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Rule_5_1" }; context.Add(r1); #endregion #region [No] r1 = new() { RuleType = AwardRuleType.HandWritingHabit, TargetUser = "******", DoneOfFact = false, DaysFrom = 1, DaysTo = 1, Point = -2, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Rule_1_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.HandWritingHabit, TargetUser = "******", DoneOfFact = false, DaysFrom = 2, DaysTo = 2, Point = -5, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Rule_2_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.HandWritingHabit, TargetUser = "******", DoneOfFact = false, DaysFrom = 3, DaysTo = 3, Point = -10, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Rule_3_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.HandWritingHabit, TargetUser = "******", DoneOfFact = false, DaysFrom = 4, DaysTo = 4, Point = -15, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Rule_4_1" }; context.Add(r1); r1 = new AwardRule { RuleType = AwardRuleType.HandWritingHabit, TargetUser = "******", DoneOfFact = false, DaysFrom = 5, DaysTo = 9999, Point = -20, ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = "Rule_5_1" }; context.Add(r1); #endregion await context.SaveChangesAsync(); // Add first daily trace on 2021-05-01 DailyTrace dt = new() { RecordDate = new DateTime(2021, 5, 1), TargetUser = "******", HandWriting = true }; var points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(2, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Add second daily trace on 2021-05-02 dt = new() { RecordDate = new DateTime(2021, 5, 2), TargetUser = "******", HandWriting = true }; points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(4, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Add third daily trace on 2021-05-03 dt = new() { RecordDate = new DateTime(2021, 5, 3), TargetUser = "******", HandWriting = false }; points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(-2, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Add fourth daily trace on 2021-05-05 dt = new() { RecordDate = new DateTime(2021, 5, 5), TargetUser = "******", HandWriting = false }; points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(-2, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Clean award data DataSetupUtility.DeleteAwardData(context); await context.DisposeAsync(); } [Fact] public async Task CalculatePoints_BodyExerciseRule() { var context = this.fixture.GetCurrentDataContext(); DailyTracesController control = new(context); // RuleMatrix // Days [0] [1] [2] [3-9999] // [1,1] -2 1 2 3 // [2,2] -5 2 4 6 // [3,3] -10 3 6 9 // [4,4] -15 4 8 12 // [5,INFIN] -20 5 10 15 // int[,] dataMatrix = new int[, ] { { -2, 1, 2, 3 }, { -5, 2, 4, 6 }, { -10, 3, 6, 9 }, { -15, 4, 8, 12 }, { -20, 5, 10, 15 }, }; for (int i = 0; i < dataMatrix.GetLength(0); i++) { int daysfrom = i + 1, daysto = i + 1; if (daysto == 5) { daysto = 9999; } for (int j = 0; j < dataMatrix.GetLength(1); j++) { int countOfFactLow = j, countofFactHigh = j; if (countofFactHigh == 3) { countofFactHigh = 9999; } AwardRule r1 = new() { RuleType = AwardRuleType.BodyExerciseCount, TargetUser = "******", CountOfFactLow = countOfFactLow, CountOfFactHigh = countofFactHigh, DaysFrom = daysfrom, DaysTo = daysto, Point = dataMatrix[i, j], ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = String.Format("Rule_{0}_{1}", i, j) }; context.Add(r1); } } await context.SaveChangesAsync(); // Add first daily trace on 2021-05-01 DailyTrace dt = new() { RecordDate = new DateTime(2021, 5, 1), TargetUser = "******", BodyExerciseCount = 2 }; var points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(2, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Add second daily trace on 2021-05-02 dt = new() { RecordDate = new DateTime(2021, 5, 2), TargetUser = "******", BodyExerciseCount = 1 }; points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(1, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Add third daily trace on 2021-05-03 dt = new() { RecordDate = new DateTime(2021, 5, 3), TargetUser = "******", BodyExerciseCount = 1 }; points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(2, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Add fourth daily trace on 2021-05-05 dt = new() { RecordDate = new DateTime(2021, 5, 5), TargetUser = "******", BodyExerciseCount = 0 }; points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(-2, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Clean award data DataSetupUtility.DeleteAwardData(context); await context.DisposeAsync(); } [Fact] public async Task CalculatePoints_HomeWorkRule() { var context = this.fixture.GetCurrentDataContext(); DailyTracesController control = new(context); // RuleMatrix // Days [1] [2] [3-9999] // [1,1] 1 2 3 // [2,2] 2 4 6 // [3,3] 3 6 9 // [4,4] 4 8 12 // [5,5] 5 10 15 // [6,INFIN] 6 12 18 // int[,] dataMatrix = new int[, ] { { 1, 2, 3 }, { 2, 4, 6 }, { 3, 6, 9 }, { 4, 8, 12 }, { 5, 10, 15 }, { 6, 12, 18 }, }; for (int i = 0; i < dataMatrix.GetLength(0); i++) { int daysfrom = i + 1, daysto = i + 1; if (daysto == 6) { daysto = 9999; } for (int j = 0; j < dataMatrix.GetLength(1); j++) { int countOfFactLow = j + 1, countofFactHigh = j + 1; if (countofFactHigh == 3) { countofFactHigh = 9999; } AwardRule r1 = new() { RuleType = AwardRuleType.HomeWorkCount, TargetUser = "******", CountOfFactLow = countOfFactLow, CountOfFactHigh = countofFactHigh, DaysFrom = daysfrom, DaysTo = daysto, Point = dataMatrix[i, j], ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = String.Format("Rule_{0}_{1}", i, j) }; context.Add(r1); } } await context.SaveChangesAsync(); // Add first daily trace on 2021-05-01 DailyTrace dt = new() { RecordDate = new DateTime(2021, 5, 1), TargetUser = "******", HomeWorkCount = 2 }; var points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(2, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Add second daily trace on 2021-05-02 dt = new() { RecordDate = new DateTime(2021, 5, 2), TargetUser = "******", HomeWorkCount = 2 }; points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(4, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Add third daily trace on 2021-05-03 dt = new() { RecordDate = new DateTime(2021, 5, 3), TargetUser = "******", HomeWorkCount = 1 }; points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(1, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Add fourth daily trace on 2021-05-05 dt = new() { RecordDate = new DateTime(2021, 5, 5), TargetUser = "******", HomeWorkCount = 3 }; points = control.CalculatePoints(dt); Assert.Single(points); Assert.Equal(3, points[0].Point); context.Add(points[0]); await context.SaveChangesAsync(); // Clean award data DataSetupUtility.DeleteAwardData(context); await context.DisposeAsync(); } [Fact] public async Task CalculatePoints_MultipleRules() { var context = this.fixture.GetCurrentDataContext(); DailyTracesController control = new(context); // RuleMatrix for homework // Days [1] [2] [3-9999] // [1,1] 1 2 3 // [2,2] 2 4 6 // [3,3] 3 6 9 // [4,4] 4 8 12 // [5,5] 5 10 15 // [6,INFIN] 6 12 18 // int[,] dataMatrix = new int[, ] { { 1, 2, 3 }, { 2, 4, 6 }, { 3, 6, 9 }, { 4, 8, 12 }, { 5, 10, 15 }, { 6, 12, 18 }, }; for (int i = 0; i < dataMatrix.GetLength(0); i++) { int daysfrom = i + 1, daysto = i + 1; if (daysto == 6) { daysto = 9999; } for (int j = 0; j < dataMatrix.GetLength(1); j++) { int countOfFactLow = j + 1, countofFactHigh = j + 1; if (countofFactHigh == 3) { countofFactHigh = 9999; } AwardRule r1 = new() { RuleType = AwardRuleType.HomeWorkCount, TargetUser = "******", CountOfFactLow = countOfFactLow, CountOfFactHigh = countofFactHigh, DaysFrom = daysfrom, DaysTo = daysto, Point = dataMatrix[i, j], ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = String.Format("Rule_{0}_{1}", i, j) }; context.Add(r1); } } // RuleMatrix for body exercise // Days [0] [1] [2] [3-9999] // [1,1] -2 1 2 3 // [2,2] -5 2 4 6 // [3,3] -10 3 6 9 // [4,4] -15 4 8 12 // [5,INFIN] -20 5 10 15 // int[,] dataMatrix2 = new int[, ] { { -2, 1, 2, 3 }, { -5, 2, 4, 6 }, { -10, 3, 6, 9 }, { -15, 4, 8, 12 }, { -20, 5, 10, 15 }, }; for (int i = 0; i < dataMatrix2.GetLength(0); i++) { int daysfrom = i + 1, daysto = i + 1; if (daysto == 5) { daysto = 9999; } for (int j = 0; j < dataMatrix2.GetLength(1); j++) { int countOfFactLow = j, countofFactHigh = j; if (countofFactHigh == 3) { countofFactHigh = 9999; } AwardRule r1 = new() { RuleType = AwardRuleType.BodyExerciseCount, TargetUser = "******", CountOfFactLow = countOfFactLow, CountOfFactHigh = countofFactHigh, DaysFrom = daysfrom, DaysTo = daysto, Point = dataMatrix2[i, j], ValidFrom = new DateTime(2021, 1, 1), ValidTo = new DateTime(2021, 12, 31), Desp = String.Format("Rule_{0}_{1}", i, j) }; context.Add(r1); } } await context.SaveChangesAsync(); // Add first daily trace on 2021-05-01 DailyTrace dt = new() { RecordDate = new DateTime(2021, 5, 1), TargetUser = "******", HomeWorkCount = 2, BodyExerciseCount = 0 }; var points = control.CalculatePoints(dt); Assert.Equal(2, points.Count); context.AddRange(points); await context.SaveChangesAsync(); // Clean award data DataSetupUtility.DeleteAwardData(context); await context.DisposeAsync(); } } }
public async Task TestCase1(int hid, string currency, string user) { var context = this.fixture.GetCurrentDataContext(); // 0a. Prepare the context for other homes to test the filters var secondhid = hid; if (hid == DataSetupUtility.Home1ID) { if (user == DataSetupUtility.UserA || user == DataSetupUtility.UserB) { secondhid = DataSetupUtility.Home3ID; } else if (user == DataSetupUtility.UserC) { secondhid = DataSetupUtility.Home4ID; } else if (user == DataSetupUtility.UserD) { secondhid = DataSetupUtility.Home5ID; } } else if (hid == DataSetupUtility.Home2ID) { secondhid = DataSetupUtility.Home3ID; } if (hid == DataSetupUtility.Home1ID || secondhid == DataSetupUtility.Home1ID) { fixture.InitHome1TestData(context); } else if (hid == DataSetupUtility.Home2ID || secondhid == DataSetupUtility.Home2ID) { fixture.InitHome2TestData(context); } else if (hid == DataSetupUtility.Home3ID || secondhid == DataSetupUtility.Home3ID) { fixture.InitHome3TestData(context); } else if (hid == DataSetupUtility.Home4ID || secondhid == DataSetupUtility.Home4ID) { fixture.InitHome4TestData(context); } else if (hid == DataSetupUtility.Home5ID || secondhid == DataSetupUtility.Home5ID) { fixture.InitHome5TestData(context); } var account = context.FinanceAccount.Where(p => p.HomeID == hid && p.Status != (Byte)FinanceAccountStatus.Closed).FirstOrDefault(); var cc = context.FinanceControlCenter.Where(p => p.HomeID == hid).FirstOrDefault(); var curhmemquery = (from homemem in context.HomeMembers where homemem.HomeID == hid && homemem.User == user select homemem).FirstOrDefault(); var curhmem = Assert.IsType <HomeMember>(curhmemquery); var existamt = (from homemem in context.HomeMembers join findoc in context.FinanceDocument on new { homemem.HomeID, homemem.User } equals new { findoc.HomeID, User = user } select findoc.ID).ToList().Count(); var existamt_curhome = context.FinanceDocument.Where(p => p.HomeID == hid).Count(); // 1. Create first docs. var control = new FinanceDocumentsController(context); var userclaim = DataSetupUtility.GetClaimForUser(user); var httpctx = UnitTestUtility.GetDefaultHttpContext(provider, userclaim); control.ControllerContext = new ControllerContext() { HttpContext = httpctx }; var doc = new FinanceDocument() { HomeID = hid, DocType = FinanceDocumentType.DocType_Normal, TranCurr = currency, Desp = "Test 1" }; var item = new FinanceDocumentItem() { DocumentHeader = doc, ItemID = 1, Desp = "Item 1.1", TranType = 2, // Wage TranAmount = 10, AccountID = account.ID, ControlCenterID = cc.ID, }; doc.Items.Add(item); var rst = await control.Post(doc); Assert.NotNull(rst); var rst2 = Assert.IsType <CreatedODataResult <FinanceDocument> >(rst); Assert.Equal(rst2.Entity.TranCurr, doc.TranCurr); Assert.Equal(rst2.Entity.Desp, doc.Desp); var firstdocid = rst2.Entity.ID; documentsCreated.Add(firstdocid); Assert.True(firstdocid > 0); // 2a. Now read the whole orders (without home id) var queryUrl = "http://localhost/api/FinanceDocuments"; var req = UnitTestUtility.GetHttpRequest(httpctx, "GET", queryUrl); var odatacontext = UnitTestUtility.GetODataQueryContext <FinanceDocument>(this.model); var options = UnitTestUtility.GetODataQueryOptions <FinanceDocument>(odatacontext, req); var rst3 = control.Get(options); Assert.NotNull(rst3); //if (curhmem.IsChild.HasValue && curhmem.IsChild == true) //{ // existamt = context.FinanceDocument.Where(p => p.Createdby == user).Count(); // Assert.Equal(existamt, rst3.Cast<FinanceDocument>().Count()); //} //else //{ // Assert.Equal(existamt + 1, rst3.Cast<FinanceDocument>().Count()); //} // 2b. Now read the whole orders (with home id) queryUrl = "http://localhost/api/FinanceDocuments?$filter=HomeID eq " + hid.ToString(); req = UnitTestUtility.GetHttpRequest(httpctx, "GET", queryUrl); // odatacontext = UnitTestUtility.GetODataQueryContext<FinanceDocument>(this.model); options = UnitTestUtility.GetODataQueryOptions <FinanceDocument>(odatacontext, req); rst3 = control.Get(options); Assert.NotNull(rst3); if (curhmem.IsChild.HasValue && curhmem.IsChild == true) { existamt_curhome = context.FinanceDocument.Where(p => p.HomeID == hid && p.Createdby == user).Count(); Assert.Equal(existamt_curhome, rst3.Cast <FinanceDocument>().Count()); } else { Assert.Equal(existamt_curhome + 1, rst3.Cast <FinanceDocument>().Count()); } // 3. Now create another one! doc = new FinanceDocument() { HomeID = hid, DocType = FinanceDocumentType.DocType_Normal, TranCurr = currency, Desp = "Test 2" }; item = new FinanceDocumentItem() { DocumentHeader = doc, ItemID = 1, Desp = "Item 2.1", TranType = 2, // Wage TranAmount = 10, AccountID = account.ID, ControlCenterID = cc.ID }; doc.Items.Add(item); rst = await control.Post(doc); Assert.NotNull(rst); rst2 = Assert.IsType <CreatedODataResult <FinanceDocument> >(rst); Assert.Equal(rst2.Entity.TranCurr, doc.TranCurr); Assert.Equal(rst2.Entity.Desp, doc.Desp); // documentsCreated.Add(rst2.Entity); var seconddocid = rst2.Entity.ID; Assert.True(seconddocid > 0); // 4a. Change one document - add new document item doc.Desp = "Change Test"; doc.Items.Add(new FinanceDocumentItem { DocumentHeader = doc, ItemID = 2, Desp = "Item 2.2", TranType = 2, // Wage TranAmount = 20, AccountID = account.ID, ControlCenterID = cc.ID }); rst = await control.Put(seconddocid, doc); Assert.NotNull(rst); var rst4 = Assert.IsType <UpdatedODataResult <FinanceDocument> >(rst); Assert.Equal(doc.Desp, rst4.Entity.Desp); Assert.Equal(2, rst4.Entity.Items.Count); Assert.Equal("Item 2.1", rst4.Entity.Items.First(p => p.ItemID == 1).Desp); Assert.Equal("Item 2.2", rst4.Entity.Items.First(p => p.ItemID == 2).Desp); // 4b. Change one document - remove document item var itemidx = doc.Items.First(p => p.ItemID == 2); doc.Items.Remove(item); rst = await control.Put(seconddocid, doc); Assert.NotNull(rst); rst4 = Assert.IsType <UpdatedODataResult <FinanceDocument> >(rst); Assert.Equal(1, rst4.Entity.Items.Count); // Assert.Equal("Item 2.1", rst4.Entity.Items.First(p => p.ItemID == 1).Desp); // 5. Delete the second document var rst5 = await control.Delete(seconddocid); Assert.NotNull(rst5); var rst6 = Assert.IsType <StatusCodeResult>(rst5); Assert.Equal(204, rst6.StatusCode); Assert.Equal(0, context.FinanceDocumentItem.Where(p => p.DocID == seconddocid).Count()); // 6. Now read the whole documents rst3 = control.Get(options); Assert.NotNull(rst3); if (curhmem.IsChild.HasValue && curhmem.IsChild == true) { existamt_curhome = context.FinanceDocument.Where(p => p.HomeID == hid && p.Createdby == user).Count(); Assert.Equal(existamt_curhome, rst3.Cast <FinanceDocument>().Count()); } else { Assert.Equal(existamt_curhome + 1, rst3.Cast <FinanceDocument>().Count()); } // Last, clear all created objects CleanupCreatedEntries(); await context.SaveChangesAsync(); await context.DisposeAsync(); }
public async Task CalculatePoints(UserHabitRecordsControllerTestData_MonthNoOfCount testData) { var context = this.fixture.GetCurrentDataContext(); UserHabitRecordsController control = new(context); // Add Habit, Habit Rules UserHabit habit = new UserHabit(); habit.TargetUser = DataSetupUtility.UserA; habit.ValidFrom = new DateTime(2021, 1, 1); habit.ValidTo = new DateTime(2022, 12, 31); habit.Name = "Habit_1"; habit.Category = HabitCategory.Positive; habit.Comment = habit.Name; habit.Frequency = HabitFrequency.Monthly; habit.CompleteCategory = HabitCompleteCategory.NumberOfCount; habit.CompleteCondition = testData.CompleteCondition; habit.StartDate = testData.DateInMonth; context.UserHabits.Add(habit); context.SaveChanges(); Int32 nNewHabitID = habit.ID; foreach (var rule in testData.RuleList) { rule.HabitID = nNewHabitID; context.UserHabitRules.Add(rule); } context.SaveChanges(); // Add user record. foreach (UserHabitRecord record in testData.RecordList) { record.HabitID = nNewHabitID; var rst = control.Post(record); Assert.NotNull(rst); if (rst != null) { CreatedODataResult <UserHabitRecord> rstrecord = (CreatedODataResult <UserHabitRecord>)rst.Result; Assert.NotNull(rstrecord); } } // Check on DB directly var dbrecords = (from dbrecord in context.UserHabitRecords where dbrecord.HabitID == nNewHabitID select dbrecord).ToList(); Assert.Equal(testData.RecordCount, dbrecords.Count); // Ensure rule is assigned correctly if (testData.ExpectedRuleDateList.Count > 0) { var rulecnt = 0; dbrecords.ForEach(dbr => { if (dbr.RuleID != null) { rulecnt++; var ridx = testData.ExpectedRuleDateList.FindIndex(rd => rd.Date == dbr.RecordDate.Date); Assert.NotEqual(-1, ridx); } }); Assert.Equal(testData.ExpectedRuleDateList.Count, rulecnt); } DataSetupUtility.ClearUserHabitData(context, nNewHabitID); context.SaveChanges(); await context.DisposeAsync(); }