|
| 1 | +// Copyright 2022 Democratized Data Foundation |
| 2 | +// |
| 3 | +// Use of this software is governed by the Business Source License |
| 4 | +// included in the file licenses/BSL.txt. |
| 5 | +// |
| 6 | +// As of the Change Date specified in that file, in accordance with |
| 7 | +// the Business Source License, use of this software will be governed |
| 8 | +// by the Apache License, Version 2.0, included in the file |
| 9 | +// licenses/APL.txt. |
| 10 | + |
| 11 | +package relation_create |
| 12 | + |
| 13 | +import ( |
| 14 | + "testing" |
| 15 | + |
| 16 | + testUtils "github.com/sourcenetwork/defradb/tests/integration" |
| 17 | + |
| 18 | + relationTests "github.com/sourcenetwork/defradb/tests/integration/mutation/relation" |
| 19 | +) |
| 20 | + |
| 21 | +func TestTransactionalCreationAndLinkingOfRelationalDocumentsForward(t *testing.T) { |
| 22 | + test := testUtils.QueryTestCase{ |
| 23 | + Description: "Create relational documents, and check the links in forward direction.", |
| 24 | + |
| 25 | + Docs: map[int][]string{ |
| 26 | + // publishers |
| 27 | + 2: { |
| 28 | + // "_key": "bae-0e7c3bb5-4917-5d98-9fcf-b9db369ea6e4", |
| 29 | + `{ |
| 30 | + "name": "Website", |
| 31 | + "address": "Manning Publications" |
| 32 | + }`, |
| 33 | + |
| 34 | + // "_key": "bae-8a381044-9206-51e7-8bc8-dc683d5f2523", |
| 35 | + `{ |
| 36 | + "name": "Online", |
| 37 | + "address": "Manning Early Access Program (MEAP)" |
| 38 | + }`, |
| 39 | + }, |
| 40 | + }, |
| 41 | + |
| 42 | + TransactionalQueries: []testUtils.TransactionQuery{ |
| 43 | + // Create books related to publishers, and ensure they are correctly linked (in and out of transactions). |
| 44 | + { |
| 45 | + TransactionId: 0, |
| 46 | + |
| 47 | + Query: `mutation { |
| 48 | + create_book(data: "{\"name\": \"Book By Website\",\"rating\": 4.0, \"publisher_id\": \"bae-0e7c3bb5-4917-5d98-9fcf-b9db369ea6e4\"}") { |
| 49 | + _key |
| 50 | + } |
| 51 | + }`, |
| 52 | + |
| 53 | + Results: []map[string]any{ |
| 54 | + { |
| 55 | + "_key": "bae-5b16ccd7-9cae-5145-a56c-03cfe7787722", |
| 56 | + }, |
| 57 | + }, |
| 58 | + }, |
| 59 | + |
| 60 | + { |
| 61 | + TransactionId: 1, |
| 62 | + |
| 63 | + Query: `mutation { |
| 64 | + create_book(data: "{\"name\": \"Book By Online\",\"rating\": 4.0, \"publisher_id\": \"bae-8a381044-9206-51e7-8bc8-dc683d5f2523\"}") { |
| 65 | + _key |
| 66 | + } |
| 67 | + }`, |
| 68 | + |
| 69 | + Results: []map[string]any{ |
| 70 | + { |
| 71 | + "_key": "bae-edf7f0fc-f0fd-57e2-b695-569d87e1b251", |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + |
| 76 | + // Assert publisher -> books direction within transaction 0. |
| 77 | + { |
| 78 | + TransactionId: 0, |
| 79 | + |
| 80 | + Query: `query { |
| 81 | + publisher { |
| 82 | + _key |
| 83 | + name |
| 84 | + published { |
| 85 | + _key |
| 86 | + name |
| 87 | + } |
| 88 | + } |
| 89 | + }`, |
| 90 | + |
| 91 | + Results: []map[string]any{ |
| 92 | + { |
| 93 | + "_key": "bae-0e7c3bb5-4917-5d98-9fcf-b9db369ea6e4", |
| 94 | + "name": "Website", |
| 95 | + "published": map[string]any{ |
| 96 | + "_key": "bae-5b16ccd7-9cae-5145-a56c-03cfe7787722", |
| 97 | + "name": "Book By Website", |
| 98 | + }, |
| 99 | + }, |
| 100 | + |
| 101 | + { |
| 102 | + "_key": "bae-8a381044-9206-51e7-8bc8-dc683d5f2523", |
| 103 | + "name": "Online", |
| 104 | + "published": nil, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + |
| 109 | + // Assert publisher -> books direction within transaction 1. |
| 110 | + { |
| 111 | + TransactionId: 1, |
| 112 | + |
| 113 | + Query: `query { |
| 114 | + publisher { |
| 115 | + _key |
| 116 | + name |
| 117 | + published { |
| 118 | + _key |
| 119 | + name |
| 120 | + } |
| 121 | + } |
| 122 | + }`, |
| 123 | + |
| 124 | + Results: []map[string]any{ |
| 125 | + { |
| 126 | + "_key": "bae-0e7c3bb5-4917-5d98-9fcf-b9db369ea6e4", |
| 127 | + "name": "Website", |
| 128 | + "published": nil, |
| 129 | + }, |
| 130 | + |
| 131 | + { |
| 132 | + "_key": "bae-8a381044-9206-51e7-8bc8-dc683d5f2523", |
| 133 | + "name": "Online", |
| 134 | + "published": map[string]any{ |
| 135 | + "_key": "bae-edf7f0fc-f0fd-57e2-b695-569d87e1b251", |
| 136 | + "name": "Book By Online", |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + |
| 143 | + // Assert books -> publisher direction outside the transactions. |
| 144 | + Query: `query { |
| 145 | + book { |
| 146 | + _key |
| 147 | + name |
| 148 | + publisher { |
| 149 | + _key |
| 150 | + name |
| 151 | + } |
| 152 | + } |
| 153 | + }`, |
| 154 | + |
| 155 | + Results: []map[string]any{ |
| 156 | + { |
| 157 | + "_key": "bae-5b16ccd7-9cae-5145-a56c-03cfe7787722", |
| 158 | + "name": "Book By Website", |
| 159 | + "publisher": map[string]any{ |
| 160 | + "_key": "bae-0e7c3bb5-4917-5d98-9fcf-b9db369ea6e4", |
| 161 | + "name": "Website", |
| 162 | + }, |
| 163 | + }, |
| 164 | + |
| 165 | + { |
| 166 | + "_key": "bae-edf7f0fc-f0fd-57e2-b695-569d87e1b251", |
| 167 | + "name": "Book By Online", |
| 168 | + "publisher": map[string]any{ |
| 169 | + "_key": "bae-8a381044-9206-51e7-8bc8-dc683d5f2523", |
| 170 | + "name": "Online", |
| 171 | + }, |
| 172 | + }, |
| 173 | + }, |
| 174 | + |
| 175 | + DisableMapStore: true, |
| 176 | + } |
| 177 | + |
| 178 | + relationTests.ExecuteTestCase(t, test) |
| 179 | +} |
| 180 | + |
| 181 | +func TestTransactionalCreationAndLinkingOfRelationalDocumentsBackward(t *testing.T) { |
| 182 | + test := testUtils.QueryTestCase{ |
| 183 | + Description: "Create relational documents, and check the links in backward direction.", |
| 184 | + |
| 185 | + Docs: map[int][]string{ |
| 186 | + // publishers |
| 187 | + 2: { |
| 188 | + // "_key": "bae-0e7c3bb5-4917-5d98-9fcf-b9db369ea6e4", |
| 189 | + `{ |
| 190 | + "name": "Website", |
| 191 | + "address": "Manning Publications" |
| 192 | + }`, |
| 193 | + |
| 194 | + // "_key": "bae-8a381044-9206-51e7-8bc8-dc683d5f2523", |
| 195 | + `{ |
| 196 | + "name": "Online", |
| 197 | + "address": "Manning Early Access Program (MEAP)" |
| 198 | + }`, |
| 199 | + }, |
| 200 | + }, |
| 201 | + |
| 202 | + TransactionalQueries: []testUtils.TransactionQuery{ |
| 203 | + // Create books related to publishers, and ensure they are correctly linked (in and out of transactions). |
| 204 | + { |
| 205 | + TransactionId: 0, |
| 206 | + |
| 207 | + Query: `mutation { |
| 208 | + create_book(data: "{\"name\": \"Book By Website\",\"rating\": 4.0, \"publisher_id\": \"bae-0e7c3bb5-4917-5d98-9fcf-b9db369ea6e4\"}") { |
| 209 | + _key |
| 210 | + } |
| 211 | + }`, |
| 212 | + |
| 213 | + Results: []map[string]any{ |
| 214 | + { |
| 215 | + "_key": "bae-5b16ccd7-9cae-5145-a56c-03cfe7787722", |
| 216 | + }, |
| 217 | + }, |
| 218 | + }, |
| 219 | + |
| 220 | + { |
| 221 | + TransactionId: 1, |
| 222 | + |
| 223 | + Query: `mutation { |
| 224 | + create_book(data: "{\"name\": \"Book By Online\",\"rating\": 4.0, \"publisher_id\": \"bae-8a381044-9206-51e7-8bc8-dc683d5f2523\"}") { |
| 225 | + _key |
| 226 | + } |
| 227 | + }`, |
| 228 | + |
| 229 | + Results: []map[string]any{ |
| 230 | + { |
| 231 | + "_key": "bae-edf7f0fc-f0fd-57e2-b695-569d87e1b251", |
| 232 | + }, |
| 233 | + }, |
| 234 | + }, |
| 235 | + |
| 236 | + // Assert publisher -> books direction within transaction 0. |
| 237 | + { |
| 238 | + TransactionId: 0, |
| 239 | + |
| 240 | + Query: `query { |
| 241 | + book { |
| 242 | + _key |
| 243 | + name |
| 244 | + publisher { |
| 245 | + _key |
| 246 | + name |
| 247 | + } |
| 248 | + } |
| 249 | + }`, |
| 250 | + |
| 251 | + Results: []map[string]any{ |
| 252 | + { |
| 253 | + "_key": "bae-5b16ccd7-9cae-5145-a56c-03cfe7787722", |
| 254 | + "name": "Book By Website", |
| 255 | + "publisher": map[string]any{ |
| 256 | + "_key": "bae-0e7c3bb5-4917-5d98-9fcf-b9db369ea6e4", |
| 257 | + "name": "Website", |
| 258 | + }, |
| 259 | + }, |
| 260 | + }, |
| 261 | + }, |
| 262 | + |
| 263 | + // Assert publisher -> books direction within transaction 1. |
| 264 | + { |
| 265 | + TransactionId: 1, |
| 266 | + |
| 267 | + Query: `query { |
| 268 | + book { |
| 269 | + _key |
| 270 | + name |
| 271 | + publisher { |
| 272 | + _key |
| 273 | + name |
| 274 | + } |
| 275 | + } |
| 276 | + }`, |
| 277 | + |
| 278 | + Results: []map[string]any{ |
| 279 | + { |
| 280 | + "_key": "bae-edf7f0fc-f0fd-57e2-b695-569d87e1b251", |
| 281 | + "name": "Book By Online", |
| 282 | + "publisher": map[string]any{ |
| 283 | + "_key": "bae-8a381044-9206-51e7-8bc8-dc683d5f2523", |
| 284 | + "name": "Online", |
| 285 | + }, |
| 286 | + }, |
| 287 | + }, |
| 288 | + }, |
| 289 | + }, |
| 290 | + |
| 291 | + // Assert publishers -> books direction outside the transactions. |
| 292 | + Query: `query { |
| 293 | + publisher { |
| 294 | + _key |
| 295 | + name |
| 296 | + published { |
| 297 | + _key |
| 298 | + name |
| 299 | + } |
| 300 | + } |
| 301 | + }`, |
| 302 | + |
| 303 | + Results: []map[string]any{ |
| 304 | + { |
| 305 | + "_key": "bae-0e7c3bb5-4917-5d98-9fcf-b9db369ea6e4", |
| 306 | + "name": "Website", |
| 307 | + "published": map[string]any{ |
| 308 | + "_key": "bae-5b16ccd7-9cae-5145-a56c-03cfe7787722", |
| 309 | + "name": "Book By Website", |
| 310 | + }, |
| 311 | + }, |
| 312 | + |
| 313 | + { |
| 314 | + "_key": "bae-8a381044-9206-51e7-8bc8-dc683d5f2523", |
| 315 | + "name": "Online", |
| 316 | + "published": map[string]any{ |
| 317 | + "_key": "bae-edf7f0fc-f0fd-57e2-b695-569d87e1b251", |
| 318 | + "name": "Book By Online", |
| 319 | + }, |
| 320 | + }, |
| 321 | + }, |
| 322 | + |
| 323 | + DisableMapStore: true, |
| 324 | + } |
| 325 | + |
| 326 | + relationTests.ExecuteTestCase(t, test) |
| 327 | +} |
0 commit comments