-
Notifications
You must be signed in to change notification settings - Fork 6
/
snooze-save-test.ss
126 lines (109 loc) · 5.43 KB
/
snooze-save-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
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
#lang scheme/base
(require "test-base.ss")
(require scheme/dict
srfi/19
(unlib-in hash time)
"snooze-api.ss"
"core/core.ss"
(prefix-in real: "core/snooze-struct.ss"))
; Tests -------------------------------------------
; test-suite
(define snooze-save-tests
(test-suite "snooze-save-tests"
(test-case "save! : affects snooze-struct-saved?, temporary-guid? and database-guid?"
(recreate-test-tables)
(let ([per1 (make-person "Dave")])
(check-false (snooze-struct-saved? per1))
(check-pred temporary-guid? (snooze-struct-guid per1))
(check-false (database-guid? (snooze-struct-guid per1)))
(let ([per2 (save! per1)])
(check-pred snooze-struct-saved? per1)
(check-pred snooze-struct-saved? per2)
(check-false (temporary-guid? (snooze-struct-guid per1)))
(check-false (temporary-guid? (snooze-struct-guid per2)))
(check-pred database-guid? (snooze-struct-guid per1))
(check-pred database-guid? (snooze-struct-guid per2)))))
(test-case "save! : stores information correctly in the database"
(recreate-test-tables)
(let* ([per1 (save! (make-person "Dave"))]
[per2 (save! (make-person "Dave"))]
[per3 (save! per1)]
[per4 (save! (make-person "Noel"))])
(check-equal? (direct-query "select guid,name from person order by guid asc;")
(list (list (snooze-struct-id per1) "Dave")
(list (snooze-struct-id per2) "Dave")
(list (snooze-struct-id per4) "Noel")))))
(test-case "save! : consecutive saves"
(recreate-test-tables)
(let* ([per1 (save! (make-person "Jon"))]
[per2 (save! (person-set per1 #:name "Lyman"))]
[per3 (save! (make-person "Liz"))]
[per4 (save! (make-person "Liz"))])
(check-equal? (direct-query "select count(guid) from person where name = 'Jon';") (list (list 0)))
(check-equal? (direct-query "select count(guid) from person where name = 'Lyman';") (list (list 1)))
(check-equal? (direct-query "select count(guid) from person where name = 'Liz';") (list (list 2)))))
(test-case "save! : bad data types"
(recreate-test-tables)
(check-exn exn:fail:snooze?
(cut save! ((entity-constructor person)
(entity-make-temporary-guid person)
#f
'R2D2)))
(let ([pet1 (save! (make-pet #f "Garfield"))])
(check-exn exn:fail:snooze?
(cut save! ((entity-constructor pet)
(entity-make-temporary-guid pet)
#f
pet1
"Odie")))))
(test-case "save! : revision incremented on save"
(recreate-test-tables)
(let ([per0 (make-person "Dave")])
(check-false (snooze-struct-revision per0))
(let ([per1 (save! per0)])
(check-equal? (snooze-struct-revision per1) 0)
(check-equal? (snooze-struct-revision per0) #f)
(let ([per2 (save! (person-set per1 #:name "Noel"))])
(check-equal? (snooze-struct-revision per2) 1)
(check-equal? (snooze-struct-revision per1) 0)
(check-equal? (snooze-struct-revision per0) #f))))
(check-equal? (direct-query "select count(guid) from person where revision = 0;") (list (list 0)))
(check-equal? (direct-query "select count(guid) from person where revision = 1;") (list (list 1))))
(test-case "save! : cannot save an out-of-date struct"
(recreate-test-tables)
(let* ([per1 (save! (make-person "Dave"))]
[per2 (save! (person-set per1 #:name "Noel"))])
(check-exn exn:fail:snooze? (cut save! (person-set per1 #:name "Matt")))))
(test-case "save! : cannot save a struct with local-only foreign keys"
(recreate-test-tables)
(let* ([per (make-person "Jon")]
[pet (make-pet per "Garfield")])
(check-exn exn:fail:snooze? (cut save! pet)))
(recreate-test-tables)
(let* ([per (save! (make-person "Jon"))]
[pet1 (save! (make-pet per "Garfield"))]
[pet2 (save! (make-pet (pet-owner pet1) "Garfield"))])
(check-not-exn (cut save! pet2)))
(recreate-test-tables)
(let* ([per (make-person "Jon")]
[pet (make-pet per "Garfield")])
(check-exn exn:fail:snooze? (cut save! pet)))
(recreate-test-tables)
(let* ([per0 (make-person "Jon")]
[per1 (save! per0)]
[per2 (person-set per1 #:name "Lyman")]
[pet (make-pet per2 "Garfield")])
(check-not-exn (cut save! pet))))
(test-case "save! : data serialized/deserialized successfuly"
(let* ([struct1 (save! (make-course 'COURSE1 "Course 1" 1 0.1 #t (date->time-tai (make-date 0 0 0 12 01 01 2009 3600)) '(a b c)))]
[vals1 (snooze-struct-data-ref* struct1)]
[id1 (snooze-struct-id struct1)]
[struct2 (find-by-id course id1)]
[vals2 (snooze-struct-data-ref* struct2)]
[attrs (entity-data-attributes course)])
(for ([attr (in-list attrs)]
[val1 (in-list vals1)]
[val2 (in-list vals2)])
(check-equal? val1 val2 (format "attribute failed: ~a" (attribute-name attr))))))))
; Provide statements -----------------------------
(provide snooze-save-tests)