Skip to content

Instantly share code, notes, and snippets.

@mput
Created July 30, 2021 10:39
Show Gist options
  • Save mput/4b0ca32396d3b8ff09da951b961e2cde to your computer and use it in GitHub Desktop.
Save mput/4b0ca32396d3b8ff09da951b961e2cde to your computer and use it in GitHub Desktop.
(ns wellcore.mapper.core-test
(:require [clojure.test :refer [deftest use-fixtures is ]]
[zen.core :as zen]
[test-utils :refer [match-mapping]]))
(use-fixtures :once
(fn [t]
(try
(def ztx' (zen/new-context))
(def ztx {:zen/ctx ztx'})
(zen/read-ns ztx' 'test.mapper_test)
(zen/read-ns ztx' 'fhir-map.primitives)
(when-let [mapp-errs (:errors @ztx')]
(throw (ex-info "Mapping Errors: "
{:errors mapp-errs :mapping 'test.mapper_test})))
(catch Exception e
(prn "Error in use fixture!!!")
(clojure.pprint/pprint e)))
(t)))
(deftest errors-handling
(match-mapping
'test.mapper_test/test-for-err
{:seq "4"
:diag_1 "sick"
:diag_1_seq "1"
:diag_2 "wrong"
:diag_2_seq "b2"
:a 31
:b 1
:c {:d 1}
:trans_id "b4"
:items [{:n "2"}
{:n "10"}
{:n "b1"}]}
[{:sequence 4
:diags [{:value "sick" :seq 1} {:value "wrong" :seq nil}]
:item-int [{:nint 2}
{:nint 10}
{:nint nil}]
:identifier {:id nil
:num 1
:num_err nil}}
[{:error "Expected: :int, got b4"
:to [:identifier :id]
:from [:trans_id]}
{:to [:identifier :num_err]
:from nil}
{:to [:diags 1 :seq],
:from [:diag_2_seq]}
{:to [:item-int 2 :nint],
:from [:items 2 :n]}]]))
(deftest deep-split
(match-mapping
'test.mapper_test/deep-split
{:sys "my"
:items [{:sub "phrmc"
:diags [{:cod "1"}
{:cod "2"}]}
{:sub "prof"
:diags [{:cod "3"}
{:cod "err4"}]}]}
[{:item [{:system "my"
:subtype "phrmc"
:smth [{:code 1} {:code 2}]}
{:system "my"
:subtype "prof"
:smth [{:code 3} {:code nil}]}]}
[{:error "Expected: :int, got err4"
:action {:type :int,
:action 'zhir.actions/coerce}
:to [:item 1 :smth 1 :code]
:from [:items 1 :diags 1 :cod]}]]))
(deftest test-mapping
(match-mapping
'test.mapper_test/test-mapping
{:transaction_id "202402097938025999"}
[{:resourceType "ExplanationOfBenefit"
:id "202402097938025999"
:identifier [{:value "202402097938025999"
:system "transactionid"}]
:patient {:resourceType "Patient"}}
empty?]))
(deftest actions-on-maps
(match-mapping
'test.mapper_test/actions-on-maps
{:items [{:s "2"
:se "2"}
{:s "10"
:se "10"}
{:s "4"
:se "bb10"}]}
[{:items-flat [2 10 4]
:item-sorted [{:sequence "10"}
{:sequence "2"}
{:sequence "4"}]
:item-sorted-int [{:sequence 2}
{:sequence 4}
{:sequence 10}]
:min-seq 2
:min-seq-error [2 10 nil]
}
[{:error "Expected: :int, got bb10",
:value "bb10",
:to [:min-seq-error 2],
:from [:items] ;;FIXME: where is index???
}
]]))
(deftest apply-test
(match-mapping
'test.mapper_test/apply
{:cob "21.88"
:copay "18.12"
:disc "10"}
[{:patient-copay 18.12M
:patient-share 40.00M
:total 30M}
empty?]))
(deftest actions-on-maps-2
(match-mapping
'test.mapper_test/simple-get
{:cob "21.88"
:copay "18.12"
:disc "10"}
[[21.88M]
empty?]))
(deftest empty-object
(match-mapping
'test.mapper_test/empty-hashmap
{:cob "21.88"
:copay "18.12"
:disc "10"}
[empty? empty?]))
(deftest min-action
(match-mapping
'test.mapper_test/min-action
{:a "21.88"
:b 1}
[nil [{:error "Got arguments of different types"
:value ["21.88" 1]
:action {:action 'zhir.actions/min}
:to [],
:from nil} nil]])
(match-mapping
'test.mapper_test/min-action
{:a 10}
[nil [{:error "Some arguments are null",
:value [10 nil],
:action {:action 'zhir.actions/min},
:to [],
:from nil} nil]]))
(deftest map-to-action
(match-mapping
'test.mapper_test/map-to-action
{:a "P"}
["active" empty?])
#_(match-mapping
'test.mapper_test/map-to-action
{:a "r"}
[nil [{:error "No value 'r' in mapping",
:value "r",
:action nil,
:to [],
:from nil} nil]]))
(deftest skip-incomplete
(match-mapping
'test.mapper_test/incomplete
{:a "P"}
[[{:system "system", :value "P"} nil]
empty?]))
(deftest value-required
(match-mapping
'test.mapper_test/incomplete
{}
[nil [{:error "Value is required",
:value nil,
:transformation 'zhir/build-object
:action {:action 'zhir.actions/required},
:to [1],
}]]))
(deftest actions-on-vector
(match-mapping
'test.mapper_test/actions-on-vector
{:a "a"
:b "b"}
[[{:val "a"
:seq 1}
{:val "b"
:seq 2}]
empty?]))
(deftest steps-resolver
(match-mapping
'test.mapper_test/items
{:a "a"}
[[{:val "a"}
{:val "a"}]
empty?]))
(deftest macro-test
(match-mapping
'test.mapper_test/macro-test
{:payer-payment "22"
:cob "42"}
[[{:category {:coding [{:system "https://payment"
:code 1}]}
:amount {:value 22M
:currency "USD"}}
{:category {:coding [{:system "https://cob"
:code 2}]}
:amount {:value 42
:currency "USD"}}]
empty?]))
#_
(wellcore.world/stop-box)
(deftest supporting-info-category-code-multiple-test
(match-mapping
'test.mapper_test/category-multiple-usage
{:formulary_flag "F"}
[{:category
{:coding
[{:system
"https://wellnecity.com/fhir/wellnecity/codesystem/rx-information-category",
:code "formulary-indicator"}]},
:code
{:coding
[{:system
"https://wellnecity.com/fhir/wellnecity/codesystem/formulary-indicator",
:code "in-formulary"}
{:system
"https://wellnecity.com/fhir/ascend/codesystem/formulary-indicator",
:code "F"}
nil]},
:_for-all-items true}
empty?]))
(deftest rearrange-sequence-elements
(match-mapping
'test.mapper_test/rearrange-sequence-elements
{:test {:careTeam [{:b 1 :a 2}
{:a 3}
{:a 4 :_for-all-items true}]
:item [{:careTeam [{:a 3}]
:diagnosis [{:code "il"}]}
{:careTeam nil}
{}]}}
[{:careTeam [{:b 1, :a 2, :sequence 1}
{:a 3, :sequence 2}
{:a 4, :sequence 3}],
:diagnosis [{:code "il", :sequence 1}]
:item
[{:careTeamSequence [2 3]
:diagnosisSequence [1]}
{:careTeamSequence [3]}
{:careTeamSequence [3]}]}
empty?]))
(deftest macro-in-macro
(match-mapping
'test.mapper_test/macro-in-macro
{:test 42}
[{:category {:coding [{:system "some-category", :code "cat6"}]},
:amount {:value 42M, :currency "USD"}}
empty?]))
(deftest identity-transformation
(match-mapping
'test.mapper_test/identity-transformation
{:given "Hickey"}
[{:name "Rich" :last "Hickey"}
empty?]))
(deftest cond-test-1
(match-mapping
'test.mapper_test/cond-test
{:dispatcher "a" :a 1 :b 2}
[1
empty?]))
(deftest cond-test-2
(match-mapping
'test.mapper_test/cond-test
{:dispatcher "b" :a 1 :b 2}
[2
empty?]))
(deftest cond-test-3
(match-mapping
'test.mapper_test/cond-test
{:dispatcher "c" :a 1 :b 2}
[3
empty?]))
(deftest cond-test-error
(is (thrown?
Throwable
(match-mapping
'test.mapper_test/cond-test-error
{:dispatcher "c" :a 1 :b 2}
[nil nil]))))
(deftest or-test
(match-mapping
'test.mapper_test/or
{:a 1 :b 2}
[1 empty?]))
(deftest or-test-2
(match-mapping
'test.mapper_test/or
{:b 2}
[2 empty?]))
(deftest or-test-3
(match-mapping
'test.mapper_test/or
{}
[nil empty?]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment