Skip to content

Commit 9e12605

Browse files
authored
Fixing empty redis collection count bug (#74)
1 parent 972dad8 commit 9e12605

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

src/Redis.OM/Searching/RedisQueryProvider.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,20 @@ public async Task<IEnumerable<AggregationResult<T>>> ExecuteAggregationAsync<T>(
164164
public RedisReply ExecuteReductiveAggregation(MethodCallExpression expression, Type underpinningType)
165165
{
166166
var aggregation = ExpressionTranslator.BuildAggregationFromExpression(expression, underpinningType);
167-
var res = AggregationResult.FromRedisResult(Connection.Execute("FT.AGGREGATE", aggregation.Serialize()));
167+
var reply = Connection.Execute("FT.AGGREGATE", aggregation.Serialize());
168+
var res = AggregationResult.FromRedisResult(reply);
168169
var reductionName = ((Reduction)aggregation.Predicates.Last()).ResultName;
169-
return res.First()[reductionName];
170+
if (res.Any())
171+
{
172+
return res.First()[reductionName];
173+
}
174+
175+
if (reductionName == "COUNT")
176+
{
177+
return reply.ToArray().First();
178+
}
179+
180+
throw new Exception("Invalid value returned by server");
170181
}
171182

172183
/// <summary>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using Redis.OM.Modeling;
2+
3+
namespace Redis.OM.Unit.Tests.RediSearchTests
4+
{
5+
[Document(IndexName = "empty-index")]
6+
public class ClassForEmptyRedisCollection
7+
{
8+
[Indexed] public string TagField { get; set; }
9+
}
10+
}

test/Redis.OM.Unit.Tests/RediSearchTests/SearchFunctionalTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,15 @@ public void TestSetGetWithLoc()
261261
Assert.Equal(new GeoLoc(1.0,1.0), reconstituded.Home);
262262
}
263263

264+
[Fact]
265+
266+
public void TestCountWithEmptyCollection()
267+
{
268+
var collection = new RedisCollection<ClassForEmptyRedisCollection>(_connection);
269+
var count = collection.Count();
270+
Assert.Equal(0,count);
271+
}
272+
264273
[Fact]
265274
public async Task TestUpdate()
266275
{

test/Redis.OM.Unit.Tests/RedisSetupCollection.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Redis.OM.Contracts;
22
using System;
3+
using Redis.OM.Unit.Tests.RediSearchTests;
34
using Xunit;
45

56
namespace Redis.OM.Unit.Tests
@@ -15,7 +16,8 @@ public RedisSetup()
1516
{
1617
var personIndexExists = false;
1718
var hashPersonIndexExists = false;
18-
19+
var emptyIndexExists = false;
20+
1921
try
2022
{
2123
Connection.Execute("FT.INFO", "person-idx");
@@ -36,10 +38,23 @@ public RedisSetup()
3638
// ignored
3739
}
3840

41+
try
42+
{
43+
Connection.Execute("FT.INFO", "empty-index");
44+
emptyIndexExists = true;
45+
}
46+
catch
47+
{
48+
//ignored
49+
}
50+
3951
if(!personIndexExists)
4052
Connection.CreateIndex(typeof(RediSearchTests.Person));
4153
if (!hashPersonIndexExists)
4254
Connection.CreateIndex(typeof(RediSearchTests.HashPerson));
55+
if(!emptyIndexExists)
56+
Connection.CreateIndex(typeof(ClassForEmptyRedisCollection));
57+
4358
}
4459

4560
private IRedisConnection _connection = null;

0 commit comments

Comments
 (0)