Skip to content

Commit cf7f4ee

Browse files
committed
Added caching
1 parent 838dab3 commit cf7f4ee

9 files changed

Lines changed: 75 additions & 16 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace NTestDataBuilder.Tests.DataSources.Dictionaries
7+
{
8+
public class CacheTests
9+
{
10+
public CacheTests()
11+
{
12+
Cache.Clear()
13+
}
14+
}
15+
}

NTestDataBuilder.Tests/DataSources/Dictionaries/FileDictionaryRepositoryIntegrationTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class FileDictionaryRepositoryIntegrationTests
1010
[Fact]
1111
public void GivenAnExternalFileDictionaryExists_WhenRetrievingWords_ThenExternalDictionaryIsUsed()
1212
{
13-
var sut = new FileDictionaryRepository();
13+
var sut = new CachedFileDictionaryRepository();
1414

1515
var result = sut.GetWordsFrom("SampleDictionaryFile");
1616

@@ -21,7 +21,7 @@ public void GivenAnExternalFileDictionaryExists_WhenRetrievingWords_ThenExternal
2121
[Fact]
2222
public void GivenAResourceDictionaryAndNoExternalFileDictionary_WhenRetrievingWords_ThenResourceDictionaryIsUsed()
2323
{
24-
var sut = new FileDictionaryRepository();
24+
var sut = new CachedFileDictionaryRepository();
2525

2626
var result = sut.GetWordsFrom("GeoContinent");
2727

@@ -32,7 +32,7 @@ public void GivenAResourceDictionaryAndNoExternalFileDictionary_WhenRetrievingWo
3232
[Fact]
3333
public void GivenNoResourceDictionaryAndNoExternalFileDictionary_WhenRetrievingWords_ThenFileNotFoundException()
3434
{
35-
var sut = new FileDictionaryRepository();
35+
var sut = new CachedFileDictionaryRepository();
3636
Should.Throw<FileNotFoundException>(() => sut.GetWordsFrom("NonExistentDictionary"));
3737
}
3838

NTestDataBuilder.Tests/EquivalenceClasses/DictionaryEquivalenceClassesTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ public DictionaryEquivalenceClassesTests()
2121
[Theory]
2222
[PropertyData("TestCases")]
2323
public void WhenGettingAnyDictionaryData_ThenReturnRandomDictionaryDataWhichIsReasonablyUnique(DataSource<string> source,
24-
List<string> testCases, bool checkUniqueness = true)
24+
List<string> testCases)
2525
{
2626
foreach (var testCase in testCases)
2727
{
2828
testCase.ShouldBeOfType<string>();
2929
testCase.ShouldNotBeNullOrEmpty();
3030
source.Data.ShouldContain(testCase);
3131
}
32-
if (checkUniqueness)
32+
if (source.Data.Count > 15)
3333
{
3434
var unique = testCases.Distinct().Count();
3535
unique.ShouldBeGreaterThan(5);
@@ -40,6 +40,7 @@ public static IEnumerable<object[]> TestCases
4040
{
4141
get
4242
{
43+
yield return new object[] { new GeoContinentSource(), GenerateTestCasesForSut(Any.GeoContinent) };
4344
yield return new object[] { new GeoCountrySource(), GenerateTestCasesForSut(Any.GeoCountry) };
4445
}
4546
}

NTestDataBuilder.Tests/NTestDataBuilder.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
</ItemGroup>
5454
<ItemGroup>
5555
<Compile Include="DataSources\DataSourceTests.cs" />
56+
<Compile Include="DataSources\Dictionaries\CacheTests.cs" />
5657
<Compile Include="DataSources\Dictionaries\FileDictionaryRepositoryIntegrationTests.cs" />
5758
<Compile Include="DataSources\Dictionaries\FileDictionarySourceTests.cs" />
5859
<Compile Include="DataSources\Generators\RandomGeneratorTests.cs" />
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Collections.Generic;
2+
3+
namespace NTestDataBuilder.DataSources.Dictionaries
4+
{
5+
internal static class Cache
6+
{
7+
private static IDictionary<string, IList<string>> _cache = new Dictionary<string, IList<string>>();
8+
9+
internal static IList<string> Get(string dictionary)
10+
{
11+
return _cache.ContainsKey(dictionary) ? _cache[dictionary] : null;
12+
}
13+
14+
internal static void Set(string key, IList<string> items)
15+
{
16+
_cache[key] = items;
17+
}
18+
19+
public static bool Contains(string key)
20+
{
21+
return _cache.ContainsKey(key);
22+
}
23+
24+
public static void Clear()
25+
{
26+
_cache = new Dictionary<string, IList<string>>();
27+
}
28+
}
29+
}

NTestDataBuilder/DataSources/Dictionaries/FileDictionaryRepository.cs renamed to NTestDataBuilder/DataSources/Dictionaries/CachedFileDictionaryRepository.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,37 @@
1-
using System;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.IO;
3+
using System.Linq;
44
using System.Reflection;
55

66
namespace NTestDataBuilder.DataSources.Dictionaries
77
{
88
/// <summary>
9-
/// Retrieves words from dictionaries stored in files
9+
/// Retrieves words from dictionaries stored in files. First looks for external file that user might have created. If this does not exist then data is retrieved from embedded resource files.
1010
/// </summary>
11-
public class FileDictionaryRepository : IDictionaryRepository
11+
public class CachedFileDictionaryRepository : IDictionaryRepository
1212
{
1313
public IList<string> GetWordsFrom(string dictionary)
1414
{
15-
// let users override values with file in bin directory
15+
if (Cache.Contains(dictionary))
16+
{
17+
return Cache.Get(dictionary);
18+
}
19+
20+
var words = new List<string>();
21+
1622
var name = string.Format("{0}.txt", dictionary);
1723
if (File.Exists(name))
1824
{
19-
return File.ReadAllLines(name);
25+
words = File.ReadAllLines(name).ToList();
26+
}
27+
else
28+
{
29+
var resourceName = string.Format("NTestDataBuilder.DataSources.Dictionaries.Resources.{0}", name);
30+
words = GetWordsFromEmbeddedResource(GetType().Assembly, resourceName).ToList();
2031
}
2132

22-
// otherwise get data from embedded resource files
23-
var resourceName = string.Format("NTestDataBuilder.DataSources.Dictionaries.Resources.{0}", name);
24-
return GetWordsFromEmbeddedResource(GetType().Assembly, resourceName);
33+
Cache.Set(dictionary, words);
34+
return words;
2535
}
2636

2737
internal IList<string> GetWordsFromEmbeddedResource(Assembly assembly, string resourceName)

NTestDataBuilder/DataSources/Dictionaries/FileDictionarySource.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public abstract class FileDictionarySource : DataSource<string>
1212

1313
/// <inheritdoc />
1414
protected FileDictionarySource()
15-
: this(new RandomGenerator(), new FileDictionaryRepository()) { }
15+
: this(new RandomGenerator(), new CachedFileDictionaryRepository()) { }
1616

1717
/// <inheritdoc />
1818
public FileDictionarySource(IGenerator generator, IDictionaryRepository repository)

NTestDataBuilder/NTestDataBuilder.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,10 @@
5555
<ItemGroup>
5656
<Compile Include="AnonymousValueFixture.cs" />
5757
<Compile Include="DataSources\CitySource.cs" />
58+
<Compile Include="DataSources\Dictionaries\Cache.cs" />
5859
<Compile Include="DataSources\Dictionaries\IDictionaryRepository.cs" />
5960
<Compile Include="DataSources\CountySource.cs" />
60-
<Compile Include="DataSources\Dictionaries\FileDictionaryRepository.cs" />
61+
<Compile Include="DataSources\Dictionaries\CachedFileDictionaryRepository.cs" />
6162
<Compile Include="DataSources\Dictionaries\FileDictionarySource.cs" />
6263
<Compile Include="DataSources\Geography\GeoContinentSource.cs" />
6364
<Compile Include="DataSources\Geography\GeoCountrySource.cs" />

NTestDataBuilder/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
[assembly: AssemblyTrademark("")]
1515
[assembly: AssemblyCulture("")]
1616

17+
[assembly: InternalsVisibleTo("NTestDataBuilder.Tests")]
18+
1719
// Setting ComVisible to false makes the types in this assembly not visible
1820
// to COM components. If you need to access a type in this assembly from
1921
// COM, set the ComVisible attribute to true on that type.

0 commit comments

Comments
 (0)