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

Add test for changes being available in after-update #171

Merged
merged 3 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/toucan2/instance.clj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

```clj
(instance-of? ::bird (instance ::toucan {})) ; -> true
(instance-of? ::toucan (instance ::bird {})) ; -> false
(instance-of? ::toucan (instance ::bird {})) ; -> false
```"
[model x]
(and (instance? x)
Expand Down
5 changes: 3 additions & 2 deletions src/toucan2/protocols.clj
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
"Get a map with any changes made to `instance` since it came out of the DB. Only includes keys that have been
added or given different values; keys that were removed are not counted. Returns `nil` if there are no changes."))

;;; `nil` and `IPersistentMap` can implement so of the methods that make sense for them -- `nil` or a plain map doesn't
;;; have any changes, so [[changes]] can return `nil`. I don't know what sort of implementation for stuff like
;;; `nil` and `IPersistentMap` can implement the methods that make sense for them: `nil` or a plain map doesn't have any
;;; changes, so [[changes]] can return `nil`. I don't know what sort of implementation for stuff like
;;; [[with-original]] or [[with-current]] makes sense so I'm not implementing those for now.
(extend-protocol IRecordChanges
nil
Expand Down Expand Up @@ -76,6 +76,7 @@
this)
(with-current [_this new-current]
new-current)

;; treat the entire map as `changes` -- that way if you accidentally do something like
;;
;; (merge plain-map instance)
Expand Down
27 changes: 27 additions & 0 deletions test/toucan2/tools/after_update_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
[clojure.string :as str]
[clojure.test :refer :all]
[clojure.walk :as walk]
[toucan2.insert :as insert]
[toucan2.instance :as instance]
[toucan2.protocols :as protocols]
[toucan2.select :as select]
[toucan2.test :as test]
[toucan2.test.track-realized-columns :as test.track-realized]
Expand Down Expand Up @@ -175,3 +177,28 @@
(generated-name `(after-update/define-after-update :model-2
[~'venue]
~'venue)))))))

(derive ::people.bird-lovers ::test/people)

;; Unfortunately the changes-available-test needs to do something side-effecty since the value of after-update is
;; discarded. Toggling an atom seemed like the easiest side effect.
(def ^:private bird-lover-found? (atom false))

(after-update/define-after-update ::people.bird-lovers
[person]
(when ((fnil str/includes? "") (:name (protocols/changes person)) "Cam")
(reset! bird-lover-found? true))
person)

(deftest ^:synchronized changes-available-test
(test/with-discarded-table-changes :people
(testing "Changes made via update should be available in after-update"
(reset! bird-lover-found? false)
(let [[{row-id :id}] (insert/insert-returning-instances! ::people.bird-lovers
{:name "Gwynydd Purves Wynne-Aubrey Meredith"})]
;; Update without the relevant changes:
(update/update! ::people.bird-lovers row-id {:created-at (LocalDateTime/parse "2017-01-01T00:00")})
(is (false? @bird-lover-found?))
tsmacdonald marked this conversation as resolved.
Show resolved Hide resolved
;; Update with relevant changes:
(update/update! ::people.bird-lovers row-id {:name "Cam Era"})
(is (true? @bird-lover-found?))))))
Loading