Permalink
Browse files

Added better caching mechanism that is dependant on the file modified…

… date and time.
  • Loading branch information...
1 parent ddbeea3 commit 7c0c1f36125012a99f15c2ccd92ef16aaeeef981 @shashankshetty shashankshetty committed Nov 10, 2009
@@ -59,6 +59,10 @@
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\lib\Rhino.Mocks.dll</HintPath>
</Reference>
+ <Reference Include="StructureMap.AutoMocking, Version=2.5.3.0, Culture=neutral, PublicKeyToken=e60ad81abae3c223, processorArchitecture=MSIL">
+ <SpecificVersion>False</SpecificVersion>
+ <HintPath>..\..\lib\StructureMap.AutoMocking.dll</HintPath>
+ </Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
@@ -79,6 +83,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Repository\Connection\ConnectionStringTests.cs" />
<Compile Include="Repository\Connection\ConnectionTests.cs" />
+ <Compile Include="Repository\OleDBDataProviderTests.cs" />
<Compile Include="Repository\ExcelRepositoryTests.cs" />
<Compile Include="TestData.cs" />
</ItemGroup>
@@ -23,7 +23,7 @@ public void SetUp()
{
_file = TestData.UsersXlsx;
_connectionString = MockRepository.GenerateMock<IConnectionString>();
- _connectionBuilder = new global::ExcelMapper.Repository.Connection.ConnectionBuilder(_connectionString);
+ _connectionBuilder = new ConnectionBuilder(_connectionString);
}
[Test]
@@ -1,143 +1,59 @@
using System;
using System.Collections.Generic;
using System.Linq;
-using ExcelMapper.Configuration;
+
using ExcelMapper.Repository;
-using ExcelMapper.Repository.Connection;
using ExcelMapper.Tests.DTO;
+
using NUnit.Framework;
+
using Rhino.Mocks;
-using RunTimeCodeGenerator.ClassGeneration;
+
+using StructureMap.AutoMocking;
namespace ExcelMapper.Tests.Repository
{
- public class ExcelRepositoryTests
- {
- public class ExcelRepositoryTestsBase
- {
- private IConnectionString _connectionString;
- protected IRepository _excelRepository;
- protected IFileConfiguration _fileConfiguration;
-
- protected string _xlsxFile;
- protected string _xlsFile;
- protected string _workSheetName;
-
- [SetUp]
- public void SetUp()
- {
- _connectionString = MockRepository.GenerateMock<IConnectionString>();
- _fileConfiguration = MockRepository.GenerateMock<IFileConfiguration>();
- _excelRepository = new ExcelRepository(new ConnectionBuilder(_connectionString), _fileConfiguration);
-
- _xlsxFile = TestData.UsersXlsx;
- _xlsFile = TestData.UsersXls;
- _workSheetName = "User";
-
- _connectionString.Expect(x => x.Get(_xlsxFile)).Return(TestData.UsersXlsxConnectionString);
- _connectionString.Expect(x => x.Get(_xlsFile)).Return(TestData.UsersXlsConnectionString);
- }
- }
-
- [TestFixture]
- public class When_given_an_Excel_File : ExcelRepositoryTestsBase
- {
- [Test]
- public void Should_give_the_list_of_WorkSheets_in_the_excel()
- {
- _fileConfiguration.Expect(x => x.FileName).Return(_xlsxFile);
- List<string> workSheets = _excelRepository.GetWorkSheetNames().ToList();
- Assert.IsTrue(workSheets.Count == 3);
- Assert.IsTrue(workSheets.Exists(x => x.Equals(String.Format("{0}$", _workSheetName))));
- }
- }
-
- [TestFixture]
- public class When_given_a_WorkSheet : ExcelRepositoryTestsBase
- {
- [Test]
- public void Should_return_Class_Properties_object_that_includes_all_the_properties_in_the_class()
- {
- _fileConfiguration.Expect(x => x.FileName).Return(_xlsxFile);
- ClassAttributes classAttributes =
- _excelRepository.GetDTOClassAttributes(String.Format("{0}$", _workSheetName));
- Assert.IsNotNull(classAttributes);
- Assert.AreEqual(_workSheetName, classAttributes.Name);
- Assert.IsTrue(classAttributes.Properties.Count == 4);
- }
- }
-
- [TestFixture]
- public class When_given_a_excel_file_name_and_worksheet_name : ExcelRepositoryTestsBase
- {
- [Test]
- public void Should_map_the_excel_columns_to_the_given_dto_object()
- {
- _fileConfiguration.Expect(x => x.FileName).Return(_xlsxFile);
- User expectedUser = TestData.GetUsers(_xlsxFile, _workSheetName).FirstOrDefault();
- User actualUser = _excelRepository.Get<User>(_workSheetName).FirstOrDefault();
-
- Assert.IsNotNull(actualUser);
-
- Assert.AreEqual(expectedUser.Id, actualUser.Id);
- Assert.AreEqual(expectedUser.FirstName, actualUser.FirstName);
- Assert.AreEqual(expectedUser.DateOfBirth, actualUser.DateOfBirth);
- Assert.AreEqual(expectedUser.LastName, actualUser.LastName);
- }
-
- [Test]
- public void Should_return_dto_objects_for_each_row_in_the_Xlsx_worksheet()
- {
- _fileConfiguration.Expect(x => x.FileName).Return(_xlsxFile);
- List<User> expectedUsers = TestData.GetUsers(_xlsxFile, _workSheetName).ToList();
- List<User> actualUsers = _excelRepository.Get<User>(_workSheetName).ToList();
- Assert.IsNotNull(actualUsers);
- Assert.AreEqual(expectedUsers.Count, actualUsers.Count);
- }
-
- [Test]
- public void Should_return_dto_objects_for_each_row_in_the_Xls_worksheet()
- {
- _fileConfiguration.Expect(x => x.FileName).Return(_xlsFile);
- List<User> expectedUsers = TestData.GetUsers(_xlsFile, _workSheetName).ToList();
- List<User> actualUsers = _excelRepository.Get<User>(_workSheetName).ToList();
- Assert.IsNotNull(actualUsers);
- Assert.AreEqual(expectedUsers.Count, actualUsers.Count);
- }
-
- [Test]
- public void Should_insert_the_values_provided_in_the_list_of__dto_objects_in_to_the_Xls_worksheet()
- {
- _fileConfiguration.Expect(x => x.FileName).Return(_xlsFile);
- List<User> users = new List<User>
- {
- new User
- {
- Id = 5,
- FirstName = "John",
- LastName = "Doe",
- DateOfBirth = Convert.ToDateTime("1/1/2008")
- }
- };
- _excelRepository.Put(users);
- }
-
- [Test]
- public void Should_insert_the_values_provided_in_the_list_of__dto_objects_in_to_the_Xlsx_worksheet()
- {
- _fileConfiguration.Expect(x => x.FileName).Return(_xlsxFile);
- List<User> users = new List<User>
- {
- new User
- {
- Id = 5,
- FirstName = "John",
- LastName = "Doe",
- DateOfBirth = Convert.ToDateTime("1/1/2008")
- }
- };
- _excelRepository.Put(users);
- }
- }
- }
+ public class ExcelRepositoryTests
+ {
+ [TestFixture]
+ public class When_asked_to_retrieve_an_entity_from_an_Excel_file
+ {
+ private IDataProvider _dataProvider;
+ private DateTime _fileModifiedDate;
+ private IFileService _fileService;
+ private IRepository _repository;
+ private List<User> _users;
+
+ [SetUp]
+ public void SetUp()
+ {
+ _users = new List<User>
+ {
+ new User
+ {
+ Id = 1,
+ LastName = "LastName",
+ FirstName = "FirstName"
+ }
+ };
+ _fileModifiedDate = Convert.ToDateTime("1/1/2009");
+
+ var autoMocker = new RhinoAutoMocker<ExcelRepository>();
+ _fileService = autoMocker.Get<IFileService>();
+ _dataProvider = autoMocker.Get<IDataProvider>();
+ _repository = autoMocker.ClassUnderTest;
+
+ _fileService.Expect(x => x.GetLastModifiedDate("")).IgnoreArguments().Return(_fileModifiedDate).Repeat.Any();
+ _dataProvider.Expect(x => x.Get<User>("")).IgnoreArguments().Return(_users);
+ }
+
+ [Test]
+ public void Should_get_the_values_from_cache_or_file()
+ {
+ var result = _repository.Get<User>("").ToList();
+ result = _repository.Get<User>("").ToList();
+ Assert.AreEqual(_users.Count, result.Count);
+ }
+ }
+ }
}
@@ -0,0 +1,157 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+using ExcelMapper.Configuration;
+using ExcelMapper.Repository;
+using ExcelMapper.Repository.Connection;
+using ExcelMapper.Tests.DTO;
+
+using NUnit.Framework;
+
+using Rhino.Mocks;
+
+namespace ExcelMapper.Tests.Repository
+{
+ public class OleDBDataProviderTests
+ {
+ public class OleDBDataProviderTestsBase
+ {
+ private IConnectionString _connectionString;
+ protected IDataProvider _dataProvider;
+ protected IFileConfiguration _fileConfiguration;
+
+ protected string _workSheetName;
+ protected string _xlsFile;
+ protected string _xlsxFile;
+
+ [SetUp]
+ public void SetUp()
+ {
+ _connectionString = MockRepository.GenerateMock<IConnectionString>();
+ _fileConfiguration = MockRepository.GenerateMock<IFileConfiguration>();
+
+ _dataProvider = new OleDbDataProvider(new ConnectionBuilder(_connectionString), _fileConfiguration);
+
+ _xlsxFile = TestData.UsersXlsx;
+ _xlsFile = TestData.UsersXls;
+ _workSheetName = "User";
+
+ _connectionString.Expect(x => x.Get(_xlsxFile)).Return(TestData.UsersXlsxConnectionString);
+ _connectionString.Expect(x => x.Get(_xlsFile)).Return(TestData.UsersXlsConnectionString);
+ }
+ }
+
+ [TestFixture]
+ public class When_asked_for_the_list_of_worksheets_in_the_given_Excel_File : OleDBDataProviderTestsBase
+ {
+ [Test]
+ public void Should_return_a_list_of_WorkSheet_names()
+ {
+ _fileConfiguration.Expect(x => x.FileName).Return(_xlsxFile);
+ List<string> workSheets = _dataProvider.GetTableNames().ToList();
+ Assert.IsTrue(workSheets.Count == 3);
+ Assert.IsTrue(workSheets.Exists(x => x.Equals(String.Format("{0}$", _workSheetName))));
+ }
+ }
+
+ [TestFixture]
+ public class When_asked_to_get_a_list_of_DTOs_for_the_given_type_from_Excel : OleDBDataProviderTestsBase
+ {
+ [Test]
+ public void Should_map_the_excel_columns_to_the_given_DTO_type()
+ {
+ _fileConfiguration.Expect(x => x.FileName).Return(_xlsxFile);
+ User expectedUser = TestData.GetUsers(_xlsxFile, _workSheetName).FirstOrDefault();
+ User actualUser = _dataProvider.Get<User>(_workSheetName).FirstOrDefault();
+
+ Assert.IsNotNull(actualUser);
+
+ Assert.AreEqual(expectedUser.Id, actualUser.Id);
+ Assert.AreEqual(expectedUser.FirstName, actualUser.FirstName);
+ Assert.AreEqual(expectedUser.DateOfBirth, actualUser.DateOfBirth);
+ Assert.AreEqual(expectedUser.LastName, actualUser.LastName);
+ }
+
+ [Test]
+ public void Should_return_DTO_objects_for_each_row_in_the_Xls_worksheet()
+ {
+ _fileConfiguration.Expect(x => x.FileName).Return(_xlsFile);
+ List<User> expectedUsers = TestData.GetUsers(_xlsFile, _workSheetName).ToList();
+ List<User> actualUsers = _dataProvider.Get<User>(_workSheetName).ToList();
+ Assert.IsNotNull(actualUsers);
+ Assert.AreEqual(expectedUsers.Count, actualUsers.Count);
+ }
+
+ [Test]
+ public void Should_return_DTO_objects_for_each_row_in_the_Xlsx_worksheet()
+ {
+ _fileConfiguration.Expect(x => x.FileName).Return(_xlsxFile);
+ List<User> expectedUsers = TestData.GetUsers(_xlsxFile, _workSheetName).ToList();
+ List<User> actualUsers = _dataProvider.Get<User>(_workSheetName).ToList();
+ Assert.IsNotNull(actualUsers);
+ Assert.AreEqual(expectedUsers.Count, actualUsers.Count);
+ }
+ }
+
+ [TestFixture]
+ public class When_asked_to_get_the_column_headers_for_a_given_WorkSheet : OleDBDataProviderTestsBase
+ {
+ [Test]
+ public void Should_return_all_the_column_headers_with_type_in_the_worksheet()
+ {
+ _fileConfiguration.Expect(x => x.FileName).Return(_xlsxFile);
+ var columns = _dataProvider.GetColumns(String.Format("{0}$", _workSheetName)).ToList();
+ Assert.IsFalse(columns.Any(x => x.Name == null));
+ Assert.IsFalse(columns.Any(x => x.Type == null));
+ }
+
+ [Test]
+ public void Should_return_all_the_column_in_the_worksheet()
+ {
+ _fileConfiguration.Expect(x => x.FileName).Return(_xlsxFile);
+ var columns = _dataProvider.GetColumns(String.Format("{0}$", _workSheetName)).ToList();
+ Assert.IsNotNull(columns);
+ Assert.IsTrue(columns.Count == 4);
+ }
+ }
+
+ [TestFixture]
+ public class When_asked_to_insert_a_list_of_DTOs_into_an_Excel : OleDBDataProviderTestsBase
+ {
+ [Test]
+ public void Should_insert_the_values_in_to_the_Xls_worksheet()
+ {
+ _fileConfiguration.Expect(x => x.FileName).Return(_xlsFile);
+ List<User> users = new List<User>
+ {
+ new User
+ {
+ Id = 5,
+ FirstName = "John",
+ LastName = "Doe",
+ DateOfBirth = Convert.ToDateTime("1/1/2008")
+ }
+ };
+ _dataProvider.Put(users);
+ }
+
+ [Test]
+ public void Should_insert_the_values_in_to_the_Xlsx_worksheet()
+ {
+ _fileConfiguration.Expect(x => x.FileName).Return(_xlsxFile);
+ List<User> users = new List<User>
+ {
+ new User
+ {
+ Id = 5,
+ FirstName = "John",
+ LastName = "Doe",
+ DateOfBirth = Convert.ToDateTime("1/1/2008")
+ }
+ };
+ _dataProvider.Put(users);
+ }
+ }
+ }
+}
@@ -8,6 +8,8 @@ public class ExcelMapperRegistry : Registry
{
public ExcelMapperRegistry()
{
+ ForRequestedType<IDataProvider>()
+ .TheDefaultIsConcreteType<OleDbDataProvider>();
ForRequestedType<IRepository>()
.TheDefaultIsConcreteType<ExcelRepository>();
ForRequestedType<IFileConfiguration>()
Oops, something went wrong. Retry.

0 comments on commit 7c0c1f3

Please sign in to comment.