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 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(); }
public async Task CalculatePoints_Weekly_NumberOfTimes(UserHabitRecordsControllerTestData_WeekNoOfTimes 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_1"; habit.Category = HabitCategory.Positive; habit.Comment = habit.Name; habit.Frequency = HabitFrequency.Weekly; habit.CompleteCategory = HabitCompleteCategory.NumberOfTimes; habit.CompleteCondition = testData.CompleteCondition; habit.StartDate = (int)testData.Dow; context.UserHabits.Add(habit); context.SaveChanges(); Int32 nNewHabitID = habit.ID; UserHabitRule rule1 = new UserHabitRule(); rule1.HabitID = habit.ID; rule1.RuleID = 1; rule1.ContinuousRecordFrom = 1; rule1.ContinuousRecordTo = 2; rule1.Point = 1; context.UserHabitRules.Add(rule1); UserHabitRule rule2 = new UserHabitRule(); rule2.HabitID = habit.ID; rule2.RuleID = 2; rule2.ContinuousRecordFrom = 2; rule1.ContinuousRecordTo = 3; rule2.Point = 2; context.UserHabitRules.Add(rule2); UserHabitRule rule3 = new UserHabitRule(); rule3.HabitID = habit.ID; rule3.RuleID = 3; rule3.ContinuousRecordFrom = 3; rule3.Point = 4; context.UserHabitRules.Add(rule3); context.SaveChanges(); // Add user record. //Boolean lastRst = false; 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); //lastRst = rstrecord.Entity.RuleID.HasValue; } } // Check on DB directly var dbrecords = (from dbrecord in context.UserHabitRecords where dbrecord.HabitID == habit.ID select dbrecord).ToList(); Assert.Equal(testData.RecordCount, dbrecords.Count); // Ensure rule is assigned correctly if (testData.TargetRuleDateList.Count > 0) { var rulecnt = 0; dbrecords.ForEach(dbr => { if (dbr.RuleID != null) { rulecnt++; var ridx = testData.TargetRuleDateList.FindIndex(rd => rd.Date == dbr.RecordDate.Date); Assert.NotEqual(-1, ridx); } }); Assert.Equal(testData.TargetRuleDateList.Count, rulecnt); } DataSetupUtility.ClearUserHabitData(context, nNewHabitID); context.SaveChanges(); await context.DisposeAsync(); }