Skip to content
This repository was archived by the owner on Feb 1, 2025. It is now read-only.

Commit a18b543

Browse files
committed
Unified Mapping tests.
1 parent b7a8b04 commit a18b543

File tree

27 files changed

+443
-242
lines changed

27 files changed

+443
-242
lines changed

Source/LinqToDB.EntityFrameworkCore/EFCoreMetadataReader.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,25 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru
158158
}
159159

160160
var isIdentity = prop.GetAnnotations()
161-
.Any(a => a.Name.EndsWith(":ValueGenerationStrategy") && a.Value?.ToString() == "IdentityColumn");
161+
.Any(a =>
162+
{
163+
if (a.Name.EndsWith(":ValueGenerationStrategy"))
164+
return a.Value?.ToString().Contains("Identity") == true;
165+
166+
if (a.Name.EndsWith(":Autoincrement"))
167+
return a.Value is bool b && b;
168+
169+
// for postgres
170+
if (a.Name == "Relational:DefaultValueSql")
171+
{
172+
if (a.Value is string str)
173+
{
174+
return str.ToLower().Contains("nextval");
175+
}
176+
}
177+
178+
return false;
179+
});
162180

163181
var storeObjectId = GetStoreObjectIdentifier(et);
164182

@@ -170,8 +188,6 @@ public T[] GetAttributes<T>(Type type, MemberInfo memberInfo, bool inherit = tru
170188
DbType = prop.GetColumnType(),
171189
IsPrimaryKey = isPrimaryKey,
172190
PrimaryKeyOrder = primaryKeyOrder,
173-
SkipOnInsert = prop.ValueGenerated == ValueGenerated.OnAdd || prop.ValueGenerated == ValueGenerated.OnAddOrUpdate,
174-
SkipOnUpdate = prop.ValueGenerated == ValueGenerated.OnUpdate || prop.ValueGenerated == ValueGenerated.OnAddOrUpdate,
175191
IsIdentity = isIdentity,
176192
}};
177193
}

Source/LinqToDB.EntityFrameworkCore/LinqToDBForEFToolsImplDefault.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
using JetBrains.Annotations;
1919
using Microsoft.EntityFrameworkCore.Diagnostics;
20+
using Microsoft.Extensions.Caching.Memory;
2021

2122
namespace LinqToDB.EntityFrameworkCore
2223
{
@@ -84,10 +85,10 @@ public override int GetHashCode()
8485
#endregion
8586
}
8687

87-
readonly ConcurrentDictionary<ProviderKey, IDataProvider> _knownProviders = new ConcurrentDictionary<ProviderKey, IDataProvider>();
88+
readonly ConcurrentDictionary<ProviderKey, IDataProvider> _knownProviders = new();
8889

