-
-
Notifications
You must be signed in to change notification settings - Fork 416
Expand file tree
/
Copy pathEscapesMySqlTestBase.cs
More file actions
152 lines (128 loc) · 5.6 KB
/
EscapesMySqlTestBase.cs
File metadata and controls
152 lines (128 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.TestModels.MusicStore;
using Microsoft.EntityFrameworkCore.TestUtilities;
using Pomelo.EntityFrameworkCore.MySql.FunctionalTests.TestUtilities;
using Pomelo.EntityFrameworkCore.MySql.Infrastructure;
using Pomelo.EntityFrameworkCore.MySql.Tests;
using Xunit;
namespace Pomelo.EntityFrameworkCore.MySql.FunctionalTests.Query
{
public abstract class EscapesMySqlTestBase<TFixture> : IClassFixture<TFixture>
where TFixture : EscapesMySqlTestBase<TFixture>.EscapesMySqlFixtureBase, new()
{
protected EscapesMySqlTestBase(TFixture fixture)
{
Fixture = fixture;
fixture.ListLoggerFactory.Clear();
}
[ConditionalFact]
public virtual async Task Input_query_escapes_parameter()
{
await ExecuteWithStrategyInTransactionAsync(
async context =>
{
context.Artists.Add(new Artist
{
Name = @"Back\slash's Garden Party",
});
await context.SaveChangesAsync();
},
async context =>
{
var artists = await context.Artists.Where(x => x.Name.EndsWith(" Garden Party")).ToListAsync();
Assert.Single(artists);
Assert.True(artists[0].Name == @"Back\slash's Garden Party");
});
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Where_query_escapes_literal(bool async)
{
using (var context = CreateContext())
{
var query = context.Artists.Where(c => c.Name == @"Back\slasher's");
var artists = async
? await query.ToListAsync()
: query.ToList();
Assert.Single(artists);
}
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Where_query_escapes_parameter(bool async)
{
using (var context = CreateContext())
{
var artistName = @"Back\slasher's";
var query = context.Artists.Where(c => c.Name == artistName);
var artists = async
? await query.ToListAsync()
: query.ToList();
Assert.Single(artists);
}
}
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual async Task Where_contains_query_escapes(bool async)
{
using (var context = CreateContext())
{
// Use List<string> so Contains binds to Enumerable.Contains; array can bind to
// MemoryExtensions.Contains(ReadOnlySpan) and break the expression interpreter.
var artistNames = new List<string>
{
@"Back\slasher's",
@"John's Chill Box"
};
var query = context.Artists.Where(a => artistNames.Contains(a.Name));
var artists = async
? await query.ToListAsync()
: query.ToList();
Assert.Equal(2, artists.Count);
}
}
public static IEnumerable<object[]> IsAsyncData = new[] { new object[] { false }, new object[] { true } };
protected TFixture Fixture { get; }
protected MusicStoreContext CreateContext() => Fixture.CreateContext();
protected void AssertSql(params string[] expected)
=> Fixture.TestSqlLoggerFactory.AssertBaseline(expected);
protected virtual Task ExecuteWithStrategyInTransactionAsync(
Func<MusicStoreContext, Task> testOperation,
Func<MusicStoreContext, Task> nestedTestOperation1 = null,
Func<MusicStoreContext, Task> nestedTestOperation2 = null)
=> TestHelpers.ExecuteWithStrategyInTransactionAsync(
CreateContext,
UseTransaction,
testOperation,
nestedTestOperation1,
nestedTestOperation2);
protected virtual void UseTransaction(DatabaseFacade facade, IDbContextTransaction transaction)
=> facade.UseTransaction(transaction.GetDbTransaction());
public abstract class EscapesMySqlFixtureBase : SharedStoreFixtureBase<MusicStoreContext>
{
protected override string StoreName { get; } = "EscapesMusicStore";
protected override bool UsePooling => false;
public TestSqlLoggerFactory TestSqlLoggerFactory => (TestSqlLoggerFactory)ListLoggerFactory;
protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext context)
{
base.OnModelCreating(modelBuilder, context);
MySqlTestHelpers.Instance.EnsureSufficientKeySpace(modelBuilder.Model, TestStore);
}
protected override async Task SeedAsync(MusicStoreContext context)
{
context.Artists.AddRange(
new Artist { ArtistId = 1, Name = @"Back\slasher's" },
new Artist { ArtistId = 2, Name = @"Ice Cups" },
new Artist { ArtistId = 3, Name = @"John's Chill Box" }
);
await context.SaveChangesAsync();
}
}
}
}