Skip to content

Commit 40561f7

Browse files
committed
test: enumeration support
1 parent 9534dd0 commit 40561f7

File tree

3 files changed

+187
-1
lines changed

3 files changed

+187
-1
lines changed

tests/QueryableValues.SqlServer.Tests/Integration/DbContextFixture.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ private async Task Seed()
6666
StringUnicodeValue = "你好!",
6767
DateTimeValue = dateTimeOffset.DateTime,
6868
DateTimeOffsetValue = dateTimeOffset,
69-
GuidValue = Guid.Parse("df2c9bfe-9d83-4331-97ce-2876d5dc6576")
69+
GuidValue = Guid.Parse("df2c9bfe-9d83-4331-97ce-2876d5dc6576"),
70+
EnumValue = TestEnum.Value1000
7071
},
7172
new TestDataEntity
7273
{
@@ -85,6 +86,7 @@ private async Task Seed()
8586
DateTimeValue = DateTime.MaxValue,
8687
DateTimeOffsetValue = DateTimeOffset.MaxValue,
8788
GuidValue = Guid.Parse("f6379213-750f-42df-91b9-73756f28c4b6"),
89+
EnumValue = TestEnum.Value3,
8890
ChildEntity = new List<ChildEntity>
8991
{
9092
new ChildEntity()

tests/QueryableValues.SqlServer.Tests/Integration/MyDbContextBase.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ public class TestDataEntity
133133
public DateTime DateTimeValue { get; set; }
134134
public DateTimeOffset DateTimeOffsetValue { get; set; }
135135
public Guid GuidValue { get; set; }
136+
public TestEnum EnumValue { get; set; }
136137
public ICollection<ChildEntity> ChildEntity { get; set; } = default!;
137138
}
138139

@@ -141,5 +142,14 @@ public class ChildEntity
141142
public int Id { get; set; }
142143
public int TestDataEntityId { get; set; }
143144
}
145+
146+
public enum TestEnum
147+
{
148+
None = 0,
149+
Value1 = 1,
150+
Value2 = 2,
151+
Value3 = 3,
152+
Value1000 = 1000
153+
}
144154
}
145155
#endif

tests/QueryableValues.SqlServer.Tests/Integration/SimpleTypeTests.cs

Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,110 @@ public async Task MustMatchSequenceOfGuid()
362362
Assert.Equal(expected, actual);
363363
}
364364

365+
enum MyByteEnum : byte
366+
{
367+
None,
368+
A,
369+
B,
370+
C
371+
}
372+
373+
enum MyInt16Enum : short
374+
{
375+
None,
376+
A,
377+
B,
378+
C
379+
}
380+
381+
enum MyInt32Enum
382+
{
383+
None,
384+
A,
385+
B,
386+
C
387+
}
388+
389+
enum MyInt64Enum : long
390+
{
391+
None,
392+
A,
393+
B,
394+
C
395+
}
396+
397+
[Fact]
398+
public async Task MustMatchSequenceOfEnumByte()
399+
{
400+
var expected = new[]
401+
{
402+
MyByteEnum.None,
403+
MyByteEnum.A,
404+
MyByteEnum.A,
405+
MyByteEnum.B,
406+
MyByteEnum.C,
407+
MyByteEnum.A
408+
};
409+
410+
var actual = await _db.AsQueryableValues(expected).ToListAsync();
411+
412+
Assert.Equal(expected, actual);
413+
}
414+
415+
[Fact]
416+
public async Task MustMatchSequenceOfEnumInt16()
417+
{
418+
var expected = new[]
419+
{
420+
MyInt16Enum.None,
421+
MyInt16Enum.A,
422+
MyInt16Enum.A,
423+
MyInt16Enum.B,
424+
MyInt16Enum.C,
425+
MyInt16Enum.A
426+
};
427+
428+
var actual = await _db.AsQueryableValues(expected).ToListAsync();
429+
430+
Assert.Equal(expected, actual);
431+
}
432+
433+
[Fact]
434+
public async Task MustMatchSequenceOfEnumInt32()
435+
{
436+
var expected = new[]
437+
{
438+
MyInt32Enum.A,
439+
MyInt32Enum.A,
440+
MyInt32Enum.B,
441+
MyInt32Enum.C,
442+
MyInt32Enum.A,
443+
MyInt32Enum.None
444+
};
445+
446+
var actual = await _db.AsQueryableValues(expected).ToListAsync();
447+
448+
Assert.Equal(expected, actual);
449+
}
450+
451+
[Fact]
452+
public async Task MustMatchSequenceOfEnumInt64()
453+
{
454+
var expected = new[]
455+
{
456+
MyInt64Enum.A,
457+
MyInt64Enum.A,
458+
MyInt64Enum.B,
459+
MyInt64Enum.C,
460+
MyInt64Enum.A,
461+
MyInt64Enum.None
462+
};
463+
464+
var actual = await _db.AsQueryableValues(expected).ToListAsync();
465+
466+
Assert.Equal(expected, actual);
467+
}
468+
365469
[Fact]
366470
public async Task QueryEntityByte()
367471
{
@@ -662,6 +766,27 @@ select i.Id
662766
Assert.Equal(expected, actual);
663767
}
664768

