1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
#![allow(clippy::unwrap_used)] // Reason: test code, panics are acceptable
//! End-to-End Tests: Python Schema Authoring → JSON Compilation → Runtime Execution
//!
//! This test suite validates the complete flow from Python federation decorator authoring
//! through JSON schema compilation to Rust runtime execution.
//!
//! Test scenarios:
//! 1. Basic federation key declarations (@key)
//! 2. Extended types with @extends
//! 3. External fields with @external
//! 4. Field requirements with @requires
//! 5. Field provisions with @provides
//! 6. Multi-key entities
//! 7. Entity resolution from compiled schema
//! 8. Cross-subgraph entity references
//! 9. Query execution with federation metadata
//! 10. Error handling for schema violations
use std::collections::HashMap;
use fraiseql_core::federation::types::{
EntityRepresentation, FederatedType, FederationMetadata, FieldFederationDirectives,
FieldPathSelection, KeyDirective,
};
use serde_json::json;
// ============================================================================
// Test: Basic Federation Key Declaration (Python: @key)
// ============================================================================
#[test]
fn test_python_basic_federation_key() {
// TEST: Python @key decorator should generate correct FederatedType
// PYTHON SCHEMA:
// @type
// @key("id")
// class User:
// id: str
// name: str
//
// WHEN: Compiled to FederationMetadata
// THEN: Should have single key with id field
let mut user_type = FederatedType::new("User".to_string());
user_type.keys.push(KeyDirective {
fields: vec!["id".to_string()],
resolvable: true,
});
let metadata = FederationMetadata {
enabled: true,
version: "v2".to_string(),
types: vec![user_type],
};
assert!(metadata.enabled);
assert_eq!(metadata.version, "v2");
assert_eq!(metadata.types.len(), 1);
assert_eq!(metadata.types[0].keys.len(), 1);
assert_eq!(metadata.types[0].keys[0].fields, vec!["id".to_string()]);
}
#[test]
fn test_python_multiple_federation_keys() {
// TEST: Python @key @key pattern for multiple keys
// PYTHON SCHEMA:
// @type
// @key("tenant_id")
// @key("id")
// class Account:
// tenant_id: str
// id: str
//
// WHEN: Compiled
// THEN: Should have two keys
let mut account_type = FederatedType::new("Account".to_string());
account_type.keys.push(KeyDirective {
fields: vec!["tenant_id".to_string()],
resolvable: true,
});
account_type.keys.push(KeyDirective {
fields: vec!["id".to_string()],
resolvable: true,
});
let metadata = FederationMetadata {
enabled: true,
version: "v2".to_string(),
types: vec![account_type],
};
assert_eq!(metadata.types[0].keys.len(), 2);
}
#[test]
fn test_python_composite_key() {
// TEST: Python composite key with multiple fields
// PYTHON SCHEMA:
// @type
// @key("org_id tenant_id")
// class TeamMember:
// org_id: str
// tenant_id: str
//
// WHEN: Compiled
// THEN: Should have single key with two fields
let mut team_member = FederatedType::new("TeamMember".to_string());
team_member.keys.push(KeyDirective {
fields: vec!["org_id".to_string(), "tenant_id".to_string()],
resolvable: true,
});
let metadata = FederationMetadata {
enabled: true,
version: "v2".to_string(),
types: vec![team_member],
};
assert_eq!(metadata.types[0].keys[0].fields.len(), 2);
assert!(metadata.types[0].keys[0].fields.contains(&"org_id".to_string()));
assert!(metadata.types[0].keys[0].fields.contains(&"tenant_id".to_string()));
}
// ============================================================================
// Test: Extended Types (@extends)
// ============================================================================
#[test]
fn test_python_extended_type() {
// TEST: Python @extends decorator for type extension
// PYTHON SCHEMA IN ORDERS SUBGRAPH:
// @type
// @extends
// @key("id")
// class User:
// @external
// id: str
// orders: list[Order]
//
// WHEN: Extended type is compiled
// THEN: Should mark as extended with external fields
let mut user_type = FederatedType::new("User".to_string());
user_type.is_extends = true;
user_type.external_fields.push("id".to_string());
user_type.keys.push(KeyDirective {
fields: vec!["id".to_string()],
resolvable: true,
});
let metadata = FederationMetadata {
enabled: true,
version: "v2".to_string(),
types: vec![user_type],
};
assert!(metadata.types[0].is_extends);
assert!(metadata.types[0].external_fields.contains(&"id".to_string()));
}
// ============================================================================
// Test: Field Requirements (@requires)
// ============================================================================
#[test]
fn test_python_requires_directive() {
// TEST: Python field with @requires decorator
// PYTHON SCHEMA:
// @type
// class Order:
// id: str
// @requires("weight")
// def shipping_estimate(self) -> float:
// pass
//
// WHEN: Compiled with @requires
// THEN: Should generate field directives with requires
let mut order_type = FederatedType::new("Order".to_string());
order_type.keys.push(KeyDirective {
fields: vec!["id".to_string()],
resolvable: true,
});
order_type.set_field_directives(
"shippingEstimate".to_string(),
FieldFederationDirectives {
requires: vec![FieldPathSelection {
path: vec!["weight".to_string()],
typename: "Order".to_string(),
}],
provides: vec![],
external: false,
shareable: false,
},
);
let metadata = FederationMetadata {
enabled: true,
version: "v2".to_string(),
types: vec![order_type],
};
let directives = metadata.types[0].get_field_directives("shippingEstimate");
assert!(directives.is_some());
assert_eq!(directives.unwrap().requires.len(), 1);
}
#[test]
fn test_python_multiple_requires() {
// TEST: Python field with multiple @requires
// PYTHON SCHEMA:
// @type
// class Order:
// @requires("weight")
// @requires("dimensions")
// def shipping_cost(self) -> float:
// pass
//
// WHEN: Compiled
// THEN: Should have two requires directives
let mut order_type = FederatedType::new("Order".to_string());
order_type.keys.push(KeyDirective {
fields: vec!["id".to_string()],
resolvable: true,
});
order_type.set_field_directives(
"shippingCost".to_string(),
FieldFederationDirectives::new()
.add_requires(FieldPathSelection {
path: vec!["weight".to_string()],
typename: "Order".to_string(),
})
.add_requires(FieldPathSelection {
path: vec!["dimensions".to_string()],
typename: "Order".to_string(),
}),
);
let metadata = FederationMetadata {
enabled: true,
version: "v2".to_string(),
types: vec![order_type],
};
let directives = metadata.types[0].get_field_directives("shippingCost");
assert_eq!(directives.unwrap().requires.len(), 2);
}
// ============================================================================
// Test: Field Provisions (@provides)
// ============================================================================
#[test]
fn test_python_provides_directive() {
// TEST: Python field with @provides decorator
// PYTHON SCHEMA:
// @type
// class User:
// @provides("Order.userId")
// def orders(self) -> list[Order]:
// pass
//
// WHEN: Compiled with @provides
// THEN: Should generate field directives with provides
let mut user_type = FederatedType::new("User".to_string());
user_type.keys.push(KeyDirective {
fields: vec!["id".to_string()],
resolvable: true,
});
user_type.set_field_directives(
"orders".to_string(),
FieldFederationDirectives::new().add_provides(FieldPathSelection {
path: vec!["userId".to_string()],
typename: "Order".to_string(),
}),
);
let metadata = FederationMetadata {
enabled: true,
version: "v2".to_string(),
types: vec![user_type],
};
let directives = metadata.types[0].get_field_directives("orders");
assert!(directives.is_some());
assert_eq!(directives.unwrap().provides.len(), 1);
}
// ============================================================================
// Test: Runtime Entity Resolution from Compiled Schema
// ============================================================================
#[test]
fn test_python_entity_resolution_basic() {
// TEST: Resolve entity from compiled Python schema
// GIVEN: User type with id key field
// WHEN: Entity representation is provided
// THEN: Should resolve against compiled schema
let mut user_type = FederatedType::new("User".to_string());
user_type.keys.push(KeyDirective {
fields: vec!["id".to_string()],
resolvable: true,
});
let _federation_metadata = FederationMetadata {
enabled: true,
version: "v2".to_string(),
types: vec![user_type],
};
let mut entity_fields = HashMap::new();
entity_fields.insert("id".to_string(), json!("user-123"));
entity_fields.insert("name".to_string(), json!("Alice"));
entity_fields.insert("email".to_string(), json!("alice@example.com"));
let representation = EntityRepresentation {
typename: "User".to_string(),
key_fields: {
let mut m = HashMap::new();
m.insert("id".to_string(), json!("user-123"));
m
},
all_fields: entity_fields,
};
assert_eq!(representation.typename, "User");
assert_eq!(representation.key_fields.get("id").unwrap(), &json!("user-123"));
assert!(representation.has_field("name"));
assert!(representation.has_field("email"));
}
#[test]
fn test_python_entity_resolution_with_requires() {
// TEST: Resolve entity with @requires validation
// GIVEN: Field with @requires("email")
// WHEN: Entity includes email field
// THEN: Resolution should succeed
let mut user_type = FederatedType::new("User".to_string());
user_type.keys.push(KeyDirective {
fields: vec!["id".to_string()],
resolvable: true,
});
user_type.set_field_directives(
"premiumContent".to_string(),
FieldFederationDirectives::new().add_requires(FieldPathSelection {
path: vec!["email".to_string()],
typename: "User".to_string(),
}),
);
let metadata = FederationMetadata {
enabled: true,
version: "v2".to_string(),
types: vec![user_type],
};
let mut entity_fields = HashMap::new();
entity_fields.insert("id".to_string(), json!("user-123"));
entity_fields.insert("email".to_string(), json!("alice@example.com"));
let representation = EntityRepresentation {
typename: "User".to_string(),
key_fields: {
let mut m = HashMap::new();
m.insert("id".to_string(), json!("user-123"));
m
},
all_fields: entity_fields,
};
// Validate @requires is satisfied
let directives = metadata.types[0].get_field_directives("premiumContent");
assert!(directives.is_some());
assert!(representation.has_field("email"));
}
// ============================================================================
// Test: Multi-Subgraph Federation
// ============================================================================
#[test]
fn test_python_cross_subgraph_reference() {
// TEST: Two subgraphs with entity references
// SUBGRAPH 1 (Users):
// @type @key("id") class User
//
// SUBGRAPH 2 (Orders):
// @type @key("id") class Order
// @type class User (extended reference)
//
// WHEN: Compiled to federation metadata
// THEN: Both should be tracked
let mut user_type = FederatedType::new("User".to_string());
user_type.keys.push(KeyDirective {
fields: vec!["id".to_string()],
resolvable: true,
});
let mut order_type = FederatedType::new("Order".to_string());
order_type.keys.push(KeyDirective {
fields: vec!["id".to_string()],
resolvable: true,
});
let metadata = FederationMetadata {
enabled: true,
version: "v2".to_string(),
types: vec![user_type, order_type],
};
assert_eq!(metadata.types.len(), 2);
assert!(metadata.types.iter().any(|t| t.name == "User"));
assert!(metadata.types.iter().any(|t| t.name == "Order"));
}
// ============================================================================
// Test: Query Execution with Federation Metadata
// ============================================================================
#[test]
fn test_python_federation_query_types() {
// TEST: Standard federation schema should have Query type
// PYTHON SCHEMA:
// @type
// class Query:
// def user(self, id: str) -> User:
// pass
//
// WHEN: Compiled
// THEN: Query type should exist in federation metadata
let mut query_type = FederatedType::new("Query".to_string());
query_type.keys.push(KeyDirective {
fields: vec!["__typename".to_string()],
resolvable: false,
});
let metadata = FederationMetadata {
enabled: true,
version: "v2".to_string(),
types: vec![query_type],
};
let query = metadata.types.iter().find(|t| t.name == "Query");
assert!(query.is_some());
}
// ============================================================================
// Test: Error Handling
// ============================================================================
#[test]
fn test_python_missing_key_validation() {
// TEST: Entity without key fields should fail validation
// GIVEN: User type with id key requirement
// WHEN: Entity representation lacks id
// THEN: Should identify missing key field
let _user_type = FederatedType::new("User".to_string());
// Empty key fields means representation is incomplete
let representation = EntityRepresentation {
typename: "User".to_string(),
key_fields: HashMap::new(),
all_fields: HashMap::new(),
};
assert!(representation.key_fields.is_empty());
}
#[test]
fn test_python_schema_with_no_federation() {
// TEST: Standalone schema without federation should still work
// PYTHON SCHEMA:
// @type
// class User:
// id: str
//
// WHEN: Compiled without federation directives
// THEN: Should create type but not mark as federated
let user_type = FederatedType::new("User".to_string());
let federation_metadata = FederationMetadata {
enabled: false,
version: "v2".to_string(),
types: vec![user_type],
};
assert!(!federation_metadata.enabled);
}
// ============================================================================
// Test: Schema Compilation Verification
// ============================================================================
#[test]
fn test_python_schema_compilation_roundtrip() {
// TEST: Compiled schema should be consistent
// GIVEN: Python schema with federation directives
// WHEN: Compiled to metadata
// THEN: Should be readable and executable
let mut user_type = FederatedType::new("User".to_string());
user_type.keys.push(KeyDirective {
fields: vec!["id".to_string()],
resolvable: true,
});
user_type.set_field_directives("email".to_string(), FieldFederationDirectives::new());
let metadata = FederationMetadata {
enabled: true,
version: "v2".to_string(),
types: vec![user_type],
};
// Verify metadata is complete and consistent
assert_eq!(metadata.types.len(), 1);
assert_eq!(metadata.types[0].name, "User");
assert!(!metadata.types[0].keys.is_empty());
assert!(metadata.types[0].get_field_directives("email").is_some());
}
#[test]
fn test_python_multiple_types_in_schema() {
// TEST: Schema with multiple types
// PYTHON SCHEMA:
// @type @key("id") class User: ...
// @type @key("id") class Order: ...
// @type @key("id") class Product: ...
//
// WHEN: All compiled to single metadata
// THEN: All types should be available
let user_type = FederatedType::new("User".to_string());
let order_type = FederatedType::new("Order".to_string());
let product_type = FederatedType::new("Product".to_string());
let metadata = FederationMetadata {
enabled: true,
version: "v2".to_string(),
types: vec![user_type, order_type, product_type],
};
assert_eq!(metadata.types.len(), 3);
let type_names: Vec<_> = metadata.types.iter().map(|t| t.name.as_str()).collect();
assert!(type_names.contains(&"User"));
assert!(type_names.contains(&"Order"));
assert!(type_names.contains(&"Product"));
}