|
1 |
| -// Copyright (c) MASA Stack All rights reserved. |
| 1 | +// Copyright (c) MASA Stack All rights reserved. |
2 | 2 | // Licensed under the MIT License. See LICENSE.txt in the project root for license information.
|
3 | 3 |
|
4 | 4 | namespace Masa.Contrib.Data.Contracts.EFCore.Tests;
|
@@ -44,4 +44,57 @@ public void TestDataFilterReturnFalse()
|
44 | 44 |
|
45 | 45 | Assert.IsFalse(_dataFilter.IsEnabled<ISoftDelete>());
|
46 | 46 | }
|
| 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 | + } |
47 | 100 | }
|
0 commit comments