Skip to content

Commit 02a43fe

Browse files
committed
better alias
1 parent d8ca8a2 commit 02a43fe

17 files changed

Lines changed: 67 additions & 270 deletions

readme.md

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ builder.UseSqlServer(connection);
8787
builder.EnableRecording();
8888
var data = new SampleDbContext(builder.Options);
8989
```
90-
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L384-L391' title='Snippet source file'>snippet source</a> | <a href='#snippet-EnableRecording' title='Start of snippet'>anchor</a></sup>
90+
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L340-L347' title='Snippet source file'>snippet source</a> | <a href='#snippet-EnableRecording' title='Start of snippet'>anchor</a></sup>
9191
<!-- endSnippet -->
9292

9393
`EnableRecording` should only be called in the test context.
@@ -116,7 +116,7 @@ await data
116116

117117
await Verify();
118118
```
119-
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L483-L501' title='Snippet source file'>snippet source</a> | <a href='#snippet-Recording' title='Start of snippet'>anchor</a></sup>
119+
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L439-L457' title='Snippet source file'>snippet source</a> | <a href='#snippet-Recording' title='Start of snippet'>anchor</a></sup>
120120
<!-- endSnippet -->
121121

122122
Will result in the following verified file:
@@ -168,7 +168,7 @@ await Verify(
168168
entries
169169
});
170170
```
171-
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L674-L699' title='Snippet source file'>snippet source</a> | <a href='#snippet-RecordingSpecific' title='Start of snippet'>anchor</a></sup>
171+
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L630-L655' title='Snippet source file'>snippet source</a> | <a href='#snippet-RecordingSpecific' title='Start of snippet'>anchor</a></sup>
172172
<!-- endSnippet -->
173173

174174

@@ -200,7 +200,7 @@ await data2
200200

201201
await Verify();
202202
```
203-
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L451-L474' title='Snippet source file'>snippet source</a> | <a href='#snippet-MultiDbContexts' title='Start of snippet'>anchor</a></sup>
203+
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L407-L430' title='Snippet source file'>snippet source</a> | <a href='#snippet-MultiDbContexts' title='Start of snippet'>anchor</a></sup>
204204
<!-- endSnippet -->
205205

206206
<!-- snippet: CoreTests.MultiDbContexts.verified.txt -->
@@ -265,7 +265,7 @@ await data
265265

266266
await Verify();
267267
```
268-
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L545-L568' title='Snippet source file'>snippet source</a> | <a href='#snippet-RecordingDisableForInstance' title='Start of snippet'>anchor</a></sup>
268+
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L501-L524' title='Snippet source file'>snippet source</a> | <a href='#snippet-RecordingDisableForInstance' title='Start of snippet'>anchor</a></sup>
269269
<!-- endSnippet -->
270270

271271
<!-- snippet: CoreTests.RecordingDisabledTest.verified.txt -->
@@ -313,7 +313,7 @@ public async Task Added()
313313
await Verify(data.ChangeTracker);
314314
}
315315
```
316-
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L87-L103' title='Snippet source file'>snippet source</a> | <a href='#snippet-Added' title='Start of snippet'>anchor</a></sup>
316+
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L43-L59' title='Snippet source file'>snippet source</a> | <a href='#snippet-Added' title='Start of snippet'>anchor</a></sup>
317317
<!-- endSnippet -->
318318

319319
Will result in the following verified file:
@@ -358,7 +358,7 @@ public async Task Deleted()
358358
await Verify(data.ChangeTracker);
359359
}
360360
```
361-
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L105-L124' title='Snippet source file'>snippet source</a> | <a href='#snippet-Deleted' title='Start of snippet'>anchor</a></sup>
361+
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L61-L80' title='Snippet source file'>snippet source</a> | <a href='#snippet-Deleted' title='Start of snippet'>anchor</a></sup>
362362
<!-- endSnippet -->
363363

364364
Will result in the following verified file:
@@ -403,7 +403,7 @@ public async Task Modified()
403403
await Verify(data.ChangeTracker);
404404
}
405405
```
406-
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L126-L146' title='Snippet source file'>snippet source</a> | <a href='#snippet-Modified' title='Start of snippet'>anchor</a></sup>
406+
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L82-L102' title='Snippet source file'>snippet source</a> | <a href='#snippet-Modified' title='Start of snippet'>anchor</a></sup>
407407
<!-- endSnippet -->
408408

