Skip to content

Commit 6168638

Browse files
committed
Merge pull request #7 from mwhelan/anonymous-values
File-based dictionaries with caching.
2 parents 4b9024d + f7f3b7f commit 6168638

115 files changed

Lines changed: 31517 additions & 659 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using NTestDataBuilder.DataSources;
4+
using NTestDataBuilder.DataSources.Geography;
5+
using NTestDataBuilder.DataSources.Person;
6+
using Shouldly;
7+
using Xunit.Extensions;
8+
9+
namespace NTestDataBuilder.Tests.DataSources
10+
{
11+
public class DataSourceConventionTests
12+
{
13+
[Theory]
14+
[PropertyData("TestCases")]
15+
public void DataSourceConventions(DataSource<string> sut, int expectedCount)
16+
{
17+
var collection = sut.Data.ToList();
18+
collection.Count.ShouldBe(expectedCount);
19+
collection.Count.ShouldBe(sut.Data.Distinct().ToList().Count);
20+
collection.ForEach(item => item.ShouldNotBeNullOrEmpty());
21+
}
22+
23+
public static IEnumerable<object[]> TestCases
24+
{
25+
get
26+
{
27+
yield return new object[] { new GeoContinentSource(), 7 };
28+
yield return new object[] { new GeoCountrySource(), 249 };
29+
yield return new object[] { new GeoCountryCodeSource(), 64 };
30+
yield return new object[] { new GeoLatitudeSource(), 1000 };
31+
yield return new object[] { new GeoLongitudeSource(), 1000 };
32+
33+
yield return new object[] { new PersonEmailAddressSource(), 1000 };
34+
yield return new object[] { new PersonLanguageSource(), 97 };
35+
yield return new object[] { new PersonNameFirstFemaleSource(), 100 };
36+
yield return new object[] { new PersonNameFirstSource(), 479 };
37+
yield return new object[] { new PersonNameFullSource(), 1000 };
38+
yield return new object[] { new PersonNameLastSource(), 747 };
39+
yield return new object[] { new PersonNameFirstMaleSource(), 100 };
40+
yield return new object[] { new PersonNameSuffixSource(), 5 };
41+
yield return new object[] { new PersonNameTitleSource(), 9 };
42+
}
43+
}
44+
}
45+
}

NTestDataBuilder.Tests/DataSources/DataSourceTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void WhenCreatingADataSource_ThenListShouldContainAllTheValues()
1414
{
1515
var sut = new DummyDataSource();
1616

17-
var result = sut.List;
17+
var result = sut.Data;
1818

1919
result.Count.ShouldBe(3);
2020
result.ShouldBe(new List<string> { "Value 1", "Value 2", "Value 3" });
@@ -25,7 +25,7 @@ public void WhenGeneratingValuesFromDefaultDataSource_ThenShouldGenerateItemFrom
2525
{
2626
var sut = new DummyDataSource();
2727
var result = sut.Next();
28-
sut.List.ShouldContain(result);
28+
sut.Data.ShouldContain(result);
2929
}
3030

3131
[Fact]
@@ -39,7 +39,7 @@ public void WhenGeneratingValuesFromDefaultDataSource_ThenShouldGenerateRandomIt
3939
results.Add(sut.Next());
4040
}
4141

42-
foreach (var item in sut.List)
42+
foreach (var item in sut.Data)
4343
{
4444
results.ShouldContain(item);
4545
}
@@ -50,20 +50,20 @@ public void WhenGeneratingValuesFromSequentialDataSource_ThenShouldGenerateSeque
5050
{
5151
var sut = new DummyDataSource(new SequentialGenerator());
5252

53-
sut.Next().ShouldBe(sut.List[0]);
54-
sut.Next().ShouldBe(sut.List[1]);
55-
sut.Next().ShouldBe(sut.List[2]);
56-
sut.Next().ShouldBe(sut.List[0]);
53+
sut.Next().ShouldBe(sut.Data[0]);
54+
sut.Next().ShouldBe(sut.Data[1]);
55+
sut.Next().ShouldBe(sut.Data[2]);
56+
sut.Next().ShouldBe(sut.Data[0]);
5757
}
5858

5959
[Fact]
6060
public void WhenGeneratingValuesFromUniqueSequentialDataSource_ThenShouldThrowWhenCollectionExceeded()
6161
{
6262
var sut = new DummyDataSource(new SequentialGenerator(0,1,true));
6363

64-
sut.Next().ShouldBe(sut.List[0]);
65-
sut.Next().ShouldBe(sut.List[1]);
66-
sut.Next().ShouldBe(sut.List[2]);
64+
sut.Next().ShouldBe(sut.Data[0]);
65+
sut.Next().ShouldBe(sut.Data[1]);
66+
sut.Next().ShouldBe(sut.Data[2]);
6767
Should.Throw<InvalidOperationException>(() => sut.Next());
6868
}
6969
}
@@ -76,7 +76,7 @@ public DummyDataSource(IGenerator generator)
7676
public DummyDataSource()
7777
: this(new RandomGenerator()) { }
7878

