Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve key representation #37

Merged
merged 10 commits into from
Oct 1, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using DynamoDBGenerator.Attributes;
namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests;

[DynamoDBMarshaller(typeof(GsiHashAndRangeKey))]
public partial class DynamoDBGsiKeyMarshallerTests
{
[Fact]
public void PartitionKey_MissMatchedIndexName_ShouldThrow()
{
var act = () => GsiHashAndRangeKeyMarshallerWithIndex("Unknown").PartitionKey("test");

act.Should().Throw<ArgumentOutOfRangeException>();
}

[Fact]
public void RangeKey_MissMatchedIndexName_ShouldThrow()
{
var act = () => GsiHashAndRangeKeyMarshallerWithIndex("Unknown").RangeKey("test");

act.Should().Throw<ArgumentOutOfRangeException>();
}

[Fact]
public void Keys_MissMatchedIndexName_ShouldThrow()
{
var act = () => GsiHashAndRangeKeyMarshallerWithIndex("Unknown").Keys("1", 1);

act.Should().Throw<ArgumentOutOfRangeException>();
}

[Fact]
public void PartitionKey_MatchedIndexName_ShouldMapCorrectly()
{
GsiHashAndRangeKeyMarshallerWithIndex(GsiHashAndRangeKey.IndexName)
.PartitionKey("[email protected]")
.Should()
.SatisfyRespectively(x =>
{
x.Key.Should().Be(nameof(GsiHashAndRangeKey.Email));
x.Value.S.Should().Be("[email protected]");
});
}

[Fact]
public void RangeKey_MatchedIndexName_ShouldMapCorrectly()
{
GsiHashAndRangeKeyMarshallerWithIndex(GsiHashAndRangeKey.IndexName)
.RangeKey(1)
.Should()
.SatisfyRespectively(x =>
{
x.Key.Should().Be(nameof(GsiHashAndRangeKey.EmailRanking));
x.Value.N.Should().Be("1");
});
}

[Fact]
public void Keys_MatchedIndexName_ShouldMapCorrectly()
{
GsiHashAndRangeKeyMarshallerWithIndex(GsiHashAndRangeKey.IndexName)
.Keys("[email protected]", 1)
.Should()
.SatisfyRespectively(x =>
{

x.Key.Should().Be(nameof(GsiHashAndRangeKey.Email));
x.Value.S.Should().Be("[email protected]");
},
x =>
{
x.Key.Should().Be(nameof(GsiHashAndRangeKey.EmailRanking));
x.Value.N.Should().Be("1");

}
);
}
}

