Skip to content
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
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# aws-api

## DEV

* Fix test-double in Babashka [#312](https://github.com/cognitect-labs/aws-api/issues/312)

## 0.8.824 / 2026-04-14

* Support JSON serialization of document shapes (by @GaspardP) [#270](https://github.com/cognitect-labs/aws-api/issues/270)
Expand Down
119 changes: 61 additions & 58 deletions src/cognitect/aws/client/test_double.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,64 +20,67 @@
(defprotocol TestDoubleClient
(-instrument [c ops]))

(deftype Client [info handlers]
ILookup
(valAt [this k]
(.valAt this k nil))

(valAt [_this k default]
(case k
:api
(-> info :service :metadata :cognitect.aws/service-name)
:service
(:service info)
default))

client.protocol/Client
(-get-info [_] info)

(-invoke [this {:keys [op request] :as op-map}]
(let [spec (validation/request-spec (:service info) op)
handler (get @handlers op)]
(cond
(not (get-in info [:service :operations op]))
(validation/unsupported-op-anomaly (:service info) op)

(and (validation/validate-requests? this)
spec
(not (validation/valid? spec request)))
(validation/invalid-request-anomaly spec request)

(not handler)
(no-handler-provided-anomaly op)

:else
(handler op-map))))

(-invoke-async [this {:keys [ch] :as op-map}]
(let [response-chan (or ch (a/promise-chan))]
(a/go
(let [resp (.-invoke this op-map)]
(a/>! response-chan resp)))
response-chan))

(-stop [_aws-client])

TestDoubleClient
(-instrument [client ops]
(swap! (.handlers client)
(fn [handlers]
(reduce-kv
(fn [m op handler]
(when-not (some-> client :service :operations op)
(throw (ex-info "Operation not supported"
(validation/unsupported-op-anomaly (-> client :service) op))))
(assoc m op (if (fn? handler) handler (constantly handler))))
handlers
ops)))))

;; ->Client is intended for internal use
(alter-meta! #'->Client assoc :skip-wiki true)
(defn- invoke* [client {:keys [op request] :as op-map} info handlers]

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extracted new fn so we can call it from both -invoke and -invoke-async. Can't use .-invoke with reify.

(let [spec (validation/request-spec (:service info) op)
handler (get @handlers op)]
(cond
(not (get-in info [:service :operations op]))
(validation/unsupported-op-anomaly (:service info) op)

(and (validation/validate-requests? client)
spec
(not (validation/valid? spec request)))
(validation/invalid-request-anomaly spec request)

(not handler)
(no-handler-provided-anomaly op)

:else
(handler op-map))))

(defn ^:skip-wiki ->Client [info handlers]
(reify
ILookup
(valAt [this k]
(.valAt this k nil))

(valAt [_this k default]
(case k
:api
(-> info :service :metadata :cognitect.aws/service-name)
:service
(:service info)
default))

client.protocol/Client
(-get-info [_] info)

(-invoke [this op-map]
(invoke* this op-map info handlers))

(-invoke-async [this {:keys [ch] :as op-map}]
(let [response-chan (or ch (a/promise-chan))]
(a/go
(let [resp (invoke* this op-map info handlers)]
(a/>! response-chan resp)))
response-chan))

(-stop [_aws-client])

TestDoubleClient
(-instrument [client ops]
(swap! handlers
(fn [handlers]
(reduce-kv
(fn [m op handler]
(when-not (some-> client :service :operations op)
(throw (ex-info "Operation not supported"
(validation/unsupported-op-anomaly (-> client :service) op))))
(assoc m op (if (fn? handler) handler (constantly handler))))
handlers
ops))))))

;; Intended for internal use
(alter-meta! #'TestDoubleClient assoc :skip-wiki true)

(defn instrument
Expand Down
4 changes: 2 additions & 2 deletions test/src/bb_test_runner.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
; NOTE: some tests won't run in babashka:
; cognitect.aws.http.default-test - all reify instances start with `babashka.impl.reify`, test won't pass
; cognitect.aws.signers-test - requires loading AWS SDK, which is not supported (no Java libs)
; cognitect.client.test-double-test - test double not supported in babashka
(def test-namespaces
['cognitect.aws.api-test
'cognitect.aws.client.shared-test
Expand All @@ -33,7 +32,8 @@
'cognitect.aws.retry-test
'cognitect.aws.shape-test
'cognitect.aws.util-test
'cognitect.client.impl-test])
'cognitect.client.impl-test
'cognitect.client.test-double-test])

(defn run-tests [& _args]
(apply require test-namespaces)
Expand Down