79-
protected override IList<string> InitializeList()
79+
protected override IList<string> InitializeDataSource()
8080
{
8181
return new List<string>{"Value 1", "Value 2", "Value 3"};
8282
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using NTestDataBuilder.DataSources.Dictionaries;
4+
using Shouldly;
5+
using Xunit;
6+
7+
namespace NTestDataBuilder.Tests.DataSources.Dictionaries
8+
{
9+
public class CacheTests : IDisposable
10+
{
11+
public CacheTests()
12+
{
13+
Cache.Clear();
14+
}
15+
16+
[Fact]
17+
public void WhenRequestingAnItemNotInTheCache_ThenReturnsNull()
18+
{
19+
Cache.Get("SomethingThatDoesNotExist").ShouldBe(null);
20+
}
21+
22+
[Fact]
23+
public void WhenAddingAnItemToTheCache_ThenTheItemIsPersistedInTheCache()
24+
{
25+
var collection = new List<string>{"item1", "item2"};
26+
Cache.Set("myCollection", collection);
27+
Cache.Get("myCollection").ShouldBeSameAs(collection);
28+
}
29+
30+
[Fact]
31+
public void WhenAddingAnItemToTheCacheWithTheSameKeyAsAnExistingItem_ThenTheOriginalItemIsReplacedWithTheNewItem()
32+
{
33+
var collection = new List<string> { "item1", "item2" };
34+
var collection2 = new List<string> { "item3" };
35+
Cache.Set("myCollection", collection);
36+
37+
Cache.Set("myCollection", collection2);
38+
39+
Cache.Get("myCollection").ShouldBeSameAs(collection2);
40+
}
41+
42+
[Fact]
43+
public void WhenCheckingCacheContainesAnItemThatIsInCache_ThenShouldReturnTrue()
44+
{
45+
var collection = new List<string> {"item1", "item2"};
46+
Cache.Set("myCollection", collection);
47+
Cache.Contains("myCollection").ShouldBe(true);
48+
}
49+
50+
[Fact]
51+
public void WhenCheckingCacheContainesAnItemThatIsNotInCache_ThenShouldReturnFalse()
52+
{
53+
Cache.Contains("SomethingThatDoesNotExist").ShouldBe(false);
54+
}
55+
56+
57+
public void Dispose()
58+
{
59+
Cache.Clear();
60+
}
61+
}
62+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.IO;
2+
using NTestDataBuilder.DataSources.Dictionaries;
3+
using Shouldly;
4+
using Xunit;
5+
6+
namespace NTestDataBuilder.Tests.DataSources.Dictionaries
7+
{
8+
public class FileDictionaryRepositoryIntegrationTests
9+
{
10+
[Fact]
11+
public void GivenAnExternalFileDictionaryExists_WhenRetrievingWords_ThenExternalDictionaryIsUsed()
12+
{
13+
var sut = new CachedFileDictionaryRepository();
14+
15+
var result = sut.GetWordsFrom("SampleDictionaryFile");
16+
17+
result.Count.ShouldBe(2);
18+
result[0].ShouldBe("File first word");
19+
}
20+
21+
[Fact]
22+
public void GivenAResourceDictionaryAndNoExternalFileDictionary_WhenRetrievingWords_ThenResourceDictionaryIsUsed()
23+
{
24+
var sut = new CachedFileDictionaryRepository();
25+
26+
var result = sut.GetWordsFrom("GeoContinent");
27+
28+
result.Count.ShouldBe(7);
29+
result[0].ShouldBe("Asia");
30+
}
31+
32+
[Fact]
33+
public void GivenNoResourceDictionaryAndNoExternalFileDictionary_WhenRetrievingWords_ThenFileNotFoundException()
34+
{
35+
var sut = new CachedFileDictionaryRepository();
36+
Should.Throw<FileNotFoundException>(() => sut.GetWordsFrom("NonExistentDictionary"));
37+
}
38+
39+
}
40+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using NSubstitute;
2+
using NTestDataBuilder.DataSources.Dictionaries;
3+
using NTestDataBuilder.DataSources.Generators;
4+
using Xunit;
5+
6+
namespace NTestDataBuilder.Tests.DataSources.Dictionaries
7+
{
8+
public class FileDictionarySourceTests
9+
{
10+
[Fact]
11+
public void WhenInitializingList_ThenPassClassNameToRepositoryAsDictionaryName()
12+
{
13+
var repository = Substitute.For<IDictionaryRepository>();
14+
var sut = new DummySource(repository);
15+
16+
var result = sut.Data;
17+
18+
repository.Received().GetWordsFrom("Dummy");
19+
}
20+
}
21+
22+
public class DummySource : FileDictionarySource
23+
{
24+
internal DummySource(IDictionaryRepository repository)
25+
: base(Substitute.For<IGenerator>(), repository) { }
26+
}
27+
}
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: 0 additions & 40 deletions
This file was deleted.

0 commit comments

Comments
 (0)