From 2ce333e26becf6d930ddebf54021c4d629c37cc6 Mon Sep 17 00:00:00 2001 From: tr00d Date: Thu, 26 Nov 2026 10:46:58 +0100 Subject: [PATCH 1/3] feat(storage): list the tables of an Iceberg namespace --- .../Common/IcebergNamespacePathTests.cs | 25 -------------- .../Common/IcebergNamespaceTests.cs | 21 ++++++++++++ .../Storage.Tests/Common/IcebergTableTests.cs | 23 +++++++++++++ .../Storage/Common/IcebergNamespace.cs | 2 ++ .../Storage/Common/IcebergNamespacePath.cs | 7 ---- .../Storage/Storage/Common/IcebergTable.cs | 2 ++ .../Storage/Storage/Common/QueryString.cs | 11 ++++++ packages/Storage/Storage/IcebergCatalog.cs | 19 +++++++---- .../Storage/Interfaces/IIcebergCatalog.cs | 27 +++++++++++++++ .../ListNamespaces/ListNamespacesQuery.cs | 12 +++---- .../Storage/ListTables/IcebergTablePage.cs | 17 ++++++++++ .../Storage/ListTables/ListTablesOptions.cs | 14 ++++++++ .../Storage/ListTables/ListTablesQuery.cs | 20 +++++++++++ .../Storage/ListTables/ListTablesResponse.cs | 34 +++++++++++++++++++ 14 files changed, 189 insertions(+), 45 deletions(-) delete mode 100644 packages/Storage/Storage.Tests/Common/IcebergNamespacePathTests.cs delete mode 100644 packages/Storage/Storage/Common/IcebergNamespacePath.cs create mode 100644 packages/Storage/Storage/Common/QueryString.cs create mode 100644 packages/Storage/Storage/ListTables/IcebergTablePage.cs create mode 100644 packages/Storage/Storage/ListTables/ListTablesOptions.cs create mode 100644 packages/Storage/Storage/ListTables/ListTablesQuery.cs create mode 100644 packages/Storage/Storage/ListTables/ListTablesResponse.cs diff --git a/packages/Storage/Storage.Tests/Common/IcebergNamespacePathTests.cs b/packages/Storage/Storage.Tests/Common/IcebergNamespacePathTests.cs deleted file mode 100644 index b178374c..00000000 --- a/packages/Storage/Storage.Tests/Common/IcebergNamespacePathTests.cs +++ /dev/null @@ -1,25 +0,0 @@ -using FluentAssertions; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using Supabase.Storage.Common; - -namespace Storage.Tests.Common; - -/// -/// Unit tests for how a namespace is written into a request path. -/// -[TestClass] -[TestCategory("Unit")] -public class IcebergNamespacePathTests -{ - [TestMethod] - public void For_ShouldEscapeAndJoinLevelsWithTheEncodedUnitSeparator() => - IcebergNamespacePath.For(IcebergNamespace.Of("a b", "c/d")).Should().Be("a%20b%1Fc%2Fd"); - - [TestMethod] - public void For_ShouldAcceptLevelsContainingDots() => - IcebergNamespacePath.For(IcebergNamespace.Of("a..b")).Should().Be("a..b"); - - [TestMethod] - public void For_ShouldAcceptDotLevelsInAMultiLevelNamespace() => - IcebergNamespacePath.For(IcebergNamespace.Of("..", "a")).Should().Be("..%1Fa", "the joined segment is no longer a relative one"); -} diff --git a/packages/Storage/Storage.Tests/Common/IcebergNamespaceTests.cs b/packages/Storage/Storage.Tests/Common/IcebergNamespaceTests.cs index b0072871..20f0279a 100644 --- a/packages/Storage/Storage.Tests/Common/IcebergNamespaceTests.cs +++ b/packages/Storage/Storage.Tests/Common/IcebergNamespaceTests.cs @@ -79,4 +79,25 @@ public void GetHashCode_ShouldDiffer_GivenOtherLevels() => [TestMethod] public void ToString_ShouldJoinLevelsWithDots() => IcebergNamespace.Of("sales", "europe").ToString().Should().Be("sales.europe"); + + [TestMethod] + public void Path_ShouldEscapeAndJoinLevelsWithTheEncodedUnitSeparator() => + IcebergNamespace.Of("a b", "c/d").Path.Should().Be("namespaces/a%20b%1Fc%2Fd", "the Iceberg REST spec joins levels with the 0x1F separator"); + + [TestMethod] + public void Path_ShouldAcceptLevelsContainingDots() => + IcebergNamespace.Of("a..b").Path.Should().Be("namespaces/a..b"); + + [TestMethod] + public void Path_ShouldAcceptDotLevelsInAMultiLevelNamespace() => + IcebergNamespace.Of("..", "a").Path.Should().Be("namespaces/..%1Fa", "the joined segment is no longer a relative one"); + + [TestMethod] + [DataRow(".")] + [DataRow("..")] + public void Path_ShouldThrowArgumentException_GivenRelativeSegmentNamespace(string level) => + FluentActions.Invoking(() => IcebergNamespace.Of(level).Path) + .Should().Throw("a dot-segment namespace would reach another route") + .WithMessage("A namespace cannot be a relative path segment.*") + .Which.ParamName.Should().Be("namespace"); } diff --git a/packages/Storage/Storage.Tests/Common/IcebergTableTests.cs b/packages/Storage/Storage.Tests/Common/IcebergTableTests.cs index 9b53692e..05a43d31 100644 --- a/packages/Storage/Storage.Tests/Common/IcebergTableTests.cs +++ b/packages/Storage/Storage.Tests/Common/IcebergTableTests.cs @@ -43,4 +43,27 @@ public void Equals_ShouldBeTrue_GivenSameNamespaceAndName() => [TestMethod] public void Equals_ShouldBeFalse_GivenDifferentName() => IcebergTable.Of(IcebergNamespace.Of("sales"), "events").Should().NotBe(IcebergTable.Of(IcebergNamespace.Of("sales"), "orders")); + + [TestMethod] + public void Path_ShouldNestTheEscapedNameUnderTheTablesOfItsNamespace() => + IcebergTable.Of(IcebergNamespace.Of("sales", "europe"), "a/b?c#d").Path + .Should().Be("namespaces/sales%1Feurope/tables/a%2Fb%3Fc%23d", "a name must not start a path segment, query or fragment"); + + [TestMethod] + [DataRow("")] + [DataRow(" ")] + public void Path_ShouldThrowArgumentException_GivenBlankName(string name) => + FluentActions.Invoking(() => IcebergTable.Of(IcebergNamespace.Of("sales"), name).Path) + .Should().Throw("a blank name would reach the route of the namespace's tables") + .WithMessage("A table name must be provided.*") + .Which.ParamName.Should().Be("table"); + + [TestMethod] + [DataRow(".")] + [DataRow("..")] + public void Path_ShouldThrowArgumentException_GivenRelativeSegmentName(string name) => + FluentActions.Invoking(() => IcebergTable.Of(IcebergNamespace.Of("sales"), name).Path) + .Should().Throw("a dot-segment name would reach another route") + .WithMessage("A table name cannot be a relative path segment.*") + .Which.ParamName.Should().Be("table"); } diff --git a/packages/Storage/Storage/Common/IcebergNamespace.cs b/packages/Storage/Storage/Common/IcebergNamespace.cs index a43de2fa..d4166fe1 100644 --- a/packages/Storage/Storage/Common/IcebergNamespace.cs +++ b/packages/Storage/Storage/Common/IcebergNamespace.cs @@ -52,6 +52,8 @@ public override int GetHashCode() => internal string Joined() => string.Join(LevelSeparator.ToString(), this.Levels); + internal string Path => $"namespaces/{PathSegment.Escape(this.Joined(), "A namespace", "namespace")}"; + private static void RejectMissing(string[]? levels) { if (levels is null) diff --git a/packages/Storage/Storage/Common/IcebergNamespacePath.cs b/packages/Storage/Storage/Common/IcebergNamespacePath.cs deleted file mode 100644 index ff23e776..00000000 --- a/packages/Storage/Storage/Common/IcebergNamespacePath.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace Supabase.Storage.Common; - -internal static class IcebergNamespacePath -{ - internal static string For(IcebergNamespace @namespace) => - PathSegment.Escape(@namespace.Joined(), "A namespace", nameof(@namespace)); -} diff --git a/packages/Storage/Storage/Common/IcebergTable.cs b/packages/Storage/Storage/Common/IcebergTable.cs index cf52d71e..7670004d 100644 --- a/packages/Storage/Storage/Common/IcebergTable.cs +++ b/packages/Storage/Storage/Common/IcebergTable.cs @@ -36,4 +36,6 @@ public static IcebergTable Of(IcebergNamespace @namespace, string name) => /// public override string ToString() => $"{this.Namespace}.{this.Name}"; + + internal string Path => $"{this.Namespace.Path}/tables/{PathSegment.Escape(this.Name, "A table name", "table")}"; } diff --git a/packages/Storage/Storage/Common/QueryString.cs b/packages/Storage/Storage/Common/QueryString.cs new file mode 100644 index 00000000..d4fceee9 --- /dev/null +++ b/packages/Storage/Storage/Common/QueryString.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; + +namespace Supabase.Storage.Common; + +internal static class QueryString +{ + internal static string Append(string url, IEnumerable parameters) => $"{url}?{string.Join("&", parameters)}"; + + internal static string Parameter(string name, string value) => $"{name}={Uri.EscapeDataString(value)}"; +} diff --git a/packages/Storage/Storage/IcebergCatalog.cs b/packages/Storage/Storage/IcebergCatalog.cs index d80c9895..482106ba 100644 --- a/packages/Storage/Storage/IcebergCatalog.cs +++ b/packages/Storage/Storage/IcebergCatalog.cs @@ -9,6 +9,7 @@ using Supabase.Storage.Exceptions; using Supabase.Storage.Interfaces; using Supabase.Storage.ListNamespaces; +using Supabase.Storage.ListTables; namespace Supabase.Storage; @@ -35,7 +36,7 @@ public async Task NamespaceExistsAsync(IcebergNamespace @namespace, Cancel RequireArgument(@namespace, nameof(@namespace)); try { - using var response = await Helpers.MakeRequestAsync(this.requestClient, this.options, HttpMethod.Head, this.NamespaceUrl(@namespace), null, this.headers(), cancellationToken).ConfigureAwait(false); + using var response = await Helpers.MakeRequestAsync(this.requestClient, this.options, HttpMethod.Head, this.ResourceUrl(@namespace.Path), null, this.headers(), cancellationToken).ConfigureAwait(false); return true; } catch (SupabaseStorageException exception) when (IsNotFound(exception)) @@ -64,7 +65,7 @@ public async Task ListNamespacesAsync(ListNamespacesOption public Task DeleteNamespaceAsync(IcebergNamespace @namespace, CancellationToken cancellationToken = default) { RequireArgument(@namespace, nameof(@namespace)); - return Helpers.MakeRequestAsync(this.requestClient, this.options, HttpMethod.Delete, this.NamespaceUrl(@namespace), null, this.headers(), cancellationToken); + return Helpers.MakeRequestAsync(this.requestClient, this.options, HttpMethod.Delete, this.ResourceUrl(@namespace.Path), null, this.headers(), cancellationToken); } /// @@ -72,14 +73,20 @@ public async Task CreateTableAsync(IcebergTable table, Ice { RequireArgument(table, nameof(table)); RequireArgument(schema, nameof(schema)); - var response = await Helpers.MakeRequestAsync(this.requestClient, this.options, HttpMethod.Post, this.TablesUrl(table.Namespace), CreateTableBody.For(table, schema, options), this.headers(), cancellationToken).ConfigureAwait(false); + var response = await Helpers.MakeRequestAsync(this.requestClient, this.options, HttpMethod.Post, this.ResourceUrl($"{table.Namespace.Path}/tables"), CreateTableBody.For(table, schema, options), this.headers(), cancellationToken).ConfigureAwait(false); return response!.ToMetadata(); } - private string TablesUrl(IcebergNamespace @namespace) => $"{this.NamespaceUrl(@namespace)}/tables"; + /// + public async Task ListTablesAsync(IcebergNamespace @namespace, ListTablesOptions? options = null, CancellationToken cancellationToken = default) + { + RequireArgument(@namespace, nameof(@namespace)); + var url = ListTablesQuery.For(this.ResourceUrl($"{@namespace.Path}/tables"), options); + var response = await Helpers.MakeRequestAsync(this.requestClient, this.options, HttpMethod.Get, url, null, this.headers(), cancellationToken).ConfigureAwait(false); + return response!.ToPage(@namespace); + } - private string NamespaceUrl(IcebergNamespace @namespace) => - $"{this.catalogUrl}/namespaces/{IcebergNamespacePath.For(@namespace)}"; + private string ResourceUrl(string path) => $"{this.catalogUrl}/{path}"; private static bool IsNotFound(SupabaseStorageException exception) => exception.StatusCode == NotFoundStatus; diff --git a/packages/Storage/Storage/Interfaces/IIcebergCatalog.cs b/packages/Storage/Storage/Interfaces/IIcebergCatalog.cs index f22a8796..bd736359 100644 --- a/packages/Storage/Storage/Interfaces/IIcebergCatalog.cs +++ b/packages/Storage/Storage/Interfaces/IIcebergCatalog.cs @@ -5,6 +5,7 @@ using Supabase.Storage.CreateNamespace; using Supabase.Storage.CreateTable; using Supabase.Storage.ListNamespaces; +using Supabase.Storage.ListTables; namespace Supabase.Storage.Interfaces; @@ -143,4 +144,30 @@ public interface IIcebergCatalog /// [ImplementedCapability("storage.analytics.create_table")] Task CreateTableAsync(IcebergTable table, IcebergSchema schema, CreateTableOptions? options = null, CancellationToken cancellationToken = default); + + /// + /// Lists the tables of a namespace, one page at a time. Storage applies the page size as a limit and ignores the + /// page token today: it returns the first tables in creation order, without a next-page token. + /// + /// The namespace whose tables to list. + /// Page size and page token, following the Iceberg REST Catalog spec. When null, none is sent. + /// Token used to cancel the request. + /// One page of tables. Empty when the namespace holds none. + /// is null. + /// The namespace is a relative path segment. + /// + /// The service refused the call, for example because the namespace or its bucket does not exist (status 404, + /// Code NoSuchNamespaceException or NoSuchCatalog), the page size is negative (status 400) or + /// the caller is not authorized. + /// + /// + /// + /// var catalog = storage.Analytics.From("analytics-data"); + /// var page = await catalog.ListTablesAsync(IcebergNamespace.Of("sales")); + /// foreach (var table in page.Tables) + /// Console.WriteLine(table.Name); + /// + /// + [ImplementedCapability("storage.analytics.list_tables")] + Task ListTablesAsync(IcebergNamespace @namespace, ListTablesOptions? options = null, CancellationToken cancellationToken = default); } diff --git a/packages/Storage/Storage/ListNamespaces/ListNamespacesQuery.cs b/packages/Storage/Storage/ListNamespaces/ListNamespacesQuery.cs index acc7b418..d92cc4f2 100644 --- a/packages/Storage/Storage/ListNamespaces/ListNamespacesQuery.cs +++ b/packages/Storage/Storage/ListNamespaces/ListNamespacesQuery.cs @@ -1,25 +1,23 @@ -using System; using System.Collections.Generic; using System.Globalization; +using Supabase.Storage.Common; namespace Supabase.Storage.ListNamespaces; internal static class ListNamespacesQuery { internal static string For(string namespacesUrl, ListNamespacesOptions? options) => - $"{namespacesUrl}?{string.Join("&", ParametersOf(options ?? new ListNamespacesOptions()))}"; + QueryString.Append(namespacesUrl, ParametersOf(options ?? new ListNamespacesOptions())); private static IEnumerable ParametersOf(ListNamespacesOptions options) { if (options.Parent is { } parent) - yield return Parameter("parent", parent.Joined()); + yield return QueryString.Parameter("parent", parent.Joined()); if (options.PageToken is { } pageToken) - yield return Parameter("pageToken", pageToken); + yield return QueryString.Parameter("pageToken", pageToken); if (options.PageSize is { } pageSize) - yield return Parameter("pageSize", pageSize.ToString(CultureInfo.InvariantCulture)); + yield return QueryString.Parameter("pageSize", pageSize.ToString(CultureInfo.InvariantCulture)); } - - private static string Parameter(string name, string value) => $"{name}={Uri.EscapeDataString(value)}"; } diff --git a/packages/Storage/Storage/ListTables/IcebergTablePage.cs b/packages/Storage/Storage/ListTables/IcebergTablePage.cs new file mode 100644 index 00000000..0a3d775a --- /dev/null +++ b/packages/Storage/Storage/ListTables/IcebergTablePage.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using Supabase.Storage.Common; + +namespace Supabase.Storage.ListTables; + +/// +/// One page of the tables of a namespace. +/// +public sealed record IcebergTablePage +{ + /// The tables of this page, in the order the service listed them. + public IReadOnlyList Tables { get; init; } = Array.Empty(); + + /// The token that requests the next page, or null when this page is the last. + public string? NextPageToken { get; init; } +} diff --git a/packages/Storage/Storage/ListTables/ListTablesOptions.cs b/packages/Storage/Storage/ListTables/ListTablesOptions.cs new file mode 100644 index 00000000..12677343 --- /dev/null +++ b/packages/Storage/Storage/ListTables/ListTablesOptions.cs @@ -0,0 +1,14 @@ +namespace Supabase.Storage.ListTables; + +/// +/// Options for listing the tables of a namespace. An option left unset is not sent. They follow the Iceberg REST +/// Catalog spec; Storage applies as a limit and ignores today. +/// +public sealed record ListTablesOptions +{ + /// The maximum number of tables to return in one page. + public int? PageSize { get; init; } + + /// The of the previous page, to read the next one. + public string? PageToken { get; init; } +} diff --git a/packages/Storage/Storage/ListTables/ListTablesQuery.cs b/packages/Storage/Storage/ListTables/ListTablesQuery.cs new file mode 100644 index 00000000..5ccb0395 --- /dev/null +++ b/packages/Storage/Storage/ListTables/ListTablesQuery.cs @@ -0,0 +1,20 @@ +using System.Collections.Generic; +using System.Globalization; +using Supabase.Storage.Common; + +namespace Supabase.Storage.ListTables; + +internal static class ListTablesQuery +{ + internal static string For(string tablesUrl, ListTablesOptions? options) => + QueryString.Append(tablesUrl, ParametersOf(options ?? new ListTablesOptions())); + + private static IEnumerable ParametersOf(ListTablesOptions options) + { + if (options.PageToken is { } pageToken) + yield return QueryString.Parameter("pageToken", pageToken); + + if (options.PageSize is { } pageSize) + yield return QueryString.Parameter("pageSize", pageSize.ToString(CultureInfo.InvariantCulture)); + } +} diff --git a/packages/Storage/Storage/ListTables/ListTablesResponse.cs b/packages/Storage/Storage/ListTables/ListTablesResponse.cs new file mode 100644 index 00000000..5189a053 --- /dev/null +++ b/packages/Storage/Storage/ListTables/ListTablesResponse.cs @@ -0,0 +1,34 @@ +using System; +using System.Linq; +using System.Text.Json.Serialization; +using Supabase.Storage.Common; + +namespace Supabase.Storage.ListTables; + +internal sealed class ListTablesResponse +{ + [JsonPropertyName("identifiers")] + public IdentifierBody[]? Identifiers { get; set; } + + [JsonPropertyName("next-page-token")] + public string? NextPageToken { get; set; } + + internal IcebergTablePage ToPage(IcebergNamespace listed) => new() + { + Tables = (this.Identifiers ?? Array.Empty()).Select(identifier => identifier.ToTable(listed)).ToList(), + NextPageToken = this.NextPageToken, + }; + + internal sealed class IdentifierBody + { + [JsonPropertyName("namespace")] + public string[]? Namespace { get; set; } + + [JsonRequired] + [JsonPropertyName("name")] + public string Name { get; set; } = null!; + + internal IcebergTable ToTable(IcebergNamespace listed) => + IcebergTable.Of(this.Namespace is { Length: > 0 } levels ? IcebergNamespace.Of(levels) : listed, this.Name); + } +} From a2dba1a227f8fffc1f76bfbbafe940a1ee2a7f2b Mon Sep 17 00:00:00 2001 From: tr00d Date: Thu, 26 Nov 2026 10:52:03 +0100 Subject: [PATCH 2/3] test(storage): cover listing the tables of an Iceberg namespace --- .../ListTables/ListTablesContractTests.cs | 217 ++++++++++++++++++ .../ListTables/ListTablesTests.cs | 48 ++++ .../Storage/ListTables/ListTablesResponse.cs | 2 +- 3 files changed, 266 insertions(+), 1 deletion(-) create mode 100644 packages/Storage/Storage.Tests/ListTables/ListTablesContractTests.cs create mode 100644 packages/Storage/Storage.Tests/ListTables/ListTablesTests.cs diff --git a/packages/Storage/Storage.Tests/ListTables/ListTablesContractTests.cs b/packages/Storage/Storage.Tests/ListTables/ListTablesContractTests.cs new file mode 100644 index 00000000..53b263b4 --- /dev/null +++ b/packages/Storage/Storage.Tests/ListTables/ListTablesContractTests.cs @@ -0,0 +1,217 @@ +using System; +using System.Collections.Generic; +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; +using FluentAssertions; +using FluentAssertions.Execution; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Storage.Tests.Support; +using Supabase.Storage; +using Supabase.Storage.Common; +using Supabase.Storage.Exceptions; +using Supabase.Storage.Interfaces; +using Supabase.Storage.ListTables; +using WireMock.RequestBuilders; +using WireMock.ResponseBuilders; +using WireMock.Server; + +namespace Storage.Tests.ListTables; + +/// +/// Contract tests for listing the tables of a namespace: the GET request the catalog puts on the wire (method, path, +/// namespace encoding, paging query, headers, no body), how the page in the response is mapped, and how each failure +/// the Iceberg route can give is surfaced. Failure bodies are pinned from the storage route source, not observed on a +/// running service. +/// +[TestClass] +[TestCategory("Contract")] +public class ListTablesContractTests +{ + private const string TablesPath = "/storage/v1/iceberg/v1/analytics-data/namespaces/sales/tables"; + private const string Listed = "{\"identifiers\":[{\"namespace\":[\"sales\"],\"name\":\"events\"},{\"namespace\":[\"sales\"],\"name\":\"orders\"}]}"; + private static readonly IcebergNamespace Sales = IcebergNamespace.Of("sales"); + private WireMockServer server = null!; + private Client client = null!; + + [TestInitialize] + public void TestInitialize() + { + this.server = WireMockServer.Start(); + this.client = new Client($"{this.server.Url}/storage/v1", new Dictionary + { + { "Authorization", "Bearer service-key" } + }); + } + + [TestCleanup] + public void TestCleanup() => this.server.Stop(); + + [TestMethod] + public async Task ListTablesAsync_ShouldSendBodylessGetToTheTablesRouteOfTheNamespace() + { + this.Respond(TablesPath, 200, Listed); + await this.Catalog().ListTablesAsync(Sales); + var request = this.server.LogEntries.Should().ContainSingle().Which.RequestMessage!; + using (new AssertionScope()) + { + request.Method.Should().Be("GET", "the Iceberg route that lists tables is GET /:prefix/namespaces/:namespace/tables"); + request.Path.Should().Be(TablesPath, "the bucket is the Iceberg warehouse, sent as the path prefix"); + request.Body.Should().BeNullOrEmpty(); + request.Headers!["Authorization"].Should().ContainSingle().Which.Should().Be("Bearer service-key"); + } + } + + [TestMethod] + public async Task ListTablesAsync_ShouldSendNoPagingParameter_GivenNoOptions() + { + this.Respond(TablesPath, 200, Listed); + await this.Catalog().ListTablesAsync(Sales); + this.server.LogEntries.Should().ContainSingle().Which.RequestMessage!.Query + .Should().BeNullOrEmpty("an option left unset is not sent"); + } + + [TestMethod] + public async Task ListTablesAsync_ShouldSendPageSizeAndToken_GivenOptions() + { + this.Respond(TablesPath, 200, Listed); + await this.Catalog().ListTablesAsync(Sales, new ListTablesOptions { PageSize = 2, PageToken = "next/page token" }); + var query = this.server.LogEntries.Should().ContainSingle().Which.RequestMessage!.Query!; + using (new AssertionScope()) + { + query["pageSize"].Should().Equal(new[] { "2" }, "the Iceberg REST spec names it pageSize"); + query["pageToken"].Should().Equal(new[] { "next/page token" }, "the Iceberg REST spec names it pageToken"); + } + } + + [TestMethod] + public async Task ListTablesAsync_ShouldJoinNamespaceLevelsWithTheUnitSeparator() + { + this.Respond("/storage/v1/iceberg/v1/*", 200, Listed); + await this.Catalog().ListTablesAsync(IcebergNamespace.Of("sales", "europe")); + this.server.LogEntries.Should().ContainSingle().Which.RequestMessage!.Url + .Should().Contain("/namespaces/sales%1Feurope/tables", "the Iceberg REST spec joins levels with the 0x1F separator"); + } + + [TestMethod] + public async Task ListTablesAsync_ShouldReturnTheListedTablesInOrder() + { + this.Respond(TablesPath, 200, Listed); + (await this.Catalog().ListTablesAsync(Sales)).Tables + .Should().Equal(Sales.Table("events"), Sales.Table("orders")); + } + + [TestMethod] + public async Task ListTablesAsync_ShouldReturnTheNamespaceTheServiceSent() + { + this.Respond(TablesPath, 200, "{\"identifiers\":[{\"namespace\":[\"sales\",\"europe\"],\"name\":\"events\"}]}"); + (await this.Catalog().ListTablesAsync(Sales)).Tables + .Should().ContainSingle().Which.Should().Be(IcebergNamespace.Of("sales", "europe").Table("events")); + } + + [TestMethod] + [DataRow("{\"identifiers\":[{\"name\":\"events\"}]}")] + [DataRow("{\"identifiers\":[{\"namespace\":[],\"name\":\"events\"}]}")] + public async Task ListTablesAsync_ShouldReturnTheListedNamespace_GivenIdentifierWithoutNamespace(string body) + { + this.Respond(TablesPath, 200, body); + (await this.Catalog().ListTablesAsync(Sales)).Tables + .Should().ContainSingle().Which.Should().Be(Sales.Table("events")); + } + + [TestMethod] + public async Task ListTablesAsync_ShouldReturnNoTable_GivenResponseOmitsIdentifiers() + { + this.Respond(TablesPath, 200, "{}"); + (await this.Catalog().ListTablesAsync(Sales)).Tables.Should().BeEmpty(); + } + + [TestMethod] + public async Task ListTablesAsync_ShouldReturnTheNextPageToken() + { + this.Respond(TablesPath, 200, "{\"identifiers\":[],\"next-page-token\":\"page-2\"}"); + (await this.Catalog().ListTablesAsync(Sales)).NextPageToken.Should().Be("page-2"); + } + + [TestMethod] + public async Task ListTablesAsync_ShouldReturnNoNextPageToken_GivenLastPage() + { + this.Respond(TablesPath, 200, Listed); + (await this.Catalog().ListTablesAsync(Sales)).NextPageToken.Should().BeNull("Storage never sends one today"); + } + + [TestMethod] + [DataRow("Namespace not found", "NoSuchNamespaceException")] + [DataRow("Catalog name \"analytics-data\" not found", "NoSuchCatalog")] + public async Task ListTablesAsync_ShouldThrowNotFound_GivenMissingNamespaceOrBucket(string message, string code) + { + this.Respond(TablesPath, 404, Envelope(message, code, 404)); + var act = () => this.Catalog().ListTablesAsync(Sales); + var exception = (await act.Should().ThrowAsync()).Which; + using (new AssertionScope()) + { + exception.Reason.Should().Be(FailureHint.Reason.NotFound); + exception.Code.Should().Be(code); + } + } + + [TestMethod] + public async Task ListTablesAsync_ShouldThrow_GivenNegativePageSize() + { + this.Respond(TablesPath, 400, Envelope("querystring/pageSize must be >= 0", "InvalidRequest", 400)); + var act = () => this.Catalog().ListTablesAsync(Sales, new ListTablesOptions { PageSize = -1 }); + (await act.Should().ThrowAsync("the service, not the SDK, validates the page size")) + .Which.StatusCode.Should().Be(400); + } + + [TestMethod] + public async Task ListTablesAsync_ShouldThrow_GivenForbidden() + { + this.Respond(TablesPath, 403, Envelope("Access denied: Invalid role", "AccessDenied", 403)); + var act = () => this.Catalog().ListTablesAsync(Sales); + var exception = (await act.Should().ThrowAsync()).Which; + using (new AssertionScope()) + { + exception.StatusCode.Should().Be(403, "only the service role may use the catalog"); + exception.Code.Should().Be("AccessDenied"); + } + } + + [TestMethod] + public async Task ListTablesAsync_ShouldReadHeadersAtCallTime() + { + this.Respond(TablesPath, 200, Listed); + var catalog = this.Catalog(); + this.client.GetHeaders = () => new Dictionary { { "apikey", "rotated" } }; + await catalog.ListTablesAsync(Sales); + this.server.LogEntries.Should().ContainSingle().Which.RequestMessage!.Headers!["apikey"] + .Should().ContainSingle().Which.Should().Be("rotated"); + } + + [TestMethod] + public async Task ListTablesAsync_ShouldHonorCancellation() + { + this.Respond(TablesPath, 200, Listed); + using var cts = new CancellationTokenSource(); + cts.Cancel(); + var act = () => this.Catalog().ListTablesAsync(Sales, null, cts.Token); + await act.Should().ThrowAsync("the token must reach the HTTP call"); + } + + [TestMethod] + public void ListTablesAsync_ShouldNotResumeOnTheCallersContext() + { + this.Respond(TablesPath, 200, Listed); + BlockingCaller.Completes(() => this.Catalog().ListTablesAsync(Sales)) + .Should().BeTrue("the SDK never resumes on the caller's synchronization context"); + } + + private static string Envelope(string message, string type, int code) => + $"{{\"error\":{{\"message\":{JsonSerializer.Serialize(message)},\"type\":\"{type}\",\"code\":{code}}}}}"; + + private IIcebergCatalog Catalog() => this.client.Analytics.From("analytics-data"); + + private void Respond(string path, int status, string body) => + this.server.Given(Request.Create().WithPath(path).UsingGet()) + .RespondWith(Response.Create().WithStatusCode(status).WithHeader("Content-Type", "application/json").WithBody(body)); +} diff --git a/packages/Storage/Storage.Tests/ListTables/ListTablesTests.cs b/packages/Storage/Storage.Tests/ListTables/ListTablesTests.cs new file mode 100644 index 00000000..54e4978f --- /dev/null +++ b/packages/Storage/Storage.Tests/ListTables/ListTablesTests.cs @@ -0,0 +1,48 @@ +using System; +using System.Threading.Tasks; +using FluentAssertions; +using FluentAssertions.Execution; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Supabase.Storage; +using Supabase.Storage.Common; +using Supabase.Storage.ListTables; + +namespace Storage.Tests.ListTables; + +/// +/// Unit tests for the checks made before the wire when listing the tables of a namespace, and for the defaults of its +/// options and page. +/// +[TestClass] +[TestCategory("Unit")] +public class ListTablesTests +{ + private readonly Client sut = new("http://localhost:1/storage/v1"); + + [TestMethod] + public async Task ListTablesAsync_ShouldThrowArgumentNullException_GivenNoNamespace() => + (await FluentActions.Awaiting(() => this.sut.Analytics.From("analytics-data").ListTablesAsync(null!)) + .Should().ThrowAsync("there is no namespace to list")) + .Which.ParamName.Should().Be("namespace"); + + [TestMethod] + public async Task ListTablesAsync_ShouldThrowArgumentException_GivenRelativeSegmentNamespace() => + await FluentActions.Awaiting(() => this.sut.Analytics.From("analytics-data").ListTablesAsync(IcebergNamespace.Of(".."))) + .Should().ThrowAsync("a dot-segment namespace would reach another route") + .WithMessage("A namespace cannot be a relative path segment.*"); + + [TestMethod] + public void ListTablesOptions_ShouldSendNothing_GivenNoneSet() + { + var options = new ListTablesOptions(); + using (new AssertionScope()) + { + options.PageSize.Should().BeNull("the service owns the default page size"); + options.PageToken.Should().BeNull("the first page needs no token"); + } + } + + [TestMethod] + public void IcebergTablePage_ShouldHaveNoTable_GivenNoneSet() => + new IcebergTablePage().Tables.Should().BeEmpty(); +} diff --git a/packages/Storage/Storage/ListTables/ListTablesResponse.cs b/packages/Storage/Storage/ListTables/ListTablesResponse.cs index 5189a053..d49cc06d 100644 --- a/packages/Storage/Storage/ListTables/ListTablesResponse.cs +++ b/packages/Storage/Storage/ListTables/ListTablesResponse.cs @@ -29,6 +29,6 @@ internal sealed class IdentifierBody public string Name { get; set; } = null!; internal IcebergTable ToTable(IcebergNamespace listed) => - IcebergTable.Of(this.Namespace is { Length: > 0 } levels ? IcebergNamespace.Of(levels) : listed, this.Name); + (this.Namespace is { Length: > 0 } levels ? IcebergNamespace.Of(levels) : listed).Table(this.Name); } } From 6a1efbd3e75cc044aa2a6ffea37d87d4f526d33d Mon Sep 17 00:00:00 2001 From: tr00d Date: Thu, 26 Nov 2026 11:14:42 +0100 Subject: [PATCH 3/3] feat(storage): declare the public surface of listing Iceberg tables --- .../Storage/Storage/PublicAPI.Unshipped.txt | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/packages/Storage/Storage/PublicAPI.Unshipped.txt b/packages/Storage/Storage/PublicAPI.Unshipped.txt index b31f32cb..4e1011e1 100644 --- a/packages/Storage/Storage/PublicAPI.Unshipped.txt +++ b/packages/Storage/Storage/PublicAPI.Unshipped.txt @@ -98,6 +98,12 @@ override Supabase.Storage.ListNamespaces.IcebergNamespacePage.ToString() -> stri override Supabase.Storage.ListNamespaces.ListNamespacesOptions.Equals(object? obj) -> bool override Supabase.Storage.ListNamespaces.ListNamespacesOptions.GetHashCode() -> int override Supabase.Storage.ListNamespaces.ListNamespacesOptions.ToString() -> string! +override Supabase.Storage.ListTables.IcebergTablePage.Equals(object? obj) -> bool +override Supabase.Storage.ListTables.IcebergTablePage.GetHashCode() -> int +override Supabase.Storage.ListTables.IcebergTablePage.ToString() -> string! +override Supabase.Storage.ListTables.ListTablesOptions.Equals(object? obj) -> bool +override Supabase.Storage.ListTables.ListTablesOptions.GetHashCode() -> int +override Supabase.Storage.ListTables.ListTablesOptions.ToString() -> string! override Supabase.Storage.ListVectorBuckets.ListVectorBucketsOptions.Equals(object? obj) -> bool override Supabase.Storage.ListVectorBuckets.ListVectorBucketsOptions.GetHashCode() -> int override Supabase.Storage.ListVectorBuckets.ListVectorBucketsOptions.ToString() -> string! @@ -218,6 +224,10 @@ static Supabase.Storage.ListNamespaces.IcebergNamespacePage.operator !=(Supabase static Supabase.Storage.ListNamespaces.IcebergNamespacePage.operator ==(Supabase.Storage.ListNamespaces.IcebergNamespacePage? left, Supabase.Storage.ListNamespaces.IcebergNamespacePage? right) -> bool static Supabase.Storage.ListNamespaces.ListNamespacesOptions.operator !=(Supabase.Storage.ListNamespaces.ListNamespacesOptions? left, Supabase.Storage.ListNamespaces.ListNamespacesOptions? right) -> bool static Supabase.Storage.ListNamespaces.ListNamespacesOptions.operator ==(Supabase.Storage.ListNamespaces.ListNamespacesOptions? left, Supabase.Storage.ListNamespaces.ListNamespacesOptions? right) -> bool +static Supabase.Storage.ListTables.IcebergTablePage.operator !=(Supabase.Storage.ListTables.IcebergTablePage? left, Supabase.Storage.ListTables.IcebergTablePage? right) -> bool +static Supabase.Storage.ListTables.IcebergTablePage.operator ==(Supabase.Storage.ListTables.IcebergTablePage? left, Supabase.Storage.ListTables.IcebergTablePage? right) -> bool +static Supabase.Storage.ListTables.ListTablesOptions.operator !=(Supabase.Storage.ListTables.ListTablesOptions? left, Supabase.Storage.ListTables.ListTablesOptions? right) -> bool +static Supabase.Storage.ListTables.ListTablesOptions.operator ==(Supabase.Storage.ListTables.ListTablesOptions? left, Supabase.Storage.ListTables.ListTablesOptions? right) -> bool static Supabase.Storage.ListVectorBuckets.ListVectorBucketsOptions.operator !=(Supabase.Storage.ListVectorBuckets.ListVectorBucketsOptions? left, Supabase.Storage.ListVectorBuckets.ListVectorBucketsOptions? right) -> bool static Supabase.Storage.ListVectorBuckets.ListVectorBucketsOptions.operator ==(Supabase.Storage.ListVectorBuckets.ListVectorBucketsOptions? left, Supabase.Storage.ListVectorBuckets.ListVectorBucketsOptions? right) -> bool static Supabase.Storage.ListVectorBuckets.VectorBucketSummary.operator !=(Supabase.Storage.ListVectorBuckets.VectorBucketSummary? left, Supabase.Storage.ListVectorBuckets.VectorBucketSummary? right) -> bool @@ -483,6 +493,7 @@ Supabase.Storage.Interfaces.IIcebergCatalog.CreateTableAsync(Supabase.Storage.Co Supabase.Storage.Interfaces.IIcebergCatalog.DeleteNamespaceAsync(Supabase.Storage.Common.IcebergNamespace! namespace, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! Supabase.Storage.Interfaces.IIcebergCatalog.DeleteTableAsync(Supabase.Storage.Common.IcebergTable! table, Supabase.Storage.DeleteTable.DeleteTableOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! Supabase.Storage.Interfaces.IIcebergCatalog.ListNamespacesAsync(Supabase.Storage.ListNamespaces.ListNamespacesOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! +Supabase.Storage.Interfaces.IIcebergCatalog.ListTablesAsync(Supabase.Storage.Common.IcebergNamespace! namespace, Supabase.Storage.ListTables.ListTablesOptions? options = null, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! Supabase.Storage.Interfaces.IIcebergCatalog.LoadTableAsync(Supabase.Storage.Common.IcebergTable! table, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! Supabase.Storage.Interfaces.IIcebergCatalog.NamespaceExistsAsync(Supabase.Storage.Common.IcebergNamespace! namespace, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! Supabase.Storage.Interfaces.IIcebergCatalog.TableExistsAsync(Supabase.Storage.Common.IcebergTable! table, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task! @@ -559,6 +570,22 @@ Supabase.Storage.ListNamespaces.ListNamespacesOptions.PageToken.get -> string? Supabase.Storage.ListNamespaces.ListNamespacesOptions.PageToken.init -> void Supabase.Storage.ListNamespaces.ListNamespacesOptions.Parent.get -> Supabase.Storage.Common.IcebergNamespace? Supabase.Storage.ListNamespaces.ListNamespacesOptions.Parent.init -> void +Supabase.Storage.ListTables.IcebergTablePage +Supabase.Storage.ListTables.IcebergTablePage.$() -> Supabase.Storage.ListTables.IcebergTablePage! +Supabase.Storage.ListTables.IcebergTablePage.Equals(Supabase.Storage.ListTables.IcebergTablePage? other) -> bool +Supabase.Storage.ListTables.IcebergTablePage.IcebergTablePage() -> void +Supabase.Storage.ListTables.IcebergTablePage.NextPageToken.get -> string? +Supabase.Storage.ListTables.IcebergTablePage.NextPageToken.init -> void +Supabase.Storage.ListTables.IcebergTablePage.Tables.get -> System.Collections.Generic.IReadOnlyList! +Supabase.Storage.ListTables.IcebergTablePage.Tables.init -> void +Supabase.Storage.ListTables.ListTablesOptions +Supabase.Storage.ListTables.ListTablesOptions.$() -> Supabase.Storage.ListTables.ListTablesOptions! +Supabase.Storage.ListTables.ListTablesOptions.Equals(Supabase.Storage.ListTables.ListTablesOptions? other) -> bool +Supabase.Storage.ListTables.ListTablesOptions.ListTablesOptions() -> void +Supabase.Storage.ListTables.ListTablesOptions.PageSize.get -> int? +Supabase.Storage.ListTables.ListTablesOptions.PageSize.init -> void +Supabase.Storage.ListTables.ListTablesOptions.PageToken.get -> string? +Supabase.Storage.ListTables.ListTablesOptions.PageToken.init -> void Supabase.Storage.ListVectorBuckets.ListVectorBucketsOptions Supabase.Storage.ListVectorBuckets.ListVectorBucketsOptions.$() -> Supabase.Storage.ListVectorBuckets.ListVectorBucketsOptions! Supabase.Storage.ListVectorBuckets.ListVectorBucketsOptions.Equals(Supabase.Storage.ListVectorBuckets.ListVectorBucketsOptions? other) -> bool