Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion packages/Storage/Storage.Tests/Common/IcebergNamespaceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
namespace Storage.Tests.Common;

/// <summary>
/// Unit tests for building the identifier of a namespace: which levels it refuses and how two identifiers compare.
/// Unit tests for building the identifier of a namespace and of its tables: which arguments it refuses and how two
/// identifiers compare. Table names are validated by the service.
/// </summary>
[TestClass]
[TestCategory("Unit")]
Expand Down Expand Up @@ -100,4 +101,18 @@ public void Path_ShouldThrowArgumentException_GivenRelativeSegmentNamespace(stri
.Should().Throw<ArgumentException>("a dot-segment namespace would reach another route")
.WithMessage("A namespace cannot be a relative path segment.*")
.Which.ParamName.Should().Be("namespace");

[TestMethod]
public void Table_ShouldKeepNamespaceAndName() =>
IcebergNamespace.Of("sales").Table("events").Should().Match<IcebergTable>(table =>
table.Namespace == IcebergNamespace.Of("sales") && table.Name == "events");

[TestMethod]
public void Table_ShouldThrowArgumentNullException_GivenNoName() =>
FluentActions.Invoking(() => IcebergNamespace.Of("sales").Table(null!))
.Should().Throw<ArgumentNullException>().Which.ParamName.Should().Be("name");

[TestMethod]
public void Table_ShouldAcceptBlankName() =>
IcebergNamespace.Of("sales").Table(" ").Name.Should().Be(" ", "the service, not the SDK, judges the name");
}
34 changes: 7 additions & 27 deletions packages/Storage/Storage.Tests/Common/IcebergTableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,34 @@
namespace Storage.Tests.Common;

