-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestData.cs
86 lines (75 loc) · 3.31 KB
/
TestData.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
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
using System;
using System.Linq;
namespace GraphQL.Benchmarks
{
public class TestDataOptions
{
public int Seed { get; set; } = 42;
public int NumberOfHumans { get; set; } = 100;
public int NumberOfDroids { get; set; } = 100;
public int NumberOfFriendships { get; set; } = 200;
public int NumberOfAppearances { get; set; } = 2;
}
public class TestDataGenerator
{
public static void InitializeDb() => InitializeDb(new TestDataOptions());
public static void InitializeDb(TestDataOptions options)
{
using (var db = new StarWarsContext())
{
db.Database.EnsureCreated();
db.Droids.RemoveRange(db.Droids);
db.Humans.RemoveRange(db.Humans);
db.Episodes.RemoveRange(db.Episodes);
db.SaveChanges();
// Generation
var random = new Random(options.Seed);
// Episodes
db.Episodes.AddRange(new[]
{
new Episode { EpisodeId = 4, Name = "A New Hope", Year = "1978" },
new Episode { EpisodeId = 5, Name = "Rise of the Empire", Year = "1980" },
new Episode { EpisodeId = 6, Name = "Return of the Jedi", Year = "1983" }
});
// Humans
db.Humans.AddRange(
Enumerable.Range(1, options.NumberOfHumans).Select(id => new Human
{
HumanId = id,
Name = Faker.Name.First(),
HomePlanet = Faker.Company.Name(),
Appearances = Enumerable.Repeat(1, options.NumberOfAppearances).Select(_ => new HumanAppearance
{
EpisodeId = random.Next(4, 7),
HumanId = id
}).ToList()
}));
// Droids
db.Droids.AddRange(
Enumerable.Range(1, options.NumberOfDroids).Select(id => new Droid
{
DroidId = id,
Name = $"{(char)random.Next('A', 'Z')}"
+ $"{random.Next(1, 9)}"
+ $"{(char)random.Next('A', 'Z')}"
+ $"{random.Next(1, 9)}",
PrimaryFunction = Faker.Company.BS(),
Appearances = Enumerable.Range(1, options.NumberOfAppearances).Select(_ => new DroidAppearance
{
EpisodeId = random.Next(4, 7),
DroidId = random.Next(1, options.NumberOfDroids)
}).ToList()
}));
// Friendships
db.Friendships.AddRange(
Enumerable.Range(1, options.NumberOfFriendships).Select(_ => new Friendship
{
DroidId = random.Next(1, options.NumberOfDroids),
HumanId = random.Next(1, options.NumberOfHumans)
}));
// Save
db.SaveChanges();
}
}
}
}