Skip to content

Commit aec0f56

Browse files
Test TypeQL reference (#985)
## Goal Add tests to TypeQL reference to notice if we ever break any syntax. ## Implementation Adds tests as far as possible. Notably misses `data-model.adoc`. Also skips some which are quite hand-wavy. Points of improvement (Maybe todo in this PR) * Add pre-conditions to a dedicated typeql block that's block-commented out. * Add counts to read queries I may have forgotten.
1 parent d9263dd commit aec0f56

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+913
-139
lines changed

.factory/automation.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ build:
4545
4646
# TODO: test node and java
4747
48-
TEST_DIRS=("home" "core-concepts")
48+
TEST_DIRS=("home" "core-concepts" "reference/modules/ROOT/pages/typeql")
4949
TEST_LANGS=("typeql" "python" "rust")
5050
5151
FAILED=false

reference/modules/ROOT/examples/tql/conjunction.tql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,21 @@ define
44
attribute name, value string;
55
# pipeline::schema::end
66

7+
# tag::patterns-schema[]
8+
define
9+
attribute email, value string;
10+
attribute phone, value string;
11+
attribute username, value string;
12+
relation friendship, relates friend @card(0..2);
13+
relation marriage, relates spouse @card(0..2);
14+
relation policy, relates covered @card(0..);
15+
entity user,
16+
owns username, owns email, owns phone,
17+
plays friendship:friend;
18+
entity person, plays marriage:spouse, plays policy:covered;
19+
20+
# end::patterns-schema[]
21+
722
# pipeline::read::start
823
match
924
# tag::conjunction-example[]
Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,39 @@
1-
// tag::with-fun[]
1+
# tag::with-fun[]
22
with fun karma_with_squared_value($user: user) -> karma, double:
33
match
44
$user has karma $karma;
55
let $karma-squared = $karma * $karma;
66
return first $karma, $karma-squared;
7-
// tag::with-fun-match[]
7+
8+
# tag::with-fun-match[]
89
match
910
$user isa user, has username $name;
1011
let $karma, $karma-squared = karma_with_squared_value($user);
1112
select $name, $karma-squared;
12-
// end::with-fun-match[]
13-
// end::with-fun[]
13+
# end::with-fun-match[]
14+
# end::with-fun[]
1415

1516

1617

17-
// tag::fun-stream-scalar-return-type[]
18+
# tag::fun-stream-scalar-return-type[]
1819
match
1920
$user isa user;
2021
let $phone in user_phones($user);
21-
// end::fun-stream-scalar-return-type[]
22+
# end::fun-stream-scalar-return-type[]
2223

2324

24-
// tag::match-users[]
25+
# tag::match-users[]
2526
match
2627
$user isa user;
27-
// end::match-users[]
28+
# end::match-users[]
2829

29-
// tag::fun-stream-tuple-return-type[]
30+
# tag::fun-stream-tuple-return-type[]
3031
match
3132
let $user, $phone, $phone-value in all_users_and_phones();
32-
// end::fun-stream-tuple-return-type[]
33+
# end::fun-stream-tuple-return-type[]
3334

3435

35-
// tag::fun-single-scalar-return-value[]
36+
# tag::fun-single-scalar-return-value[]
3637
match
3738
let $answer = add(2016, 9);
38-
// end::fun-single-scalar-return-value[]
39+
# end::fun-single-scalar-return-value[]
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
// tag::redefine-keyword[]
1+
# tag::redefine-keyword[]
22
redefine
3-
// end::redefine-keyword[]
3+
# end::redefine-keyword[]
44

5-
// tag::with-fun-redefined[]
5+
# tag::with-fun-redefined[]
66
fun karma_with_squared_value($karma: karma) -> double:
77
match
88
let $karma-squared = $karma * $karma;
99
return first $karma-squared;
10-
// end::with-fun-redefined[]
10+
# end::with-fun-redefined[]
Lines changed: 64 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,139 +1,156 @@
1-
// tag::define-keyword[]
1+
# tag::define-keyword[]
22
define
3-
// end::define-keyword[]
4-
// tag::entities[]
3+
# end::define-keyword[]
4+
# tag::entities[]
55
entity content owns id;
66
entity page sub content,
77
owns page-id,
88
owns name;
99
entity profile sub page,
1010
owns username;
11-
// tag::entity-user[]
11+
# tag::entity-user[]
1212
entity user sub profile,
1313
owns phone,
14-
owns karma,
15-
plays parentship:parent,
16-
plays parentship:child;
17-
// end::entity-user[]
14+
owns karma;
15+
# end::entity-user[]
1816

19-
// end::entities[]
17+
# end::entities[]
2018

21-
// tag::entity-user-breakdown[]
19+
# tag::entity-user-breakdown[]
2220
entity user;
2321
user sub profile;
2422
user owns phone;
2523
user owns karma;
26-
user plays parentship:parent;
27-
user plays parentship:child;
28-
// end::entity-user-breakdown[]
24+
# end::entity-user-breakdown[]
2925

30-
// tag::entities-annotations[]
26+
# tag::entities-annotations[]
3127
content @abstract, owns id @key;
3228
page @abstract, owns name @card(0..);
3329
profile @abstract, owns name @card(0..3);
3430
user owns phone @regex("^\d{8,15}$") @unique;
35-
// end::entities-annotations[]
31+
# end::entities-annotations[]
3632

37-
// tag::entities-type-annotations[]
33+
# tag::entities-type-annotations[]
3834
content @abstract;
3935
page @abstract;
4036
profile @abstract;
41-
// end::entities-type-annotations[]
37+
# end::entities-type-annotations[]
4238

43-
// tag::entities-cap-annotations[]
39+
# tag::entities-cap-annotations[]
4440
content owns id @key;
4541
page owns name @card(0..);
4642
profile owns name @card(0..3);
4743
user owns phone @regex("^\d{8,15}$") @unique;
48-
// end::entities-cap-annotations[]
44+
# end::entities-cap-annotations[]
4945

50-
// tag::all-owns-name[]
46+
# tag::all-owns-name[]
5147
entity content;
52-
// tag::all-owns-name-cards[]
48+
# tag::all-owns-name-cards[]
5349
entity page sub content, owns name @card(0..);
5450
entity profile sub page, owns name @card(0..3);
5551
entity user sub profile;
56-
// end::all-owns-name-cards[]
52+
# end::all-owns-name-cards[]
5753
attribute name value string;
58-
// end::all-owns-name[]
54+
# end::all-owns-name[]
5955

60-
// tag::attributes[]
56+
# tag::attributes[]
6157
attribute id value string;
6258
attribute page-id sub id;
6359
attribute username sub page-id;
6460
attribute name value string;
6561
attribute phone value string;
6662
attribute karma value double;
67-
// end::attributes[]
63+
# end::attributes[]
6864

69-
// tag::attributes-type-annotations[]
65+
# tag::extended-attributes[]
66+
attribute bio value string;
67+
attribute profile-picture value string;
68+
attribute email @independent, value string @regex("^.*@.*\.com");
69+
user owns bio, owns profile-picture;
70+
# end::extended-attributes[]
71+
72+
# tag::attributes-type-annotations[]
7073
attribute id @abstract;
7174
attribute page-id @abstract;
72-
// end::attributes-type-annotations[]
73-
74-
75-
// tag::fun-stream-scalar-return-type[]
75+
# end::attributes-type-annotations[]
76+
77+
# tag::relation-friendship[]
78+
attribute start-date, value date;
79+
relation friendship relates friend @card(0..2), owns start-date;
80+
user plays friendship:friend;
81+
# end::relation-friendship[]
82+
83+
# tag::relation-group[]
84+
attribute group-id sub id;
85+
attribute rank, value string @values("admin", "member");
86+
attribute tag value string;
87+
relation group-membership, relates group, relates member, owns rank;
88+
entity group, owns name, owns group-id, owns tag, plays group-membership:group;
89+
user, plays group-membership:member;
90+
# end::relation-group[]
91+
92+
# tag::fun-stream-scalar-return-type[]
7693
fun user_phones($user: user) -> { phone }:
7794
match
7895
$user has phone $phone;
7996
return { $phone };
80-
// end::fun-stream-scalar-return-type[]
97+
# end::fun-stream-scalar-return-type[]
8198

82-
// tag::fun-stream-tuple-return-type[]
99+
# tag::fun-stream-tuple-return-type[]
83100
fun all_users_and_phones() -> { user, phone, string }:
84101
match
85102
$user isa user, has phone $phone;
86103
let $phone-value = $phone;
87104
return { $user, $phone, $phone-value };
88-
// end::fun-stream-tuple-return-type[]
105+
# end::fun-stream-tuple-return-type[]
89106

90-
// tag::fun-stream-scalar-return-value[]
107+
# tag::fun-stream-scalar-return-value[]
91108
fun add_streamed($x: integer, $y: integer) -> { integer }:
92109
match
93110
let $z = $x + $y;
94111
return { $z };
95-
// end::fun-stream-scalar-return-value[]
112+
# end::fun-stream-scalar-return-value[]
96113

97-
// tag::fun-single-scalar-return-value[]
114+
# tag::fun-single-scalar-return-value[]
98115
fun add($x: integer, $y: integer) -> integer:
99116
match
100117
let $z = $x + $y;
101118
return first $z;
102-
// end::fun-single-scalar-return-value[]
119+
# end::fun-single-scalar-return-value[]
103120

104-
// tag::fun-aggregate-single-scalar-return[]
121+
# tag::fun-aggregate-single-scalar-return[]
105122
fun mean_karma() -> double:
106123
match
107124
$user isa user, has karma $karma;
108125
return mean($karma);
109-
// end::fun-aggregate-single-scalar-return[]
126+
# end::fun-aggregate-single-scalar-return[]
110127

111-
// tag::fun-aggregate-single-tuple-return[]
128+
# tag::fun-aggregate-single-tuple-return[]
112129
fun karma_sum_and_sum_squares() -> double, double:
113130
match
114131
$karma isa karma;
115132
let $karma-squared = $karma * $karma;
116133
return sum($karma), sum($karma-squared);
117-
// end::fun-aggregate-single-tuple-return[]
134+
# end::fun-aggregate-single-tuple-return[]
118135

119-
// tag::with-fun-defined[]
136+
# tag::with-fun-defined[]
120137
fun karma_with_squared_value($user: user) -> karma, double:
121138
match
122139
$user has karma $karma;
123140
let $karma-squared = $karma * $karma;
124141
return first $karma, $karma-squared;
125-
// end::with-fun-defined[]
142+
# end::with-fun-defined[]
126143

127-
// tag::fun-stream-users[]
144+
# tag::fun-stream-users[]
128145
fun users() -> { user }:
129146
match
130147
$user isa user;
131148
return { $user };
132-
// end::fun-stream-users[]
149+
# end::fun-stream-users[]
133150

134-
// tag::fun-stream-users-with-first[]
151+
# tag::fun-stream-users-with-first[]
135152
fun first_user() -> user:
136153
match
137154
$user isa user;
138155
return first $user;
139-
// end::fun-stream-users-with-first[]
156+
# end::fun-stream-users-with-first[]
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
// tag::undefine-keyword[]
1+
# tag::undefine-keyword[]
22
undefine
3-
// end::undefine-keyword[]
3+
# end::undefine-keyword[]
44

5-
// tag::with-fun-undefined[]
5+
# tag::with-fun-undefined[]
66
fun karma_with_squared_value;
7-
// end::with-fun-undefined[]
7+
# end::with-fun-undefined[]

reference/modules/ROOT/pages/typeql/annotations/abstract.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
= `@abstract` annotation
22
:page-aliases: {page-version}@reference::typeql/statements/abstract.adoc
3+
:test-typeql: linear
34

45
The `@abstract` annotation is used
56
// tag::overview[]
@@ -36,6 +37,7 @@ However, it is possible to define an abstract subtype of an abstract type.
3637

3738
[,typeql]
3839
----
40+
#!test[schema]
3941
include::{page-version}@reference::example$tql/schema_annotations.tql[tags=define-keyword;abstract-attribute]
4042
----
4143

@@ -44,6 +46,7 @@ It is impossible to define an abstract subtype of a concrete type.
4446
.Wrong definition!
4547
[,typeql]
4648
----
49+
#!test[schema, fail_at=runtime]
4750
define
4851
attribute categorized-username @abstract, sub username;
4952
----
@@ -56,5 +59,6 @@ A concrete sub role type can be defined using **specialization** (keyword `as`).
5659

5760
[,typeql]
5861
----
62+
#!test[schema]
5963
include::{page-version}@reference::example$tql/schema_annotations.tql[tags=define-keyword;abstract-relation]
6064
----

reference/modules/ROOT/pages/typeql/annotations/card.adoc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
= `@card` annotation
2+
:test-typeql: linear
23

34
The `@card` annotation is used
45
// tag::overview[]
@@ -46,20 +47,31 @@ For example, the following definition specifies that an instance of the `page` t
4647

4748
[,typeql]
4849
----
50+
#!test[schema]
51+
#{{
52+
include::{page-version}@reference::example$tql/schema_annotations.tql[tags=define-keyword;name-attribute]
53+
#}}
54+
#!test[schema]
4955
include::{page-version}@reference::example$tql/schema_annotations.tql[tags=define-keyword;card-owns-13]
5056
----
5157

5258
At the same time, the `page` type can play the `posting:page` role an unlimited number of times:
5359

5460
[,typeql]
5561
----
62+
#!test[schema]
63+
#{{
64+
define relation posting relates page;
65+
#}}
66+
#!test[schema, rollback]
5667
include::{page-version}@reference::example$tql/schema_annotations.tql[tags=define-keyword;card-plays-0inf]
5768
----
5869

5970
However, an instance of the `posting` relation type can have up to a thousand `page` instances:
6071

6172
[,typeql]
6273
----
74+
#!test[schema]
6375
include::{page-version}@reference::example$tql/schema_annotations.tql[tags=define-keyword;card-relates-01k]
6476
----
6577

@@ -76,6 +88,7 @@ For example, the following query defines that:
7688

7789
[,typeql]
7890
----
91+
#!test[schema]
7992
include::{page-version}@reference::example$tql/schema_annotations.tql[tags=define-keyword;card-owns-13;profile-sub-page;user-owns-name;name-subattributes]
8093
----
8194

0 commit comments

Comments
 (0)