-
Notifications
You must be signed in to change notification settings - Fork 2
/
cl-linq-tests.lisp
89 lines (82 loc) · 2.21 KB
/
cl-linq-tests.lisp
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
(defpackage :cl-linq-tests
(:use
:common-lisp
:cl-linq)
(:export
#:run-tests))
(in-package :cl-linq-tests)
(def-suite cl-linq-tests
:description "CL-LINQ tests")
(in-suite cl-linq-tests)
(test select-columns
(let ((data
'((a b c)
(1 2 3)
(4 5 6)))
(data-assoc
'(((:name . "bob") (:age . 20))
((:name . "frank") (:age . 25)))))
(is (equalp
'((a c)
(1 3)
(4 6))
(cl-linq::select-columns data '(0 2))))
(is (equalp
'((3 (c b a))
(3 (3 2 1))
(3 (6 5 4)))
(cl-linq::select-columns data (list #'length #'reverse))))
(is (equalp
'((20 "bob")
(25 "frank"))
(cl-linq::select-columns data-assoc '(:age :name))))))
(defparameter *people*
`(((:name . "stephen") (:email . "[email protected]") (:age . 33))
((:name . "bob") (:email . "[email protected]") (:age . 49))
((:name . "foo") (:email . "[email protected]") (:age . 10))))
(defparameter *subjects*
'((itb001 1 john 4.0)
(itb001 1 bob 4.0)
(itb001 1 mickey 2.0)
(itb001 2 jenny 4.0)
(itb001 2 james 3.0)
(mkb114 1 john 3.0)
(mkb114 1 erica 3.3)))
(test more-data-tests
(is (equalp
'(("bob" 49) ("stephen" 33))
(query
:select '(:name :age)
:from *people*
:where #'(lambda (row)
(> (cdr (assoc :age row)) 21)))))
(is (equalp
'((ITB001 5) (MKB114 2))
(query
:select t
:from *subjects*
:group-by '(0) ;first index
:aggregating-by (list #'length))))
(is (equalp
'(((ITB001 5) (ITB001 3.4))
((MKB114 2) (MKB114 3.15)))
(query
:select t
:from *subjects*
:group-by '(0)
:aggregating-by
(list #'length
#'(lambda (data)
(/ (apply #'+ (mapcar #'fourth data)) (length data)))))))
(is (equalp
'((ITB001 . 4)
(MKB114 . 2))
(cl-linq:QUERY
:select t
:from *subjects*
:where #'(lambda (row)
(> (fourth row) 2.0 ))
:group-by '(0)
:aggregating-by #'length))))
(defun run-tests ()
(run! 'cl-linq-tests))