moar specs, fukkn specs

integration-tests
Josha von Gizycki 5 years ago
parent 0b587c4f37
commit 9af92a7055

@ -5,6 +5,7 @@
:dependencies [;;clojure core :dependencies [;;clojure core
[org.clojure/clojure "1.10.1"] [org.clojure/clojure "1.10.1"]
[org.clojure/tools.logging "0.4.1"]
;; static site ;; static site
[compojure "1.6.1"] [compojure "1.6.1"]

@ -14,6 +14,11 @@
(spec/def ::created-at (spec/def ::created-at
(spec/and string? ::neo4j/date-str)) (spec/and string? ::neo4j/date-str))
(spec/def ::attribute
(spec/keys ::req-un [::name
::type
::created-at]))
(neo4j/defquery (neo4j/defquery
findy-by-schema findy-by-schema
"MATCH (a:attribute)-->(s:schema) "MATCH (a:attribute)-->(s:schema)

@ -7,6 +7,7 @@
[ring.util.response :as rur] [ring.util.response :as rur]
[ring.middleware.session.cookie :as session-cookie] [ring.middleware.session.cookie :as session-cookie]
[ring.middleware.json :as ring-json] [ring.middleware.json :as ring-json]
[clojure.spec.alpha :as spec]
[wanijo.home.routes :as home-routes] [wanijo.home.routes :as home-routes]
[wanijo.schema [wanijo.schema
[routes :as schema-routes] [routes :as schema-routes]
@ -43,8 +44,14 @@
schema-middleware/wrap-user-schemas schema-middleware/wrap-user-schemas
(wrap-defaults site-defaults))) (wrap-defaults site-defaults)))
(defn wrap-spec-asserts [handler]
(fn [req]
(spec/check-asserts true)
(handler req)))
(def app (def app
(-> app-routes (-> app-routes
wrap-spec-asserts
devmode/wrap-devmode devmode/wrap-devmode
schema-middleware/wrap-user-schemas schema-middleware/wrap-user-schemas
ring-json/wrap-json-response ring-json/wrap-json-response

@ -1,11 +1,14 @@
(ns wanijo.instance.domain (ns wanijo.instance.domain
(:require [clojure.spec.alpha :as spec] (:require [clojure.spec.alpha :as spec]
[wanijo.framework.neo4j :as neo4j] [wanijo.framework.neo4j :as neo4j]
[clojure.pprint :as pprint])) [wanijo.schema.domain :as domain-schema]
[wanijo.attribute.domain :as domain-attr]))
(spec/def ::created-at ::neo4j/date-str) (spec/def ::created-at ::neo4j/date-str)
(spec/def ::created_at ::neo4j/date-str)
(spec/def ::updated-at ::neo4j/date-str) (spec/def ::updated-at ::neo4j/date-str)
(spec/def ::name (spec/and (complement empty?) string?)) (spec/def ::updated_at ::neo4j/date-str)
(spec/def ::name string?)
(spec/def ::value (spec/def ::value
(spec/or :string string? (spec/or :string string?
:bool boolean? :bool boolean?
@ -13,6 +16,38 @@
:number number?)) :number number?))
(spec/def ::instance_uuid ::neo4j/uuid) (spec/def ::instance_uuid ::neo4j/uuid)
(spec/def ::attribute_uuid ::neo4j/uuid) (spec/def ::attribute_uuid ::neo4j/uuid)
(spec/def ::instance
(spec/keys :req-un [::neo4j/uuid
::created_at
::updated_at
::name]))
(spec/def ::properties
(spec/coll-of
(spec/keys :req-un [::neo4j/uuid
::created_at
::updated_at
::domain-attr/attribute])))
(spec/def ::link
(spec/keys :req-un [::neo4j/uuid
::created_at
::name]))
(spec/def ::target ::instance)
(spec/def ::source ::instance)
(spec/def ::links-out
(spec/coll-of
(spec/keys :req-un [::link
::target
::domain-schema/schema])))
(spec/def ::links-in
(spec/coll-of
(spec/keys :req-un [::link
::source
::domain-schema/schema])))
(spec/def ::tags
(spec/coll-of
(spec/keys :req-un [::neo4j/uuid
::name
::created-at])))
(neo4j/defquery findy-by-schema (neo4j/defquery findy-by-schema
"MATCH (i:instance)-[:of]->(s:schema) "MATCH (i:instance)-[:of]->(s:schema)
@ -70,7 +105,13 @@
-[:of]->(s:schema) -[:of]->(s:schema)
RETURN i, s") RETURN i, s")
(spec/def ::instance-with-schema
(spec/and ::instance
(spec/keys :req-un
[::domain-schema/schema])))
(defn find-by-uuid! [uuid] (defn find-by-uuid! [uuid]
{:post [(spec/assert ::instance-with-schema %)]}
(->> (neo4j/exec-query! find-by-uuid (->> (neo4j/exec-query! find-by-uuid
{:uuid uuid}) {:uuid uuid})
(map #(assoc (:i %) (map #(assoc (:i %)
@ -96,7 +137,6 @@
"MATCH (i:instance {uuid:{uuid}}) "MATCH (i:instance {uuid:{uuid}})
SET i.name = {name}, SET i.name = {name},
i.updated_at = {updated_at}") i.updated_at = {updated_at}")
(neo4j/defquery edit-property (neo4j/defquery edit-property
"MATCH (i:instance {uuid:{instance_uuid}}), "MATCH (i:instance {uuid:{instance_uuid}}),
(a:attribute {uuid:{attribute_uuid}}) (a:attribute {uuid:{attribute_uuid}})
@ -107,7 +147,6 @@
p.value = {value} p.value = {value}
ON MATCH SET p.updated_at = {now}, ON MATCH SET p.updated_at = {now},
p.value = {value}") p.value = {value}")
(spec/def ::now ::neo4j/date-str) (spec/def ::now ::neo4j/date-str)
(spec/def ::prop-tuple-values (spec/def ::prop-tuple-values
(spec/keys :req-un [::neo4j/uuid (spec/keys :req-un [::neo4j/uuid
@ -118,7 +157,6 @@
(spec/def ::prop-tuple (spec/def ::prop-tuple
(spec/coll-of (spec/coll-of
(spec/tuple fn? ::prop-tuple-values))) (spec/tuple fn? ::prop-tuple-values)))
(defn instance->prop-tuples [instance] (defn instance->prop-tuples [instance]
{:post [(spec/assert ::prop-tuple %)]} {:post [(spec/assert ::prop-tuple %)]}
(map (fn [prop] (map (fn [prop]
@ -133,7 +171,6 @@
:instance_uuid (:uuid instance) :instance_uuid (:uuid instance)
:attribute_uuid (-> prop :attribute :uuid)}])) :attribute_uuid (-> prop :attribute :uuid)}]))
(:properties instance))) (:properties instance)))
(defn edit! [instance] (defn edit! [instance]
(let [prop-tuples (instance->prop-tuples instance)] (let [prop-tuples (instance->prop-tuples instance)]
(apply neo4j/exec-queries! (apply neo4j/exec-queries!
@ -151,7 +188,6 @@
(p:property)-[pc:of]->(i), (p:property)-[pc:of]->(i),
(p)-[pac:of]->(a:attribute) (p)-[pac:of]->(a:attribute)
DELETE pac, pc, cb, ic, p, i") DELETE pac, pc, cb, ic, p, i")
(defn delete! [uuid] (defn delete! [uuid]
(neo4j/exec-query! delete {:uuid uuid})) (neo4j/exec-query! delete {:uuid uuid}))
@ -163,7 +199,6 @@
SET l.created_at = {created_at}, SET l.created_at = {created_at},
l.name = {name} l.name = {name}
CREATE (i)<-[:link_from]-(l)-[:link_to]->(t)") CREATE (i)<-[:link_from]-(l)-[:link_to]->(t)")
(defn create-link! [link] (defn create-link! [link]
(let [tuples (map (fn [target-uuid] (let [tuples (map (fn [target-uuid]
[create-link [create-link
@ -183,8 +218,8 @@
(t)-[:of]->(s:schema) (t)-[:of]->(s:schema)
RETURN i, l, t, s RETURN i, l, t, s
ORDER BY s.name, t.name, t.created_at") ORDER BY s.name, t.name, t.created_at")
(defn outgoing-links! [uuid] (defn outgoing-links! [uuid]
{:post [(spec/assert ::links-out %)]}
(map (fn [row] (map (fn [row]
{:link (:l row) {:link (:l row)
:target (:t row) :target (:t row)
@ -199,8 +234,8 @@
(source)-[:of]->(schema:schema) (source)-[:of]->(schema:schema)
RETURN i, l, source, schema RETURN i, l, source, schema
ORDER BY schema.name, source.name, source.created_at") ORDER BY schema.name, source.name, source.created_at")
(defn incoming-links! [uuid] (defn incoming-links! [uuid]
{:post [(spec/assert ::links-in %)]}
(map (fn [row] (map (fn [row]
{:link (:l row) {:link (:l row)
:source (:source row) :source (:source row)
@ -212,7 +247,6 @@
"MATCH (l:link {uuid:{uuid}}), "MATCH (l:link {uuid:{uuid}}),
(l)-[r]-() (l)-[r]-()
DELETE r, l") DELETE r, l")
(defn delete-link! [uuid] (defn delete-link! [uuid]
(neo4j/exec-query! delete-link (neo4j/exec-query! delete-link
{:uuid uuid})) {:uuid uuid}))
@ -221,12 +255,23 @@
"MATCH (i:instance {uuid:{uuid}})-[:has]->(t:tag) "MATCH (i:instance {uuid:{uuid}})-[:has]->(t:tag)
RETURN t RETURN t
ORDER BY t.name") ORDER BY t.name")
(defn tags! [uuid] (defn tags! [uuid]
{:post [(spec/assert ::tags %)]}
(neo4j/exec-query! tags (neo4j/exec-query! tags
{:uuid uuid})) {:uuid uuid}))
(spec/def ::full-instance
(spec/and ::instance-with-schema
(spec/keys ::req-un [::neo4j/uuid
::created-at
::updated-at
::name
::properties
::links-out
::links-in
::tags])))
(defn full-instance-by-uuid! [uuid] (defn full-instance-by-uuid! [uuid]
{:post [(spec/assert ::full-instance %)]}
(assoc (find-by-uuid! uuid) (assoc (find-by-uuid! uuid)
:properties :properties
(find-properties! uuid) (find-properties! uuid)
@ -241,7 +286,6 @@
"MATCH (u:user {uuid:{user_uuid}}), "MATCH (u:user {uuid:{user_uuid}}),
(i:instance {uuid:{uuid}}) (i:instance {uuid:{uuid}})
RETURN EXISTS((i)-[:starred_by]->(u)) AS starred") RETURN EXISTS((i)-[:starred_by]->(u)) AS starred")
(defn is-starred! [uuid user-uuid] (defn is-starred! [uuid user-uuid]
(-> (neo4j/exec-query! is-starred (-> (neo4j/exec-query! is-starred
{:user_uuid user-uuid {:user_uuid user-uuid
@ -255,7 +299,6 @@
MERGE (i)-[s:starred_by]->(u) MERGE (i)-[s:starred_by]->(u)
ON CREATE ON CREATE
SET s.created_at = {now}") SET s.created_at = {now}")
(defn mark-starred! [uuid user-uuid] (defn mark-starred! [uuid user-uuid]
(neo4j/exec-query! mark-starred (neo4j/exec-query! mark-starred
{:uuid uuid {:uuid uuid
@ -267,7 +310,6 @@
-[s:starred_by]-> -[s:starred_by]->
(:user {uuid:{user_uuid}}) (:user {uuid:{user_uuid}})
DELETE s") DELETE s")
(defn remove-starred! [uuid user-uuid] (defn remove-starred! [uuid user-uuid]
(neo4j/exec-query! remove-starred (neo4j/exec-query! remove-starred
{:uuid uuid {:uuid uuid
@ -279,7 +321,6 @@
(i:instance) (i:instance)
RETURN i, s RETURN i, s
ORDER BY s.created_at DESC") ORDER BY s.created_at DESC")
(defn starred-by-user! [user-uuid] (defn starred-by-user! [user-uuid]
(map #(assoc (:i %) (map #(assoc (:i %)
:starred_at (-> % :s :created_at)) :starred_at (-> % :s :created_at))

@ -1,11 +1,8 @@
(ns wanijo.main (ns wanijo.main
(:require [ring.adapter.jetty :refer [run-jetty]] (:require [ring.adapter.jetty :refer [run-jetty]]
[clojure.spec.alpha :as spec] [wanijo.handler :as wanijo-handler])
[wanijo.handler :as wanijo])
(:gen-class)) (:gen-class))
(spec/check-asserts true)
(defonce server (atom nil)) (defonce server (atom nil))
(defn start-server! (defn start-server!
@ -14,7 +11,7 @@
:or {port 3080 :or {port 3080
join? true}}] join? true}}]
(reset! server (reset! server
(run-jetty wanijo/standalone-app (run-jetty wanijo-handler/standalone-app
{:port port {:port port
:join? join?}))) :join? join?})))

@ -4,17 +4,18 @@
(spec/def ::name (spec/def ::name
(spec/and string? not-empty)) (spec/and string? not-empty))
(spec/def ::created-at (spec/def ::created-at
(spec/and string? ::neo4j/date-str)) (spec/and string? ::neo4j/date-str))
(spec/def ::assigned-to (spec/def ::assigned-to
(spec/or :public empty? (spec/or :public empty?
:assigned-single ::neo4j/uuid :assigned-single ::neo4j/uuid
:assigned (spec/coll-of ::neo4j/uuid))) :assigned (spec/coll-of ::neo4j/uuid)))
(spec/def ::schema
(spec/keys ::req-un [::name
::created-at
::neo4j/uuid]))
(neo4j/defquery (neo4j/defquery all-created-by
all-created-by
"MATCH (s:schema)-[:created_by]->(u:user) "MATCH (s:schema)-[:created_by]->(u:user)
WHERE u.uuid = {uuid} WHERE u.uuid = {uuid}
RETURN s RETURN s
@ -26,8 +27,7 @@
all-created-by all-created-by
{:uuid user-uuid}))) {:uuid user-uuid})))
(neo4j/defquery (neo4j/defquery all
all
"MATCH (s:schema) "MATCH (s:schema)
RETURN s RETURN s
ORDER BY s.name") ORDER BY s.name")
@ -36,8 +36,7 @@
(map :s (map :s
(neo4j/exec-query! all {}))) (neo4j/exec-query! all {})))
(neo4j/defquery (neo4j/defquery create-new
create-new
"MATCH (u:user) "MATCH (u:user)
WHERE u.uuid = {u_uuid} WHERE u.uuid = {u_uuid}
CREATE (s:schema)-[:created_by]->(u) CREATE (s:schema)-[:created_by]->(u)
@ -57,8 +56,7 @@
first first
:uuid)) :uuid))
(neo4j/defquery (neo4j/defquery find-by-uuid
find-by-uuid
"MATCH (s:schema) "MATCH (s:schema)
WHERE s.uuid = {uuid} WHERE s.uuid = {uuid}
RETURN s") RETURN s")
@ -71,8 +69,7 @@
first first
:s)) :s))
(neo4j/defquery (neo4j/defquery schema-creator
schema-creator
"MATCH (s:schema)-->(u:user) "MATCH (s:schema)-->(u:user)
WHERE s.uuid = {uuid} WHERE s.uuid = {uuid}
RETURN u.uuid as uuid") RETURN u.uuid as uuid")
@ -85,8 +82,7 @@
first first
:uuid)) :uuid))
(neo4j/defquery (neo4j/defquery schema-permissions
schema-permissions
"MATCH (s:schema {uuid:{schema_uuid}}) "MATCH (s:schema {uuid:{schema_uuid}})
RETURN RETURN
EXISTS((:user {uuid:{user_uuid}}) EXISTS((:user {uuid:{user_uuid}})
@ -111,8 +107,7 @@
(defn has-user-read-permissions? [schema-uuid user-uuid] (defn has-user-read-permissions? [schema-uuid user-uuid]
(has-user-permission? "read" schema-uuid user-uuid)) (has-user-permission? "read" schema-uuid user-uuid))
(neo4j/defquery (neo4j/defquery accessible-schemas
accessible-schemas
"MATCH (s:schema), "MATCH (s:schema),
(u:user {uuid:{user_uuid}}) (u:user {uuid:{user_uuid}})
WHERE EXISTS((u)-[:permission {type:'read'}]->(s)) WHERE EXISTS((u)-[:permission {type:'read'}]->(s))
@ -125,8 +120,7 @@
(neo4j/exec-query! accessible-schemas (neo4j/exec-query! accessible-schemas
{:user_uuid user-uuid}))) {:user_uuid user-uuid})))
(neo4j/defquery (neo4j/defquery delete
delete
"MATCH (s:schema) "MATCH (s:schema)
WHERE s.uuid = {uuid} WHERE s.uuid = {uuid}
OPTIONAL MATCH (s)--(a:attribute) OPTIONAL MATCH (s)--(a:attribute)
@ -140,8 +134,7 @@
delete delete
{:uuid uuid})) {:uuid uuid}))
(neo4j/defquery (neo4j/defquery edit
edit
"MATCH (s:schema) "MATCH (s:schema)
WHERE s.uuid = {uuid} WHERE s.uuid = {uuid}
SET s.name = {name}") SET s.name = {name}")
@ -151,8 +144,7 @@
edit edit
schema)) schema))
(neo4j/defquery (neo4j/defquery assigned-users
assigned-users
"MATCH (s:schema)-[p:permission]-(u:user) "MATCH (s:schema)-[p:permission]-(u:user)
WHERE s.uuid = {uuid} WHERE s.uuid = {uuid}
RETURN u, p RETURN u, p
@ -163,8 +155,7 @@
assigned-users assigned-users
{:uuid uuid})) {:uuid uuid}))
(neo4j/defquery (neo4j/defquery assigned-schemas
assigned-schemas
"MATCH (s1:schema)-[p:permission]-(s2:schema) "MATCH (s1:schema)-[p:permission]-(s2:schema)
WHERE s1.uuid = {uuid} WHERE s1.uuid = {uuid}
RETURN s2 RETURN s2
@ -189,15 +180,13 @@
:assigned-write-users (:write users) :assigned-write-users (:write users)
:assigned-schemas schemas))) :assigned-schemas schemas)))
(neo4j/defquery (neo4j/defquery remove-assignments
remove-assignments
"MATCH (s:schema)-[c:permission]-(:user) "MATCH (s:schema)-[c:permission]-(:user)
WHERE s.uuid = {uuid} WHERE s.uuid = {uuid}
AND c.type = {permtype} AND c.type = {permtype}
DELETE c") DELETE c")
(neo4j/defquery (neo4j/defquery create-assignments
create-assignments
"MATCH (s:schema), (u:user) "MATCH (s:schema), (u:user)
WHERE s.uuid = {uuid} WHERE s.uuid = {uuid}
AND u.uuid IN {users} AND u.uuid IN {users}
@ -213,14 +202,12 @@
:users users :users users
:permtype permission}])) :permtype permission}]))
(neo4j/defquery (neo4j/defquery remove-schema-assignments
remove-schema-assignments
"MATCH (s1:schema)-[p:permission]-(s2:schema) "MATCH (s1:schema)-[p:permission]-(s2:schema)
WHERE s1.uuid = {uuid} WHERE s1.uuid = {uuid}
DELETE p") DELETE p")
(neo4j/defquery (neo4j/defquery create-schema-assignments
create-schema-assignments
"MATCH (s1:schema), (s2:schema) "MATCH (s1:schema), (s2:schema)
WHERE s1.uuid = {uuid} WHERE s1.uuid = {uuid}
AND s2.uuid IN {schemas} AND s2.uuid IN {schemas}

Loading…
Cancel
Save