dimanche 4 octobre 2015

Clojure tests running out of order?

I have an application with a unit test. In this test I am creating a user account and then testing it. Then I modify it and test the modified version. The problem is, it seems to be doing some things out of order. Am I wrong? Either way, how do I fix it?

It seems to me that when I run the test, then modify the database AFTER the test

In this version, it works-- no errors:

(ns myapp.db.account-test
  (:use expectations)
  (:require
    [myapp.db         :as DB]
    [myapp.db.account :as Account]
    ))

(defn- $reset-database
  "Reset the database to a pristine state."
  []
  (DB/create-db))

($reset-database)
(let [acct-1 {:email "one@email.com" :username "user-one" :password "goodpwd1"}
      acct-2 {:email "two@email.com" :username "user-two" :password "goodpwd2"}]
  (Account/create acct-1)
  (let [db-acct (Account/seek {:email (:email acct-1)})]
    (expect (:email    acct-1) (:email    db-acct))
    (expect (:username acct-1) (:username db-acct))

    (expect true  (Account/authenticate {:email (:email acct-1) :password "goodpwd1"}))
    (expect false (Account/authenticate {:email (:email acct-1) :password "badpwd"}))

  ;; Now try to change some attributes of the account
  #_(Account/modify acct-1 acct-2)
)

then...

myapp 10:28 PM ~/Projects/myapp/myapp.net/code/myapp $ lein test myapp.db.account-testPicked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar
Rewriting src/cljx to target/generated/clj (clj) with features #{clj} and 0 transformations.
Rewriting src/cljx to target/generated/cljs (cljs) with features #{cljs} and 1 transformations.
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar

lein test myapp.db.account-test

Ran 0 tests containing 0 assertions.
0 failures, 0 errors.

Ran 4 tests containing 4 assertions in 171 msecs
0 failures, 0 errors.

But if I uncomment the last line, the 'prior' tests fail.

  ...
  ;; Now try to change some attributes of the account
  (Account/modify acct-1 acct-2)
)

then ...

myapp 10:40 PM ~/Projects/myapp/myapp.net/code/myapp $ lein test myapp.db.account-testPicked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar
Rewriting src/cljx to target/generated/clj (clj) with features #{clj} and 0 transformations.
Rewriting src/cljx to target/generated/cljs (cljs) with features #{cljs} and 1 transformations.
Picked up JAVA_TOOL_OPTIONS: -javaagent:/usr/share/java/jayatanaag.jar

lein test myapp.db.account-test

Ran 0 tests containing 0 assertions.
0 failures, 0 errors.

failure in (account_test.clj:21) : myapp.db.account-test
(expect
 true
 (Account/authenticate {:email (:email acct-1), :password "goodpwd1"}))

  act-msg: exception in actual: (Account/authenticate {:email (:email acct-1), :password "goodpwd1"})
    threw: class java.lang.NullPointerException -
           com.lambdaworks.crypto.SCryptUtil$check (SCryptUtil.java:74)
           on (core.clj:41)
           on (account.clj:109)
           on (account_test.clj:21)

failure in (account_test.clj:22) : myapp.db.account-test
(expect
 false
 (Account/authenticate {:email (:email acct-1), :password "badpwd"}))

  act-msg: exception in actual: (Account/authenticate {:email (:email acct-1), :password "badpwd"})
    threw: class java.lang.NullPointerException -
           com.lambdaworks.crypto.SCryptUtil$check (SCryptUtil.java:74)
           on (core.clj:41)
           on (account.clj:109)
           on (account_test.clj:22)

Ran 4 tests containing 4 assertions in 234 msecs
0 failures, 2 errors.

In case it matters, the relevant part of myapp.db.account is:

(defn seek
  "Get an account by email address"
  [{:keys [email]}]
  (row-to-hash (get-account-by-email {:email email})))

(defn modify
  "Modify an account."
  [{current-email :email}
   {new-email     :email new-username :username new-password :password}]

  (jdbc/with-db-transaction [connection db-env/spec]
    (let [current (seek {:email current-email})]

      (update-email-by-email! {:current_email current-email :new_email new-email}))))

(defn authenticate
  "Try to authenticate given an identifier and a password.
  Note that this returns a false case in the case of actual falsity, but nil
  in the case of a poorly formed query (such as missing credentials)"
  [{:keys [id email username password]}]
  (and
    password
    (scrypt/verify
      ;; compare this password attempt
      password
      ;; with this hash from the db (when possible)
      (:passhash
        (first
          (cond
            ;; Branch on which field is present.
            id       (get-authorization-fields-by-id       {:id id})
            email    (get-authorization-fields-by-email    {:email email})
            username (get-authorization-fields-by-username {:username username})
            ;; Default to false
            :else nil))))))

Aucun commentaire:

Enregistrer un commentaire