Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LRS-72 lrs 1.2.10 #225

Merged
merged 9 commits into from
May 12, 2022
2 changes: 1 addition & 1 deletion deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
less-awful-ssl/less-awful-ssl {:mvn/version "1.0.6"}
;; Yet Analytics deps
com.yetanalytics/lrs
{:mvn/version "1.2.6"
{:mvn/version "1.2.10"
:exclusions [org.clojure/clojure
org.clojure/clojurescript
com.yetanalytics/xapi-schema]}
Expand Down
30 changes: 23 additions & 7 deletions src/main/lrsql/ops/query/statement.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@
[format ltags query-res]
(-> query-res :payload (us/format-statement format ltags)))

(defn- dedupe-attachment-res
[attachment-query-res]
(loop [res-in attachment-query-res
res-out []
seen #{}]
(if-let [{att-sha :attachment_sha :as att} (first res-in)]
(if (contains? seen att-sha)
(recur (rest res-in)
res-out
seen)
(recur (rest res-in)
(conj res-out att)
(conj seen att-sha)))
res-out)))

(defn- conform-attachment-res
[{att-sha :attachment_sha
content-type :content_type
Expand Down Expand Up @@ -56,13 +71,14 @@
(butlast query-results)
query-results))
att-results (if attachments?
(doall (->> (map (fn [stmt]
(->> (get stmt "id")
u/str->uuid
(assoc {} :statement-id)
(bp/-query-attachments bk tx)))
stmt-results)
(apply concat)
(doall (->> (mapcat
(fn [stmt]
(->> (get stmt "id")
u/str->uuid
(assoc {} :statement-id)
(bp/-query-attachments bk tx)))
stmt-results)
dedupe-attachment-res
(map conform-attachment-res)))
[])]
{:statement-result
Expand Down
110 changes: 102 additions & 8 deletions src/test/lrsql/lrs_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@
(update-in [:statement-result :statements]
(partial map remove-props)))))

(defn- string-result-attachment-content
[get-ss-result]
(update get-ss-result
:attachments
(fn [atts]
(mapv
(fn [att]
(update att :content #(String. %)))
atts))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Statement Tests
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -131,6 +141,40 @@
:length 27
:sha2 "495395e777cd98da653df9615d09c0fd6bb2f8d4788394cd53c56a3bfdcd848a"})

(def stmt-5 (assoc stmt-4 "id" "00000000-0000-4000-8000-000000000006"))

(def stmt-6
{"id" "00000000-0000-4000-8000-000000000005"
"actor" {"mbox" "mailto:sample.foo@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"}
"attachments" [{"usageType" "http://example.com/attachment-usage/test"
"display" {"en-US" "A test attachment"}
"description" {"en-US" "A test attachment (description)"}
"contentType" "text/plain"
"length" 27
"sha2" "495395e777cd98da653df9615d09c0fd6bb2f8d4788394cd53c56a3bfdcd848a"}
{"usageType" "http://example.com/attachment-usage/test"
"display" {"en-US" "A test attachment"}
"description" {"en-US" "A test attachment (description)"}
"contentType" "text/plain"
"length" 33
"sha2" "7063d0a4cfa93373753ad2f5a6ffcf684559fb1df3c2f0473a14ece7d4edb06a"}
{"usageType" "http://example.com/attachment-usage/test"
"display" {"en-US" "A test attachment"}
"description" {"en-US" "A test attachment (description)"}
"contentType" "text/plain"
"length" 33
"sha2" "7063d0a4cfa93373753ad2f5a6ffcf684559fb1df3c2f0473a14ece7d4edb06a"}]})

(def stmt-6-attach
{:content (.getBytes "here is a simple attachment")
:contentType "text/plain"
:length 33
:sha2 "7063d0a4cfa93373753ad2f5a6ffcf684559fb1df3c2f0473a14ece7d4edb06a"})

(deftest test-statement-fns
(let [sys (support/test-system)
sys' (component/start sys)
Expand Down Expand Up @@ -331,10 +375,7 @@
auth-ident
{:activity act-4 :attachments true}
#{})
(update-in [:attachments]
vec)
(update-in [:attachments 0 :content]
#(String. %))))))
string-result-attachment-content))))

(testing "(single)"
(is (= {:statement stmt-4
Expand All @@ -343,10 +384,7 @@
auth-ident
{:statementId (get stmt-4 "id") :attachments true}
#{})
(update-in [:attachments]
vec)
(update-in [:attachments 0 :content]
#(String. %)))))))
string-result-attachment-content)))))

(testing "agent query"
(is (= {:person
Expand All @@ -367,6 +405,62 @@
(component/stop sys')
(support/unstrument-lrsql)))

(deftest attachment-normalization-test
(let [sys (support/test-system)
sys' (component/start sys)
lrs (-> sys' :lrs)
id-4 (get stmt-4 "id")
id-5 (get stmt-5 "id")
id-6 (get stmt-6 "id")
act-4 (get-in stmt-4 ["object" "id"])]

(testing "accepts normalized attachments"
(is (= {:statement-ids [id-4
id-5
id-6]}
(lrsp/-store-statements
;; stmt-5 references stmt-4-attach
;; stmt-6 references stmt-4-attach AND stmt-6-attach (twice)
lrs auth-ident [stmt-4 stmt-5 stmt-6] [stmt-4-attach stmt-6-attach]))))

(testing "returns normalized attachments"
kelvinqian00 marked this conversation as resolved.
Show resolved Hide resolved
(testing "(multiple)"
(testing "single attachment"
(is (= {:statement-result {:statements [stmt-5 stmt-4] :more ""}
:attachments [(update stmt-4-attach :content #(String. %))]}
(-> (get-ss lrs
auth-ident
{:activity act-4
:attachments true}
#{})
string-result-attachment-content))))

(testing "multiple attachments"
(is (= {:statement-result {:statements [stmt-6 stmt-5 stmt-4] :more ""}
;; Compare attachments as a set, their order is different on the
;; postgres backend
:attachments #{(update stmt-6-attach :content #(String. %))
(update stmt-4-attach :content #(String. %))}}
(-> (get-ss lrs
auth-ident
{:attachments true}
#{})
string-result-attachment-content
(update :attachments set))))))

(testing "(single)"
(is (= {:statement stmt-6
:attachments #{(update stmt-6-attach :content #(String. %))
(update stmt-4-attach :content #(String. %))}}
(-> (get-ss lrs
auth-ident
{:statementId id-6 :attachments true}
#{})
string-result-attachment-content
(update :attachments set))))))
(component/stop sys')
(support/unstrument-lrsql)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Statement Ref Tests
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down