summaryrefslogtreecommitdiff
path: root/test/clojure_sql/core_test.clj
blob: 6a12698c46a6297726ef3813ab984b1a231872b6 (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
(ns clojure-sql.core-test
  (:refer-clojure :exclude [sort-by])
  (:require [clojure-sql.core :refer :all]
            [midje.sweet :refer :all]))

(comment

  (fact
    @(table :user)
    => ["SELECT * FROM \"user\""]

    @(-> (table :user) (project [:username]))
    => ["SELECT \"user\".\"username\" AS \"username\" FROM \"user\""]
    
    @(-> (table :user) (select '(= :username "george")))
    => ["SELECT * FROM \"user\" WHERE (\"user\".\"username\" = ?)" "george"]

    @(-> (table :user) (project {:username :u}))
    => ["SELECT \"user\".\"username\" AS \"u\" FROM \"user\""]

    @(-> (table :user) (project {'(+ :age :modifier) :u}))
    => ["SELECT (\"user\".\"age\" + \"user\".\"modifier\") AS \"u\" FROM \"user\""]

    @(-> (table :user)
         (project [:id])
         (join (-> (table :x)
                   (project [:id])
                   (join (-> (table :y)
                             (project [:id]))
                         :type :left))))
    => ["SELECT \"user\".\"id\" AS \"id\" FROM \"user\" INNER JOIN \"x\" ON ((\"user\".\"id\" = \"x\".\"id\"))"])

  (into {} (-> (table :user) (project '{(+ :username :password) :u})))

  (-> (table :users)
      (project '[:username (+ 1 2 3)])
      (rename '{(+ 1 2 3) :x})
      (select `(exists ~(-> (table :users)
                            (select '(= 10 :username))))))

  (-> (table :users)
      (project {:username :un})
      (select '(= :username 10))))