Skip to content

Commit c91d793

Browse files
committed
example of file-based data for Person data
1 parent 46ee37f commit c91d793

6 files changed

Lines changed: 167 additions & 0 deletions

File tree

NTestDataBuilder.Tests/DataSources/DataSourceTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ public void WhenGeneratingValuesFromUniqueSequentialDataSource_ThenShouldThrowWh
6767
Should.Throw<InvalidOperationException>(() => sut.Next());
6868
}
6969

70+
[Fact]
71+
public void WhenGeneratingValuesFromPersonDataSource_ThenShouldHave500Records()
72+
{
73+
new FirstNameSource().List.Count.ShouldBe(500);
74+
new LastNameSource().List.Count.ShouldBe(500);
75+
new FullNameSource().List.Count.ShouldBe(500);
76+
}
7077
}
7178

7279
public class DummyDataSource : DataSource<string>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.IO;
5+
using System.Linq;
6+
using System.Reflection;
7+
8+
namespace NTestDataBuilder.DataSources.FileData
9+
{
10+
public static class DataTableExtensions
11+
{
12+
public static IEnumerable<T> AsEnumerable<T>(this DataTable table) where T : new()
13+
{
14+
if (table == null)
15+
throw new NullReferenceException("DataTable");
16+
int propertiesLength = typeof(T).GetProperties().Length;
17+
if (propertiesLength == 0)
18+
throw new NullReferenceException("Properties");
19+
var objList = new List<T>();
20+
21+
foreach (DataRow row in table.Rows)
22+
{
23+
var obj = new T();
24+
PropertyInfo[] objProperties = obj.GetType().GetProperties();
25+
for (int i = 0; i < propertiesLength; i++)
26+
{
27+
PropertyInfo property = objProperties[i];
28+
if (table.Columns.Contains(property.Name))
29+
{
30+
object objValue = row[property.Name];
31+
var propertyType = property.PropertyType;
32+
if (propertyType.IsGenericType && propertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
33+
propertyType = propertyType.GetGenericArguments()[0];
34+
objProperties[i].SetValue(obj, Convert.ChangeType(objValue, propertyType, System.Globalization.CultureInfo.CurrentCulture), null);
35+
}
36+
}
37+
objList.Add(obj);
38+
}
39+
40+
return objList;
41+
}
42+
}
43+
44+
public class Person
45+
{
46+
public string FirstName { get; set; }
47+
public string LastName { get; set; }
48+
public string CompanyName { get; set; }
49+
public string Address { get; set; }
50+
public string City { get; set; }
51+
public string County { get; set; }
52+
public string Postal { get; set; }
53+
public string Phone1 { get; set; }
54+
public string Phone2 { get; set; }
55+
public string Email { get; set; }
56+
public string Web { get; set; }
57+
}
58+
public class PersonData
59+
{
60+
public static IList<Person> People { get; private set; }
61+
62+
static PersonData()
63+
{
64+
People = ConvertCSVtoDataTable()
65+
.AsEnumerable<Person>()
66+
.ToList();
67+
}
68+
69+
private static DataTable ConvertCSVtoDataTable()
70+
{
71+
var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("NTestDataBuilder.DataSources.FileData.uk-500.csv");
72+
var sr = new StreamReader(stream);
73+
var headers = sr.ReadLine().Split(',');
74+
var dt = new DataTable();
75+
foreach (string header in headers)
76+
{
77+
dt.Columns.Add(header);
78+
}
79+
while (!sr.EndOfStream)
80+
{
81+
var rows = sr.ReadLine().Split(',');
82+
var dr = dt.NewRow();
83+
for (int i = 0; i < headers.Length; i++)
84+
{
85+
dr[i] = rows[i];
86+
}
87+
dt.Rows.Add(dr);
88+
}
89+
return dt;
90+
}
91+
}
92+
}

NTestDataBuilder/DataSources/FileData/uk-500.csv

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using NTestDataBuilder.DataSources.FileData;
4+
5+
namespace NTestDataBuilder.DataSources
6+
{
7+
public class FirstNameSource : DataSource<string>
8+
{
9+
protected override IList<string> InitializeList()
10+
{
11+
return PersonData.People
12+
.Select(person => person.FirstName)
13+
.ToList();
14+
}
15+
}
16+
public class LastNameSource : DataSource<string>
17+
{
18+
protected override IList<string> InitializeList()
19+
{
20+
return PersonData.People
21+
.Select(person => person.LastName)
22+
.ToList();
23+
}
24+
}
25+
public class FullNameSource : DataSource<string>
26+
{
27+
protected override IList<string> InitializeList()
28+
{
29+
return PersonData.People
30+
.Select(person => person.FirstName + " " + person.LastName)
31+
.ToList();
32+
}
33+
}
34+
35+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using NTestDataBuilder.DataSources;
2+
3+
namespace NTestDataBuilder.EquivalenceClasses
4+
{
5+
public static class PersonEquivalenceClasses
6+
{
7+
private static readonly FirstNameSource _firstNameSource = new FirstNameSource();
8+
private static readonly LastNameSource _lastNameSource = new LastNameSource();
9+
private static readonly FullNameSource _fullNameSource = new FullNameSource();
10+
11+
public static string FirstName(this AnonymousValueFixture fixture)
12+
{
13+
return _firstNameSource.Next();
14+
}
15+
public static string LastName(this AnonymousValueFixture fixture)
16+
{
17+
return _lastNameSource.Next();
18+
}
19+
public static string FullName(this AnonymousValueFixture fixture)
20+
{
21+
return _fullNameSource.Next();
22+
}
23+
24+
}
25+
26+
}

NTestDataBuilder/NTestDataBuilder.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,16 @@
4949
<Reference Include="System" />
5050
<Reference Include="System.Core" />
5151
<Reference Include="Microsoft.CSharp" />
52+
<Reference Include="System.Data" />
53+
<Reference Include="System.XML" />
5254
</ItemGroup>
5355
<ItemGroup>
5456
<Compile Include="AnonymousValueFixture.cs" />
57+
<Compile Include="DataSources\FileData\PersonData.cs" />
5558
<Compile Include="DataSources\CompanySource.cs" />
5659
<Compile Include="DataSources\DataSource.cs" />
60+
<Compile Include="DataSources\FirstNameSource.cs" />
61+
<Compile Include="EquivalenceClasses\Class1.cs" />
5762
<Compile Include="EquivalenceClasses\CompanyEquivalenceClass.cs" />
5863
<Compile Include="Guard.cs" />
5964
<Compile Include="DataSources\Generators\IGenerator.cs" />
@@ -72,6 +77,7 @@
7277
<Compile Include="ProxyBuilder.cs" />
7378
</ItemGroup>
7479
<ItemGroup>
80+
<EmbeddedResource Include="DataSources\FileData\uk-500.csv" />
7581
<None Include="NTestDataBuilder.nuspec" />
7682
<None Include="packages.config" />
7783
</ItemGroup>

0 commit comments

Comments
 (0)