-
Notifications
You must be signed in to change notification settings - Fork 2
/
DataGenerator.cs
157 lines (143 loc) · 6.4 KB
/
DataGenerator.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
namespace JsonConverterGenerator
{
internal static class DataGenerator
{
internal static T Generate<T>()
{
if (typeof(T) == typeof(LoginViewModel))
return (T)(object)CreateLoginViewModel();
if (typeof(T) == typeof(Location))
return (T)(object)CreateLocation();
if (typeof(T) == typeof(IndexViewModel))
return (T)(object)CreateIndexViewModel();
if (typeof(T) == typeof(MyEventsListerViewModel))
return (T)(object)CreateMyEventsListerViewModel();
if (typeof(T) == typeof(CollectionsOfPrimitives))
return (T)(object)CreateCollectionsOfPrimitives(1024); // 1024 values was copied from CoreFX benchmarks
throw new NotImplementedException();
}
private static LoginViewModel CreateLoginViewModel()
=> new LoginViewModel
{
Email = "[email protected]",
Password = "abcdefgh123456!@",
RememberMe = true
};
private static Location CreateLocation()
=> new Location
{
Id = 1234,
Address1 = "The Street Name",
Address2 = "20/11",
City = "The City",
State = "The State",
PostalCode = "abc-12",
Name = "Nonexisting",
PhoneNumber = "+0 11 222 333 44",
Country = "The Greatest"
};
private static IndexViewModel CreateIndexViewModel()
=> new IndexViewModel
{
IsNewAccount = false,
FeaturedCampaign = new CampaignSummaryViewModel
{
Description = "Very nice campaing",
Headline = "The Headline",
Id = 234235,
OrganizationName = "The Company XYZ",
ImageUrl = "https://www.dotnetfoundation.org/theme/img/carousel/foundation-diagram-content.png",
Title = "Promoting Open Source"
},
ActiveOrUpcomingEvents = Enumerable.Repeat(
new ActiveOrUpcomingEvent
{
Id = 10,
CampaignManagedOrganizerName = "Name FamiltyName",
CampaignName = "The very new campaing",
Description = "The .NET Foundation works with Microsoft and the broader industry to increase the exposure of open source projects in the .NET community and the .NET Foundation. The .NET Foundation provides access to these resources to projects and looks to promote the activities of our communities.",
EndDate = DateTime.UtcNow.AddYears(1),
Name = "Just a name",
ImageUrl = "https://www.dotnetfoundation.org/theme/img/carousel/foundation-diagram-content.png",
StartDate = DateTime.UtcNow
},
count: 20).ToList()
};
private static MyEventsListerViewModel CreateMyEventsListerViewModel()
=> new MyEventsListerViewModel
{
CurrentEvents = Enumerable.Repeat(CreateMyEventsListerItem(), 3).ToList(),
FutureEvents = Enumerable.Repeat(CreateMyEventsListerItem(), 9).ToList(),
PastEvents = Enumerable.Repeat(CreateMyEventsListerItem(), 60).ToList() // usually there is a lot of historical data
};
private static MyEventsListerItem CreateMyEventsListerItem()
=> new MyEventsListerItem
{
Campaign = "A very nice campaing",
EndDate = DateTime.UtcNow.AddDays(7),
EventId = 321,
EventName = "wonderful name",
Organization = "Local Animal Shelter",
StartDate = DateTime.UtcNow.AddDays(-7),
TimeZone = TimeZoneInfo.Utc.DisplayName,
VolunteerCount = 15,
Tasks = Enumerable.Repeat(
new MyEventsListerItemTask
{
StartDate = DateTime.UtcNow,
EndDate = DateTime.UtcNow.AddDays(1),
Name = "A very nice task to have"
}, 4).ToList()
};
private static CollectionsOfPrimitives CreateCollectionsOfPrimitives(int count)
=> new CollectionsOfPrimitives
{
ByteArray = CreateByteArray(count),
DateTimeArray = CreateDateTimeArray(count),
Dictionary = CreateDictionaryOfStringString(count),
ListOfInt = CreateListOfInt(count)
};
private static DateTime[] CreateDateTimeArray(int count)
{
DateTime[] arr = new DateTime[count];
int kind = (int)DateTimeKind.Unspecified;
int maxDateTimeKind = (int)DateTimeKind.Local;
DateTime val = DateTime.Now.AddHours(count / 2);
for (int i = 0; i < count; i++)
{
arr[i] = DateTime.SpecifyKind(val, (DateTimeKind)kind);
val = val.AddHours(1);
kind = (kind + 1) % maxDateTimeKind;
}
return arr;
}
private static Dictionary<string, string> CreateDictionaryOfStringString(int count)
{
Dictionary<string, string> dictOfStringString = new Dictionary<string, string>(count);
for (int i = 0; i < count; ++i)
{
dictOfStringString[$"{i}"] = i.ToString();
}
return dictOfStringString;
}
private static byte[] CreateByteArray(int size)
{
byte[] obj = new byte[size];
for (int i = 0; i < obj.Length; ++i)
{
unchecked
{
obj[i] = (byte)i;
}
}
return obj;
}
private static List<int> CreateListOfInt(int count) => Enumerable.Range(0, count).ToList();
}
}