-
-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathEntitiesTests.cs
More file actions
93 lines (79 loc) · 2.81 KB
/
EntitiesTests.cs
File metadata and controls
93 lines (79 loc) · 2.81 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System.Linq.Dynamic.Core.Tests.Helpers.Entities;
using Xunit;
#if EFCORE
using Microsoft.EntityFrameworkCore;
#else
using System.Data.Entity;
#endif
namespace System.Linq.Dynamic.Core.Tests;
public partial class EntitiesTests : IClassFixture<EntitiesTestsDatabaseFixture>
{
private static readonly Random Rnd = new(1);
private readonly BlogContext _context;
public EntitiesTests(EntitiesTestsDatabaseFixture fixture)
{
#if EFCORE
var builder = new DbContextOptionsBuilder();
if (fixture.UseInMemory)
{
builder.UseInMemoryDatabase($"System.Linq.Dynamic.Core.{Guid.NewGuid()}");
}
else
{
builder.UseSqlServer(fixture.ConnectionString);
}
_context = new BlogContext(builder.Options);
_context.Database.EnsureCreated();
#else
_context = new BlogContext(fixture.ConnectionString);
#endif
InternalPopulateTestData();
}
private void InternalPopulateTestData()
{
if (_context.Blogs.Any())
{
return;
}
for (int i = 0; i < 25; i++)
{
var blog = new Blog
{
X = i.ToString(),
Name = "Blog" + (i + 1),
BlogId = 1000 + i,
Created = DateTime.Now.AddDays(-Rnd.Next(0, 100))
};
_context.Blogs.Add(blog);
for (int j = 0; j < 10; j++)
{
var postDate = DateTime.Today.AddDays(-Rnd.Next(0, 100)).AddSeconds(Rnd.Next(0, 30000));
var post = new Post
{
PostId = 10000 + i * 10 + j,
Blog = blog,
Title = $"Blog {i + 1} - Post {j + 1}",
Content = "My Content",
PostDate = postDate,
CloseDate = Rnd.Next(0, 10) < 5 ? postDate.AddDays(1) : null,
NumberOfReads = Rnd.Next(0, 5000),
Item = "Item " + Rnd.Next(0, 1000)
};
_context.Posts.Add(post);
}
}
var singleBlog = new Blog
{
X = "42",
Name = "SingleBlog",
BlogId = 12345678,
Created = DateTime.Now.AddDays(-Rnd.Next(0, 100))
};
_context.Blogs.Add(singleBlog);
_context.Blogs.Add(new Blog { BlogId = 2000, X = "0", Name = "blog a", Created = DateTime.Now });
_context.Blogs.Add(new Blog { BlogId = 2001, X = "0", Name = "blog b", Created = DateTime.Now });
_context.Blogs.Add(new Blog { BlogId = 3000, X = "0", Name = "Blog1", Created = DateTime.Now, NullableInt = null });
_context.Blogs.Add(new Blog { BlogId = 3001, X = "0", Name = "Blog2", Created = DateTime.Now, NullableInt = 5 });
_context.SaveChanges();
}
}