You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
158 lines
6.0 KiB
158 lines
6.0 KiB
(ns wanijo.infra.system-test
|
|
(:require [clojure.test :refer :all]
|
|
[neo4j-clj.core :as drv]
|
|
[clojure.string :as string]
|
|
[wanijo.infra.neo4j :as neo4j]
|
|
[wanijo.infra.repl :as repl]
|
|
[wanijo.schema.routes :as schema-routes]
|
|
[wanijo.attribute.routes :as attr-routes]
|
|
[wanijo.instance.routes :as inst-routes]
|
|
[wanijo.instance.forms :as inst-forms]
|
|
[wanijo.instance.db :as inst-db]))
|
|
|
|
(defn single-result
|
|
([cypher]
|
|
(single-result cypher
|
|
#(val (first %))))
|
|
([cypher extractor]
|
|
(extractor
|
|
(first
|
|
(neo4j/exec-query!
|
|
(drv/create-query cypher) {})))))
|
|
|
|
(defn multi-results [cypher extractor]
|
|
(extractor
|
|
(neo4j/exec-query!
|
|
(drv/create-query cypher) {})))
|
|
|
|
(deftest allround-system-test
|
|
(repl/setup-in-memory!)
|
|
(def user
|
|
(single-result "MATCH (u:user) RETURN u"
|
|
:u))
|
|
|
|
(testing "create schema"
|
|
(schema-routes/new! {:params {:name "test-schema"}
|
|
:session {:uuid (:uuid user)}})
|
|
(def schema
|
|
(single-result "MATCH (s:schema) RETURN s LIMIT 1" :s))
|
|
(is (= "test-schema" (:name schema)))
|
|
(is (not= nil (:created_at schema)))
|
|
(is (= (:uuid user)
|
|
(single-result "MATCH (s:schema)-[:created_by]->(u:user)
|
|
RETURN u.uuid")))
|
|
(is (= (:uuid user)
|
|
(single-result "MATCH (s:schema)
|
|
<-[:permission {type:'write'}]-(u:user)
|
|
RETURN u.uuid"))))
|
|
|
|
(testing "create attribute"
|
|
(attr-routes/new! {:params {:schema (:uuid schema)
|
|
:name "test-attr"
|
|
:type "string"
|
|
:required "1"}
|
|
:session {:uuid (:uuid user)}})
|
|
(def attr
|
|
(single-result "MATCH (a:attribute) RETURN a"))
|
|
(is (= "string" (:type attr)))
|
|
(is (= "test-attr" (:name attr)))
|
|
(is (= 1 (:required attr)))
|
|
(is (= (:uuid user)
|
|
(single-result "MATCH (a:attribute)-[:created_by]->(u:user)
|
|
RETURN u.uuid")))
|
|
(is (= (:uuid schema)
|
|
(single-result "MATCH (a:attribute)-[:of]->(s:schema)
|
|
RETURN s.uuid"))))
|
|
|
|
(testing "assign read"
|
|
(schema-routes/assign-users! {:params {:uuid (:uuid schema)
|
|
:assigned [(:uuid user)]
|
|
:permission "read"}})
|
|
(is (= (list ["read" (:uuid user)] ["write" (:uuid user)])
|
|
(multi-results "MATCH (s:schema)
|
|
<-[p:permission]-(u:user)
|
|
RETURN p.type AS type, u.uuid AS uuid"
|
|
#(map (juxt :type :uuid) %)))))
|
|
|
|
(testing "create instance"
|
|
(inst-routes/route-new!
|
|
{:params {:schema-uuid (:uuid schema)
|
|
:name "instance"
|
|
(inst-forms/attr->field-id attr) "attr-value"}
|
|
:session {:uuid (:uuid user)}})
|
|
(def instance
|
|
(single-result "MATCH (i:instance) RETURN i"))
|
|
(is (= "attr-value"
|
|
(single-result "MATCH (p:property) RETURN p.value")))
|
|
(is (= (:uuid user)
|
|
(single-result "MATCH (i:instance)-[:created_by]->(u:user)
|
|
RETURN u.uuid")))
|
|
(let [full-inst (inst-db/full-instance-by-uuid! (:uuid instance))]
|
|
(is (= "attr-value"
|
|
(-> full-inst :properties first :value)))
|
|
(is (= (:uuid attr)
|
|
(-> full-inst :properties first :attribute :uuid)))
|
|
(is (empty? (:tags full-inst)))
|
|
(is (empty? (:links-in full-inst)))
|
|
(is (empty? (:links-out full-inst)))))
|
|
|
|
(testing "create second instance and linking"
|
|
(inst-routes/route-new!
|
|
{:params {:schema-uuid (:uuid schema)
|
|
:name "instance2"
|
|
(inst-forms/attr->field-id attr) "attr-value2"}
|
|
:session {:uuid (:uuid user)}})
|
|
(def instance2
|
|
(single-result "MATCH (i:instance)
|
|
WITH MAX(i.created_at) AS max_created_at
|
|
MATCH (i:instance)
|
|
WHERE i.created_at = max_created_at
|
|
RETURN i"))
|
|
(is (= "instance2" (:name instance2)))
|
|
|
|
(inst-routes/route-create-link!
|
|
(:uuid instance)
|
|
(:uuid schema)
|
|
{:params {:name "link-name"
|
|
:instances [(:uuid instance2)]}
|
|
:session {:uuid (:uuid user)}})
|
|
(let [link (single-result "MATCH ()-[l:link]-() RETURN l")]
|
|
(is (= "link-name" (:name link)))
|
|
(is (= (:uuid user) (:created_by link)))
|
|
(is (some? (:created_at link))))
|
|
|
|
(is (= (:uuid instance)
|
|
(single-result "MATCH (i:instance)-[:link]->(:instance)
|
|
RETURN i.uuid")))
|
|
|
|
(is (= (:uuid instance2)
|
|
(single-result "MATCH (i:instance)<-[:link]-(:instance)
|
|
RETURN i.uuid"))))
|
|
|
|
(testing "show instances"
|
|
(let [resp (inst-routes/route-show!
|
|
(:uuid instance)
|
|
{:session {:uuid (:uuid user)}})]
|
|
(is (true? (string? resp)))
|
|
(is (true? (string/includes? resp (:name instance))))
|
|
(is (true? (string/includes? resp (:name schema)))))
|
|
(let [resp (inst-routes/route-show!
|
|
(:uuid instance2)
|
|
{:session {:uuid (:uuid user)}})]
|
|
(is (true? (string? resp)))
|
|
(is (true? (string/includes? resp (:name instance))))
|
|
(is (true? (string/includes? resp (:name schema))))))
|
|
|
|
(testing "delete first instance"
|
|
(inst-routes/route-delete! (:uuid instance))
|
|
(is (= 1 (single-result "MATCH (i:instance) RETURN COUNT(i)")))
|
|
(is (= 1 (single-result "MATCH (p:property) RETURN COUNT(p)")))
|
|
(is (= 1 (single-result "MATCH (s:schema) RETURN COUNT(s)")))
|
|
(is (= 0 (single-result "MATCH ()-[l:link]-() RETURN COUNT(l)"))))
|
|
|
|
(testing "delete second instance"
|
|
(inst-routes/route-delete! (:uuid instance2))
|
|
(is (= 0 (single-result "MATCH (i:instance) RETURN COUNT(i)")))
|
|
(is (= 0 (single-result "MATCH (p:property) RETURN COUNT(p)")))
|
|
(is (= 1 (single-result "MATCH (s:schema) RETURN COUNT(s)")))))
|