Commit 39b1fd5a authored by Brian Drupieski's avatar Brian Drupieski Committed by Nick Craver

IEnumerable<T> check when updating and deleting

parent f29981a7
...@@ -221,7 +221,15 @@ public static partial class SqlMapperExtensions ...@@ -221,7 +221,15 @@ public static partial class SqlMapperExtensions
} }
else if (type.IsGenericType()) else if (type.IsGenericType())
{ {
type = type.GetGenericArguments()[0]; var typeInfo = type.GetTypeInfo();
bool implementsGenericIEnumerableOrIsGenericIEnumerable =
typeInfo.ImplementedInterfaces.Any(ti => ti.IsGenericType() && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ||
typeInfo.GetGenericTypeDefinition() == typeof(IEnumerable<>);
if (implementsGenericIEnumerableOrIsGenericIEnumerable)
{
type = type.GetGenericArguments()[0];
}
} }
var keyProperties = KeyPropertiesCache(type); var keyProperties = KeyPropertiesCache(type);
...@@ -282,7 +290,15 @@ public static partial class SqlMapperExtensions ...@@ -282,7 +290,15 @@ public static partial class SqlMapperExtensions
} }
else if (type.IsGenericType()) else if (type.IsGenericType())
{ {
type = type.GetGenericArguments()[0]; var typeInfo = type.GetTypeInfo();
bool implementsGenericIEnumerableOrIsGenericIEnumerable =
typeInfo.ImplementedInterfaces.Any(ti => ti.IsGenericType() && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ||
typeInfo.GetGenericTypeDefinition() == typeof(IEnumerable<>);
if (implementsGenericIEnumerableOrIsGenericIEnumerable)
{
type = type.GetGenericArguments()[0];
}
} }
var keyProperties = KeyPropertiesCache(type); var keyProperties = KeyPropertiesCache(type);
......
...@@ -415,7 +415,15 @@ private static string GetTableName(Type type) ...@@ -415,7 +415,15 @@ private static string GetTableName(Type type)
} }
else if (type.IsGenericType()) else if (type.IsGenericType())
{ {
type = type.GetGenericArguments()[0]; var typeInfo = type.GetTypeInfo();
bool implementsGenericIEnumerableOrIsGenericIEnumerable =
typeInfo.ImplementedInterfaces.Any(ti => ti.IsGenericType() && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ||
typeInfo.GetGenericTypeDefinition() == typeof(IEnumerable<>);
if (implementsGenericIEnumerableOrIsGenericIEnumerable)
{
type = type.GetGenericArguments()[0];
}
} }
var keyProperties = KeyPropertiesCache(type).ToList(); //added ToList() due to issue #418, must work on a list copy var keyProperties = KeyPropertiesCache(type).ToList(); //added ToList() due to issue #418, must work on a list copy
...@@ -476,7 +484,15 @@ private static string GetTableName(Type type) ...@@ -476,7 +484,15 @@ private static string GetTableName(Type type)
} }
else if (type.IsGenericType()) else if (type.IsGenericType())
{ {
type = type.GetGenericArguments()[0]; var typeInfo = type.GetTypeInfo();
bool implementsGenericIEnumerableOrIsGenericIEnumerable =
typeInfo.ImplementedInterfaces.Any(ti => ti.IsGenericType() && ti.GetGenericTypeDefinition() == typeof(IEnumerable<>)) ||
typeInfo.GetGenericTypeDefinition() == typeof(IEnumerable<>);
if (implementsGenericIEnumerableOrIsGenericIEnumerable)
{
type = type.GetGenericArguments()[0];
}
} }
var keyProperties = KeyPropertiesCache(type).ToList(); //added ToList() due to issue #418, must work on a list copy var keyProperties = KeyPropertiesCache(type).ToList(); //added ToList() due to issue #418, must work on a list copy
......
...@@ -46,6 +46,43 @@ public async Task TypeWithGenericParameterCanBeInsertedAsync() ...@@ -46,6 +46,43 @@ public async Task TypeWithGenericParameterCanBeInsertedAsync()
} }
} }
[Fact]
public async Task TypeWithGenericParameterCanBeUpdatedAsync()
{
using (var connection = GetOpenConnection())
{
var objectToInsert = new GenericType<string>
{
Id = Guid.NewGuid().ToString(),
Name = "something"
};
await connection.InsertAsync(objectToInsert);
objectToInsert.Name = "somethingelse";
await connection.UpdateAsync(objectToInsert);
var updatedObject = connection.Get<GenericType<string>>(objectToInsert.Id);
Assert.Equal(objectToInsert.Name, updatedObject.Name);
}
}
[Fact]
public async Task TypeWithGenericParameterCanBeDeletedAsync()
{
using (var connection = GetOpenConnection())
{
var objectToInsert = new GenericType<string>
{
Id = Guid.NewGuid().ToString(),
Name = "something"
};
await connection.InsertAsync(objectToInsert);
bool deleted = await connection.DeleteAsync(objectToInsert);
Assert.True(deleted);
}
}
/// <summary> /// <summary>
/// Tests for issue #351 /// Tests for issue #351
/// </summary> /// </summary>
...@@ -263,6 +300,12 @@ private async Task InsertHelperAsync<T>(Func<IEnumerable<User>, T> helper) ...@@ -263,6 +300,12 @@ private async Task InsertHelperAsync<T>(Func<IEnumerable<User>, T> helper)
} }
} }
[Fact]
public async Task UpdateEnumerableAsync()
{
await UpdateHelperAsync(src => src.AsEnumerable()).ConfigureAwait(false);
}
[Fact] [Fact]
public async Task UpdateArrayAsync() public async Task UpdateArrayAsync()
{ {
...@@ -302,6 +345,12 @@ private async Task UpdateHelperAsync<T>(Func<IEnumerable<User>, T> helper) ...@@ -302,6 +345,12 @@ private async Task UpdateHelperAsync<T>(Func<IEnumerable<User>, T> helper)
} }
} }
[Fact]
public async Task DeleteEnumerableAsync()
{
await DeleteHelperAsync(src => src.AsEnumerable()).ConfigureAwait(false);
}
[Fact] [Fact]
public async Task DeleteArrayAsync() public async Task DeleteArrayAsync()
{ {
......
...@@ -154,6 +154,43 @@ public void TypeWithGenericParameterCanBeInserted() ...@@ -154,6 +154,43 @@ public void TypeWithGenericParameterCanBeInserted()
} }
} }
[Fact]
public void TypeWithGenericParameterCanBeUpdated()
{
using (var connection = GetOpenConnection())
{
var objectToInsert = new GenericType<string>
{
Id = Guid.NewGuid().ToString(),
Name = "something"
};
connection.Insert(objectToInsert);
objectToInsert.Name = "somethingelse";
connection.Update(objectToInsert);
var updatedObject = connection.Get<GenericType<string>>(objectToInsert.Id);
Assert.Equal(objectToInsert.Name, updatedObject.Name);
}
}
[Fact]
public void TypeWithGenericParameterCanBeDeleted()
{
using (var connection = GetOpenConnection())
{
var objectToInsert = new GenericType<string>
{
Id = Guid.NewGuid().ToString(),
Name = "something"
};
connection.Insert(objectToInsert);
bool deleted = connection.Delete(objectToInsert);
Assert.True(deleted);
}
}
[Fact] [Fact]
public void Issue418() public void Issue418()
{ {
...@@ -362,6 +399,12 @@ private void InsertHelper<T>(Func<IEnumerable<User>, T> helper) ...@@ -362,6 +399,12 @@ private void InsertHelper<T>(Func<IEnumerable<User>, T> helper)
} }
} }
[Fact]
public void UpdateEnumerable()
{
UpdateHelper(src => src.AsEnumerable());
}
[Fact] [Fact]
public void UpdateArray() public void UpdateArray()
{ {
...@@ -401,6 +444,12 @@ private void UpdateHelper<T>(Func<IEnumerable<User>, T> helper) ...@@ -401,6 +444,12 @@ private void UpdateHelper<T>(Func<IEnumerable<User>, T> helper)
} }
} }
[Fact]
public void DeleteEnumerable()
{
DeleteHelper(src => src.AsEnumerable());
}
[Fact] [Fact]
public void DeleteArray() public void DeleteArray()
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment