-
Notifications
You must be signed in to change notification settings - Fork 6
/
snooze-hook-test.ss
109 lines (91 loc) · 2.91 KB
/
snooze-hook-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
#lang scheme/base
(require "test-base.ss")
(require "snooze-api.ss"
"core/core.ss"
"sql/sql.ss")
; Helpers ----------------------------------------
(define saved (box #f))
(define deleted (box #f))
(define (clear-boxes!)
(set-box! saved #f)
(set-box! deleted #f))
(define-values (old-save-hook old-delete-hook)
(values (entity-on-save person)
(entity-on-delete person)))
(define (new-save-hook continue conn per)
(if (equal? (person-name per) "Jason")
(error "Aieeeeeee!")
(let ([per2 (continue conn per)])
(set-box! saved per2)
per2)))
(define (new-delete-hook continue conn per)
(if (equal? (person-name per) "Freddy")
(error "Aieeeeeee!")
(begin
(set-box! deleted per)
(continue conn per))))
; Tests ------------------------------------------
; test-suite
(define/provide-test-suite snooze-hook-tests
#:before
(lambda ()
(set-entity-on-save! person new-save-hook)
(set-entity-on-delete! person new-delete-hook))
#:after
(lambda ()
(set-entity-on-save! person old-save-hook)
(set-entity-on-delete! person old-delete-hook))
(test-suite "on-save"
(test-case "normal"
(around
(recreate-test-tables)
(let* ([p1 (make-person "Freddy")]
[p2 (save! p1)])
(check snooze=? p2 p1)
; Check side effects:
(check-equal? (unbox saved) p2)
(check-false (unbox deleted))
; Check database:
(check-equal? (find-people) (list p2)))
(clear-boxes!)))
(test-case "error"
(around
(recreate-test-tables)
(let* ([p1 (make-person "Jason")]
[p2 (with-handlers ([exn? (lambda _ #f)])
(save! p1))])
(check-false p2)
; Check side effects:
(check-false (unbox saved))
(check-false (unbox deleted))
; Check database:
(check-equal? (find-people) null))
(clear-boxes!))))
(test-suite "on-delete"
(test-case "normal"
(around
(recreate-test-tables)
(let* ([p1 (after (save! (make-person "Joe"))
(clear-boxes!))]
[p2 (delete! p1)])
(check snooze=? p2 p1)
; Check side effects:
(check-false (unbox saved))
(check-equal? (unbox deleted) p1)
; Check database:
(check-equal? (find-people) null))
(clear-boxes!)))
(test-case "error"
(around
(recreate-test-tables)
(let* ([p1 (after (save! (make-person "Freddy"))
(clear-boxes!))]
[p2 (with-handlers ([exn? (lambda _ #f)])
(delete! p1))])
(check-false p2)
; Check side effects:
(check-false (unbox saved))
(check-false (unbox deleted))
; Check database:
(check-equal? (find-people) (list p1)))
(clear-boxes!)))))