/// <summary>
/// Unit tests for building the identifier of a table: which arguments it refuses, how it reads and how two
/// identifiers compare. The name itself is validated by the service.
/// Unit tests for the identifier of a table: how it reads, how two identifiers compare and the path it addresses.
/// </summary>
[TestClass]
[TestCategory("Unit")]
public class IcebergTableTests
{
[TestMethod]
public void Of_ShouldKeepNamespaceAndName() =>
IcebergTable.Of(IcebergNamespace.Of("sales"), "events").Should().Match<IcebergTable>(table =>
table.Namespace == IcebergNamespace.Of("sales") && table.Name == "events");

[TestMethod]
public void Of_ShouldThrowArgumentNullException_GivenNoNamespace() =>
FluentActions.Invoking(() => IcebergTable.Of(null!, "events"))
.Should().Throw<ArgumentNullException>().Which.ParamName.Should().Be("namespace");

[TestMethod]
public void Of_ShouldThrowArgumentNullException_GivenNoName() =>
FluentActions.Invoking(() => IcebergTable.Of(IcebergNamespace.Of("sales"), null!))
.Should().Throw<ArgumentNullException>().Which.ParamName.Should().Be("name");

[TestMethod]
public void Of_ShouldAcceptBlankName() =>
IcebergTable.Of(IcebergNamespace.Of("sales"), " ").Name.Should().Be(" ", "the service, not the SDK, judges the name");

[TestMethod]
public void ToString_ShouldJoinNamespaceAndNameWithDots() =>
IcebergTable.Of(IcebergNamespace.Of("sales", "europe"), "events").ToString().Should().Be("sales.europe.events");
IcebergNamespace.Of("sales", "europe").Table("events").ToString().Should().Be("sales.europe.events");

[TestMethod]
public void Equals_ShouldBeTrue_GivenSameNamespaceAndName() =>
IcebergTable.Of(IcebergNamespace.Of("sales"), "events").Should().Be(IcebergTable.Of(IcebergNamespace.Of("sales"), "events"));
IcebergNamespace.Of("sales").Table("events").Should().Be(IcebergNamespace.Of("sales").Table("events"));

[TestMethod]
public void Equals_ShouldBeFalse_GivenDifferentName() =>
IcebergTable.Of(IcebergNamespace.Of("sales"), "events").Should().NotBe(IcebergTable.Of(IcebergNamespace.Of("sales"), "orders"));
IcebergNamespace.Of("sales").Table("events").Should().NotBe(IcebergNamespace.Of("sales").Table("orders"));

[TestMethod]
public void Path_ShouldNestTheEscapedNameUnderTheTablesOfItsNamespace() =>
IcebergTable.Of(IcebergNamespace.Of("sales", "europe"), "a/b?c#d").Path
IcebergNamespace.Of("sales", "europe").Table("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)
FluentActions.Invoking(() => IcebergNamespace.Of("sales").Table(name).Path)
.Should().Throw<ArgumentException>("a blank name would reach the route of the namespace's tables")
.WithMessage("A table name must be provided.*")
.Which.ParamName.Should().Be("table");
Expand All @@ -62,7 +42,7 @@ public void Path_ShouldThrowArgumentException_GivenBlankName(string name) =>
[DataRow(".")]
[DataRow("..")]
public void Path_ShouldThrowArgumentException_GivenRelativeSegmentName(string name) =>
FluentActions.Invoking(() => IcebergTable.Of(IcebergNamespace.Of("sales"), name).Path)
FluentActions.Invoking(() => IcebergNamespace.Of("sales").Table(name).Path)
.Should().Throw<ArgumentException>("a dot-segment name would reach another route")
.WithMessage("A table name cannot be a relative path segment.*")
.Which.ParamName.Should().Be("table");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Storage.Tests.CreateTable;
[TestCategory("Contract")]
public class CreateTableApprovalTests : RequestApprovalFixture
{
private static readonly IcebergTable Events = IcebergTable.Of(IcebergNamespace.Of("sales"), "events");
private static readonly IcebergTable Events = IcebergNamespace.Of("sales").Table("events");

[TestMethod]
public async Task CreateTableRequest_ShouldSerializeToExpectedPayload()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ namespace Storage.Tests.CreateTable;
public class CreateTableContractTests
{
private const string TablesPath = "/storage/v1/iceberg/v1/analytics-data/namespaces/sales/tables";
private static readonly IcebergTable Events = IcebergTable.Of(IcebergNamespace.Of("sales"), "events");
private static readonly IcebergTable Events = IcebergNamespace.Of("sales").Table("events");
private static readonly IcebergSchema Schema = new()
{
Fields = new[] { new IcebergField { Id = 1, Name = "id", Type = IcebergPrimitiveType.Long, Required = true } },
Expand Down Expand Up @@ -72,7 +72,7 @@ public async Task CreateTableAsync_ShouldJoinNamespaceLevelsWithTheUnitSeparator
{
this.server.Given(Request.Create().WithPath("/storage/v1/iceberg/v1/*").UsingPost())
.RespondWith(Response.Create().WithStatusCode(200).WithBody(Fixture("TableCreated.json")));
await this.Catalog().CreateTableAsync(IcebergTable.Of(IcebergNamespace.Of("sales", "europe"), "events"), Schema);
await this.Catalog().CreateTableAsync(IcebergNamespace.Of("sales", "europe").Table("events"), Schema);
this.server.LogEntries.Should().ContainSingle().Which.RequestMessage!.Url
.Should().EndWith("/namespaces/sales%1Feurope/tables", "the Iceberg REST spec joins levels with the 0x1F separator");
}
Expand Down Expand Up @@ -230,7 +230,7 @@ public async Task CreateTableAsync_ShouldThrow_GivenTypeThatIsNeitherNameNorObje
public async Task CreateTableAsync_ShouldThrowInvalidInput_GivenInvalidName()
{
this.Respond(400, Envelope("Resource name must not contain hyphens (-) or periods (.)", "InvalidParameter", 400));
var act = () => this.Catalog().CreateTableAsync(IcebergTable.Of(IcebergNamespace.Of("sales"), "my-events"), Schema);
var act = () => this.Catalog().CreateTableAsync(IcebergNamespace.Of("sales").Table("my-events"), Schema);
var exception = (await act.Should().ThrowAsync<SupabaseStorageException>()).Which;
using (new AssertionScope())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Storage.Tests.CreateTable;
[TestCategory("Unit")]
public class CreateTableTests
{
private static readonly IcebergTable Events = IcebergTable.Of(IcebergNamespace.Of("sales"), "events");
private static readonly IcebergTable Events = IcebergNamespace.Of("sales").Table("events");
private static readonly IcebergSchema Schema = new() { Fields = Array.Empty<IcebergField>() };
private readonly Client sut = new("http://localhost:1/storage/v1");

Expand All @@ -34,7 +34,7 @@ public async Task CreateTableAsync_ShouldThrowArgumentNullException_GivenNoSchem

[TestMethod]
public async Task CreateTableAsync_ShouldThrowArgumentException_GivenRelativeSegmentNamespace() =>
await FluentActions.Awaiting(() => this.sut.Analytics.From("analytics-data").CreateTableAsync(IcebergTable.Of(IcebergNamespace.Of(".."), "events"), Schema))
await FluentActions.Awaiting(() => this.sut.Analytics.From("analytics-data").CreateTableAsync(IcebergNamespace.Of("..").Table("events"), Schema))
.Should().ThrowAsync<ArgumentException>("a dot-segment level would reach another route")
.WithMessage("A namespace cannot be a relative path segment.*");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Storage.Tests.DeleteTable;
public class DeleteTableContractTests
{
private const string TablePath = "/storage/v1/iceberg/v1/analytics-data/namespaces/sales/tables/events";
private static readonly IcebergTable Events = IcebergTable.Of(IcebergNamespace.Of("sales"), "events");
private static readonly IcebergTable Events = IcebergNamespace.Of("sales").Table("events");
private WireMockServer server = null!;
private Client client = null!;

Expand Down Expand Up @@ -84,7 +84,7 @@ public async Task DeleteTableAsync_ShouldSendPurge_GivenSet(bool purge, string e
public async Task DeleteTableAsync_ShouldEscapeTheTableNameAsOneSegment()
{
this.Respond("/storage/v1/iceberg/v1/*", 204, "");
await this.Catalog().DeleteTableAsync(IcebergTable.Of(IcebergNamespace.Of("sales", "europe"), "a/b?c#d"));
await this.Catalog().DeleteTableAsync(IcebergNamespace.Of("sales", "europe").Table("a/b?c#d"));
this.server.LogEntries.Should().ContainSingle().Which.RequestMessage!.Url
.Should().EndWith("/namespaces/sales%1Feurope/tables/a%2Fb%3Fc%23d", "a name must not start a path segment, query or fragment");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task DeleteTableAsync_ShouldThrowArgumentNullException_GivenNoTable
[DataRow("")]
[DataRow(" ")]
public async Task DeleteTableAsync_ShouldThrowArgumentException_GivenBlankName(string name) =>
(await FluentActions.Awaiting(() => this.sut.Analytics.From("analytics-data").DeleteTableAsync(IcebergTable.Of(IcebergNamespace.Of("sales"), name)))
(await FluentActions.Awaiting(() => this.sut.Analytics.From("analytics-data").DeleteTableAsync(IcebergNamespace.Of("sales").Table(name)))
.Should().ThrowAsync<ArgumentException>("a blank name would reach the route of the namespace's tables")
.WithMessage("A table name must be provided.*"))
.Which.ParamName.Should().Be("table");
Expand All @@ -36,7 +36,7 @@ public async Task DeleteTableAsync_ShouldThrowArgumentException_GivenBlankName(s
[DataRow(".")]
[DataRow("..")]
public async Task DeleteTableAsync_ShouldThrowArgumentException_GivenRelativeSegmentName(string name) =>
(await FluentActions.Awaiting(() => this.sut.Analytics.From("analytics-data").DeleteTableAsync(IcebergTable.Of(IcebergNamespace.Of("sales"), name)))
(await FluentActions.Awaiting(() => this.sut.Analytics.From("analytics-data").DeleteTableAsync(IcebergNamespace.Of("sales").Table(name)))
.Should().ThrowAsync<ArgumentException>("a dot-segment name would reach another route, such as the namespace itself")
.WithMessage("A table name cannot be a relative path segment.*"))
.Which.ParamName.Should().Be("table");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Storage.Tests.LoadTable;
public class LoadTableContractTests
{
private const string TablePath = "/storage/v1/iceberg/v1/analytics-data/namespaces/sales/tables/events";
private static readonly IcebergTable Events = IcebergTable.Of(IcebergNamespace.Of("sales"), "events");
private static readonly IcebergTable Events = IcebergNamespace.Of("sales").Table("events");
private WireMockServer server = null!;
private Client client = null!;

Expand Down Expand Up @@ -73,7 +73,7 @@ public async Task LoadTableAsync_ShouldSendNoQuery()
public async Task LoadTableAsync_ShouldEscapeTheTableNameAsOneSegment()
{
this.Respond("/storage/v1/iceberg/v1/*", 200, Fixture("TableLoaded.json"));
await this.Catalog().LoadTableAsync(IcebergTable.Of(IcebergNamespace.Of("sales", "europe"), "a/b?c#d"));
await this.Catalog().LoadTableAsync(IcebergNamespace.Of("sales", "europe").Table("a/b?c#d"));
this.server.LogEntries.Should().ContainSingle().Which.RequestMessage!.Url
.Should().EndWith("/namespaces/sales%1Feurope/tables/a%2Fb%3Fc%23d", "a name must not start a path segment, query or fragment");
}
Expand Down
4 changes: 2 additions & 2 deletions packages/Storage/Storage.Tests/LoadTable/LoadTableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task LoadTableAsync_ShouldThrowArgumentNullException_GivenNoTable()
[DataRow("")]
[DataRow(" ")]
public async Task LoadTableAsync_ShouldThrowArgumentException_GivenBlankName(string name) =>
(await FluentActions.Awaiting(() => this.sut.Analytics.From("analytics-data").LoadTableAsync(IcebergTable.Of(IcebergNamespace.Of("sales"), name)))
(await FluentActions.Awaiting(() => this.sut.Analytics.From("analytics-data").LoadTableAsync(IcebergNamespace.Of("sales").Table(name)))
.Should().ThrowAsync<ArgumentException>("a blank name would reach the route that lists tables")
.WithMessage("A table name must be provided.*"))
.Which.ParamName.Should().Be("table");
Expand All @@ -36,7 +36,7 @@ public async Task LoadTableAsync_ShouldThrowArgumentException_GivenBlankName(str
[DataRow(".")]
[DataRow("..")]
public async Task LoadTableAsync_ShouldThrowArgumentException_GivenRelativeSegmentName(string name) =>
(await FluentActions.Awaiting(() => this.sut.Analytics.From("analytics-data").LoadTableAsync(IcebergTable.Of(IcebergNamespace.Of("sales"), name)))
(await FluentActions.Awaiting(() => this.sut.Analytics.From("analytics-data").LoadTableAsync(IcebergNamespace.Of("sales").Table(name)))
.Should().ThrowAsync<ArgumentException>("a dot-segment name would reach another route")
.WithMessage("A table name cannot be a relative path segment.*"))
.Which.ParamName.Should().Be("table");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace Storage.Tests.TableExists;
public class TableExistsContractTests
{
private const string TablePath = "/storage/v1/iceberg/v1/analytics-data/namespaces/sales/tables/events";
private static readonly IcebergTable Events = IcebergTable.Of(IcebergNamespace.Of("sales"), "events");
private static readonly IcebergTable Events = IcebergNamespace.Of("sales").Table("events");
private WireMockServer server = null!;
private Client client = null!;

Expand Down Expand Up @@ -78,7 +78,7 @@ public async Task TableExistsAsync_ShouldReturnFalse_GivenNotFound()
public async Task TableExistsAsync_ShouldEscapeTheTableNameAsOneSegment()
{
this.Respond("/storage/v1/iceberg/v1/*", 204, "");
await this.Catalog().TableExistsAsync(IcebergTable.Of(IcebergNamespace.Of("sales", "europe"), "a/b?c#d"));
await this.Catalog().TableExistsAsync(IcebergNamespace.Of("sales", "europe").Table("a/b?c#d"));
this.server.LogEntries.Should().ContainSingle().Which.RequestMessage!.Url
.Should().EndWith("/namespaces/sales%1Feurope/tables/a%2Fb%3Fc%23d", "a name must not start a path segment, query or fragment");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public async Task TableExistsAsync_ShouldThrowArgumentNullException_GivenNoTable
[DataRow("")]
[DataRow(" ")]
public async Task TableExistsAsync_ShouldThrowArgumentException_GivenBlankName(string name) =>
(await FluentActions.Awaiting(() => this.sut.Analytics.From("analytics-data").TableExistsAsync(IcebergTable.Of(IcebergNamespace.Of("sales"), name)))
(await FluentActions.Awaiting(() => this.sut.Analytics.From("analytics-data").TableExistsAsync(IcebergNamespace.Of("sales").Table(name)))
.Should().ThrowAsync<ArgumentException>("a blank name would reach the route that lists tables")
.WithMessage("A table name must be provided.*"))
.Which.ParamName.Should().Be("table");
Expand All @@ -36,7 +36,7 @@ public async Task TableExistsAsync_ShouldThrowArgumentException_GivenBlankName(s
[DataRow(".")]
[DataRow("..")]
public async Task TableExistsAsync_ShouldThrowArgumentException_GivenRelativeSegmentName(string name) =>
(await FluentActions.Awaiting(() => this.sut.Analytics.From("analytics-data").TableExistsAsync(IcebergTable.Of(IcebergNamespace.Of("sales"), name)))
(await FluentActions.Awaiting(() => this.sut.Analytics.From("analytics-data").TableExistsAsync(IcebergNamespace.Of("sales").Table(name)))
.Should().ThrowAsync<ArgumentException>("a dot-segment name would reach another route")
.WithMessage("A table name cannot be a relative path segment.*"))
.Which.ParamName.Should().Be("table");
Expand Down
14 changes: 14 additions & 0 deletions packages/Storage/Storage/Common/IcebergNamespace.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,20 @@ public static IcebergNamespace Of(params string[] levels)
return new IcebergNamespace(Array.AsReadOnly(levels.ToArray()));
}

/// <summary>
/// Builds the identifier of a table held by this namespace. The service validates the name.
/// </summary>
/// <param name="name">The name of the table within this namespace.</param>
/// <returns>The identifier of the table.</returns>
/// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception>
/// <example>
/// <code>
/// var events = IcebergNamespace.Of("sales").Table("events");
/// </code>
/// </example>
public IcebergTable Table(string name) =>
new(this, name ?? throw new ArgumentNullException(nameof(name)));

/// <inheritdoc />
public bool Equals(IcebergNamespace? other) =>
other is not null && this.Levels.SequenceEqual(other.Levels);
Expand Down
19 changes: 1 addition & 18 deletions packages/Storage/Storage/Common/IcebergTable.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
using System;

namespace Supabase.Storage.Common;

/// <summary>
/// The identifier of an Iceberg table: the namespace that holds it and its name.
/// </summary>
public sealed record IcebergTable
{
private IcebergTable(IcebergNamespace @namespace, string name)
internal IcebergTable(IcebergNamespace @namespace, string name)
{
this.Namespace = @namespace;
this.Name = name;
Expand All @@ -19,21 +17,6 @@ private IcebergTable(IcebergNamespace @namespace, string name)
/// <summary>The name of the table within its namespace.</summary>
public string Name { get; }

/// <summary>
/// Builds the identifier of a table. The service validates the name.
/// </summary>
/// <param name="namespace">The namespace that holds the table.</param>
/// <param name="name">The name of the table within its namespace.</param>
/// <returns>The identifier of the table.</returns>
/// <exception cref="ArgumentNullException"><paramref name="namespace"/> or <paramref name="name"/> is null.</exception>
/// <example>
/// <code>
/// var events = IcebergTable.Of(IcebergNamespace.Of("sales"), "events");
/// </code>
/// </example>
public static IcebergTable Of(IcebergNamespace @namespace, string name) =>
new(@namespace ?? throw new ArgumentNullException(nameof(@namespace)), name ?? throw new ArgumentNullException(nameof(name)));

/// <inheritdoc />
public override string ToString() => $"{this.Namespace}.{this.Name}";

Expand Down
Loading