-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
LoggingSqliteTest.cs
56 lines (44 loc) · 2.37 KB
/
LoggingSqliteTest.cs
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Transactions;
using Microsoft.EntityFrameworkCore.Diagnostics.Internal;
using Microsoft.EntityFrameworkCore.Sqlite.Diagnostics.Internal;
using Microsoft.EntityFrameworkCore.Sqlite.Infrastructure.Internal;
// ReSharper disable InconsistentNaming
namespace Microsoft.EntityFrameworkCore;
#nullable disable
public class LoggingSqliteTest : LoggingRelationalTestBase<SqliteDbContextOptionsBuilder, SqliteOptionsExtension>
{
[ConditionalFact]
public void AmbientTransactionWarning_throws_by_default()
{
using var context = new AmbientTransactionWarningContext(CreateOptionsBuilder(new ServiceCollection()));
context.Add(new Animal());
using var transactionScope = new TransactionScope();
Assert.Equal(
CoreStrings.WarningAsErrorTemplate(
RelationalEventId.AmbientTransactionWarning.ToString(),
RelationalResources.LogAmbientTransaction(CreateTestLogger()).GenerateMessage(),
"RelationalEventId.AmbientTransactionWarning"),
Assert.Throws<InvalidOperationException>(
() => context.SaveChanges()).Message);
}
protected class AmbientTransactionWarningContext(DbContextOptionsBuilder optionsBuilder) : DbContext(optionsBuilder.Options)
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
=> modelBuilder.Entity<Animal>();
}
protected override DbContextOptionsBuilder CreateOptionsBuilder(
IServiceCollection services,
Action<RelationalDbContextOptionsBuilder<SqliteDbContextOptionsBuilder, SqliteOptionsExtension>> relationalAction)
=> new DbContextOptionsBuilder()
.UseInternalServiceProvider(services.AddEntityFrameworkSqlite().BuildServiceProvider(validateScopes: true))
.UseSqlite("Data Source=LoggingSqliteTest.db", relationalAction);
protected override TestLogger CreateTestLogger()
=> new TestLogger<SqliteLoggingDefinitions>();
protected override string ProviderName
=> "Microsoft.EntityFrameworkCore.Sqlite";
protected override string ProviderVersion
=> typeof(SqliteOptionsExtension).Assembly
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()?.InformationalVersion;
}