Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Simpleverse.Repository.Db.Test/QueryBuilder/GuidTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,21 @@ public void TestGuid_NotIn()
$"WHERE [Guid] NOT IN ('{Value}','{Value}')",
Array.Empty<string>()
);

[Fact]
public void TestGuidNullable_In()
=> Test<Model>(
queryBuilder => queryBuilder.Where(x => x.GuidNullable, new List<Guid?>() { Value, Value }),
$"WHERE [GuidNullable] IN ('{Value}','{Value}')",
Array.Empty<string>()
);

[Fact]
public void TestGuidNullable_NotIn()
=> Test<Model>(
queryBuilder => queryBuilder.WhereNot(x => x.GuidNullable, new List<Guid?>() { Value, Value }),
$"WHERE [GuidNullable] NOT IN ('{Value}','{Value}')",
Array.Empty<string>()
);
}
}
67 changes: 67 additions & 0 deletions src/Simpleverse.Repository.Db.Test/SqlServer/Entity/EntityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,54 @@ public async Task UpsertAsync_WhenProvidedSqlRepositoryAndIdentityExists_Updates
Assert.NotNull(fetchedIdentity);
}
}

[Fact]
public async Task ListAsync_WhenFilteredByNullableGuidList_ReturnsMatchingRecords()
{
using (var profiler = Profile())
using (var connection = _fixture.GetProfiledConnection())
{
// arrange
connection.Open();
connection.Truncate<DataTypeNullable>();
var records = TestData.DataTypeNullableData(4).ToList();
connection.Insert(records);
var recordsWithGuid = records.Where(x => x.Guid.HasValue).ToList();
var guidFilter = recordsWithGuid.Select(x => x.Guid).ToList();
var entity = new DataTypeNullableEntity(_sqlRepository);

// act
var returned = (await entity.ListAsync(filter => filter.Guids = guidFilter)).ToList();

// assert
Assert.Equal(recordsWithGuid.Count, returned.Count);
Assert.All(returned, x => Assert.Contains(x.Guid, guidFilter));
}
}

[Fact]
public async Task ListAsync_WhenFilteredByNullableGuidListContainingNull_SkipsNullAndReturnsMatchingRecord()
{
using (var profiler = Profile())
using (var connection = _fixture.GetProfiledConnection())
{
// arrange
connection.Open();
connection.Truncate<DataTypeNullable>();
var records = TestData.DataTypeNullableData(4).ToList();
connection.Insert(records);
var recordWithGuid = records.First(x => x.Guid.HasValue);
var guidFilter = new List<Guid?> { recordWithGuid.Guid, null };
var entity = new DataTypeNullableEntity(_sqlRepository);

// act
var returned = (await entity.ListAsync(filter => filter.Guids = guidFilter)).ToList();

// assert
Assert.Single(returned);
Assert.Equal(recordWithGuid.Guid, returned[0].Guid);
}
}
}

public class IdentityEntity : Entity<Identity, IdentityQueryFilter, DbQueryOptions>
Expand Down Expand Up @@ -362,4 +410,23 @@ protected override IEnumerable<string> GetFilterConditions(IdentityDateOfBirth f
return changed;
}
}

public class DataTypeNullableFilter
{
public virtual IEnumerable<Guid?> Guids { get; set; }
}

public class DataTypeNullableEntity : Entity<DataTypeNullable, DataTypeNullableFilter, DbQueryOptions>
{
public DataTypeNullableEntity(DbRepository repository)
: base(repository, new Table<DataTypeNullable>("DT"))
{
}

protected override void Filter(QueryBuilder<DataTypeNullable> builder, DataTypeNullableFilter filter)
{
builder.Where(x => x.Guid, filter.Guids);
base.Filter(builder, filter);
}
}
}
17 changes: 13 additions & 4 deletions src/Simpleverse.Repository.Db/Selector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,19 @@ public Selector In<T>(IEnumerable<T> values, bool not = false)
{
if (values == null || !values.Any())
return this;
var valuesJoined = values.Join(',');
var tType = typeof(T);
if (tType.IsEnum)
valuesJoined = string.Join(',', values.Select(x => Enum.Format(tType, x, "d")));

var underlyingType = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);
var nonNullValues = values.Where(x => x != null);

string valuesJoined;
if (underlyingType == typeof(Guid))
valuesJoined = string.Join(',', nonNullValues.Select(x => $"'{x}'"));
else if (underlyingType == typeof(DateTime))
valuesJoined = string.Join(',', nonNullValues.Select(x => $"'{Convert.ToDateTime(x):yyyy-MM-ddTHH:mm:ss.fff}'"));
else if (underlyingType.IsEnum)
valuesJoined = string.Join(',', nonNullValues.Select(x => Enum.Format(underlyingType, x, "d")));
else
valuesJoined = nonNullValues.Join(',');

return In(valuesJoined, not);
}
Expand Down
Loading