Skip to content

Commit

Permalink
[EFCore] Resolve ambiguities
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivan Ristović committed Mar 21, 2021
1 parent a227aca commit f5c4c26
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ await TestDbProvider.AlterAndVerifyAsync(
void AssertFiltersRemoved(TheGodfatherDbContext db, ulong gid, params int[]? ids)
{
if (ids?.Any() ?? false) {
Assert.That(db.Filters.Where(f => f.GuildIdDb == (long)gid).Select(f => f.Id), Has.No.AnyOf(ids));
Assert.That(db.Filters.AsQueryable().Where(f => f.GuildIdDb == (long)gid).Select(f => f.Id), Has.No.AnyOf(ids));
Assert.That(this.Service.GetGuildFilters(gid).Select(f => f.Id), Has.No.AnyOf(ids));
} else {
Assert.Fail("No IDs provided to assert function.");
Expand All @@ -525,6 +525,7 @@ void AssertFilterRegexesRemoved(TheGodfatherDbContext db, ulong gid, params stri
{
if (regexStrings?.Any() ?? false) {
Assert.That(db.Filters
.AsQueryable()
.Where(f => f.GuildIdDb == (long)gid)
.AsEnumerable()
.Any(f => regexStrings.Any(s => string.Compare(s, f.RegexString, true) == 0)),
Expand Down Expand Up @@ -578,6 +579,7 @@ private void AssertSingleAndTest(TheGodfatherDbContext db, int index, string reg
Assert.IsNotNull(filter);

Filter dbf = db.Filters
.AsQueryable()
.Where(f => f.GuildIdDb == (long)MockData.Ids[index])
.AsEnumerable()
.Single(f => string.Compare(f.RegexString, regex, true) == 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ await TestDbProvider.AlterAndVerifyAsync(
void AssertForbiddenNamesRemoved(TheGodfatherDbContext db, ulong gid, params int[]? ids)
{
if (ids?.Any() ?? false) {
Assert.That(db.ForbiddenNames.Where(f => f.GuildIdDb == (long)gid).Select(f => f.Id), Has.No.AnyOf(ids));
Assert.That(db.ForbiddenNames.AsQueryable().Where(f => f.GuildIdDb == (long)gid).Select(f => f.Id), Has.No.AnyOf(ids));
Assert.That(this.Service.GetGuildForbiddenNames(gid).Select(f => f.Id), Has.No.AnyOf(ids));
} else {
Assert.Fail("No IDs provided to assert function.");
Expand All @@ -505,6 +505,7 @@ void AssertForbiddenNameRegexesRemoved(TheGodfatherDbContext db, ulong gid, para
{
if (regexStrings?.Any() ?? false) {
Assert.That(db.ForbiddenNames
.AsQueryable()
.Where(f => f.GuildIdDb == (long)gid)
.AsEnumerable()
.Any(f => regexStrings.Any(s => string.Compare(s, f.RegexString, true) == 0)),
Expand Down Expand Up @@ -558,6 +559,7 @@ private void AssertSingleAndTest(TheGodfatherDbContext db, int index, string reg
Assert.IsNotNull(filter);

ForbiddenName dbf = db.ForbiddenNames
.AsQueryable()
.Where(f => f.GuildIdDb == (long)MockData.Ids[index])
.AsEnumerable()
.Single(f => string.Compare(f.RegexString, regex, true) == 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,10 @@ private async Task AssertBlockedAsync(TheGodfatherDbContext db, ulong[] bcExpect
Assert.That(busrs.Select(u => u.Id), Is.EquivalentTo(buExpected));
Assert.That(bchns.Select(c => c.Reason), Is.EquivalentTo(bcReasons));
Assert.That(busrs.Select(u => u.Reason), Is.EquivalentTo(buReasons));
Assert.That(db.BlockedChannels.Select(c => c.Id), Is.EquivalentTo(bcExpected));
Assert.That(db.BlockedUsers.Select(c => c.Id), Is.EquivalentTo(buExpected));
Assert.That(db.BlockedChannels.Select(c => c.Reason), Is.EquivalentTo(bcReasons));
Assert.That(db.BlockedUsers.Select(c => c.Reason), Is.EquivalentTo(buReasons));
Assert.That(db.BlockedChannels.AsQueryable().Select(c => c.Id), Is.EquivalentTo(bcExpected));
Assert.That(db.BlockedUsers.AsQueryable().Select(c => c.Id), Is.EquivalentTo(buExpected));
Assert.That(db.BlockedChannels.AsQueryable().Select(c => c.Reason), Is.EquivalentTo(bcReasons));
Assert.That(db.BlockedUsers.AsQueryable().Select(c => c.Reason), Is.EquivalentTo(buReasons));
}

private void AddMockData(TheGodfatherDbContext db)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ await this.Service.AddEmojiReactionAsync(MockData.Ids[0], Emojis.Information.Get
Assert.That(ers, Has.Exactly(this.erCount[0] + 1).Items);
Assert.That(ers.Select(er => er.Id), Is.Unique);
IEnumerable<EmojiReaction> x = db.EmojiReactions
.AsQueryable()
.Where(er => er.GuildIdDb == (long)MockData.Ids[0])
.Include(er => er.DbTriggers)
.AsEnumerable();
Expand Down Expand Up @@ -155,6 +156,7 @@ await this.Service.AddEmojiReactionAsync(MockData.Ids[1], Emojis.Information.Get

Assert.That(
db.EmojiReactions
.AsQueryable()
.Where(er => er.GuildIdDb == (long)MockData.Ids[0])
.AsEnumerable()
.Single(er => er.Response == Emojis.Information.GetDiscordName()
Expand All @@ -163,6 +165,7 @@ await this.Service.AddEmojiReactionAsync(MockData.Ids[1], Emojis.Information.Get
);
Assert.That(
db.EmojiReactions
.AsQueryable()
.Where(er => er.GuildIdDb == (long)MockData.Ids[1])
.AsEnumerable()
.Single(er => er.Response == Emojis.Information.GetDiscordName()
Expand Down Expand Up @@ -204,6 +207,7 @@ await this.Service.AddEmojiReactionAsync(MockData.Ids[0], Emojis.Information.Get

Assert.That(
db.EmojiReactions
.AsQueryable()
.Where(er => er.GuildIdDb == (long)MockData.Ids[0])
.AsEnumerable()
.Single(er => er.Response == Emojis.Information.GetDiscordName()
Expand Down Expand Up @@ -239,6 +243,7 @@ await this.Service.AddEmojiReactionAsync(MockData.Ids[0], Emojis.Information.Get

Assert.That(
db.EmojiReactions
.AsQueryable()
.Where(er => er.GuildIdDb == (long)MockData.Ids[0])
.AsEnumerable()
.Single(er => er.Response == Emojis.Information.GetDiscordName()
Expand Down Expand Up @@ -282,6 +287,7 @@ await this.Service.AddEmojiReactionAsync(MockData.Ids[0], Emojis.Information.Get

Assert.That(
db.EmojiReactions
.AsQueryable()
.Where(er => er.GuildIdDb == (long)MockData.Ids[0])
.AsEnumerable()
.Single(er => er.Response == Emojis.Information.GetDiscordName()
Expand Down Expand Up @@ -325,6 +331,7 @@ await this.Service.AddEmojiReactionAsync(

Assert.That(
db.EmojiReactions
.AsQueryable()
.Where(er => er.GuildIdDb == (long)MockData.Ids[0])
.AsEnumerable()
.Single(er => er.Response == Emojis.Information.GetDiscordName()
Expand Down Expand Up @@ -368,6 +375,7 @@ await this.Service.AddEmojiReactionAsync(MockData.Ids[0], Emojis.Chicken.GetDisc

Assert.That(
db.EmojiReactions
.AsQueryable()
.Where(er => er.GuildIdDb == (long)MockData.Ids[0])
.AsEnumerable()
.Single(er => er.Response == Emojis.Chicken.GetDiscordName()
Expand Down Expand Up @@ -419,6 +427,7 @@ await TestDbProvider.SetupAlterAndVerifyAsync(
Assert.That(ers.Any(er => er.Response == Emojis.Information.GetDiscordName()), Is.False);
Assert.That(
db.EmojiReactions
.AsQueryable()
.Where(er => er.GuildIdDb == (long)MockData.Ids[0])
.AsEnumerable()
.Any(er => er.Response == Emojis.Information.GetDiscordName()),
Expand All @@ -445,6 +454,7 @@ await TestDbProvider.SetupAlterAndVerifyAsync(
Assert.That(this.Service.GetGuildEmojiReactions(MockData.Ids[0]).Any(er => er.Response == Emojis.Chicken.GetDiscordName()), Is.False);
Assert.That(
db.EmojiReactions
.AsQueryable()
.Where(er => er.GuildIdDb == (long)MockData.Ids[0])
.AsEnumerable()
.Any(er => er.Response == Emojis.Chicken.GetDiscordName()),
Expand All @@ -453,6 +463,7 @@ await TestDbProvider.SetupAlterAndVerifyAsync(
Assert.That(this.Service.GetGuildEmojiReactions(MockData.Ids[2]).Single(er => er.Response == Emojis.Chicken.GetDiscordName()), Is.Not.Null);
Assert.That(
db.EmojiReactions
.AsQueryable()
.Where(er => er.GuildIdDb == (long)MockData.Ids[2])
.AsEnumerable()
.Single(er => er.Response == Emojis.Chicken.GetDiscordName()),
Expand Down Expand Up @@ -497,6 +508,7 @@ await TestDbProvider.SetupAlterAndVerifyAsync(
verify: db => {
Assert.That(db.EmojiReactions, Has.Exactly(this.erCount.Sum(kvp => kvp.Value)).Items);
EmojiReaction dber = db.EmojiReactions
.AsQueryable()
.Where(er => er.GuildIdDb == (long)MockData.Ids[0])
.AsEnumerable()
.Single(er => er.Response == Emojis.Cloud.GetDiscordName());
Expand Down Expand Up @@ -592,7 +604,7 @@ await TestDbProvider.SetupAlterAndVerifyAsync(
Assert.That(db.EmojiReactions, Has.Exactly(this.erCount.Sum(kvp => kvp.Value) - 3).Items);
Assert.That(this.Service.GetGuildEmojiReactions(MockData.Ids[0]), Has.Exactly(this.erCount[0]).Items);
Assert.That(this.Service.GetGuildEmojiReactions(MockData.Ids[1]), Is.Empty);
Assert.That(db.EmojiReactions.Select(tr => tr.GuildId), Does.Not.Contain(MockData.Ids[1]));
Assert.That(db.EmojiReactions.AsQueryable().Select(tr => tr.GuildId), Does.Not.Contain(MockData.Ids[1]));
return Task.CompletedTask;
}
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ void AssertTextReactionExists(TheGodfatherDbContext db, ulong gid, string respon
if (triggers?.Any() ?? false) {
Assert.That(
db.TextReactions
.AsQueryable()
.Where(tr => tr.GuildIdDb == (long)gid)
.AsEnumerable()
.SingleOrDefault(tr => tr.Response == response && CheckTriggers(triggers, tr.DbTriggers.Select(t => t.Trigger.ToLower()))),
Expand Down Expand Up @@ -326,7 +327,7 @@ await TestDbProvider.SetupAlterAndVerifyAsync(
Assert.That(db.TextReactions, Has.Exactly(this.trCount.Sum(kvp => kvp.Value)).Items);
for (int i = 0; i < MockData.Ids.Count; i++) {
Assert.That(this.Service.GetGuildTextReactions(MockData.Ids[i]), Has.Exactly(this.trCount[i]).Items);
Assert.That(db.TextReactions.Where(tr => tr.GuildIdDb == (long)MockData.Ids[i]), Has.Exactly(this.trCount[i]).Items);
Assert.That(db.TextReactions.AsQueryable().Where(tr => tr.GuildIdDb == (long)MockData.Ids[i]), Has.Exactly(this.trCount[i]).Items);
}
return Task.CompletedTask;
}
Expand Down Expand Up @@ -357,7 +358,7 @@ await TestDbProvider.SetupAlterAndVerifyAsync(
void AssertReactionsRemoved(TheGodfatherDbContext db, ulong gid, params int[] ids)
{
if (ids?.Any() ?? false) {
Assert.That(db.TextReactions.Where(tr => tr.GuildIdDb == (long)gid).Select(tr => tr.Id), Has.No.AnyOf(ids));
Assert.That(db.TextReactions.AsQueryable().Where(tr => tr.GuildIdDb == (long)gid).Select(tr => tr.Id), Has.No.AnyOf(ids));
Assert.That(this.Service.GetGuildTextReactions(gid).Select(f => f.Id), Has.No.AnyOf(ids));
} else {
Assert.Fail("No IDs provided to assert function.");
Expand Down Expand Up @@ -386,6 +387,7 @@ await TestDbProvider.SetupAlterAndVerifyAsync(
verify: db => {
Assert.That(db.TextReactions, Has.Exactly(this.trCount.Sum(kvp => kvp.Value)).Items);
TextReaction dber = db.TextReactions
.AsQueryable()
.Where(tr => tr.GuildIdDb == (long)MockData.Ids[0])
.Single(tr => tr.Response == "response5");
Assert.That(dber.DbTriggers.Single().Trigger, Is.EqualTo("kill"));
Expand All @@ -405,6 +407,7 @@ await TestDbProvider.SetupAlterAndVerifyAsync(
IReadOnlyCollection<TextReaction> trs = this.Service.GetGuildTextReactions(MockData.Ids[0]);

TextReaction dbtr = db.TextReactions
.AsQueryable()
.Where(tr => tr.GuildIdDb == (long)MockData.Ids[0])
.Single(tr => tr.Response == "response1");

Expand Down
2 changes: 1 addition & 1 deletion TheGodfather.Tests/Services/DbAbstractionServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ private void AddMockData(TheGodfatherDbContext db)
}

private IEnumerable<ulong> GetGuildRoles(TheGodfatherDbContext db, ulong gid)
=> db.AutoRoles.Where(ar => ar.GuildIdDb == (long)gid).AsEnumerable().Select(ar => ar.RoleId);
=> db.AutoRoles.AsQueryable().Where(ar => ar.GuildIdDb == (long)gid).AsEnumerable().Select(ar => ar.RoleId);
}

public class ConcreteService2 : DbAbstractionServiceBase<PrivilegedUser, ulong>
Expand Down

0 comments on commit f5c4c26

Please sign in to comment.