Skip to content

Commit

Permalink
More compile time checks for Relations.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bobris committed Jan 1, 2025
1 parent a6a742c commit 49b5b70
Show file tree
Hide file tree
Showing 11 changed files with 358 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
{
Location: /*

public interface IPersonTable : IRelation<Person>
^^^^^^^^^^^^
{
public interface IPersonTable : IRelation<Person>
^^^^^^^^^^^^
{
*/
: (11,29)-(11,41),
: (10,17)-(10,29),
Message: Cannot use PrimaryKey together with InKeyValue in Id,
Severity: Error,
Descriptor: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
Diagnostics: [
{
Location: /*

public interface IPersonTable : IRelation<Person>
^^^^^^^^^^^^
{
*/
: (13,17)-(13,29),
Message: Cannot use InKeyValue cannot be part of any SecondaryKey in Name,
Severity: Error,
Descriptor: {
Id: BTDB0004,
Title: Cannot use InKeyValue cannot be part of any SecondaryKey in Name,
MessageFormat: Cannot use InKeyValue cannot be part of any SecondaryKey in Name,
Category: BTDB,
DefaultSeverity: Error,
IsEnabledByDefault: true
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
Diagnostics: [
{
Location: /*
}
public interface IPersonTable : IRelation<Person>
^^^^^^^^^^^^
{
*/
: (11,17)-(11,29),
Message: InKeyValue InKeyValue must be in order after PrimaryKey PrimaryKey,
Severity: Error,
Descriptor: {
Id: BTDB0006,
Title: InKeyValue InKeyValue must be in order after PrimaryKey PrimaryKey,
MessageFormat: InKeyValue InKeyValue must be in order after PrimaryKey PrimaryKey,
Category: BTDB,
DefaultSeverity: Error,
IsEnabledByDefault: true
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
Diagnostics: [
{
Location: /*

public interface IPersonTable : IRelation<Person>
^^^^^^^^^^^^
{
*/
: (10,17)-(10,29),
Message: Cannot use Id as name of secondary key in Name,
Severity: Error,
Descriptor: {
Id: BTDB0003,
Title: Cannot use Id as name of secondary key in Name,
MessageFormat: Cannot use Id as name of secondary key in Name,
Category: BTDB,
DefaultSeverity: Error,
IsEnabledByDefault: true
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
Diagnostics: [
{
Location: /*
}
public interface IPersonTable : IRelation<Person>
^^^^^^^^^^^^
{
*/
: (11,17)-(11,29),
Message: Cannot have multiple PrimaryKey with same order in P1Also as in P1,
Severity: Error,
Descriptor: {
Id: BTDB0005,
Title: Cannot have multiple PrimaryKey with same order in P1Also as in P1,
MessageFormat: Cannot have multiple PrimaryKey with same order in P1Also as in P1,
Category: BTDB,
DefaultSeverity: Error,
IsEnabledByDefault: true
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
Diagnostics: [
{
Location: /*
}
public interface IPersonTable : IRelation<Person>
^^^^^^^^^^^^
{
*/
: (13,17)-(13,29),
Message: Cannot have multiple SecondaryKey with same order in S1Also as in S1,
Severity: Error,
Descriptor: {
Id: BTDB0007,
Title: Cannot have multiple SecondaryKey with same order in S1Also as in S1,
MessageFormat: Cannot have multiple SecondaryKey with same order in S1Also as in S1,
Category: BTDB,
DefaultSeverity: Error,
IsEnabledByDefault: true
}
}
]
}
123 changes: 121 additions & 2 deletions BTDB.SourceGenerator.Test/RelationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public interface IPersonTable : IRelation<Person>
public Task VerifyCannotUsePrimaryKeyTogetherWithInKeyValue()
{
// language=cs
return VerifySourceGenerator(@"
return VerifySourceGenerator("""
using BTDB.ODBLayer;
namespace TestNamespace;
Expand All @@ -69,6 +69,125 @@ public class Person
public interface IPersonTable : IRelation<Person>
{
}
");
""");
}

[Fact]
public Task VerifyThatSecondaryKeyCannotBeNamedId()
{
// language=cs
return VerifySourceGenerator("""
using BTDB.ODBLayer;
namespace TestNamespace;
public class Person
{
[PrimaryKey(1)] public int Id { get; set; }
[SecondaryKey("Id")] public string Name { get; set; } = null!;
}
public interface IPersonTable : IRelation<Person>
{
}
""");
}

[Fact]
public Task VerifyThatInKeyValueCannotBeAlsoSecondaryKey()
{
// language=cs
return VerifySourceGenerator("""
using BTDB.ODBLayer;
namespace TestNamespace;
public class Person
{
[PrimaryKey(1)]
public int Id { get; set; }
[SecondaryKey("Name")]
[InKeyValue(2)]
public string Name { get; set; } = null!;
}
public interface IPersonTable : IRelation<Person>
{
}
""");
}

[Fact]
public Task VerifyThatInKeyValueCannotBeBeforePrimaryKey()
{
// language=cs
return VerifySourceGenerator("""
using BTDB.ODBLayer;
namespace TestNamespace;
public class Person
{
[InKeyValue(1)]
public int InKeyValue { get; set; }
[PrimaryKey(2)]
public int PrimaryKey { get; set; }
}
public interface IPersonTable : IRelation<Person>
{
}
""");
}

[Fact]
public Task VerifyThatTwoPrimaryKeysCannotHaveSameOrder()
{
// language=cs
return VerifySourceGenerator("""
using BTDB.ODBLayer;
namespace TestNamespace;
public class Person
{
[PrimaryKey(1)]
public int P1 { get; set; }
[PrimaryKey(1)]
public int P1Also { get; set; }
}
public interface IPersonTable : IRelation<Person>
{
}
""");
}

[Fact]
public Task VerifyThatTwoSecondaryKeysCannotHaveSameOrder()
{
// language=cs
return VerifySourceGenerator("""
using BTDB.ODBLayer;
namespace TestNamespace;
public class Person
{
[PrimaryKey(1)]
public int P1 { get; set; }
[SecondaryKey("SK", Order = 1)]
public int S1 { get; set; }
[SecondaryKey("SK", Order = 1)]
public int S1Also { get; set; }
}
public interface IPersonTable : IRelation<Person>
{
}
""");
}
}
Loading

0 comments on commit 49b5b70

Please sign in to comment.