Skip to content

Commit e4577ba

Browse files
committed
Clear Trail before counting (#659)
1 parent 34deb5f commit e4577ba

File tree

4 files changed

+42
-3
lines changed

4 files changed

+42
-3
lines changed

src/YesSql.Core/Services/DefaultQuery.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,7 @@ public async Task<int> CountAsync(CancellationToken cancellationToken = default)
11321132
// Clear paging and order when counting
11331133
localBuilder.ClearOrder();
11341134
localBuilder.ClearGroupBy();
1135+
localBuilder.ClearTrail();
11351136
localBuilder.Skip(null);
11361137
localBuilder.Take(null);
11371138

src/YesSql.Core/Sql/SqlBuilder.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
43
using YesSql.Utils;
54

65
namespace YesSql.Sql
@@ -344,7 +343,7 @@ public virtual void Trail(string segment)
344343

345344
public virtual void ClearTrail()
346345
{
347-
TrailSegments.Clear();
346+
_trail?.Clear();
348347
}
349348

350349
public virtual string ToSqlString()
@@ -365,7 +364,7 @@ public virtual string ToSqlString()
365364
if (_order != null)
366365
{
367366
sb.Append("ON(");
368-
sb.Append(OrderSegments.First());
367+
sb.Append(OrderSegments[0]);
369368
sb.Append(") ");
370369
}
371370
}

test/YesSql.Tests/CoreTests.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,42 @@ public async Task SavingRefreshedDocument()
10431043
await Assert.ThrowsAsync<InvalidOperationException>(() => session.SaveAsync(newBill1));
10441044
}
10451045

1046+
[Fact]
1047+
public async Task CountAsync_WhenCalledAfterListAsync_ReturnCorrectTotal()
1048+
{
1049+
_store.RegisterIndexes<PersonIndexProvider>();
1050+
1051+
await using (var session = _store.CreateSession())
1052+
{
1053+
var bill = new Person
1054+
{
1055+
Firstname = "Bill",
1056+
Lastname = "Gates"
1057+
};
1058+
1059+
var mike = new Person
1060+
{
1061+
Firstname = "Mike",
1062+
Lastname = "Alhayek"
1063+
};
1064+
1065+
await session.SaveAsync(bill);
1066+
await session.SaveAsync(mike);
1067+
1068+
await session.SaveChangesAsync();
1069+
1070+
var query = session.Query<Person, PersonByName>().OrderBy(x => x.DocumentId).Skip(1).Take(1);
1071+
1072+
var records = await query.ListAsync();
1073+
1074+
Assert.Equal(2, await query.CountAsync());
1075+
1076+
Assert.Single(records);
1077+
1078+
Assert.Equal("Mike", records.First().Firstname);
1079+
}
1080+
}
1081+
10461082
[Fact]
10471083
public async Task ShouldUpdateAutoFlushedIndex()
10481084
{

test/YesSql.Tests/Indexes/PersonByName.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ namespace YesSql.Tests.Indexes
66
{
77
public class PersonByName : MapIndex
88
{
9+
public long DocumentId { get; set; }
10+
911
public string SomeName { get; set; }
12+
1013
public static string Normalize(string name)
1114
{
1215
return name.ToUpperInvariant();

0 commit comments

Comments
 (0)