public void Deserialize(IXunitSerializationInfo info) { String val = info.GetValue <String>("Value"); UserHabitRecordsControllerTestData_WeekNoOfTimes other = JsonSerializer.Deserialize <UserHabitRecordsControllerTestData_WeekNoOfTimes>(val); // CaseID = other.CaseID; Dow = other.Dow; CompleteCondition = other.CompleteCondition; RecordCount = other.RecordCount; if (other.RecordDatesList.Count > 0) { RecordDatesList.AddRange(other.RecordDatesList); } if (other.TargetRuleDateList.Count > 0) { TargetRuleDateList.AddRange(other.TargetRuleDateList); } }
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(); }