<commit_before>(ns clj-money.models.users-test
  (:require [clojure.test :refer :all]
            [clojure.pprint :refer [pprint]]
            [clojure.data :refer [diff]]
            [environ.core :refer [env]])
  (:use [clj-money.models.users :as users]
        [clj-money.test-helpers :refer [reset-db]]))

(def data-store (env :db))

(use-fixtures :each (partial reset-db data-store))

(def attributes {:first_name "John"
                 :last_name "Doe"
                 :email "<EMAIL>"
                 :password "<PASSWORD>"})

(deftest create-a-user
  (let [user (users/create data-store attributes)]
    (testing "An created user can be retreived"
      (let [users (->> (users/select data-store)
                       (map #(select-keys % [:first_name
                                             :last_name
                                             :email
                                             :password])))
            expected [{:first_name "John"
                       :last_name "Doe"
                       :email "<EMAIL>"}]]
        (is (= expected users))))
    (testing "It returns a user map"
      (is (number? (:id user)) "The id should be a number")
      (is (= {:first_name "John"
              :last_name "Doe"
              :email "<EMAIL>"}
             (dissoc user :id))
          "The map should contain the user properties"))))

(deftest try-to-create-with-invalid-data
  (testing "Email is required"
    (try
      (users/create data-store (dissoc attributes :email))
      (catch clojure.lang.ExceptionInfo e
        (is (= {:email 'missing-required-key}
               (:error (ex-data e)))))))
  (testing "Email must be a valid email address"
    (try
      (users/create data-store (assoc attributes :email "notavalidemail"))
      (is false "The expected exception was not thrown")
      (catch clojure.lang.ExceptionInfo e
        (is (= {:email "Email mmust be a valid email address"}
               (:error (ex-data e)))))))
  (testing "First name is required"
    (try
      (users/create data-store (dissoc attributes :first_name))
      (is false "The expected exception was not thrown")
      (catch clojure.lang.ExceptionInfo e
        (is (= {:first_name 'missing-required-key}
               (:error (ex-data e)))))))
  (testing "Last name is required"
    (try
      (users/create data-store (dissoc attributes :last_name))
      (is false "The expected exception was not thrown")
      (catch clojure.lang.ExceptionInfo e
        (is (= {:last_name 'missing-required-key}
               (:error (ex-data e))))))))
<commit_msg>rework email validation test<commit_after>(ns clj-money.models.users-test
  (:require [clojure.test :refer :all]
            [clojure.pprint :refer [pprint]]
            [clojure.data :refer [diff]]
            [environ.core :refer [env]])
  (:use [clj-money.models.users :as users]
        [clj-money.test-helpers :refer [reset-db]]))

(def data-store (env :db))

(use-fixtures :each (partial reset-db data-store))

(def attributes {:first_name "John"
                 :last_name "Doe"
                 :email "<EMAIL>"
                 :password "<PASSWORD>"})

(deftest create-a-user
  (let [user (users/create data-store attributes)]
    (testing "An created user can be retreived"
      (let [users (->> (users/select data-store)
                       (map #(select-keys % [:first_name
                                             :last_name
                                             :email
                                             :password])))
            expected [{:first_name "John"
                       :last_name "Doe"
                       :email "<EMAIL>"}]]
        (is (= expected users))))
    (testing "It returns a user map"
      (is (number? (:id user)) "The id should be a number")
      (is (= {:first_name "John"
              :last_name "Doe"
              :email "<EMAIL>"}
             (dissoc user :id))
          "The map should contain the user properties"))))

(deftest try-to-create-with-invalid-data
  (testing "Email is required"
    (try
      (users/create data-store (dissoc attributes :email))
      (catch clojure.lang.ExceptionInfo e
        (is (= {:email 'missing-required-key}
               (:error (ex-data e)))))))
  (testing "Email must be a valid email address"
    (try
      (users/create data-store (assoc attributes :email "notavalidemail"))
      (is false "The expected exception was not thrown")
      (catch clojure.lang.ExceptionInfo e
        (is (contains? (-> e ex-data :error) :email) "There should be an error for the email"))))
  (testing "First name is required"
    (try
      (users/create data-store (dissoc attributes :first_name))
      (is false "The expected exception was not thrown")
      (catch clojure.lang.ExceptionInfo e
        (is (= {:first_name 'missing-required-key}
               (:error (ex-data e)))))))
  (testing "Last name is required"
    (try
      (users/create data-store (dissoc attributes :last_name))
      (is false "The expected exception was not thrown")
      (catch clojure.lang.ExceptionInfo e
        (is (= {:last_name 'missing-required-key}
               (:error (ex-data e))))))))

{'fraction_non_alphanumeric': 0.13138123675592372, 'fraction_numerical': 0.0, 'mean_word_length': 1.9702517162471396, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '393507', 'n_tokens_mistral': 1528, 'n_tokens_neox': 1488, 'n_words': 451}