Skip to content

Commit

Permalink
Fix the apply-type-fns issue (#90)
Browse files Browse the repository at this point in the history
* Add a failing test to prove the issue #70 hypothesis

* Fix the issue #70
  • Loading branch information
marksto authored Jan 21, 2022
1 parent 02b34a1 commit ae341d9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/toucan/models.clj
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,10 @@
;;; ==================================================================================================================

(defn- apply-type-fns
"Apply the appropriate `type-fns` for OBJ."
"Apply the appropriate `type-fns` for an `obj` implementing some `IModel`."
[obj direction]
(into obj (for [[col type] (types obj)]
(when-let [v (get obj col)]
(when-some [v (get obj col)]
{col ((get-in @type-fns [type direction]) v)}))))

(defn- apply-property-fns
Expand Down
16 changes: 16 additions & 0 deletions test/toucan/db_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
[honeysql.core :as hsql]
[toucan
[db :as db]
[models :as models]
[test-setup :as test]
[util :as u]]
[toucan.test-models
[address :refer [Address]]
[category :refer [Category]]
[falsey :refer [Falsey]]
[food :refer [Food]]
[pg-enum :refer [TypedThing]]
[phone-number :refer [PhoneNumber]]
Expand Down Expand Up @@ -170,6 +172,20 @@
(expect Exception (db/resolve-model :user))
(expect Exception (db/resolve-model 'user)) ; entities symbols are case-sensitive

;; Test applying `type-fns`
(expect
#toucan.test_models.falsey.FalseyInstance{:bool? "true"}
(#'models/apply-type-fns #toucan.test_models.falsey.FalseyInstance{:bool? "Text"} :in))
(expect
#toucan.test_models.falsey.FalseyInstance{:bool? nil}
(#'models/apply-type-fns #toucan.test_models.falsey.FalseyInstance{:bool? nil} :in))
(expect
#toucan.test_models.falsey.FalseyInstance{:bool? "true"}
(#'models/apply-type-fns #toucan.test_models.falsey.FalseyInstance{:bool? true} :in))
(expect
#toucan.test_models.falsey.FalseyInstance{:bool? "false"}
(#'models/apply-type-fns #toucan.test_models.falsey.FalseyInstance{:bool? false} :in))

;; Test with-call-counting
(expect
2
Expand Down
12 changes: 12 additions & 0 deletions test/toucan/test_models/falsey.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(ns toucan.test-models.falsey
"A model with `:types` that include a `falsey-type` which reproduces an issue #70."
(:require [toucan.models :as models]))

(models/add-type! :falsey-type
:in #(if % "true" "false")
:out boolean)

(models/defmodel Falsey :falsey
models/IModel
(types [_]
{:bool? :falsey-type}))

0 comments on commit ae341d9

Please sign in to comment.