Ошибка теста: как использовать: id? - PullRequest
0 голосов
/ 07 мая 2018

Я пытаюсь сделать это впервые в luminus, h2, hugsql и clojure.

Оператор insert прекрасно работает при вводе в клиенте SQL, подключенном к базе данных h2, но в коде происходит сбой. Кажется, это связано с предложением WHERE id = :id запроса get-assessor, но не может найти способ сделать это.

В файле ./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.

Как это исправить?

1 Ответ

0 голосов
/ 07 мая 2018
-- :name save-assessor! :n

должно быть

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

см .: https://www.hugsql.org/#command

команда по умолчанию имеет значение :?, что указывает на то, что это неизменяемый запрос. Для операторов вставки / обновления в определении следует использовать :!.

Edit: После получения дополнительной информации - похоже, что (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 {}) в своем тесте

...