Skip to content

Commit 74f62ed

Browse files
committed
test(Data.Contracts.EFCore): Add UnitTest
1 parent f56c52c commit 74f62ed

File tree

13 files changed

+182
-4
lines changed

13 files changed

+182
-4
lines changed

src/Contrib/Data/Contracts/Masa.Contrib.Data.Contracts.EFCore/DataFiltering/SoftDeleteSaveChangesFilter.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void OnExecuting(ChangeTracker changeTracker)
2929
return;
3030

3131
changeTracker.DetectChanges();
32-
var entries = changeTracker.Entries().Where(entry => entry.State == EntityState.Deleted && entry.Entity is ISoftDelete).ToList();
32+
var entries = changeTracker.Entries().Where(entry => entry.State == EntityState.Deleted && entry.Entity is ISoftDelete);
3333
foreach (var entity in entries)
3434
{
3535
var navigationEntries = entity.Navigations
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.Contrib.Data.Contracts.EFCore.Tests;
5+
6+
public class CustomDbContext : MasaDbContext<CustomDbContext>
7+
{
8+
public CustomDbContext(MasaDbContextOptions<CustomDbContext> options) : base(options)
9+
{
10+
}
11+
12+
protected override void OnModelCreatingExecuting(ModelBuilder modelBuilder)
13+
{
14+
modelBuilder.ApplyConfiguration(new StudentEntityTypeConfiguration());
15+
modelBuilder.ApplyConfiguration(new AddressEntityTypeConfiguration());
16+
}
17+
}

src/Contrib/Data/Contracts/Tests/Masa.Contrib.Data.Contracts.EFCore.Tests/DataFilterTest.cs

+54-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) MASA Stack All rights reserved.
1+
// Copyright (c) MASA Stack All rights reserved.
22
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
33

44
namespace Masa.Contrib.Data.Contracts.EFCore.Tests;
@@ -44,4 +44,57 @@ public void TestDataFilterReturnFalse()
4444

