Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

s/vector?/sequential?/ when applying transform functions #115

Open
wants to merge 1 commit into from

2 participants

@gfredericks

The transform function code decides whether or not to use map by
testing with vector?, which breaks when the result set is a seq
instead, as is the case when you do a query of the form
(select foos (with bars)). As far as I can tell this could be
fixed either by replacing the vector? test with a sequential?
test, or using a mapv in apply-posts (and maybe elsewhere).

This commit implements the former, and adds a test.

@gfredericks gfredericks s/vector?/sequential?/ when applying transform functions
The transform function code decides whether or not to use `map` by
testing with `vector?`, which breaks when the result set is a seq
instead, as is the case when you do a query of the form
`(select foos (with bars))`. As far as I can tell this could be
fixed either by replacing the `vector?` test with a `sequential?`
test, or using a `mapv` in `apply-posts` (and maybe elsewhere).

This commit implements the former, and adds a test.
663c0d4
@gfredericks

After browsing the code a minimal amount I haven't seen why apply-transforms should even expect that it might have a single map instead of a sequence, but decided to not question and leave that as is, since it doesn't seem to have any tests.

@AlexBaranosky

Sets aren't sequential?, they are however coll? I'll have to take a look at this later tonight.

@gfredericks
@AlexBaranosky
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 21, 2013
  1. @gfredericks

    s/vector?/sequential?/ when applying transform functions

    gfredericks authored
    The transform function code decides whether or not to use `map` by
    testing with `vector?`, which breaks when the result set is a seq
    instead, as is the case when you do a query of the form
    `(select foos (with bars))`. As far as I can tell this could be
    fixed either by replacing the `vector?` test with a `sequential?`
    test, or using a `mapv` in `apply-posts` (and maybe elsewhere).
    
    This commit implements the former, and adds a test.
This page is out of date. Refresh to see the latest.
Showing with 12 additions and 1 deletion.
  1. +1 −1  src/korma/core.clj
  2. +11 −0 test/korma/test/core.clj
View
2  src/korma/core.clj
@@ -437,7 +437,7 @@
results
(if-let [trans (seq (-> query :ent :transforms))]
(let [trans-fn (apply comp trans)]
- (if (vector? results) (map trans-fn results) (trans-fn results)))
+ (if (sequential? results) (map trans-fn results) (trans-fn results)))
results)))
(defn- apply-prepares
View
11 test/korma/test/core.clj
@@ -609,3 +609,14 @@
:c 3})))
(order :a)))))))
+(defentity foos-abc-2)
+
+(defentity foos-abc-1
+ (has-many foos-abc-2)
+ (transform #(assoc % :bar :baz)))
+
+;; Tests that transform functions work correctly with (with ...)
+(deftest transform-fn-test
+ (with-redefs [do-query (constantly [{:id 12}])]
+ (is (= [{:id 12, :bar :baz, :foos-abc-2 [{:id 12}]}]
+ (select foos-abc-1 (with foos-abc-2))))))
Something went wrong with that request. Please try again.