1
1
using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using FluentAssertions ;
2
5
using Neo4jClient . Cypher ;
3
6
using NSubstitute ;
4
7
using Xunit ;
@@ -8,6 +11,11 @@ namespace Neo4jClient.Tests.Cypher
8
11
9
12
public class CypherFluentQueryCallTests : IClassFixture < CultureInfoSetupFixture >
10
13
{
14
+ private class Foo
15
+ {
16
+ public int Id { get ; set ; }
17
+ }
18
+
11
19
private static IRawGraphClient GraphClient_30
12
20
{
13
21
get
@@ -44,5 +52,165 @@ public void ThrowsInvalidOperationException_WhenClientVersionIsLessThan_30()
44
52
45
53
Assert . Throws < InvalidOperationException > ( ( ) => new CypherFluentQuery ( client ) . Call ( "apoc.sp" ) . Query ) ;
46
54
}
55
+
56
+ [ Fact ]
57
+ public void Call_SubQueriesAsLambda ( )
58
+ {
59
+ const string expected = @"CALL { MATCH (n)
60
+ RETURN count(n) AS c }
61
+ RETURN c" ;
62
+
63
+ var client = GraphClient_30 ;
64
+
65
+ var query = new CypherFluentQuery ( client )
66
+ . Match ( "(n)" )
67
+ . Return ( n => new { c = n . Count ( ) } ) ;
68
+
69
+ var callQuery = new CypherFluentQuery ( client )
70
+ . Call ( ( ) => query )
71
+ . Return ( c => c . As < long > ( ) ) ;
72
+
73
+ callQuery . Query . QueryText . Should ( ) . Be ( expected ) ;
74
+ }
75
+
76
+ [ Fact ]
77
+ public void Call_SubQueriesAsLambdaWithParameters ( )
78
+ {
79
+ const string expected = @"CALL { MATCH (n)
80
+ WHERE (n.Id = $p0)
81
+ RETURN count(n) AS c }
82
+ RETURN c" ;
83
+
84
+ var client = GraphClient_30 ;
85
+ var query = new CypherFluentQuery ( client )
86
+ . Match ( "(n)" )
87
+ . Where ( ( Foo n ) => n . Id == 1 )
88
+ . Return ( n => new { c = n . Count ( ) } ) ;
89
+
90
+ var callQuery = new CypherFluentQuery ( client )
91
+ . Call ( ( ) => query )
92
+ . Return ( c => c . As < long > ( ) ) ;
93
+
94
+ callQuery . Query . QueryText . Should ( ) . Be ( expected ) ;
95
+ callQuery . Query . QueryParameters . Should ( ) . HaveCount ( 1 ) ;
96
+ }
97
+
98
+ [ Fact ]
99
+ public void Call_SubQueriesAsLambdaWithParametersPriorToCall ( )
100
+ {
101
+ const string expected = @"MATCH (x)
102
+ WHERE (x.Id = $p0)
103
+ CALL { MATCH (n)
104
+ WHERE (n.Id = $p1)
105
+ RETURN count(n) AS c }
106
+ RETURN c" ;
107
+
108
+ var client = GraphClient_30 ;
109
+ var query = new CypherFluentQuery ( client )
110
+ . Match ( "(n)" )
111
+ . Where ( ( Foo n ) => n . Id == 1 )
112
+ . Return ( n => new { c = n . Count ( ) } ) ;
113
+
114
+ var callQuery = new CypherFluentQuery ( client )
115
+ . Match ( "(x)" )
116
+ . Where ( ( Foo x ) => x . Id == 2 )
117
+ . Call ( ( ) => query )
118
+ . Return ( c => c . As < long > ( ) ) ;
119
+
120
+ callQuery . Query . QueryText . Should ( ) . Be ( expected ) ;
121
+ callQuery . Query . QueryParameters . Should ( ) . HaveCount ( 2 ) ;
122
+ }
123
+
124
+ [ Fact ]
125
+ public void Call_SubQueriesAsLambdaWithParametersAfterToCall ( )
126
+ {
127
+ const string expected = @"CALL { MATCH (n)
128
+ WHERE (n.Id = $p0)
129
+ RETURN count(n) AS c }
130
+ MATCH (x)
131
+ WHERE (x.Id = $p1)
132
+ RETURN c" ;
133
+
134
+ var client = GraphClient_30 ;
135
+ var query = new CypherFluentQuery ( client )
136
+ . Match ( "(n)" )
137
+ . Where ( ( Foo n ) => n . Id == 1 )
138
+ . Return ( n => new { c = n . Count ( ) } ) ;
139
+
140
+ var callQuery = new CypherFluentQuery ( client )
141
+ . Call ( ( ) => query )
142
+ . Match ( $ "(x)")
143
+ . Where ( ( Foo x ) => x . Id == 2 )
144
+ . Return ( c => c . As < long > ( ) ) ;
145
+
146
+ callQuery . Query . QueryText . Should ( ) . Be ( expected ) ;
147
+ callQuery . Query . QueryParameters . Should ( ) . HaveCount ( 2 ) ;
148
+ }
149
+
150
+ [ Fact ]
151
+ public void Call_SubQueriesAsLambdaWithParametersPriorToCall_WholeWord ( )
152
+ {
153
+ const string expected = @"MATCH (x)
154
+ WHERE (x.Id = $p0)
155
+ CALL { MATCH (n)
156
+ WHERE (n.Id = $p1)
157
+ AND (n.Something = $p2)
158
+ RETURN count(n) AS c }
159
+ RETURN c" ;
160
+
161
+ var client = GraphClient_30 ;
162
+ var query = new CypherFluentQuery ( client )
163
+ . Match ( "(n)" )
164
+ . Where ( "(n.Id = $p0)" )
165
+ . AndWhere ( "(n.Something = $p1)" )
166
+ . WithParam ( "p0" , 1 )
167
+ . WithParam ( "p1" , 2 )
168
+ . Return ( n => new { c = n . Count ( ) } ) ;
169
+
170
+ var callQuery = new CypherFluentQuery ( client )
171
+ . Match ( "(x)" )
172
+ . Where ( "(x.Id = $p0)" )
173
+ . WithParam ( "p0" , 3 )
174
+ . Call ( ( ) => query )
175
+ . Return ( c => c . As < long > ( ) ) ;
176
+
177
+ callQuery . Query . QueryText . Should ( ) . Be ( expected ) ;
178
+ callQuery . Query . QueryParameters . Should ( ) . HaveCount ( 3 ) ;
179
+ callQuery . Query . QueryParameters . Should ( ) . ContainKeys ( "p0" , "p1" , "p2" ) ;
180
+ }
181
+
182
+ [ Fact ]
183
+ public void Call_SubQueriesAsLambdaWithParametersPriorAndAfterToCall_WholeWord ( )
184
+ {
185
+ const string expected = @"MATCH (x)
186
+ WHERE (x.Id = $p0)
187
+ CALL { MATCH (n)
188
+ WHERE (n.Id = $p1)
189
+ AND (n.Something = $p2)
190
+ RETURN count(n) AS c }
191
+ WHERE (y.Id = $p3)
192
+ RETURN c" ;
193
+
194
+ var client = GraphClient_30 ;
195
+ var query = new CypherFluentQuery ( client )
196
+ . Match ( "(n)" )
197
+ . Where ( "(n.Id = $p0)" )
198
+ . AndWhere ( "(n.Something = $p1)" )
199
+ . WithParam ( "p0" , 1 )
200
+ . WithParam ( "p1" , 2 )
201
+ . Return ( n => new { c = n . Count ( ) } ) ;
202
+
203
+ var callQuery = new CypherFluentQuery ( client )
204
+ . Match ( "(x)" )
205
+ . Where ( ( IdClass x ) => x . Id == 1 )
206
+ . Call ( ( ) => query )
207
+ . Where ( ( IdClass y ) => y . Id == 1 )
208
+ . Return ( c => c . As < long > ( ) ) ;
209
+
210
+ callQuery . Query . QueryText . Should ( ) . Be ( expected ) ;
211
+ callQuery . Query . QueryParameters . Should ( ) . HaveCount ( 4 ) ;
212
+ callQuery . Query . QueryParameters . Should ( ) . ContainKeys ( "p0" , "p1" , "p2" , "p3" ) ;
213
+ }
214
+ private class IdClass { public int Id { get ; set ; } }
47
215
}
48
216
}
0 commit comments