Skip to content

Commit b76214a

Browse files
authored
chore: resolve obsolete testcase compiler warnings (#45)
Refactor test cases to resolve obsolete compiler warnings. This includes updating test data, modifying test methods, and ensuring compatibility with the latest codebase changes. closes #43
1 parent 83db163 commit b76214a

15 files changed

Lines changed: 541 additions & 356 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ publish/
55
PeriodTracker/commit_hash.txt
66
PeriodTracker.keystore
77
**/*.user
8+
**/*.csproj.lscache
89

PeriodTrackerTests/GlobalUsings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1+
global using System;
2+
global using System.Collections;
3+
global using System.Collections.Generic;
14
global using Xunit;

PeriodTrackerTests/SeedData.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33

44
namespace PeriodTrackerTests;
55

6-
public class SeedData
6+
public interface ISeedDataProvider
77
{
8-
public required List<Cycle> Cycles {get; init;} = new();
8+
SeedData GetSeedData();
9+
}
10+
11+
public class SeedData: ISeedDataProvider
12+
{
13+
public List<AppState> AppStates {get; init;} = new();
14+
public List<Cycle> Cycles {get; init;} = new();
15+
16+
public SeedData GetSeedData() => this;
917
}
Lines changed: 119 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,143 @@
1+
using Microsoft.EntityFrameworkCore;
12
using PeriodTracker;
23

34
namespace PeriodTrackerTests;
45

56
public partial class AppDbContextTests
67
{
78

8-
[Theory, MemberData(nameof(AddCycleTestsData))]
9-
public async Task AddCycleTests(TestCase test){
9+
[Theory, ClassData(typeof(AddCycleTestsData))]
10+
public async Task AddCycleTests(TestCase<AddCycleTestsData.TestParameters> test){
1011
var testTempDir = _tempDir.CreateTestCaseDirectory(test.Name);
1112

1213
using var db = new AppDbContext(CreateDbContextOptions(testTempDir), true);
1314

14-
var inp = ((Cycle[]?)test.Inputs["cycles"])!;
15+
var inp = test.Parameters.Inputs.Cycles;
1516

16-
var actInsertedResults = new bool[inp.Length];
17-
for(var i =0; i < inp.Length; i++)
17+
var actInsertedResults = new bool[inp.Count];
18+
for(var i =0; i < inp.Count; i++)
1819
actInsertedResults[i] = await db.AddCycle(inp[i]);
1920

20-
var actInserted = (from c in db.Cycles select c).ToArray();
21+
var actInserted = await (from c in db.Cycles select c).ToListAsync();
2122

22-
var expInserted = (Cycle[]?)test.Expected["cycles"];
23-
var expInsertedResults = (bool[]?)test.Expected["insert results"];
23+
var expInserted = test.Parameters.Expected.Cycles;
24+
var expInsertedResults = test.Parameters.Expected.InsertResults;
2425

2526
Assert.Equal(expInsertedResults, actInsertedResults);
2627
AssertCycles(expInserted, actInserted);
2728
}
2829

29-
public static IEnumerable<object[]> AddCycleTestsData =>
30-
new []{
31-
new TestCase("Add single")
32-
.WithInput("cycles", new []{
33-
new Cycle{
34-
RecordedDate = DateTime.Today,
35-
StartDate = DateTime.Parse("2023-11-01"),
36-
}})
37-
.WithExpected("cycles", new []{
38-
new Cycle{
39-
RecordedDate = DateTime.Today,
40-
StartDate = DateTime.Parse("2023-11-01"),
41-
}})
42-
.WithExpected("insert results", new[]{true}),
43-
44-
new TestCase("Add many")
45-
.WithInput("cycles", new []{
46-
new Cycle{
47-
RecordedDate = DateTime.Today,
48-
StartDate = DateTime.Parse("2023-11-01"),
49-
},
50-
new Cycle{
51-
RecordedDate = DateTime.Today,
52-
StartDate = DateTime.Parse("2023-12-01")
53-
}
54-
})
55-
.WithExpected("cycles", new []{
56-
new Cycle{
57-
RecordedDate = DateTime.Today,
58-
StartDate = DateTime.Parse("2023-11-01"),
59-
},
60-
new Cycle{
61-
RecordedDate = DateTime.Today,
62-
StartDate = DateTime.Parse("2023-12-01")
63-
}
64-
})
65-
.WithExpected("insert results", new[]{true, true}),
66-
67-
new TestCase("Add many - inverted")
68-
.WithInput("cycles", new []{
69-
new Cycle{
70-
RecordedDate = DateTime.Today,
71-
StartDate = DateTime.Parse("2023-12-01")
72-
},
73-
new Cycle{
74-
RecordedDate = DateTime.Today,
75-
StartDate = DateTime.Parse("2023-11-01"),
76-
}
77-
})
78-
.WithExpected("cycles", new []{
79-
new Cycle{
80-
RecordedDate = DateTime.Today,
81-
StartDate = DateTime.Parse("2023-11-01"),
82-
},
83-
new Cycle{
84-
RecordedDate = DateTime.Today,
85-
StartDate = DateTime.Parse("2023-12-01")
86-
}
87-
})
88-
.WithExpected("insert results", new[]{true, true}),
89-
90-
new TestCase("Add many with same date")
91-
.WithInput("cycles", new []{
92-
new Cycle{
93-
RecordedDate = DateTime.Today,
94-
StartDate = DateTime.Parse("2023-11-01"),
95-
},
96-
new Cycle{
97-
RecordedDate = DateTime.Today,
98-
StartDate = DateTime.Parse("2023-11-01")
99-
}
100-
})
101-
.WithExpected("cycles", new []{
102-
new Cycle{
103-
RecordedDate = DateTime.Today,
104-
StartDate = DateTime.Parse("2023-11-01"),
105-
},
106-
})
107-
.WithExpected("insert results", new[]{true, false}),
30+
public class AddCycleTestsData: IEnumerable<object[]>
31+
{
32+
public record TestParameters(Inputs Inputs, ExpectedResults Expected);
33+
public static IEnumerable<object[]> TestCases()
34+
{
35+
yield return new[] {
36+
new TestCase<TestParameters>("Add single",
37+
new TestParameters(
38+
new Inputs{
39+
Cycles = new List<Cycle>{
40+
new (){
41+
RecordedDate = DateTime.Today,
42+
StartDate = DateTime.Parse("2023-11-01"),
43+
}}},
44+
new ExpectedResults{
45+
Cycles = new List<Cycle>{
46+
new (){
47+
RecordedDate = DateTime.Today,
48+
StartDate = DateTime.Parse("2023-11-01"),
49+
}},
50+
InsertResults = new List<bool>{true}
51+
}))};
10852

53+
yield return new[] {
54+
new TestCase<TestParameters>("Add many",
55+
new TestParameters(
56+
new Inputs{
57+
Cycles = new List<Cycle>{
58+
new (){
59+
RecordedDate = DateTime.Today,
60+
StartDate = DateTime.Parse("2023-11-01"),
61+
},
62+
new (){
63+
RecordedDate = DateTime.Today,
64+
StartDate = DateTime.Parse("2023-12-01"),
65+
}}},
66+
new ExpectedResults{
67+
Cycles = new List<Cycle>{
68+
new (){
69+
RecordedDate = DateTime.Today,
70+
StartDate = DateTime.Parse("2023-11-01"),
71+
},
72+
new (){
73+
RecordedDate = DateTime.Today,
74+
StartDate = DateTime.Parse("2023-12-01"),
75+
}},
76+
InsertResults = new List<bool>{true, true}
77+
}))};
78+
79+
yield return new[] {
80+
new TestCase<TestParameters>("Add many - inverted",
81+
new TestParameters(
82+
new Inputs{
83+
Cycles = new List<Cycle>{
84+
new (){
85+
RecordedDate = DateTime.Today,
86+
StartDate = DateTime.Parse("2023-12-01"),
87+
},
88+
new (){
89+
RecordedDate = DateTime.Today,
90+
StartDate = DateTime.Parse("2023-11-01"),
91+
}}},
92+
new ExpectedResults{
93+
Cycles = new List<Cycle>{
94+
new (){
95+
RecordedDate = DateTime.Today,
96+
StartDate = DateTime.Parse("2023-11-01"),
97+
},
98+
new (){
99+
RecordedDate = DateTime.Today,
100+
StartDate = DateTime.Parse("2023-12-01"),
101+
}},
102+
InsertResults = new List<bool>{true, true}
103+
}))};
104+
105+
yield return new[] {
106+
new TestCase<TestParameters>("Add many with same date",
107+
new TestParameters(
108+
new Inputs{
109+
Cycles = new List<Cycle>{
110+
new (){
111+
RecordedDate = DateTime.Today,
112+
StartDate = DateTime.Parse("2023-11-01"),
113+
},
114+
new (){
115+
RecordedDate = DateTime.Today,
116+
StartDate = DateTime.Parse("2023-11-01"),
117+
}}},
118+
new ExpectedResults{
119+
Cycles = new List<Cycle>{
120+
new (){
121+
RecordedDate = DateTime.Today,
122+
StartDate = DateTime.Parse("2023-11-01"),
123+
}},
124+
InsertResults = new List<bool>{true, false}
125+
}))};
126+
}
127+
128+
public IEnumerator<object[]> GetEnumerator() => TestCases().GetEnumerator();
129+
130+
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
131+
132+
public class ExpectedResults
133+
{
134+
public List<bool> InsertResults {get; init;} = new();
135+
public List<Cycle> Cycles {get; init;} = new();
109136
}
110-
.Select(tc => new object[] {tc});
111137

138+
public class Inputs
139+
{
140+
public List<Cycle> Cycles {get; init;} = new();
141+
}
142+
}
112143
}

PeriodTrackerTests/Tests/AppDbContextTests/AllAppStatePropertiesExist.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public async Task AllAppStatePropertiesExist(){
1919
object value = prop switch {
2020
AppStateProperty.NotifyUpdateAvailableInterval => await db.GetAppStateValue(prop, Convert.ToInt32),
2121
AppStateProperty.NotifyUpdateAvailableNextDate => await db.GetAppStateValue(prop, Convert.ToDateTime),
22-
_ => throw new NotImplementedException($"App state property '{prop}' not handled.")
22+
_ => throw new NotImplementedException($"App state property '{prop}' not expected.")
2323
};
2424
}
2525

0 commit comments

Comments
 (0)