public class GsiHashAndRangeKey
{
public const string IndexName = "EmailGSI";

[DynamoDBHashKey]
public string PrimaryPartitionKey { get; set; }

[DynamoDBRangeKey]
public string PrimaryRangeKey { get; set; }

[DynamoDBGlobalSecondaryIndexHashKey(IndexName)]
public string Email { get; set; }

[DynamoDBGlobalSecondaryIndexRangeKey(IndexName)]
public int EmailRanking { get; set; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using DynamoDBGenerator.Attributes;
namespace DynamoDBGenerator.SourceGenerator.Tests.DynamoDBDocumentTests;

[DynamoDBMarshaller(typeof(LsiHashAndRangeKey))]
public partial class DynamoDBLsiKeyMarshallerTests
{
[Fact]
public void PartitionKey_MissMatchedIndexName_ShouldThrow()
{
var act = () => LsiHashAndRangeKeyMarshallerWithIndex("Unknown").PartitionKey("test");

act.Should().Throw<ArgumentOutOfRangeException>();
}

[Fact]
public void RangeKey_MissMatchedIndexName_ShouldThrow()
{
var act = () => LsiHashAndRangeKeyMarshallerWithIndex("Unknown").RangeKey("test");

act.Should().Throw<ArgumentOutOfRangeException>();
}

[Fact]
public void Keys_MissMatchedIndexName_ShouldThrow()
{
var act = () => LsiHashAndRangeKeyMarshallerWithIndex("Unknown").Keys("1", 1);

act.Should().Throw<ArgumentOutOfRangeException>();
}

[Fact]
public void PartitionKey_MatchedIndexName_ShouldMapCorrectly()
{
LsiHashAndRangeKeyMarshallerWithIndex(LsiHashAndRangeKey.IndexName)
.PartitionKey("[email protected]")
.Should()
.SatisfyRespectively(x =>
{
x.Key.Should().Be(nameof(LsiHashAndRangeKey.Email));
x.Value.S.Should().Be("[email protected]");
});
}

[Fact]
public void RangeKey_MatchedIndexName_ShouldMapCorrectly()
{
LsiHashAndRangeKeyMarshallerWithIndex(LsiHashAndRangeKey.IndexName)
.RangeKey(1)
.Should()
.SatisfyRespectively(x =>
{
x.Key.Should().Be(nameof(LsiHashAndRangeKey.EmailRanking));
x.Value.N.Should().Be("1");
});
}

[Fact]
public void Keys_MatchedIndexName_ShouldMapCorrectly()
{
LsiHashAndRangeKeyMarshallerWithIndex(LsiHashAndRangeKey.IndexName)
.Keys("[email protected]", 1)
.Should()
.SatisfyRespectively(x =>
{

x.Key.Should().Be(nameof(LsiHashAndRangeKey.Email));
x.Value.S.Should().Be("[email protected]");
},
x =>
{
x.Key.Should().Be(nameof(LsiHashAndRangeKey.EmailRanking));
x.Value.N.Should().Be("1");

}
);
}

}

public class LsiHashAndRangeKey
{
public const string IndexName = "EmailLSI";


[DynamoDBHashKey]
public string Email { get; set; }

[DynamoDBRangeKey]
public string PrimaryRangeKey { get; set; }


[DynamoDBLocalSecondaryIndexRangeKey(IndexName)]
public int EmailRanking { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System;
using AutoFixture;
using DynamoDBGenerator.Attributes;
using DynamoDBGenerator.Exceptions;
namespace DynamoDBGenerator.SourceGenerator.Tests;

[DynamoDBMarshaller(typeof(TypeWithPartitionKeyOnly), PropertyName = "PartitionKeyOnly")]
[DynamoDBMarshaller(typeof(TypeWithRangeKeyOnly), PropertyName = "TypeWithRangeOnly")]
[DynamoDBMarshaller(typeof(TypeWithKeys), PropertyName = "TypeWithKeys")]
[DynamoDBMarshaller(typeof(TypeWithoutKeys), PropertyName = "TypeWithoutKeys")]
public partial class DynamoDBKeyMarshallerTests
public partial class DynamoDBPrimaryKeyMarshallerTests
{
private readonly Fixture _fixture = new();

Expand All @@ -34,7 +35,7 @@
}

[Theory]
[InlineData("abc", null, Skip = "Could make sense to allow this")]
[InlineData("abc", null)]
[InlineData(null, "abc")]
[InlineData(null, null)]
[InlineData("abc", "dfg")]
Expand All @@ -53,22 +54,17 @@
}

[Fact]
public void RangeKey_TypeWithRangeKeyOnly_ShouldSucceed()
public void RangeKey_TypeWithRangeKeyOnly_ShouldThrow()
{
var type = _fixture.Create<TypeWithRangeKeyOnly>();
TypeWithRangeOnly
.RangeKey(type.Id)
.Should()
.SatisfyRespectively(x =>
{
x.Key.Should().Be(nameof(type.Id));
x.Value.S.Should().Be(type.Id);
});
var act = () => TypeWithRangeOnly.RangeKey(type.Id);

act.Should().Throw<InvalidOperationException>();
}

[Theory]
[InlineData("abc", null)]
[InlineData(null, "abc", Skip = "Could make sense to allow this")]
[InlineData(null, "abc")]
[InlineData(null, null)]
[InlineData("abc", "dfg")]
public void Keys_TypeWithRangeKeyOnly_ShouldThrow(string partitionKey, string rangeKey)
Expand Down Expand Up @@ -155,7 +151,7 @@
public void Keys_InvalidTypeWithKeys_ShouldThrow(object partitionKey, object rangeKey)
{
var act = () => TypeWithKeys.Keys(partitionKey, rangeKey);
act.Should().Throw<InvalidOperationException>();
act.Should().Throw<DynamoDBMarshallingException>();
}

[Theory]
Expand All @@ -168,7 +164,7 @@
public void PartitionKey_InvalidTypes_ShouldThrow(object key)
{
var act = () => PartitionKeyOnly.PartitionKey(key);
act.Should().Throw<InvalidOperationException>();
act.Should().Throw<DynamoDBMarshallingException>();
}

[Theory]
Expand All @@ -195,14 +191,14 @@
public void Keys_InvalidTypes_ShouldThrow(object key)
{
var act = () => PartitionKeyOnly.Keys(key, key);
act.Should().Throw<InvalidOperationException>();
act.Should().Throw<DynamoDBMarshallingException>();
}
}

public class TypeWithPartitionKeyOnly
{
[DynamoDBHashKey]
public string Id { get; set; }

Check warning on line 201 in DynamoDBGenerator.SourceGenerator.Tests/DynamoDBPrimaryKeyMarshallerTests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Id' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}

public class TypeWithRangeKeyOnly
Expand All @@ -214,7 +210,7 @@
public class TypeWithKeys
{
[DynamoDBHashKey]
public string Id { get; set; }

Check warning on line 213 in DynamoDBGenerator.SourceGenerator.Tests/DynamoDBPrimaryKeyMarshallerTests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Id' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

[DynamoDBRangeKey]
public string RangeKey { get; set; }
Expand All @@ -222,4 +218,4 @@

public class TypeWithoutKeys
{
public string Id { get; set; }

Check warning on line 221 in DynamoDBGenerator.SourceGenerator.Tests/DynamoDBPrimaryKeyMarshallerTests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Id' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
Expand Down
Loading
Loading