Permalink
Browse files

Fixed retrieving non string values from an excel created using ExcelM…

…apper
  • Loading branch information...
shashankshetty committed Feb 15, 2010
1 parent 5e85410 commit 4e14cf44fa3e569dca8aa79220a7bb09fb9e2bf0
Binary file not shown.
@@ -7,6 +7,7 @@
using NUnit.Framework;
using Rhino.Mocks;
using StructureMap.AutoMocking;
+using ExcelMapper.Repository.Extensions;
namespace ExcelMapper.Tests.Repository
{
@@ -56,7 +57,7 @@ public void Should_get_the_values_from_cache_or_file()
}
[TestFixture]
- public class When_asked_to_SaveOrUpdate_an_entity_into_an_excel_file
+ public class When_asked_to_Save_an_entity_into_an_excel_file
{
private const string Testfile = "TestFile";
private IDataProvider _dataProvider;
@@ -89,12 +90,12 @@ public void SetUp()
}
[Test]
- public void Should_create_an_Excel_file_if_it_exists()
+ public void Should_create_an_Excel_file_if_it_doesnot_exists()
{
_fileService.Expect(x => x.Exists(Testfile)).Return(true);
_dataProvider.Expect(x => x.GetTableNames()).Return(new List<string>());
- _excelRepository.SaveOrUpdate(_users);
+ _excelRepository.Save(_users);
_fileService.AssertWasNotCalled(x => x.Create(Testfile));
}
@@ -104,27 +105,27 @@ public void Should_create_the_WorkSheet_if_it_doesnot_exist()
_fileService.Expect(x => x.Exists(Testfile)).Return(true);
_dataProvider.Expect(x => x.GetTableNames()).Return(new List<string>());
- _excelRepository.SaveOrUpdate(_users);
+ _excelRepository.Save(_users);
_dataProvider.AssertWasCalled(x => x.CreateTable<User>());
}
[Test]
public void Should_not_create_the_WorkSheet_if_it_exists()
{
_fileService.Expect(x => x.Exists(Testfile)).Return(true);
- _dataProvider.Expect(x => x.GetTableNames()).Return(new List<string> {typeof (User).Name});
+ _dataProvider.Expect(x => x.GetTableNames()).Return(new List<string> {typeof (User).Name.GetWorkSheetName()});
- _excelRepository.SaveOrUpdate(_users);
+ _excelRepository.Save(_users);
_dataProvider.AssertWasNotCalled(x => x.CreateTable<User>());
}
[Test]
public void Should_save_the_values_in_to_excel()
{
_fileService.Expect(x => x.Exists(Testfile)).Return(true);
- _dataProvider.Expect(x => x.GetTableNames()).Return(new List<string> {typeof (User).Name});
+ _dataProvider.Expect(x => x.GetTableNames()).Return(new List<string> { typeof(User).Name.GetWorkSheetName() });
- _excelRepository.SaveOrUpdate(_users);
+ _excelRepository.Save(_users);
_dataProvider.AssertWasCalled(x => x.Put(_users));
}
}
@@ -91,12 +91,13 @@
<Compile Include="Repository\Connection\IConnectionString.cs" />
<Compile Include="Repository\ExcelRepository.cs" />
<Compile Include="Repository\Extensions\DataReaderExtensions.cs" />
+ <Compile Include="Repository\Extensions\PropertyTypeExtensions.cs" />
<Compile Include="Repository\FileService.cs" />
<Compile Include="Repository\IDataProvider.cs" />
<Compile Include="Repository\IFileService.cs" />
<Compile Include="Repository\IRepository.cs" />
<Compile Include="Repository\OleDbDataProvider.cs" />
- <Compile Include="Repository\StringExtensions.cs" />
+ <Compile Include="Repository\Extensions\StringExtensions.cs" />
</ItemGroup>
<ItemGroup>
<WCFMetadata Include="Service References\" />
@@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Linq;
using ExcelMapper.Configuration;
+using ExcelMapper.Repository.Extensions;
using RunTimeCodeGenerator.ClassGeneration;
namespace ExcelMapper.Repository
@@ -55,14 +56,9 @@ public IEnumerable<T> Get<T>(string workSheet)
}
}
- public void Update<T>(IEnumerable<T> values)
+ public void Save<T>(IEnumerable<T> values)
{
- _dataProvider.Put(values);
- }
-
- public void SaveOrUpdate<T>(IEnumerable<T> values)
- {
- if (!_dataProvider.GetTableNames().Where(x => x == typeof (T).Name).Any())
+ if (!_dataProvider.GetTableNames().Where(x => x == typeof(T).Name.GetWorkSheetName()).Any())
{
_dataProvider.CreateTable<T>();
}
@@ -0,0 +1,13 @@
+using System;
+
+namespace ExcelMapper.Repository.Extensions
+{
+ public static class PropertyTypeExtensions
+ {
+ public static string GetPropertyType(this Type propertyType)
+ {
+ const string integerType = "Int";
+ return propertyType.Name.Contains(integerType) ? integerType : propertyType.Name;
+ }
+ }
+}
@@ -0,0 +1,17 @@
+using System;
+
+namespace ExcelMapper.Repository.Extensions
+{
+ public static class StringExtensions
+ {
+ public static string GetClassName(this string value)
+ {
+ return value.Replace("$", "");
+ }
+
+ public static string GetWorkSheetName(this string value)
+ {
+ return String.Format("{0}$", value);
+ }
+ }
+}
@@ -8,7 +8,6 @@ public interface IRepository
IEnumerable<string> GetWorkSheetNames();
ClassAttributes GetDTOClassAttributes(string workSheet);
IEnumerable<T> Get<T>(string workSheet);
- void Update<T>(IEnumerable<T> values);
- void SaveOrUpdate<T>(IEnumerable<T> values);
+ void Save<T>(IEnumerable<T> values);
}
}
@@ -7,6 +7,7 @@
using System.Text;
using ExcelMapper.Configuration;
using ExcelMapper.Repository.Connection;
+using ExcelMapper.Repository.Extensions;
using RunTimeCodeGenerator.ClassGeneration;
namespace ExcelMapper.Repository
@@ -77,7 +78,7 @@ public IEnumerable<T> Get<T>(string workSheet)
{
int index = i;
PropertyInfo property = properties.Single(p => p.Name.Equals(reader.GetName(index)));
- property.SetValue(instance, reader.GetValue(i), null);
+ property.SetValue(instance, Convert.ChangeType(reader.GetValue(i), property.PropertyType), null);
}
yield return instance;
}
@@ -143,13 +144,4 @@ private static string ConstructQueryValues(int length)
return query.ToString();
}
}
-
- public static class PropertyTypeExtensions
- {
- public static string GetPropertyType(this Type propertyType)
- {
- const string integerType = "Int";
- return propertyType.Name.Contains(integerType) ? integerType : propertyType.Name;
- }
- }
}
@@ -1,10 +0,0 @@
-namespace ExcelMapper.Repository
-{
- public static class StringExtensions
- {
- public static string GetClassName(this string value)
- {
- return value.Replace("$", "");
- }
- }
-}
@@ -59,14 +59,14 @@ public void WriteToExcel()
expectedDemoWorkSheet
};
- _repository.SaveOrUpdate(demoWorkSheets);
+ _repository.Save(demoWorkSheets);
- //var enumerable = _repository.Get<DemoWorkSheet>(typeof(DemoWorkSheet).Name);
- //DemoWorkSheet actualDemoWorkSheet = enumerable.Where(x => x.Id == expectedDemoWorkSheet.Id).First();
- //Assert.IsNotNull(actualDemoWorkSheet);
- //Assert.AreEqual(expectedDemoWorkSheet.Name, actualDemoWorkSheet.Name);
- //Assert.AreEqual(expectedDemoWorkSheet.StartDate, actualDemoWorkSheet.StartDate);
- //Assert.AreEqual(expectedDemoWorkSheet.StartValue, actualDemoWorkSheet.StartValue);
+ var enumerable = _repository.Get<DemoWorkSheet>(typeof(DemoWorkSheet).Name);
+ DemoWorkSheet actualDemoWorkSheet = enumerable.Where(x => x.Id == expectedDemoWorkSheet.Id).First();
+ Assert.IsNotNull(actualDemoWorkSheet);
+ Assert.AreEqual(expectedDemoWorkSheet.Name, actualDemoWorkSheet.Name);
+ Assert.AreEqual(expectedDemoWorkSheet.StartDate, actualDemoWorkSheet.StartDate);
+ Assert.AreEqual(expectedDemoWorkSheet.StartValue, actualDemoWorkSheet.StartValue);
}
[Test]
@@ -84,7 +84,7 @@ public void WriteToExistingExcel()
{
expectedUser
};
- _repository.Update(users);
+ _repository.Save(users);
User actualUser = _repository.Get<User>("User").Where(x => x.Id == expectedUser.Id).First();
Assert.IsNotNull(actualUser);

0 comments on commit 4e14cf4

Please sign in to comment.