Skip to content

Commit 1e5fbe8

Browse files
committed
Added Person data source/equivalence classes. Added convention tests for files and removed duplicates found by tests.
1 parent 1cfdbd0 commit 1e5fbe8

40 files changed

Lines changed: 253 additions & 717 deletions
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Reflection;
6+
using System.Text;
7+
using Xunit;
8+
9+
namespace NTestDataBuilder.Tests.DataSources.Dictionaries.Resources
10+
{
11+
public class FileDataConventions
12+
{
13+
[Fact]
14+
public void ApplyConventions()
15+
{
16+
var assembly = typeof(IAnonymousValueSupplier).Assembly;
17+
var resources = assembly
18+
.GetManifestResourceNames()
19+
.Where(x => x.EndsWith(".txt"))
20+
.ToList();
21+
22+
foreach (var resource in resources)
23+
{
24+
var collection = GetDataFromResource(assembly, resource);
25+
Should_not_contain_duplicates(collection, resource);
26+
Should_not_contain_null_or_empty_values(collection, resource);
27+
}
28+
}
29+
30+
public void Should_not_contain_duplicates(List<string> collection, string fileName)
31+
{
32+
var duplicates = collection
33+
.GroupBy(x => x)
34+
.Where(g => g.Count() > 1)
35+
.Select(y => y.Key)
36+
.ToList();
37+
if (duplicates.Any())
38+
{
39+
var sb = new StringBuilder();
40+
sb.AppendLine(string.Format("Duplicates in '{0}' file", fileName));
41+
duplicates.ForEach(duplicate => sb.AppendLine(duplicate));
42+
throw new Exception(sb.ToString());
43+
}
44+
}
45+
46+
public void Should_not_contain_null_or_empty_values(List<string> collection, string fileName)
47+
{
48+
var blanks = collection.Where(string.IsNullOrEmpty).ToList();
49+
if (blanks.Any())
50+
{
51+
throw new Exception(string.Format("File '{0}' contains blank entries", fileName));
52+
}
53+
}
54+
55+
private List<string> GetDataFromResource(Assembly assembly, string resourceName)
56+
{
57+
var items = new List<string>();
58+
var stream = assembly.GetManifestResourceStream(resourceName);
59+
using (var reader = new StreamReader(stream))
60+
{
61+
string line;
62+
while ((line = reader.ReadLine()) != null)
63+
{
64+
items.Add(line);
65+
}
66+
}
67+
return items;
68+
}
69+
}
70+
}

NTestDataBuilder.Tests/DataSources/PersonSourceTests.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using System.Linq;
33
using NTestDataBuilder.DataSources;
4+
using NTestDataBuilder.DataSources.Person;
45
using Shouldly;
56
using Xunit.Extensions;
67

@@ -22,18 +23,15 @@ public static IEnumerable<object[]> TestCases
2223
{
2324
get
2425
{
25-
yield return new object[] { new FirstNameSource(), 479 };
26-
yield return new object[] { new LastNameSource(), 498 };
27-
yield return new object[] { new FullNameSource(), 500 };
28-
yield return new object[] { new CompanySource(), 496 };
29-
yield return new object[] { new StreetSource(), 498 };
30-
yield return new object[] { new CitySource(), 483 };
31-
yield return new object[] { new CountySource(), 214 };
32-
yield return new object[] { new PostCodeSource(), 433 };
33-
yield return new object[] { new PhoneSource(), 500 };
34-
yield return new object[] { new EmailSource(), 500 };
35-
yield return new object[] { new WebsiteSource(), 498 };
36-
26+
yield return new object[] { new PersonEmailAddressSource(), 1000 };
27+
yield return new object[] { new PersonLanguageSource(), 97 };
28+
yield return new object[] { new PersonNameFirstFemaleSource(), 100 };
29+
yield return new object[] { new PersonNameFirstSource(), 479 };
30+
yield return new object[] { new PersonNameFullSource(), 1000 };
31+
yield return new object[] { new PersonNameLastSource(), 747 };
32+
yield return new object[] { new PersonNameFirstMaleSource(), 100 };
33+
yield return new object[] { new PersonNameSuffixSource(), 5 };
34+
yield return new object[] { new PersonNameTitleSource(), 9 };
3735
}
3836
}
3937
}

