Skip to content
This repository has been archived by the owner on Feb 7, 2024. It is now read-only.

Commit

Permalink
AE-2036: Fix update-aineistot-test
Browse files Browse the repository at this point in the history
- Improve comments
- Don't rely on the order of the line in the csv
- Add todistukset asap to test that signing them
  is the operation that makes them show in the csv
- Change kayttotarkoitus to "RT" (rivitalo) to
  make something appear in the anonymized aineisto also
  • Loading branch information
solita-juhohaa committed Dec 12, 2023
1 parent eb64742 commit ca33546
Showing 1 changed file with 53 additions and 31 deletions.
84 changes: 53 additions & 31 deletions etp-backend/src/test/clj/solita/etp/service/aineisto_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,24 @@
third-line (.readLine reader)]
[second-line third-line])))

(defn- is-in-included-in-exactly-one? [string strings]
(->> (map #(str/includes? %1 string) strings)
(filter true?)
(count)
(== 1)))

(t/deftest update-aineistot-test
(t/testing "Aineistot don't exist before generating"
(t/is (false? (file/file-exists? ts/*aws-s3-client* "/aineistot/1/energiatodistukset.csv")))
(t/is (false? (file/file-exists? ts/*aws-s3-client* "/aineistot/2/energiatodistukset.csv")))
(t/is (false? (file/file-exists? ts/*aws-s3-client* "/aineistot/3/energiatodistukset.csv"))))

(t/testing "Aineistot exist after generating"
(aineisto/update-aineistot-in-s3! ts/*db* {:id -5 :rooli 2} ts/*aws-s3-client*)
(t/is (true? (file/file-exists? ts/*aws-s3-client* "/aineistot/1/energiatodistukset.csv")))
(t/is (true? (file/file-exists? ts/*aws-s3-client* "/aineistot/2/energiatodistukset.csv")))
(t/is (true? (file/file-exists? ts/*aws-s3-client* "/aineistot/3/energiatodistukset.csv"))))

(t/testing "New energiatodistus shows up correctly when updating aineistot"
(let [;; Add laatija
laatija-id (first (keys (test-data.laatija/generate-and-insert! 1)))
Expand All @@ -109,55 +117,69 @@
rakennustunnus-2 (generators/generate-rakennustunnus)

;; Create two energiatodistus. One with rakennustunnus-1, one with rakennustunnus-2.
todistus-1 (-> (test-data.energiatodistus/generate-add 2018 true) (assoc-in [:perustiedot :rakennustunnus] rakennustunnus-1))
todistus-2 (-> (test-data.energiatodistus/generate-add 2018 true) (assoc-in [:perustiedot :rakennustunnus] rakennustunnus-2))

;; Insert todistus-1
[todistus-1-id] (test-data.energiatodistus/insert! [todistus-1] laatija-id)]
;; Also set both to RT (rivitalo) so that they show up in the aineisto 3 (anonymized set).
todistus-1 (-> (test-data.energiatodistus/generate-add 2018 true)
(assoc-in [:perustiedot :rakennustunnus] rakennustunnus-1)
(assoc-in [:perustiedot :kayttotarkoitus] "RT"))
todistus-2 (-> (test-data.energiatodistus/generate-add 2018 true)
(assoc-in [:perustiedot :rakennustunnus] rakennustunnus-2)
(assoc-in [:perustiedot :kayttotarkoitus] "RT"))

;; Insert both todistus, but they are still unsigned.
[todistus-1-id] (test-data.energiatodistus/insert! [todistus-1] laatija-id)
[todistus-2-id] (test-data.energiatodistus/insert! [todistus-2] laatija-id)]

;; Sign todistus-1
(test-data.energiatodistus/sign! todistus-1-id laatija-id true)

;; Update aineistot
;; Update aineistot. Todistus-1 should be included after the update,
;; but todistus-2 should be not as it's not signed yet.
(aineisto/update-aineistot-in-s3! ts/*db* {:id -5 :rooli 2} ts/*aws-s3-client*)

;; Aineisto 1
;; Aineisto 1 - Test that rakennustunnus-1 exists, but that there is only one row of energiatodistukset.
(let [[first second] (get-first-two-energiatodistus-lines-from-aineisto "/aineistot/1/energiatodistukset.csv")]
(t/is (true? (str/includes? first rakennustunnus-1)))
(t/is (nil? second)))

;; Aineisto 2
;; Aineisto 2 - Test that rakennustunnus-1 exists, but that there is only one row of energiatodistukset.
(let [[first second] (get-first-two-energiatodistus-lines-from-aineisto "/aineistot/2/energiatodistukset.csv")]
(t/is (true? (str/includes? first rakennustunnus-1)))
(t/is (nil? second)))

;; Aineisto 3
;; Aineisto 3 - Test that one row exists and that the rakennustunnus can't be found as this set should be
;; anonymized.
(let [[first second] (get-first-two-energiatodistus-lines-from-aineisto "/aineistot/3/energiatodistukset.csv")]
;; Anonymized sets should not contain the rows with this few energiatodistus.
(t/is (nil? first))
(t/is (false? (str/includes? first rakennustunnus-1)))
(t/is (false? (nil? first)))
(t/is (nil? second)))

(let [;; Insert todistus-2
[todistus-2-id] (test-data.energiatodistus/insert! [todistus-2] laatija-id)]
;; Sign todistus-2
(test-data.energiatodistus/sign! todistus-2-id laatija-id true)

;; Sign todistus-2
(test-data.energiatodistus/sign! todistus-2-id laatija-id true)

;; Update aineistot
(aineisto/update-aineistot-in-s3! ts/*db* {:id -5 :rooli 2} ts/*aws-s3-client*)
;; Update aineistot. Now todistus-1 and todistus-2 should be in the csv.
(aineisto/update-aineistot-in-s3! ts/*db* {:id -5 :rooli 2} ts/*aws-s3-client*)

;; Aineisto 1
(let [[first second] (get-first-two-energiatodistus-lines-from-aineisto "/aineistot/1/energiatodistukset.csv")]
(t/is (true? (str/includes? first rakennustunnus-1)))
(t/is (true? (str/includes? second rakennustunnus-2))))
;; Aineisto 1 - Test that both rakennustunnus exist. It does not matter which one is which
;; as the order of them is not guaranteed.
(let [csv-et-lines (get-first-two-energiatodistus-lines-from-aineisto "/aineistot/1/energiatodistukset.csv")]
(t/is (true? (is-in-included-in-exactly-one? rakennustunnus-1 csv-et-lines)))
(t/is (true? (is-in-included-in-exactly-one? rakennustunnus-2 csv-et-lines))))

;; Aineisto 2
(let [[first second] (get-first-two-energiatodistus-lines-from-aineisto "/aineistot/2/energiatodistukset.csv")]
(t/is (true? (str/includes? first rakennustunnus-1)))
(t/is (true? (str/includes? second rakennustunnus-2))))
;; Aineisto 2 - Test that both rakennustunnus exist. It does not matter which one is which
;; as the order of them is not guaranteed.
(let [csv-et-lines (get-first-two-energiatodistus-lines-from-aineisto "/aineistot/2/energiatodistukset.csv")]
(t/is (true? (is-in-included-in-exactly-one? rakennustunnus-1 csv-et-lines)))
(t/is (true? (is-in-included-in-exactly-one? rakennustunnus-2 csv-et-lines))))

;; Aineisto 3
(let [[first second] (get-first-two-energiatodistus-lines-from-aineisto "/aineistot/3/energiatodistukset.csv")]
;; Anonymized sets should not contain the rows with this few energiatodistus.
(t/is (nil? first))
(t/is (nil? second)))))))
;; Aineisto 3 - Test that two rows exists and that either of the rakennustunnukset can't be found
;; as this set is be anonymized.
(let [[first second] (get-first-two-energiatodistus-lines-from-aineisto "/aineistot/3/energiatodistukset.csv")]
;; Rakennustunnus-1 can't be found
(t/is (false? (str/includes? first rakennustunnus-1)))
(t/is (false? (str/includes? second rakennustunnus-1)))
;; Rakennustunnus-2 can't be found
(t/is (false? (str/includes? first rakennustunnus-2)))
(t/is (false? (str/includes? second rakennustunnus-2)))
;; The lines are not empty.
(t/is (false? (nil? first)))
(t/is (false? (nil? second)))))))

0 comments on commit ca33546

Please sign in to comment.