// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.CommandLine;
using System.CommandLine.Parsing;
using System.Text.Json;
using System.Text.Json.Serialization;
using AzureMcp.Core.Models.Command;
using AzureMcp.MySql.Commands.Table;
using AzureMcp.MySql.Services;
using AzureMcp.Tests;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NSubstitute;
using NSubstitute.ExceptionExtensions;
using Xunit;
namespace AzureMcp.MySql.UnitTests.Table;
public class TableListCommandTests
{
private readonly IServiceProvider _serviceProvider;
private readonly IMySqlService _mysqlService;
private readonly ILogger<TableListCommand> _logger;
public TableListCommandTests()
{
_mysqlService = Substitute.For<IMySqlService>();
_logger = Substitute.For<ILogger<TableListCommand>>();
var collection = new ServiceCollection();
collection.AddSingleton(_mysqlService);
_serviceProvider = collection.BuildServiceProvider();
}
[Fact]
public async Task ExecuteAsync_ReturnsTables_WhenSuccessful()
{
var expectedTables = new List<string> { "users", "products", "orders" };
_mysqlService.GetTablesAsync("sub123", "rg1", "user1", "server1", "db1").Returns(expectedTables);
var command = new TableListCommand(_logger);
var args = command.GetCommand().Parse([
"--subscription", "sub123",
"--resource-group", "rg1",
"--user", "user1",
"--server", "server1",
"--database", "db1"
]);
var context = new CommandContext(_serviceProvider);
var response = await command.ExecuteAsync(context, args);
Assert.NotNull(response);
Assert.Equal(200, response.Status);
Assert.Equal("Success", response.Message);
Assert.NotNull(response.Results);
var json = JsonSerializer.Serialize(response.Results);
var result = JsonSerializer.Deserialize<TableListResult>(json);
Assert.NotNull(result);
Assert.Equal(expectedTables, result.Tables);
}
[Fact]
public async Task ExecuteAsync_ReturnsError_WhenServiceThrows()
{
_mysqlService.GetTablesAsync("sub123", "rg1", "user1", "server1", "db1")
.ThrowsAsync(new UnauthorizedAccessException("Access denied"));
var command = new TableListCommand(_logger);
var args = command.GetCommand().Parse([
"--subscription", "sub123",
"--resource-group", "rg1",
"--user", "user1",
"--server", "server1",
"--database", "db1"
]);
var context = new CommandContext(_serviceProvider);
var response = await command.ExecuteAsync(context, args);
Assert.NotNull(response);
Assert.Equal(500, response.Status);
Assert.Contains("Access denied", response.Message);
}
[Fact]
public void Metadata_IsConfiguredCorrectly()
{
var command = new TableListCommand(_logger);
Assert.Equal("list", command.Name);
Assert.Equal("Enumerates all tables within a specified database on an Azure Database for MySQL Flexible Server instance. This command provides a complete inventory of table objects, facilitating database exploration, schema analysis, and data architecture understanding for development tasks.", command.Description);
Assert.Equal("List MySQL Tables", command.Title);
Assert.False(command.Metadata.Destructive);
Assert.True(command.Metadata.ReadOnly);
}
private class TableListResult
{
[JsonPropertyName("Tables")]
public List<string> Tables { get; set; } = new List<string>();
}
}