Skip to content

Commit

Permalink
Fix #308 - Remove useless pages in table queries (#379)
Browse files Browse the repository at this point in the history
* Fix #308 - Remove useless pages in table queries

* Add some unit test to increase code coverage
  • Loading branch information
kbeaugrand authored Mar 4, 2022
1 parent 835112d commit cafa2d8
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,52 @@ public void When_Model_NotExists_Get_Should_Return_404()
this.mockRepository.VerifyAll();
}

[Test]
public void Get_Should_Return_Device_Model_Commands()
{
// Arrange
var deviceModel = SetupMockDeviceModel();
var deviceModelId = deviceModel.RowKey;
var deviceModelCommandsController = this.CreateDeviceModelCommandsController();
var mockResponse = this.mockRepository.Create<Response>();

this.mockCommandsTableClient.Setup(c => c.Query<TableEntity>(
It.Is<string>(x => x == $"PartitionKey eq '{ deviceModelId }'"),
It.IsAny<int?>(),
It.IsAny<IEnumerable<string>>(),
It.IsAny<CancellationToken>()))
.Returns(Pageable<TableEntity>.FromPages(new[]
{
Page<TableEntity>.FromValues(new[]
{
new TableEntity(deviceModelId, Guid.NewGuid().ToString())
}, null, mockResponse.Object)
}));

this.mockTableClientFactory.Setup(c => c.GetDeviceCommands())
.Returns(mockCommandsTableClient.Object);

this.mockDeviceModelCommandMapper.Setup(c => c.GetDeviceModelCommand(
It.Is<TableEntity>(x => x.PartitionKey == deviceModelId)))
.Returns(new DeviceModelCommand { });

// Act
var response = deviceModelCommandsController.Get(deviceModelId);

// Assert
Assert.IsNotNull(response);
Assert.IsAssignableFrom<OkObjectResult>(response.Result);

var okResult = (OkObjectResult)response.Result;

Assert.IsNotNull(okResult);
Assert.IsAssignableFrom<DeviceModelCommand[]>(okResult.Value);

var result = (DeviceModelCommand[])okResult.Value;
Assert.IsNotNull(result);
Assert.AreEqual(1, result.Length);
}

private TableEntity SetupMockDeviceModel()
{
var mockResponse = this.mockRepository.Create<Response<TableEntity>>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace AzureIoTHub.Portal.Server.Controllers.v10

[Route("/api/settings/device-tags")]
[ApiController]

[ApiVersion("1.0")]
[Produces("application/json")]
[ApiExplorerSettings(GroupName = "Portal Settings")]
Expand Down Expand Up @@ -62,17 +62,13 @@ public async Task<IActionResult> Post(List<DeviceTag> tags)
{
var query = this.tableClientFactory
.GetDeviceTagSettings()
.Query<TableEntity>()
.AsPages();
.Query<TableEntity>();

foreach (var page in query)
foreach (var item in query)
{
foreach (var item in page.Values)
{
await this.tableClientFactory
.GetDeviceTagSettings()
.DeleteEntityAsync(item.PartitionKey, item.RowKey);
}
await this.tableClientFactory
.GetDeviceTagSettings()
.DeleteEntityAsync(item.PartitionKey, item.RowKey);
}

foreach (DeviceTag tag in tags)
Expand All @@ -94,13 +90,13 @@ await this.tableClientFactory
[HttpGet(Name = "GET a set of device settings")]
public ActionResult<List<DeviceTag>> Get()
{
var query = this.tableClientFactory
.GetDeviceTagSettings()
.Query<TableEntity>();
var query = this.tableClientFactory
.GetDeviceTagSettings()
.Query<TableEntity>();

var tagList = query.Select(this.deviceTagMapper.GetDeviceTag);
var tagList = query.Select(this.deviceTagMapper.GetDeviceTag);

return this.Ok(tagList.ToList());
return this.Ok(tagList.ToList());
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public async Task<IActionResult> Post(string id, DeviceModelCommand[] commands)
{
try
{
var query = this.tableClientFactory
var templateQuery = this.tableClientFactory
.GetDeviceTemplates()
.GetEntity<TableEntity>(LoRaWANDeviceModelsController.DefaultPartitionKey, id);
}
Expand All @@ -83,19 +83,15 @@ public async Task<IActionResult> Post(string id, DeviceModelCommand[] commands)
throw;
}

var pages = this.tableClientFactory
var query = this.tableClientFactory
.GetDeviceCommands()
.Query<TableEntity>(filter: $"PartitionKey eq '{id}'")
.AsPages();
.Query<TableEntity>(filter: $"PartitionKey eq '{id}'");

foreach (var page in pages)
foreach (var item in query)
{
foreach (var item in page.Values)
{
await this.tableClientFactory
.GetDeviceCommands()
.DeleteEntityAsync(item.PartitionKey, item.RowKey);
}
await this.tableClientFactory
.GetDeviceCommands()
.DeleteEntityAsync(item.PartitionKey, item.RowKey);
}

foreach (var command in commands)
Expand Down Expand Up @@ -128,7 +124,7 @@ public ActionResult<DeviceModelCommand[]> Get(string id)
{
try
{
var query = this.tableClientFactory
var templateQuery = this.tableClientFactory
.GetDeviceTemplates()
.GetEntity<TableEntity>(LoRaWANDeviceModelsController.DefaultPartitionKey, id);
}
Expand All @@ -144,19 +140,19 @@ public ActionResult<DeviceModelCommand[]> Get(string id)
throw;
}

var pages = this.tableClientFactory
var query = this.tableClientFactory
.GetDeviceCommands()
.Query<TableEntity>(filter: $"PartitionKey eq '{id}'")
.AsPages();
.Query<TableEntity>(filter: $"PartitionKey eq '{id}'");

var commands = new List<DeviceModelCommand>();

foreach (var page in pages)
foreach (var item in query)
{
commands.AddRange(page.Values.Select(this.deviceModelCommandMapper.GetDeviceModelCommand));
var command = this.deviceModelCommandMapper.GetDeviceModelCommand(item);
commands.Add(command);
}

return this.Ok(commands);
return this.Ok(commands.ToArray());
}
}
}

0 comments on commit cafa2d8

Please sign in to comment.