409409
Will result in the following verified file:
@@ -438,7 +438,7 @@ var queryable = data.Companies
438438
.Where(_ => _.Name == "company name");
439439
await Verify(queryable);
440440
```
441-
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L341-L347' title='Snippet source file'>snippet source</a> | <a href='#snippet-Queryable' title='Start of snippet'>anchor</a></sup>
441+
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L297-L303' title='Snippet source file'>snippet source</a> | <a href='#snippet-Queryable' title='Start of snippet'>anchor</a></sup>
442442
<!-- endSnippet -->
443443

444444
Will result in the following verified files:
@@ -505,7 +505,7 @@ await Verify(data.AllData())
505505
serializer =>
506506
serializer.TypeNameHandling = TypeNameHandling.Objects);
507507
```
508-
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L320-L327' title='Snippet source file'>snippet source</a> | <a href='#snippet-AllData' title='Start of snippet'>anchor</a></sup>
508+
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L276-L283' title='Snippet source file'>snippet source</a> | <a href='#snippet-AllData' title='Start of snippet'>anchor</a></sup>
509509
<!-- endSnippet -->
510510

511511
Will result in the following verified file with all data in the database:
@@ -588,7 +588,7 @@ public async Task IgnoreNavigationProperties()
588588
.IgnoreNavigationProperties();
589589
}
590590
```
591-
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L148-L170' title='Snippet source file'>snippet source</a> | <a href='#snippet-IgnoreNavigationProperties' title='Start of snippet'>anchor</a></sup>
591+
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L104-L126' title='Snippet source file'>snippet source</a> | <a href='#snippet-IgnoreNavigationProperties' title='Start of snippet'>anchor</a></sup>
592592
<!-- endSnippet -->
593593

594594

@@ -601,7 +601,7 @@ var options = DbContextOptions();
601601
using var data = new SampleDbContext(options);
602602
VerifyEntityFramework.IgnoreNavigationProperties();
603603
```
604-
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L198-L204' title='Snippet source file'>snippet source</a> | <a href='#snippet-IgnoreNavigationPropertiesGlobal' title='Start of snippet'>anchor</a></sup>
604+
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L154-L160' title='Snippet source file'>snippet source</a> | <a href='#snippet-IgnoreNavigationPropertiesGlobal' title='Start of snippet'>anchor</a></sup>
605605
<!-- endSnippet -->
606606

607607

@@ -622,7 +622,7 @@ protected override void ConfigureWebHost(IWebHostBuilder webBuilder)
622622
_ => dataBuilder.Options));
623623
}
624624
```
625-
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L628-L640' title='Snippet source file'>snippet source</a> | <a href='#snippet-EnableRecordingWithIdentifier' title='Start of snippet'>anchor</a></sup>
625+
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L584-L596' title='Snippet source file'>snippet source</a> | <a href='#snippet-EnableRecordingWithIdentifier' title='Start of snippet'>anchor</a></sup>
626626
<!-- endSnippet -->
627627

628628
Then use the same identifier for recording:
@@ -638,7 +638,7 @@ var companies = await httpClient.GetFromJsonAsync<Company[]>("/companies");
638638

639639
var entries = Recording.Stop(testName);
640640
```
641-
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L601-L611' title='Snippet source file'>snippet source</a> | <a href='#snippet-RecordWithIdentifier' title='Start of snippet'>anchor</a></sup>
641+
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L557-L567' title='Snippet source file'>snippet source</a> | <a href='#snippet-RecordWithIdentifier' title='Start of snippet'>anchor</a></sup>
642642
<!-- endSnippet -->
643643

