Skip to content

Commit 49b5b70

Browse files
committed
More compile time checks for Relations.
1 parent a6a742c commit 49b5b70

11 files changed

+358
-62
lines changed

BTDB.SourceGenerator.Test/RelationTests.VerifyCannotUsePrimaryKeyTogetherWithInKeyValue.verified.txt

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
{
44
Location: /*
55

6-
public interface IPersonTable : IRelation<Person>
7-
^^^^^^^^^^^^
8-
{
6+
public interface IPersonTable : IRelation<Person>
7+
^^^^^^^^^^^^
8+
{
99
*/
10-
: (11,29)-(11,41),
10+
: (10,17)-(10,29),
1111
Message: Cannot use PrimaryKey together with InKeyValue in Id,
1212
Severity: Error,
1313
Descriptor: {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
Diagnostics: [
3+
{
4+
Location: /*
5+
6+
public interface IPersonTable : IRelation<Person>
7+
^^^^^^^^^^^^
8+
{
9+
*/
10+
: (13,17)-(13,29),
11+
Message: Cannot use InKeyValue cannot be part of any SecondaryKey in Name,
12+
Severity: Error,
13+
Descriptor: {
14+
Id: BTDB0004,
15+
Title: Cannot use InKeyValue cannot be part of any SecondaryKey in Name,
16+
MessageFormat: Cannot use InKeyValue cannot be part of any SecondaryKey in Name,
17+
Category: BTDB,
18+
DefaultSeverity: Error,
19+
IsEnabledByDefault: true
20+
}
21+
}
22+
]
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
Diagnostics: [
3+
{
4+
Location: /*
5+
}
6+
public interface IPersonTable : IRelation<Person>
7+
^^^^^^^^^^^^
8+
{
9+
*/
10+
: (11,17)-(11,29),
11+
Message: InKeyValue InKeyValue must be in order after PrimaryKey PrimaryKey,
12+
Severity: Error,
13+
Descriptor: {
14+
Id: BTDB0006,
15+
Title: InKeyValue InKeyValue must be in order after PrimaryKey PrimaryKey,
16+
MessageFormat: InKeyValue InKeyValue must be in order after PrimaryKey PrimaryKey,
17+
Category: BTDB,
18+
DefaultSeverity: Error,
19+
IsEnabledByDefault: true
20+
}
21+
}
22+
]
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
Diagnostics: [
3+
{
4+
Location: /*
5+
6+
public interface IPersonTable : IRelation<Person>
7+
^^^^^^^^^^^^
8+
{
9+
*/
10+
: (10,17)-(10,29),
11+
Message: Cannot use Id as name of secondary key in Name,
12+
Severity: Error,
13+
Descriptor: {
14+
Id: BTDB0003,
15+
Title: Cannot use Id as name of secondary key in Name,
16+
MessageFormat: Cannot use Id as name of secondary key in Name,
17+
Category: BTDB,
18+
DefaultSeverity: Error,
19+
IsEnabledByDefault: true
20+
}
21+
}
22+
]
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
Diagnostics: [
3+
{
4+
Location: /*
5+
}
6+
public interface IPersonTable : IRelation<Person>
7+
^^^^^^^^^^^^
8+
{
9+
*/
10+
: (11,17)-(11,29),
11+
Message: Cannot have multiple PrimaryKey with same order in P1Also as in P1,
12+
Severity: Error,
13+
Descriptor: {
14+
Id: BTDB0005,
15+
Title: Cannot have multiple PrimaryKey with same order in P1Also as in P1,
16+
MessageFormat: Cannot have multiple PrimaryKey with same order in P1Also as in P1,
17+
Category: BTDB,
18+
DefaultSeverity: Error,
19+
IsEnabledByDefault: true
20+
}
21+
}
22+
]
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
Diagnostics: [
3+
{
4+
Location: /*
5+
}
6+
public interface IPersonTable : IRelation<Person>
7+
^^^^^^^^^^^^
8+
{
9+
*/
10+
: (13,17)-(13,29),
11+
Message: Cannot have multiple SecondaryKey with same order in S1Also as in S1,
12+
Severity: Error,
13+
Descriptor: {
14+
Id: BTDB0007,
15+
Title: Cannot have multiple SecondaryKey with same order in S1Also as in S1,
16+
MessageFormat: Cannot have multiple SecondaryKey with same order in S1Also as in S1,
17+
Category: BTDB,
18+
DefaultSeverity: Error,
19+
IsEnabledByDefault: true
20+
}
21+
}
22+
]
23+
}

BTDB.SourceGenerator.Test/RelationTests.cs

+121-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public interface IPersonTable : IRelation<Person>
5555
public Task VerifyCannotUsePrimaryKeyTogetherWithInKeyValue()
5656
{
5757
// language=cs
58-
return VerifySourceGenerator(@"
58+
return VerifySourceGenerator("""
5959
using BTDB.ODBLayer;
6060
6161
namespace TestNamespace;
@@ -69,6 +69,125 @@ public class Person
6969
public interface IPersonTable : IRelation<Person>
7070
{
7171
}
72-
");
72+
73+
""");
74+
}
75+
76+
[Fact]
77+
public Task VerifyThatSecondaryKeyCannotBeNamedId()
78+
{
79+
// language=cs
80+
return VerifySourceGenerator("""
81+
using BTDB.ODBLayer;
82+
83+
namespace TestNamespace;
84+
85+
public class Person
86+
{
87+
[PrimaryKey(1)] public int Id { get; set; }
88+
[SecondaryKey("Id")] public string Name { get; set; } = null!;
89+
}
90+
91+
public interface IPersonTable : IRelation<Person>
92+
{
93+
}
94+
95+
""");
96+
}
97+
98+
[Fact]
99+
public Task VerifyThatInKeyValueCannotBeAlsoSecondaryKey()
100+
{
101+
// language=cs
102+
return VerifySourceGenerator("""
103+
using BTDB.ODBLayer;
104+
105+
namespace TestNamespace;
106+
107+
public class Person
108+
{
109+
[PrimaryKey(1)]
110+
public int Id { get; set; }
111+
[SecondaryKey("Name")]
112+
[InKeyValue(2)]
113+
public string Name { get; set; } = null!;
114+
}
115+
116+
public interface IPersonTable : IRelation<Person>
117+
{
118+
}
119+
120+
""");
121+
}
122+
123+
[Fact]
124+
public Task VerifyThatInKeyValueCannotBeBeforePrimaryKey()
125+
{
126+
// language=cs
127+
return VerifySourceGenerator("""
128+
using BTDB.ODBLayer;
129+
130+
namespace TestNamespace;
131+
132+
public class Person
133+
{
134+
[InKeyValue(1)]
135+
public int InKeyValue { get; set; }
136+
[PrimaryKey(2)]
137+
public int PrimaryKey { get; set; }
138+
}
139+
public interface IPersonTable : IRelation<Person>
140+
{
141+
}
142+
143+
""");
144+
}
145+
146+
[Fact]
147+
public Task VerifyThatTwoPrimaryKeysCannotHaveSameOrder()
148+
{
149+
// language=cs
150+
return VerifySourceGenerator("""
151+
using BTDB.ODBLayer;
152+
153+
namespace TestNamespace;
154+
155+
public class Person
156+
{
157+
[PrimaryKey(1)]
158+
public int P1 { get; set; }
159+
[PrimaryKey(1)]
160+
public int P1Also { get; set; }
161+
}
162+
public interface IPersonTable : IRelation<Person>
163+
{
164+
}
165+
166+
""");
167+
}
168+
169+
[Fact]
170+
public Task VerifyThatTwoSecondaryKeysCannotHaveSameOrder()
171+
{
172+
// language=cs
173+
return VerifySourceGenerator("""
174+
using BTDB.ODBLayer;
175+
176+
namespace TestNamespace;
177+
178+
public class Person
179+
{
180+
[PrimaryKey(1)]
181+
public int P1 { get; set; }
182+
[SecondaryKey("SK", Order = 1)]
183+
public int S1 { get; set; }
184+
[SecondaryKey("SK", Order = 1)]
185+
public int S1Also { get; set; }
186+
}
187+
public interface IPersonTable : IRelation<Person>
188+
{
189+
}
190+
191+
""");
73192
}
74193
}

0 commit comments

Comments
 (0)