summaryrefslogtreecommitdiff
path: root/test/clojure_sql/dsl_test.clj
blob: e5545fc2e07cf09f786fdd9b04a3b328e4e99b49 (about) (plain)
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
(ns clojure-sql.dsl-test
  (:refer-clojure :exclude [sort-by])
  (:require [clojure-sql.dsl :refer :all]
            [midje.sweet :refer :all]))

(fact "Table creates basic queries on tables"

  (table ..name..)
  => {:tables {..name.. ..name..}}
  ;(provided (keyword? ..name..) => true)

  (table {..name.. ..alias..})
  => {:tables {..alias.. ..name..}}

  (table {..name.. ..alias.., ..name2.. ..alias2..})
  => {:tables {..alias.. ..name..,
               ..alias2.. ..name2..}})


(fact "Project restricts (and/or adds) fields of a query"
  
  (prerequisites (keyword? ..table-alias..) => true
                 (keyword? ..table..) => true
                 (keyword? ..field..) => true
                 (keyword? ..field-alias..) => true
                 (keyword? ..field2..) => true
                 (keyword? ..field2-alias..) => true)

  (fact "projecting onto a single field from one table"
    (->  (table {..table.. ..table-alias..})
         (project [..field..]))
    => {:tables {..table-alias.. ..table..}
        :fields {..field.. [..table-alias.. ..field..]}}

    (->  (table {..table.. ..table-alias..})
         (project {..field.. ..field-alias..}))
    => {:tables {..table-alias.. ..table..}
        :fields {..field-alias.. [..table-alias.. ..field..]}})


  (fact "projecting onto multiple fields from one table"
    (-> (table {..table.. ..table-alias..})
        (project [..field.. ..field2..]))
    => {:tables {..table-alias.. ..table..}
        :fields {..field.. [..table-alias.. ..field..]
                 ..field2.. [..table-alias.. ..field2..]}}

    (-> (table {..table.. ..table-alias..})
        (project {..field.. ..field-alias.., ..field2.. ..field2-alias..}))
    => {:tables {..table-alias.. ..table..}
        :fields {..field-alias.. [..table-alias.. ..field..]
                 ..field2-alias.. [..table-alias.. ..field2..]}})

  (fact "projecting one a field from multiple tables"
    (prerequisites ..tables.. =contains=> {:tables {..table.. ..table.., ..table2.. ..table2..}})
    
    (project ..tables.. [..field..])
    => (throws clojure.lang.ExceptionInfo)

    (project ..tables.. {..field.. ..field-alias..})
    => (throws clojure.lang.ExceptionInfo))

  (fact "projecting a subset of the current projection from one table"
    ;; Note that these two facts are retaining the ..table.. in the
    ;;   fields rather than using the ..table-alias.. they would have
    ;;   used otherwise. This shows they are filtering the existing
    ;;   fields rather than removing and re-adding the fields.
    (project {:tables {..table-alias.. ..table..}
              :fields {..field.. [..table.. ..field..]
                       ..field2.. [..table.. ..field2..]}}
             [..field..])
    => {:tables {..table-alias.. ..table..}
        :fields {..field.. [..table.. ..field..]}}

    (project {:tables {..table-alias.. ..table..}
              :fields {..field.. [..table.. ..field..]
                       ..field2.. [..table.. ..field2..]}}
             {..field.. ..field-alias..})
    => {:tables {..table-alias.. ..table..}
        :fields {..field-alias.. [..table.. ..field..]}})

  (fact "projecting a disjoint set from the current projection from one table"
    (project {:tables {..table-alias.. ..table..}
              :fields {[..table.. ..field..] ..field..}}
             [..field2..])
    => {:tables {..table-alias.. ..table..}
        :fields {..field2.. [..table-alias.. ..field2..]}})

  (fact "projecting a superset from the current projection from one table"
    (project {:tables {..table.. ..table..}
              :fields {..field.. [..table.. ..field..]}}
             [..field.. ..field2..])
    => {:tables {..table.. ..table..}
        :fields {..field.. [..table.. ..field..]
                 ..field2.. [..table.. ..field2..]}}

    ;; once again - note the table aliases
    (project {:tables {..table-alias.. ..table..}
              :fields {..field.. [..table.. ..field..]}}
             [..field.. ..field2..])
    => {:tables {..table-alias.. ..table..}
        :fields {..field.. [..table.. ..field..]
                 ..field2.. [..table-alias.. ..field2..]}})



  (fact "projecting a subset of the current projection from two table"
    (prerequisites ..tables.. =contains=> {:tables {..table.. ..table.., ..table2.. ..table2..}})

    (project {:tables ..tables..
              :fields {..field.. [..table.. ..field..]
                       ..field2.. [..table.. ..field2..]}}
             [..field..])
    => {:tables ..tables..
        :fields {..field.. [..table.. ..field..]}}

    (project {:tables ..tables..
              :fields {..field.. [..table.. ..field..]
                       ..field2.. [..table.. ..field2..]}}
             {..field.. ..field-alias..})
    => {:tables ..tables..
        :fields {..field-alias.. [..table.. ..field..]}})

  (fact "projecting a disjoint set from the current projection from two tables"
    (project {:tables {..table-alias.. ..table.., ..table2-alias.. ..table2..}
              :fields {[..table.. ..field..] ..field..}}
             [..field2..])
    => (throws clojure.lang.ExceptionInfo))

  (fact "projecting a superset from the current projection from two tables"
    (project {:tables {..table-alias.. ..table.., ..table2-alias.. ..table2..}
              :fields {[..table.. ..field..] ..field..}}
             [..field.. ..field2..])
    => (throws clojure.lang.ExceptionInfo)))



(fact "renaming fields does what you'd expect (renames them, removes the old alias)"
  (prerequisites (keyword? ..table..) => true
                 (keyword? ..field..) => true
                 (keyword? ..field2..) => true
                 (keyword? ..field-alias..) => true)

  (-> (table ..table..)
      (project [..field..])
      (rename {..field.. ..field-alias..}))
  => {:tables {..table.. ..table..}
      :fields {..field-alias.. [..table.. ..field..]}}

  (-> (table ..table..)
      (project [..field.. ..field2..])
      (rename {..field.. ..field-alias..}))
  => {:tables {..table.. ..table..}
      :fields {..field-alias.. [..table.. ..field..]
               ..field2.. [..table.. ..field2..]}}

  (-> {:tables {..table.. ..table..,
                ..table2.. ..table2..}
       :fields {..field.. [..table.. ..field..]
                ..field2.. [..table.. ..field2..]}}
      (rename {..field.. ..field-alias..}))
  => {:tables {..table.. ..table.., ..table2.. ..table2..}
      :fields {..field-alias.. [..table.. ..field..]
               ..field2.. [..table.. ..field2..]}})


(fact "joining tables merges tables and fields"
  )