644644
The results will not be automatically included in verified file so it will have to be verified manually:
@@ -653,7 +653,7 @@ await Verify(
653653
sql = entries
654654
});
655655
```
656-
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L613-L622' title='Snippet source file'>snippet source</a> | <a href='#snippet-VerifyRecordedCommandsWithIdentifier' title='Start of snippet'>anchor</a></sup>
656+
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L569-L578' title='Snippet source file'>snippet source</a> | <a href='#snippet-VerifyRecordedCommandsWithIdentifier' title='Start of snippet'>anchor</a></sup>
657657
<!-- endSnippet -->
658658

659659

@@ -680,7 +680,7 @@ var settings = new VerifySettings();
680680
settings.ScrubInlineEfDateTimes();
681681
await Verify(target, settings);
682682
```
683-
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L34-L40' title='Snippet source file'>snippet source</a> | <a href='#snippet-ScrubInlineEfDateTimesInstance' title='Start of snippet'>anchor</a></sup>
683+
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L21-L27' title='Snippet source file'>snippet source</a> | <a href='#snippet-ScrubInlineEfDateTimesInstance' title='Start of snippet'>anchor</a></sup>
684684
<!-- endSnippet -->
685685

686686

@@ -692,7 +692,7 @@ await Verify(target, settings);
692692
await Verify(target)
693693
.ScrubInlineEfDateTimes();
694694
```
695-
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L48-L53' title='Snippet source file'>snippet source</a> | <a href='#snippet-ScrubInlineEfDateTimesFluent' title='Start of snippet'>anchor</a></sup>
695+
<sup><a href='/src/Verify.EntityFramework.Tests/CoreTests.cs#L35-L40' title='Snippet source file'>snippet source</a> | <a href='#snippet-ScrubInlineEfDateTimesFluent' title='Start of snippet'>anchor</a></sup>
696696
<!-- endSnippet -->
697697

698698

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SELECT [companies].[Id], [companies].[Name], [employees].[Id], [employees].[Age], [employees].[CompanyId], [employees].[Name]
2+
FROM [Companies] AS [companies]
3+
LEFT JOIN [Employees] AS [employees] ON [companies].[Id] = [employees].[CompanyId]
4+
ORDER BY [companies].[Name], [companies].[Id]

src/Verify.EntityFramework.Tests/CoreTests.WithNestedOrderBy.verified.txt renamed to src/Verify.EntityFramework.Tests/CoreTests.DescriptiveTableAliases.verified.txt

File renamed without changes.

src/Verify.EntityFramework.Tests/CoreTests.MissingOrderBy.verified.txt

Lines changed: 0 additions & 8 deletions
This file was deleted.

src/Verify.EntityFramework.Tests/CoreTests.NestedMissingOrderBy.verified.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/Verify.EntityFramework.Tests/CoreTests.SingleMissingOrder.verified.txt

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/Verify.EntityFramework.Tests/CoreTests.WithOrderBy.verified.txt

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/Verify.EntityFramework.Tests/CoreTests.cs

Lines changed: 6 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,14 @@
33
public class CoreTests
44
{
55
[Test]
6-
public async Task MissingOrderBy()
6+
public async Task DescriptiveTableAliases()
77
{
8-
await using var database = await DbContextBuilder.GetOrderRequiredDatabase();
8+
await using var database = await DbContextBuilder.GetDescriptiveAliasDatabase();
99
var data = database.Context;
10-
await ThrowsTask(
11-
() => data.Companies
12-
.ToListAsync())
13-
.IgnoreStackTrace();
14-
}
15-
16-
[Test]
17-
public async Task NestedMissingOrderBy()
18-
{
19-
await using var database = await DbContextBuilder.GetOrderRequiredDatabase();
20-
var data = database.Context;
21-
await ThrowsTask(
22-
() => data.Companies
23-
.Include(_ => _.Employees)
24-
.OrderBy(_ => _.Name)
25-
.ToListAsync())
26-
.IgnoreStackTrace();
10+
var queryable = data.Companies
11+
.Include(_ => _.Employees)
12+
.OrderBy(_ => _.Name);
13+
await Verify(queryable);
2714
}
2815

2916
[Test]
@@ -53,37 +40,6 @@ await Verify(target)
5340
#endregion
5441
}
5542

56-
[Test]
57-
public async Task WithOrderBy()
58-
{
59-
await using var database = await DbContextBuilder.GetOrderRequiredDatabase();
60-
var data = database.Context;
61-
await Verify(
62-
data.Companies
63-
.OrderBy(_ => _.Name)
64-
.ToListAsync());
65-
}
66-
67-
[Test]
68-
public async Task SingleMissingOrder()
69-
{
70-
await using var database = await DbContextBuilder.GetOrderRequiredDatabase();
71-
var data = database.Context;
72-
await Verify(data.Companies.Where(_ => _.Name == "Company1").SingleAsync());
73-
}
74-
75-
[Test]
76-
public async Task WithNestedOrderBy()
77-
{
78-
await using var database = await DbContextBuilder.GetOrderRequiredDatabase();
79-
var data = database.Context;
80-
await Verify(
81-
data.Companies
82-
.Include(_ => _.Employees.OrderBy(_ => _.Age))
83-
.OrderBy(_ => _.Name)
84-
.ToListAsync());
85-
}
86-
8743
#region Added
8844

8945
[Test]

src/Verify.EntityFramework.Tests/Snippets/DbContextBuilder.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,19 @@ static DbContextBuilder()
1212
builder.EnableRecording();
1313
return new(builder.Options);
1414
});
15-
orderRequiredSqlInstance = new(
15+
descriptiveAliasSqlInstance = new(
1616
buildTemplate: CreateDb,
17-
storage: Storage.FromSuffix<SampleDbContext>("ThrowForMissingOrderBy"),
17+
storage: Storage.FromSuffix<SampleDbContext>("DescriptiveTableAliases"),
1818
constructInstance: builder =>
1919
{
2020
builder.EnableRecording();
21-
builder.ThrowForMissingOrderBy();
21+
builder.UseDescriptiveTableAliases();
2222
return new(builder.Options);
2323
});
2424
}
2525

2626
static SqlInstance<SampleDbContext> sqlInstance;
27-
static SqlInstance<SampleDbContext> orderRequiredSqlInstance;
27+
static SqlInstance<SampleDbContext> descriptiveAliasSqlInstance;
2828

2929
static async Task CreateDb(SampleDbContext data)
3030
{
@@ -78,6 +78,6 @@ static async Task CreateDb(SampleDbContext data)
7878
public static Task<SqlDatabase<SampleDbContext>> GetDatabase([CallerMemberName] string suffix = "")
7979
=> sqlInstance.Build(suffix);
8080

81-
public static Task<SqlDatabase<SampleDbContext>> GetOrderRequiredDatabase([CallerMemberName] string suffix = "")
82-
=> orderRequiredSqlInstance.Build(suffix);
81+
public static Task<SqlDatabase<SampleDbContext>> GetDescriptiveAliasDatabase([CallerMemberName] string suffix = "")
82+
=> descriptiveAliasSqlInstance.Build(suffix);
8383
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#pragma warning disable EF9002
2+
3+
class DescriptiveSqlAliasManager : SqlAliasManager
4+
{
5+
Dictionary<string, int> aliases = new(StringComparer.OrdinalIgnoreCase);
6+
7+
public override string GenerateTableAlias(string name)
8+
{
9+
var lowerName = name.ToLowerInvariant();
10+
11+
if (aliases.TryGetValue(lowerName, out var counter))
12+
{
13+
aliases[lowerName] = counter + 1;
14+
return lowerName + counter;
15+
}
16+
17+
aliases[lowerName] = 0;
18+
return lowerName;
19+
}
20+
21+
protected override Dictionary<string, string>? RemapTableAliases(IReadOnlySet<string> usedAliases) =>
22+
// Skip gap-closing since the base implementation assumes single-char alias prefixes
23+
null;
24+
}

0 commit comments

Comments
 (0)