Skip to content

Commit

Permalink
adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
invaliduser committed Sep 7, 2023
1 parent 13a7b51 commit 6125ef7
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 13 deletions.
1 change: 0 additions & 1 deletion src/db/postgres/lrsql/postgres/record.clj
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
(delete-actor-st2activ tx input)
(delete-actor-attachments tx input)
(delete-actor-statements tx input)
#_(delete-actor-st2actor tx input) ; this is done by ON DELETE CASCADE
(delete-actor-agent-profile tx input)
(delete-actor-state-document tx input)
(delete-actor-actor tx input))
Expand Down
6 changes: 6 additions & 0 deletions src/db/postgres/lrsql/postgres/sql/ddl.sql
Original file line number Diff line number Diff line change
Expand Up @@ -360,3 +360,9 @@ ALTER TABLE activity ALTER COLUMN payload SET DATA TYPE JSONB;
ALTER TABLE xapi_statement ALTER COLUMN payload SET DATA TYPE JSON;
ALTER TABLE actor ALTER COLUMN payload SET DATA TYPE JSON;
ALTER TABLE activity ALTER COLUMN payload SET DATA TYPE JSON;

-- :name add-statement-to-actor-cascading-delete!
-- :command :execute
-- :doc Adds a cascading delete to delete st2actor entries when corresponding statements are deleted
ALTER TABLE DROP CONSTRAINT statement_fk;
ALTER TABLE ADD CONSTRAINT statement_fk FOREIGN KEY (statement_id) REFERENCES xapi_statement(statement_id) ON CASCADE DELETE;
5 changes: 0 additions & 5 deletions src/db/postgres/lrsql/postgres/sql/delete.sql
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,6 @@ WHERE actor_ifi = :actor-ifi)
DELETE FROM xapi_statement WHERE statement_id IN (SELECT statement_id FROM statement_to_actor
WHERE actor_ifi = :actor-ifi)

-- :name delete-actor-st2actor
-- :command :execute
-- :result :affected
DELETE FROM statement_to_actor WHERE actor_ifi = :actor-ifi

-- :name delete-actor-agent-profile
-- :command :execute
-- :result :affected
Expand Down
1 change: 0 additions & 1 deletion src/db/sqlite/lrsql/sqlite/record.clj
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@
(delete-actor-st2activ tx input)
(delete-actor-attachments tx input)
(delete-actor-statements tx input)
#_(delete-actor-st2actor tx input) ; this is done by ON DELETE CASCADE
(delete-actor-agent-profile tx input)
(delete-actor-state-document tx input)
(delete-actor-actor tx input))
Expand Down
5 changes: 0 additions & 5 deletions src/db/sqlite/lrsql/sqlite/sql/delete.sql
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,6 @@ WHERE actor_ifi = :actor-ifi)
DELETE FROM xapi_statement WHERE statement_id IN (SELECT statement_id FROM statement_to_actor
WHERE actor_ifi = :actor-ifi)

-- :name delete-actor-st2actor
-- :command :execute
-- :result :affected
DELETE FROM statement_to_actor WHERE actor_ifi = :actor-ifi

-- :name delete-actor-agent-profile
-- :command :execute
-- :result :affected
Expand Down
75 changes: 74 additions & 1 deletion src/test/lrsql/admin/protocol_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
(:require [clojure.test :refer [deftest testing is use-fixtures]]
[com.stuartsierra.component :as component]
[com.yetanalytics.lrs.protocol :as lrsp]

[xapi-schema.spec.regex :refer [Base64RegEx]]
[lrsql.admin.protocol :as adp]
[lrsql.test-support :as support]
[lrsql.util :as u]))
[lrsql.util :as u]
[lrsql.util.actor :as ua]))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Init
Expand Down Expand Up @@ -52,6 +54,33 @@
"object" {"id" "http://www.example.com/tincan/activities/multipart"}
"context" {"platform" "another_example"}})