769+
[Fact]
770+
public async Task QueryEntityEnum()
771+
{
772+
var values = new[] {
773+
TestEnum.None,
774+
TestEnum.Value1000
775+
};
776+
777+
var expected = new[] { 1, 2 };
778+
779+
var actual = await (
780+
from i in _db.TestData
781+
join v in _db.AsQueryableValues(values) on i.EnumValue equals v
782+
orderby i.Id
783+
select i.Id
784+
)
785+
.ToArrayAsync();
786+
787+
Assert.Equal(expected, actual);
788+
}
789+
665790

666791
[Fact]
667792
public async Task MustBeEmpty()
@@ -694,6 +819,29 @@ async Task AssertEmpty<T>()
694819
}
695820
}
696821

822+
[Fact]
823+
public async Task MustBeEmptyEnum()
824+
{
825+
var testCounter = 0;
826+
827+
await AssertEmpty<MyByteEnum>();
828+
await AssertEmpty<MyInt16Enum>();
829+
await AssertEmpty<MyInt32Enum>();
830+
await AssertEmpty<MyInt64Enum>();
831+
832+
// Coverage check.
833+
var expectedTestCount = 4;
834+
Assert.Equal(expectedTestCount, testCounter);
835+
836+
async Task AssertEmpty<T>()
837+
where T : struct, Enum
838+
{
839+
testCounter++;
840+
var actual = await _db.AsQueryableValues<T>(Array.Empty<T>()).ToListAsync();
841+
Assert.Empty(actual);
842+
}
843+
}
844+
697845
[Fact]
698846
public async Task MustMatchCount()
699847
{
@@ -729,6 +877,32 @@ async Task AssertCount<T>(Func<int, T> getValue)
729877
}
730878
}
731879

880+
[Fact]
881+
public async Task MustMatchCountEnum()
882+
{
883+
const int expectedItemCount = 2500;
884+
885+
var testCounter = 0;
886+
887+
await AssertCount(i => (MyByteEnum)(i % 4));
888+
await AssertCount(i => (MyInt16Enum)(i % 4));
889+
await AssertCount(i => (MyInt32Enum)(i % 4));
890+
await AssertCount(i => (MyInt64Enum)(i % 4));
891+
892+
// Coverage check.
893+
var expectedTestCount = 4;
894+
Assert.Equal(expectedTestCount, testCounter);
895+
896+
async Task AssertCount<T>(Func<int, T> getValue)
897+
where T : struct, Enum
898+
{
899+
testCounter++;
900+
var values = Enumerable.Range(0, expectedItemCount).Select(i => getValue(i));
901+
var actualItemCount = await _db.AsQueryableValues<T>(values).CountAsync();
902+
Assert.Equal(expectedItemCount, actualItemCount);
903+
}
904+
}
905+
732906
[Fact]
733907
public async Task JoinWithInclude()
734908
{

0 commit comments

Comments
 (0)