NTestDataBuilder.Tests/EquivalenceClasses/PersonEquivalenceClassesTests.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using NTestDataBuilder.DataSources;
5+
using NTestDataBuilder.DataSources.Person;
56
using NTestDataBuilder.EquivalenceClasses;
67
using Shouldly;
78
using Xunit.Extensions;
@@ -28,25 +29,26 @@ public void WhenGettingAnyPersonData_ThenReturnRandomPersonDataWhichIsReasonably
2829
testCase.ShouldNotBeNullOrEmpty();
2930
source.Data.ShouldContain(testCase);
3031
}
31-
var unique = testCases.Distinct().Count();
32-
unique.ShouldBeGreaterThan(5);
32+
if (source.Data.Count > 15)
33+
{
34+
var unique = testCases.Distinct().Count();
35+
unique.ShouldBeGreaterThan(5);
36+
}
3337
}
3438

3539
public static IEnumerable<object[]> TestCases
3640
{
3741
get
3842
{
39-
yield return new object[] { new FirstNameSource(), GenerateTestCasesForSut(Any.FirstName) };
40-
yield return new object[] { new LastNameSource(), GenerateTestCasesForSut(Any.LastName) };
41-
yield return new object[] { new FullNameSource(), GenerateTestCasesForSut(Any.FullName) };
42-
yield return new object[] { new CompanySource(), GenerateTestCasesForSut(Any.Company) };
43-
yield return new object[] { new StreetSource(), GenerateTestCasesForSut(Any.Street) };
44-
yield return new object[] { new CitySource(), GenerateTestCasesForSut(Any.City) };
45-
yield return new object[] { new CountySource(), GenerateTestCasesForSut(Any.County) };
46-
yield return new object[] { new PostCodeSource(), GenerateTestCasesForSut(Any.PostCode) };
47-
yield return new object[] { new PhoneSource(), GenerateTestCasesForSut(Any.Phone) };
48-
yield return new object[] { new EmailSource(), GenerateTestCasesForSut(Any.Email) };
49-
yield return new object[] { new WebsiteSource(), GenerateTestCasesForSut(Any.Website) };
43+
yield return new object[] { new PersonEmailAddressSource(), GenerateTestCasesForSut(Any.PersonEmailAddress) };
44+
yield return new object[] { new PersonLanguageSource(), GenerateTestCasesForSut(Any.PersonLanguage) };
45+
yield return new object[] { new PersonNameFirstFemaleSource(), GenerateTestCasesForSut(Any.PersonNameFirstFemale) };
46+
yield return new object[] { new PersonNameFirstSource(), GenerateTestCasesForSut(Any.PersonNameFirst) };
47+
yield return new object[] { new PersonNameFullSource(), GenerateTestCasesForSut(Any.PersonNameFull) };
48+
yield return new object[] { new PersonNameLastSource(), GenerateTestCasesForSut(Any.PersonNameLast) };
49+
yield return new object[] { new PersonNameFirstMaleSource(), GenerateTestCasesForSut(Any.PersonNameFirstMale) };
50+
yield return new object[] { new PersonNameSuffixSource(), GenerateTestCasesForSut(Any.PersonNameSuffix) };
51+
yield return new object[] { new PersonNameTitleSource(), GenerateTestCasesForSut(Any.PersonNameTitle) };
5052
}
5153
}
5254

NTestDataBuilder.Tests/NTestDataBuilder.Tests.csproj

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
<Compile Include="DataSources\Dictionaries\CacheTests.cs" />
5757
<Compile Include="DataSources\Dictionaries\FileDictionaryRepositoryIntegrationTests.cs" />
5858
<Compile Include="DataSources\Dictionaries\FileDictionarySourceTests.cs" />
59+
<Compile Include="DataSources\Dictionaries\Resources\Class1.cs" />
5960
<Compile Include="DataSources\Generators\RandomGeneratorTests.cs" />
6061
<Compile Include="DataSources\Generators\SequentiaGeneratorTests.cs" />
6162
<Compile Include="DataSources\PersonSourceTests.cs" />
@@ -88,9 +89,7 @@
8889
<Name>NTestDataBuilder</Name>
8990
</ProjectReference>
9091
</ItemGroup>
91-
<ItemGroup>
92-
<Folder Include="DataSources\Dictionaries\Resources\" />
93-
</ItemGroup>
92+
<ItemGroup />
9493
<ItemGroup>
9594
<Content Include="SampleDictionaryFile.txt">
9695
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>

NTestDataBuilder/DataSources/CitySource.cs

Lines changed: 0 additions & 30 deletions
This file was deleted.

NTestDataBuilder/DataSources/CompanySource.cs

Lines changed: 0 additions & 30 deletions
This file was deleted.

NTestDataBuilder/DataSources/CountySource.cs

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)