testing - 测试错误 : how to use :id?

标签 testing clojure h2 luminus hugsql

我第一次尝试在 luminush2hugsqlclojure 中执行此操作.

insert 语句在连接到 h2 数据库的 SQL 客户端中输入时工作正常,但在代码中失败。它似乎与 get-assessor 查询的 WHERE id = :id 子句有关,但找不到执行此操作的方法。

在文件 ./resources/sql/queries.sql

-- :name get-assessor :? :1
-- :doc retrieve a assessor given the id.
SELECT * FROM assessores
WHERE id = :id

-- :name save-assessor! :n
-- :doc creates new assessor
INSERT INTO assessores
    (first_name,
    last_name,
    email,
    phone,
    address1,
    address2,
    post_code,
    post_code_city,
    birth_date,
    tax_number,
    to_buy,
    to_sell,
    to_career,
    first_message,
    is_active)
VALUES (:first_name,
    :last_name,
    :email,
    :phone,
    :address1,
    :address2,
    :post_code,
    :post_code_city,
    :birth_date,
    :tax_number,
    :to_buy,
    :to_sell,
    :to_career,
    :first_message,
    :is_active)

在文件中:./test/db/core.clj

(deftest test-assessores
  (jdbc/with-db-transaction [t-conn *db*]
    (jdbc/db-set-rollback-only! t-conn)
    (let [timestamp (java.util.Date.)]
      (is (= 1 (db/save-assessor!
                 t-conn
                 {:first_name "Bob"
          :last_name            "Singer"
          :email                "lpe@gmail.com"
          :phone                "888232418"
          :address1             "10 st"
          :address2             "VNC"
          :post_code            "9990-990"
          :post_code_city       "Cer"
          :birth_date           "1962-06-06"
          :tax_number           204559449
          :to_buy               true
          :to_sell              false
          :to_career            false
          :first_message        "how to buy?"
          :is_active            true}
                 {:connection t-conn})))

      (is (=
           {:first_name "Bob"
          :last_name            "Singer"
          :email                "lpe@gmail.com"
          :phone                "888232418"
          :address1             "10 st"
          :address2             "VNC"
          :post_code            "9990-990"
          :post_code_city       "Cer"
          :birth_date           "1962-06-06"
          :tax_number           204559449
          :to_buy               true
          :to_sell              false
          :to_career            false
          :first_message        "how to buy?"
          :is_active            true}
           (-> (db/get-assessor t-conn {})
               (first)
               (select-keys [:first_name ])))))))

返回(截断)的消息是:

org.h2.jdbc.JdbcSQLException: Method is only allowed for a query. Use execute or executeUpdate instead of executeQuery; SQL statement:
                              INSERT INTO assessores
                                (first_name,
                                last_name,
                                email,
                                phone,
                                address1,
                                address2,
                                post_code,
                                post_code_city,
                                birth_date,
                                tax_number,
                                to_buy,
                                to_sell,
                                to_career,
                                first_message,
                                is_active)
                              VALUES ( ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ,
                                ? ) [90002-192]
                SQL: "INSERT INTO assessores\n\t(first_name,\n\tlast_name,\n\temail,\n\tphone,\n\taddress1,\n\taddress2,\n\tpost_code,\n\tpost_code_city,\n\tbirth_date,\n\ttax_number,\n\tto_buy,\n\tto_sell,\n\tto_career,\n\tfirst_message,\n\tis_active)\nVALUES ( ? ,\n\t? ,\n\t? ,\n\t? ,\n\t? ,\n\t? ,\n\t? ,\n\t? ,\n\t? ,\n\t? ,\n\t? ,\n\t? ,\n\t? ,\n\t? ,\n\t? )"
           SQLState: "90002"
          errorCode: 90002
    originalMessage: "Method is only allowed for a query. Use execute or executeUpdate instead of executeQuery"

ERROR in (test-assessores) (core.clj:4617)

clojure.lang.ExceptionInfo: Parameter Mismatch: :id parameter data not found.

Testing orio.test.handler
(...)
Ran 2 tests containing 4 assertions.
0 failures, 2 errors.
Tests failed.

如何解决这个问题?

最佳答案

-- :name save-assessor! :n

应该是

-- :name save-assessor! :! :n

参见:https://www.hugsql.org/#command

命令默认为:?这表明它是一个非修改查询。对于插入/更新语句,一个 :!应在定义中使用。

编辑: 在提供了更多信息之后——它看起来像 (get-assessor)正在寻找 :id 的参数(从您在 HUGSql 文件中的定义可以看出。

我会在该文件中创建一个新查询。类似于 (get-first-assessor)

-- :name get-first-assessor :? :1 -- :doc get the first assessor in the system, ordred by id SELECT * FROM assessores ORDER BY id ASC LIMIT 1

然后将您的调用替换为 (get-assessor t-conn {})(get-first-assessor t-conn {})在你的测试中

关于testing - 测试错误 : how to use :id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50212522/

相关文章:

ruby-on-rails - 预期 CSS "Home"返回一些 RSpec 问题

java - Java 与 Clojure 的良好并发示例

java - 如何让jOOQ生成不引用主键的复合外键?

mysql - H2 "runscript"命令将所有表名转为大写

javascript - 如何测试javascript代码

testing - 使用 Grails 构建测试数据插件创建对象而不保存它

unit-testing - 在没有启动嵌入式 tomcat 的情况下运行 grails 功能测试

clojure - 修改参数作为多方法的一部分

clojure - 如何将字符串放入列表

java - org.h2.jdbc.JdbcSQL异常 : Schema "MYAPP" not found; SQL statement