Skip to content

Commit

Permalink
Add query by item test, fix query logic
Browse files Browse the repository at this point in the history
  • Loading branch information
timtay-microsoft committed Oct 10, 2022
1 parent e849e34 commit 5378b26
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
37 changes: 37 additions & 0 deletions e2e/test/iothub/service/QueryClientE2ETests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
Expand Down Expand Up @@ -113,6 +114,42 @@ public async Task TwinQuery_CustomPaginationWorks()
secondPageEnumerator.MoveNext().Should().BeFalse();
}

[LoggedTestMethod]
[Timeout(TestTimeoutMilliseconds)]
public async Task TwinQuery_IterateByItemWorksAcrossPages()
{
// arrange

using var serviceClient = new IotHubServiceClient(TestConfiguration.IotHub.ConnectionString);
using TestDevice testDevice1 = await TestDevice.GetTestDeviceAsync(Logger, _idPrefix);
using TestDevice testDevice2 = await TestDevice.GetTestDeviceAsync(Logger, _idPrefix);
using TestDevice testDevice3 = await TestDevice.GetTestDeviceAsync(Logger, _idPrefix);

string queryText = $"select * from devices where deviceId = '{testDevice1.Id}' OR deviceId = '{testDevice2.Id}' OR deviceId = '{testDevice3.Id}'";
QueryOptions queryOptions = new QueryOptions
{
PageSize = 1
};

// act

await WaitForDevicesToBeQueryableAsync(serviceClient.Query, queryText, 3);

QueryResponse<Twin> twinQuery = await serviceClient.Query.CreateAsync<Twin>(queryText, queryOptions);

// assert
List<string> returnedTwinDeviceIds = new();
while (await twinQuery.MoveNextAsync())
{
Twin queriedTwin = twinQuery.Current;
returnedTwinDeviceIds.Add(queriedTwin.DeviceId);
}

List<string> expectedDeviceIds = new List<string>() { testDevice1.Id, testDevice2.Id, testDevice3.Id };
returnedTwinDeviceIds.Count.Should().Be(3);
returnedTwinDeviceIds.Should().Contain(expectedDeviceIds);
}

[LoggedTestMethod]
[Timeout(TestTimeoutMilliseconds)]
public async Task JobQuery_QueryWorks()
Expand Down
4 changes: 2 additions & 2 deletions iothub/service/src/Query/QueryResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class QueryResponse<T>
private readonly JobType? _jobType;
private readonly JobStatus? _jobStatus;
private readonly int? _defaultPageSize;
private readonly IEnumerator<T> _items;
private IEnumerator<T> _items;

internal QueryResponse(
QueryClient client,
Expand Down Expand Up @@ -179,7 +179,7 @@ public async Task<bool> MoveNextAsync(QueryOptions queryOptions = default, Cance
.CreateAsync<T>(_originalQuery, queryOptionsClone, cancellationToken)
.ConfigureAwait(false);
CurrentPage = response.CurrentPage;
_items.Reset();
_items = response.CurrentPage.GetEnumerator();
_items.MoveNext();
Current = _items.Current;
ContinuationToken = response.ContinuationToken;
Expand Down

0 comments on commit 5378b26

Please sign in to comment.