;; A third statement with a different actor, referring to stmt-0
(def stmt-2
{"id" "00000000-0000-4000-8000-000000000002"
"actor" {"account" {"name" "Sample Agent 2"
"homePage" "https://example.org"}
"name" "Sample Agent 2"
"objectType" "Agent"}
"verb" {"id" "http://adlnet.gov/expapi/verbs/asked"
"display" {"en-US" "asked"}}
"object" {"objectType" "StatementRef"
"id" "00000000-0000-4000-8000-000000000000"}})

;; A fourth statement (with a third actor) containing a substatement (by a fourth actor
(def stmt-3
{"id" "00000000-0000-4000-8000-000000000003"
"actor" {"mbox" "mailto:sample.bar@example.com"
"objectType" "Agent"}
"verb" {"id" "http://adlnet.gov/expapi/verbs/asked"
"display" {"en-US" "asked"}}
"object" {"objectType" "SubStatement"
"actor" {"mbox" "mailto:sample.baz@example.com"
"objectType" "Agent"}
"verb" {"id" "http://adlnet.gov/expapi/verbs/answered"
"display" {"en-US" "answered"}}
"object" {"id" "http://www.example.com/tincan/activities/multipart"}
}})

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Tests
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -155,6 +184,50 @@
(is (-> (adp/-ensure-account-oidc lrs username bad-issuer)
:result
(= :lrsql.admin/oidc-issuer-mismatch-error)))))
(testing "actor-delete"
(let [stmts [stmt-0 stmt-1 stmt-2 stmt-3]
ifis (->> (conj stmts (stmt-3 "object"))
(map #(ua/actor->ifi (% "actor"))))
get-stmt-#s (fn []
(reduce (fn [m actor-ifi]
(assoc m actor-ifi
(count (:statements (:statement-result (lrsp/-get-statements lrs auth-ident {:actor-ifi actor-ifi} []))))))
{} ifis))]
(lrsp/-store-statements lrs auth-ident stmts [])

(assert (every? #(> % 0) (vals (get-stmt-#s))))
(doseq [ifi ifis]
(adp/-delete-actor lrs {:actor-ifi ifi}))
(is (every? #(= % 0) (vals (get-stmt-#s))))))
(let [stmt->ifi #(ua/actor->ifi (% "actor"))
stmts-of-actor (fn [actor-ifi] (-> (lrsp/-get-statements lrs auth-ident {:actor-ifi actor-ifi} []) :statement-result :statements))
count-of-actor (fn [actor-ifi] (-> (lrsp/-get-statements lrs auth-ident {:actor-ifi actor-ifi} []) :statement-result :statements count))
child-ifi (stmt->ifi (stmt-3 "object"))
parent-ifi (stmt->ifi stmt-3)]
(testing "substatement up"
(lrsp/-store-statements lrs auth-ident [stmt-3] [])
(assert (= 1 (count-of-actor parent-ifi)))
(adp/-delete-actor lrs {:actor-ifi child-ifi})
(is (= 0 (count-of-actor parent-ifi)));
(adp/-delete-actor lrs {:actor-ifi parent-ifi}))
(testing "substatement down"
(lrsp/-store-statements lrs auth-ident [stmt-3] [])
(assert (= 1 (count-of-actor child-ifi)))
(adp/-delete-actor lrs {:actor-ifi parent-ifi})
(is (= 0 (count-of-actor child-ifi)))
(adp/-delete-actor lrs {:actor-ifi child-ifi}))
(testing "statement ref connections deleted"
(let [[ifi-0 ifi-2] (mapv stmt->ifi [stmt-0 stmt-2])]
(lrsp/-store-statements lrs auth-ident [stmt-0] [])
(lrsp/-store-statements lrs auth-ident [stmt-2] [])
(assert (= 2 (count-of-actor ifi-0)))
(assert (= 2 (count-of-actor ifi-2)))
(adp/-delete-actor lrs {:actor-ifi ifi-0})
(is (= 1 (count-of-actor ifi-0)))
(is (= 1 (count-of-actor ifi-2)))
(adp/-delete-actor lrs {:actor-ifi ifi-2})
(is (= 0 (count-of-actor ifi-0)))
(is (= 0 (count-of-actor ifi-2))))))
(component/stop sys')))

;; TODO: Add tests for creds with no explicit scopes, once
Expand Down

0 comments on commit 6125ef7

Please sign in to comment.