4545
Assert.IsFalse(_dataFilter.IsEnabled<ISoftDelete>());
4646
}
47+
48+
[TestMethod]
49+
public void TestSoftDelete()
50+
{
51+
var services = new ServiceCollection();
52+
services.AddMasaDbContext<CustomDbContext>(options =>
53+
{
54+
options.UseTestSqlite($"data source=disabled-soft-delete-db-{Guid.NewGuid()}").UseFilter();
55+
});
56+
var serviceProvider = services.BuildServiceProvider();
57+
var dbContext = serviceProvider.GetRequiredService<CustomDbContext>();
58+
dbContext.Database.EnsureCreated();
59+
60+
DateTime createTime = DateTime.Now;
61+
var student = new Student()
62+
{
63+
Id = 1,
64+
Name = "Name",
65+
Age = 18,
66+
Address = new Address()
67+
{
68+
City = "city",
69+
Street = "street",
70+
LastLog = new LogItem()
71+
{
72+
Level = (int)LogLevel.Information,
73+
Message = "Add Student",
74+
CreateTime = createTime
75+
}
76+
},
77+
Hobbies = new List<Hobby>()
78+
{
79+
new()
80+
{
81+
Name = "Hobby.Name",
82+
Description = "Hobby.Description"
83+
}
84+
}
85+
};
86+
dbContext.Set<Student>().Add(student);
87+
dbContext.SaveChanges();
88+
89+
student = dbContext.Set<Student>().Include(s => s.Address).FirstOrDefault(s => s.Id == 1);
90+
Assert.IsNotNull(student);
91+
dbContext.Set<Student>().Remove(student);
92+
var row = dbContext.SaveChanges();
93+
Assert.IsTrue(row > 0);
94+
95+
var newStudent = dbContext.Set<Student>().IgnoreQueryFilters().FirstOrDefault(s => s.Id == student.Id);
96+
Assert.IsNotNull(newStudent);
97+
98+
Assert.AreEqual(createTime, newStudent.Address.LastLog.CreateTime);
99+
}
47100
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.Contrib.Data.Contracts.EFCore.Tests.EntityConfigurations;
5+
6+
public class AddressEntityTypeConfiguration : IEntityTypeConfiguration<Address>
7+
{
8+
public void Configure(EntityTypeBuilder<Address> builder)
9+
{
10+
builder.OwnsOne(address => address.LastLog, address =>
11+
{
12+
address.Property(log => log.Level).HasColumnName("level").IsRequired();
13+
address.Property(log => log.Message).HasColumnName("message").IsRequired();
14+
address.Property(log => log.CreateTime).HasColumnName("create_time").IsRequired();
15+
});
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.Contrib.Data.Contracts.EFCore.Tests.EntityConfigurations;
5+
6+
public class StudentEntityTypeConfiguration : IEntityTypeConfiguration<Student>
7+
{
8+
public void Configure(EntityTypeBuilder<Student> builder)
9+
{
10+
builder.HasOne(s => s.Address).WithOne(a => a.Student).HasForeignKey<Address>(t => t.Id);
11+
}
12+
}

src/Contrib/Data/Contracts/Tests/Masa.Contrib.Data.Contracts.EFCore.Tests/Masa.Contrib.Data.Contracts.EFCore.Tests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
</ItemGroup>
2424

2525
<ItemGroup>
26+
<ProjectReference Include="..\..\..\Orm\EFCore\Masa.Contrib.Data.EFCore.Sqlite\Masa.Contrib.Data.EFCore.Sqlite.csproj" />
2627
<ProjectReference Include="..\..\Masa.Contrib.Data.Contracts.EFCore\Masa.Contrib.Data.Contracts.EFCore.csproj" />
2728
</ItemGroup>
2829

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.Contrib.Data.Contracts.EFCore.Tests.Models;
5+
6+
public class Address : ISoftDelete
7+
{
8+
public int Id { get; set; }
9+
10+
public string City { get; set; } = default!;
11+
12+
public string Street { get; set; } = default!;
13+
14+
public LogItem LastLog { get; set; }
15+
16+
public Student Student { get; set; }
17+
18+
public bool IsDeleted { get; set; }
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.Contrib.Data.Contracts.EFCore.Tests.Models;
5+
6+
public class Hobby
7+
{
8+
public Guid Id { get; set; }
9+
10+
public string Name { get; set; } = default!;
11+
12+
public string Description { get; set; } = default!;
13+
14+
public Hobby()
15+
{
16+
Id = Guid.NewGuid();
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.Contrib.Data.Contracts.EFCore.Tests.Models;
5+
6+
public class LogItem
7+
{
8+
public int Level { get; set; }
9+
10+
public string Message { get; set; }
11+
12+
public DateTime CreateTime { get; set; }
13+
14+
public LogItem()
15+
{
16+
CreateTime = DateTime.Now;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) MASA Stack All rights reserved.
2+
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.
3+
4+
namespace Masa.Contrib.Data.Contracts.EFCore.Tests.Models;
5+
6+
public class Student : ISoftDelete
7+
{
8+
public int Id { get; set; }
9+
10+
public string Name { get; set; } = default!;
11+
12+
public int Age { get; set; }
13+
14+
public bool IsDeleted { get; private set; } = default!;
15+
16+
public Address Address { get; set; } = default!;
17+
18+
public List<Hobby> Hobbies { get; set; } = default!;
19+
}

src/Contrib/Data/Contracts/Tests/Masa.Contrib.Data.Contracts.EFCore.Tests/_Imports.cs

+5
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,10 @@
33

44
global using Masa.BuildingBlocks.Data.Contracts;
55
global using Masa.Contrib.Data.Contracts.EFCore.DataFiltering;
6+
global using Masa.Contrib.Data.Contracts.EFCore.Tests.EntityConfigurations;
7+
global using Masa.Contrib.Data.Contracts.EFCore.Tests.Models;
8+
global using Microsoft.EntityFrameworkCore;
9+
global using Microsoft.EntityFrameworkCore.Metadata.Builders;
610
global using Microsoft.Extensions.DependencyInjection;
11+
global using Microsoft.Extensions.Logging;
712
global using Microsoft.VisualStudio.TestTools.UnitTesting;

src/Contrib/Service/Masa.Contrib.Service.MinimalAPIs/ServiceBase.cs

-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ protected ServiceBase(IServiceCollection services)
169169
[Obsolete("service can be ignored")]
170170
protected ServiceBase(IServiceCollection services, string baseUri) : this(services)
171171
{
172-
173172
}
174173
#pragma warning restore S4136
175174

src/Contrib/Service/Masa.Contrib.Service.MinimalAPIs/ServiceGlobalRouteOptions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public ServiceGlobalRouteOptions()
1818
Version = "v1";
1919
AutoAppendId = true;
2020
PluralizeServiceName = true;
21-
GetPrefixes = new[] { "Get", "Select" };
21+
GetPrefixes = new[] { "Get", "Select", "Insert" };
2222
PostPrefixes = new[] { "Post", "Add", "Upsert", "Create" };
2323
PutPrefixes = new[] { "Put", "Update", "Modify" };
2424
DeletePrefixes = new[] { "Delete", "Remove" };

0 commit comments

Comments
 (0)