-
Notifications
You must be signed in to change notification settings - Fork 6
/
snooze-foreign-key-test.ss
76 lines (63 loc) · 2.97 KB
/
snooze-foreign-key-test.ss
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
#lang scheme/base
(require srfi/26
"snooze-api.ss"
"test-base.ss"
"core/core.ss")
; Tests ------------------------------------------
; Tests of foreign key accessors (e.g. pet-owner).
(define snooze-foreign-key-tests
(test-suite "snooze-foreign-key-tests: (foo-bar foo1) -> bar1"
(test-case "snooze-struct-ref : loads struct from database"
(recreate-test-tables)
(let* ([per (save! (make-person "Per1"))]
[pet1 (save! (make-pet per "Pet1"))]
[pet2 (select-one #:from pet)])
(check-equal? (snooze-struct-ref pet2 (attr pet owner)) per)))
(test-case "snooze-struct-ref* : loads struct from database"
(recreate-test-tables)
(let* ([per (save! (make-person "Per1"))]
[pet1 (save! (make-pet per "Pet1"))]
[pet2 (select-one #:from pet)])
(check-equal? (caddr (snooze-struct-ref* pet2)) per)))
(test-case "pet-owner : loads struct from database"
(recreate-test-tables)
(let* ([per (save! (make-person "Per1"))]
[pet1 (save! (make-pet per "Pet1"))]
[pet2 (select-one #:from pet)])
(check-equal? (pet-owner pet2) per)))
(test-case "foreign key relation between unsaved structs"
(recreate-test-tables)
(let* ([per1 (make-person "Per1")]
[pet1 (make-pet per1 "Pet1")])
(check-eq? (pet-owner pet1) per1)))
(test-case "foreign key relation between unsaved structs : retained under copying"
(recreate-test-tables)
(let* ([per1 (make-person "Per1")]
[pet1 (make-pet per1 "Pet1")]
[pet2 (pet-set pet1 #:name "Pet2")])
(check-eq? (pet-owner pet1) (pet-owner pet2))))
(test-case "foreign key relation between unsaved structs : updated (to create a new copy)"
(recreate-test-tables)
(let* ([per1 (make-person "Per1")]
[per2 (make-person "Per2")]
[pet1 (make-pet per1 "Pet1")]
[pet2 (pet-set pet1 #:owner per2)])
(check-eq? per1 (pet-owner pet1))
(check-eq? per2 (pet-owner pet2))))
(test-case "pet-owner and pet-owner-guid"
(recreate-test-tables)
(let ([pet1 (make-pet/defaults #:name "Garfield" #:owner (make-person/defaults #:name "Jon"))]
[pet2 ((entity-private-constructor pet)
(entity-make-temporary-guid pet)
#f
(snooze-struct-guid (save! (make-person/defaults #:name "Lyman")))
"Odie")]
[pet3 (make-pet/defaults #:name "Top Cat" #:owner #f)])
(check-pred person? (pet-owner pet1))
(check-pred person? (pet-owner pet2))
(check-false (pet-owner pet3))
(check-pred (entity-guid-predicate person) (pet-owner-guid pet1))
(check-pred (entity-guid-predicate person) (pet-owner-guid pet2))
(check-false (pet-owner pet3))))))
; Provide statements -----------------------------
(provide snooze-foreign-key-tests)