89-
private readonly MemoryCache _schemaCache = new MemoryCache(
90-
new MemoryCacheOptions
90+
private readonly MemoryCache _schemaCache = new(
91+
new Microsoft.Extensions.Caching.Memory.MemoryCacheOptions()
9192
{
9293
ExpirationScanFrequency = TimeSpan.FromHours(1.0)
9394
});
@@ -455,6 +456,10 @@ public virtual void DefineConvertors(
455456
if (modelType.IsEnum)
456457
continue;
457458

459+
// skipping arrays
460+
if (modelType.IsArray)
461+
continue;
462+
458463
MapEFCoreType(modelType);
459464
if (modelType.IsValueType && !typeof(Nullable<>).IsSameOrParentOf(modelType))
460465
MapEFCoreType(typeof(Nullable<>).MakeGenericType(modelType));

Source/LinqToDB.EntityFrameworkCore/linq2db.EntityFrameworkCore.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</PropertyGroup>
1818

1919
<ItemGroup>
20-
<PackageReference Include="linq2db" Version="3.3.0" />
20+
<PackageReference Include="linq2db" Version="3.3.1-rc.6076" />
2121
<PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="5.0.2" />
2222
</ItemGroup>
2323

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using FluentAssertions;
5+
using LinqToDB.Data;
6+
using LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping;
7+
using NUnit.Framework;
8+
9+
namespace LinqToDB.EntityFrameworkCore.BaseTests
10+
{
11+
public abstract class ForMappingTestsBase : TestsBase
12+
{
13+
public abstract ForMappingContextBase CreateContext();
14+
15+
[Test]
16+
public virtual void TestIdentityMapping()
17+
{
18+
using var context = CreateContext();
19+
using var connection = context.CreateLinqToDbConnection();
20+
21+
var ed = connection.MappingSchema.GetEntityDescriptor(typeof(WithIdentity));
22+
var pk = ed.Columns.Where(c => c.IsPrimaryKey).Single();
23+
24+
pk.IsIdentity.Should().BeTrue();
25+
}
26+
27+
[Test]
28+
public virtual void TestNoIdentityMapping()
29+
{
30+
using var context = CreateContext();
31+
using var connection = context.CreateLinqToDbConnection();
32+
33+
var ed = connection.MappingSchema.GetEntityDescriptor(typeof(NoIdentity));
34+
var pk = ed.Columns.Where(c => c.IsPrimaryKey).Single();
35+
36+
pk.IsIdentity.Should().BeFalse();
37+
}
38+
39+
[Test]
40+
public virtual void TestTableCreation()
41+
{
42+
using var context = CreateContext();
43+
using var connection = context.CreateLinqToDbConnection();
44+
45+
using var t1 = connection.CreateTempTable<WithIdentity>();
46+
using var t2 = connection.CreateTempTable<NoIdentity>();
47+
}
48+
49+
50+
[Test]
51+
public virtual void TestBulkCopyNoIdentity()
52+
{
53+
using var context = CreateContext();
54+
using var connection = context.CreateLinqToDbConnection();
55+
56+
using var t = connection.CreateTempTable<NoIdentity>();
57+
58+
var items = new List<NoIdentity>()
59+
{
60+
new() {Id = Guid.NewGuid(), Name = "John Doe"},
61+
new() {Id = Guid.NewGuid(), Name = "Jane Doe"}
62+
};
63+
64+
t.BulkCopy(items);
65+
66+
67+
items.Should().BeEquivalentTo(t);
68+
}
69+
70+
[Test]
71+
public virtual void TestBulkCopyWithIdentity()
72+
{
73+
using var context = CreateContext();
74+
using var connection = context.CreateLinqToDbConnection();
75+
76+
using var t = connection.CreateTempTable<WithIdentity>();
77+
78+
var items = new List<WithIdentity>()
79+
{
80+
new() {Id = 1, Name = "John Doe"},
81+
new() {Id = 2, Name = "Jane Doe"}
82+
};
83+
84+
t.BulkCopy(items);
85+
86+
87+
t.Should().HaveCount(items.Count);
88+
}
89+
90+
}
91+
}

Tests/LinqToDB.EntityFrameworkCore.BaseTests/Logging/TestLogger.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ internal class TestLogger : ILogger
1313
private static readonly string _newLineWithMessagePadding;
1414

1515
// ConsoleColor does not have a value to specify the 'Default' color
16+
#pragma warning disable 649
1617
private readonly ConsoleColor? DefaultConsoleColor;
18+
#pragma warning restore 649
1719

1820
private readonly string _name;
1921

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping
4+
{
5+
public abstract class ForMappingContextBase : DbContext
6+
{
7+
protected ForMappingContextBase(DbContextOptions options) : base(options)
8+
{
9+
}
10+
11+
public DbSet<WithIdentity> WithIdentity { get; set; } = null!;
12+
public DbSet<NoIdentity> NoIdentity { get; set; } = null!;
13+
14+
}
15+
}

Tests/LinqToDB.EntityFrameworkCore.SQLite.Tests/Models/Identity/Person.cs renamed to Tests/LinqToDB.EntityFrameworkCore.BaseTests/Models/ForMapping/NoIdentity.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System;
22

3-
namespace LinqToDB.EntityFrameworkCore.SQLite.Tests.Models.Identity
3+
namespace LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping
44
{
5-
public class Person
5+
public class NoIdentity
66
{
77
public Guid Id { get; set; }
88
public string Name { get; set; } = null!;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping
2+
{
3+
public class WithIdentity
4+
{
5+
public int Id { get; set; }
6+
public string Name { get; set; } = null!;
7+
}
8+
}

Tests/LinqToDB.EntityFrameworkCore.BaseTests/TestUtils.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ public class TestUtils
1515

1616
.AddTestLogger(o =>
1717
{
18+
#pragma warning disable 618
1819
o.IncludeScopes = true;
20+
#pragma warning restore 618
1921
o.FormatterName = ConsoleFormatterNames.Simple;
2022
});
2123
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using FluentAssertions;
5+
using LinqToDB.Data;
6+
using LinqToDB.EntityFrameworkCore.BaseTests;
7+
using LinqToDB.EntityFrameworkCore.BaseTests.Models.ForMapping;
8+
using LinqToDB.EntityFrameworkCore.PomeloMySql.Tests.Models.ForMapping;
9+
using Microsoft.EntityFrameworkCore;
10+
11+
namespace LinqToDB.EntityFrameworkCore.PomeloMySql.Tests
12+
{
13+
public class ForMappingTests : ForMappingTestsBase
14+
{
15+
public override ForMappingContextBase CreateContext()
16+
{
17+
var optionsBuilder = new DbContextOptionsBuilder<ForMappingContext>();
18+
optionsBuilder.UseMySql("Server=DBHost;Port=3306;Database=TestData;Uid=TestUser;Pwd=TestPassword;charset=utf8;");
19+
optionsBuilder.UseLoggerFactory(TestUtils.LoggerFactory);
20+
21+
var options = optionsBuilder.Options;
22+
var ctx = new ForMappingContext(options);
23+
24+
//ctx.Database.EnsureDeleted();
25+
ctx.Database.EnsureCreated();
26+
27+
return ctx;
28+
}
29+
30+
}
31+
}

0 commit comments

Comments
 (0)