-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from Cyrik/wip/co-effects
added co-effects testing
- Loading branch information
Showing
5 changed files
with
209 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,7 @@ | |
.hgignore | ||
.hg/ | ||
.DS_Store | ||
.lsp | ||
.lh | ||
.calva | ||
.clj-kondo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,125 @@ | ||
(ns jcpsantiago.bulgogi-test | ||
{:jcpsantiago.bulgogi/features true | ||
:jcpsantiago.bulgogi/coeffects true} | ||
(:require | ||
[clojure.test :refer :all] | ||
[jcpsantiago.bulgogi :refer :all])) | ||
[clojure.string :as s] | ||
[clojure.test :refer :all] | ||
[clojure.set] | ||
[jcpsantiago.features] | ||
[jcpsantiago.bulgogi :as SUT])) | ||
|
||
|
||
(deftest a-test | ||
(testing "will be gone soon :D" | ||
(is (= 1 1)))) | ||
(defn boolean->int | ||
"Cast a boolean to 1/0 integer indicator" | ||
[b] | ||
(when boolean? b | ||
(if (true? b) 1 0))) | ||
|
||
|
||
(defn email-name | ||
"Lower-cased name of an email address (the bit before @)" | ||
[email] | ||
s/lower-case | ||
(s/replace-first #"@.*" ""))) | ||
|
||
|
||
(defn n-digits-in-email-name | ||
"Number of digits in the email name" | ||
[{email :email}] | ||
(->> (email-name email) | ||
(re-seq #"\d") | ||
count)) | ||
|
||
|
||
(defn n-chars-in-email-name | ||
"Number of characters in the email name i.e. length of the email name" | ||
[{email :email}] | ||
(-> (email-name email) | ||
count)) | ||
|
||
|
||
(defn diff-eur-previous-order | ||
"Difference in euros between the current order and the previous one." | ||
[{current-amount :current-amount previous-amount :previous-amount}] | ||
(- current-amount previous-amount)) | ||
|
||
|
||
(defn risky-item? | ||
"Boolean depending on whether an item is risky or not" | ||
[{brand :brand}] | ||
(->> brand | ||
s/lower-case | ||
(re-seq #"baz corp") | ||
some? | ||
boolean->int)) | ||
|
||
|
||
(defn contains-risky-item | ||
"Indicator 1/0 depending on whether a risky item is present in the cart" | ||
[{items :items}] | ||
(->> items | ||
(map #(risky-item? %)) | ||
(some #(= 1 %)) | ||
boolean->int)) | ||
|
||
|
||
(defn ^{:bulgogi/coeffect ::added} needs-coeffect | ||
[{data ::added}] | ||
data) | ||
|
||
|
||
(defn added | ||
[_] | ||
{::added "some data"}) | ||
|
||
|
||
(defn email-name-as-coeffect | ||
[{email :email}] | ||
{::email-name-as-coeffect (-> email | ||
s/lower-case | ||
(s/replace-first #"@.*" ""))}) | ||
|
||
|
||
(defn ^{:bulgogi/coeffect ::email-name-as-coeffect} n-chars-in-email-name-w-coeffect | ||
[{email-name-as-coeffect ::email-name-as-coeffect}] | ||
(count email-name-as-coeffect)) | ||
|
||
|
||
(def test-input | ||
{:current-amount 700 | ||
:previous-amount 400 | ||
:email "[email protected]" | ||
:items [{:brand "Foo Industries" :value 1234} | ||
{:brand "Baz Corp" :value 35345}]}) | ||
|
||
|
||
(deftest preprocessed | ||
(testing "basics" | ||
(is (= {:contains-risky-item 1 | ||
:diff-eur-previous-order 300 | ||
:n-digits-in-email-name 2} | ||
(SUT/preprocessed {:input-data test-input | ||
:features ["n-digits-in-email-name" | ||
"contains-risky-item" | ||
"diff-eur-previous-order"]} | ||
)))) | ||
(testing "coeffect" | ||
(is (= {:needs-coeffect "some data"} | ||
(SUT/preprocessed {:input-data test-input | ||
:features ["needs-coeffect"]} | ||
)))) | ||
(testing "coeffect complex feature" | ||
(is (= {:n-chars-in-email-name-w-coeffect 10} | ||
(SUT/preprocessed {:input-data test-input | ||
:features ["n-chars-in-email-name-w-coeffect"]} | ||
))))) | ||
|
||
(deftest all-features | ||
(testing "finds features in marked ns" | ||
(is (clojure.set/subset? #{'distinct-feature1 'feature1} | ||
(set (keys (SUT/all-features)))))) | ||
(testing "throws on conflict" | ||
(require '[jcpsantiago.features2]) | ||
(is (thrown-with-msg? Exception #"^Conflict" (SUT/all-features))))) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
(ns jcpsantiago.features | ||
{:jcpsantiago.bulgogi/features true}) | ||
|
||
(defn feature1 [_] | ||
(inc 1)) | ||
|
||
(defn distinct-feature1 [] | ||
"some") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
(ns jcpsantiago.features2 | ||
{:jcpsantiago.bulgogi/features true}) | ||
|
||
(defn feature1 [_] | ||
(inc 1)) | ||
|
||
(defn distinct-feature2 [] | ||
"some") |