Created
August 15, 2014 09:28
-
-
Save mavericklou/adf5f714b24c821f437b to your computer and use it in GitHub Desktop.
I suppose #1 and #3 should be working
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns mastermind.test.korma_test | |
(:use korma.core | |
korma.db | |
clojure.test)) | |
(defn connect-database [] | |
(let [config-map {:db "korma_test" | |
:user "postgres" | |
:password "postgres" | |
:host "localhost" | |
:port 5432}] | |
(default-connection (create-db (postgres config-map))))) | |
(defn create-table [] | |
(exec-raw ["create table student ( | |
id varchar primary key, | |
name varchar | |
);"]) | |
(exec-raw ["create table course ( | |
id varchar primary key, | |
name varchar | |
);"]) | |
(exec-raw ["create table s_to_c ( | |
student_id varchar references student, | |
course_id varchar references course | |
);"])) | |
(defn drop-table [] | |
(exec-raw ["drop table s_to_c;"]) | |
(exec-raw ["drop table student"]) | |
(exec-raw ["drop table course"]) | |
) | |
(declare student course) | |
(defentity student | |
(many-to-many course :s_to_c)) | |
(defentity course | |
(many-to-many student :s_to_c)) | |
(defentity sc_relation | |
(table :s_to_c)) | |
(defn insert-data [] | |
(insert student (values {:id "1" :name "John"})) | |
(insert student (values {:id "2" :name "Tim"})) | |
(insert student (values {:id "3" :name "Immo"})) | |
(insert course (values {:id "a" :name "Math"})) | |
(insert course (values {:id "b" :name "English"})) | |
(insert course (values {:id "c" :name "Chinese"})) | |
(insert sc_relation (values {:student_id "1" :course_id "a"})) | |
) | |
(use-fixtures :once (fn [f] | |
(connect-database) | |
(create-table) | |
(insert-data) | |
(f) | |
(drop-table) | |
)) | |
(deftest test-many-to-many [] | |
;; test case #1 | |
(is (= (count (select student | |
(with course | |
(where {:name "Math"})))) 1) "Should this query return only one record?") | |
;; test case #2 | |
(is (= (count (select student | |
(with course | |
(where {:course.name "Math"})))) 1) "Should this query return only one record?") | |
;; test case #3 | |
(is (= (count (select student | |
(with course) | |
(where {:course.name "Math"}))) 1) "Should this query return only one record?")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok, from the very beginning, I regard "with" as "join" in korma. I think this is the very first feeling for join table query from docs on sqlkorma.com/docs