<commit_before>(ns clj-money.api.entities-test
  (:require [clojure.test :refer :all]
            [clojure.pprint :refer [pprint]]
            [environ.core :refer [env]]
            [cheshire.core :as json]
            [clj-factory.core :refer [factory]]
            [clj-money.factories.user-factory]
            [clj-money.serialization :as serialization]
            [clj-money.validation :as validation]
            [clj-money.test-helpers :refer [reset-db
                                            with-authentication
                                            find-user
                                            find-entity]]
            [clj-money.api.entities :as api]
            [clj-money.models.entities :as entities]))

(def storage-spec (env :db))

(use-fixtures :each (partial reset-db storage-spec))

(def ^:private entity-context
  {:users [(factory :user {:email "<EMAIL>"})
           (factory :user {:email "<EMAIL>"})]
   :entities [{:user-id "<EMAIL>"
               :name "Personal"}]})

(deftest create-an-entity
  (let [context (serialization/realize storage-spec entity-context)
        user (find-user context "<EMAIL>")
        response (with-authentication user
                   (api/create {:params {:name "Business"
                                         :settings {:inventory-method "fifo"}}}))
        retrieved (entities/select storage-spec (:id user))]
    (is (= 201 (:status response)) "The response is a successful creation")
    (is (= {:name "Business"
            :settings {:inventory-method :fifo}}
           (select-keys (:body response) [:name :settings]))
        "The new entity is returned in the response")
    (is (= #{"Personal" "Business"} (->> retrieved
                                         (map :name)
                                         (into #{})))
        "The new entity can be retrieved.")))

(def ^:private entities-context
  (-> entity-context
      (update-in [:entities] #(concat % [{:user-id "<EMAIL>"
                                          :name "Business"}
                                         {:user-id "<EMAIL>"
                                          :name "Other"}]))))

(deftest get-a-list-of-entities
  (let [context (serialization/realize storage-spec entities-context)
        response (with-authentication (-> context :users first)
                   (api/index {:params {}}))]
    (is (= 200 (:status response)) "The response is successful")
    (is (= #{"Business" "Personal"} (->> (:body response)
                                         (map :name)
                                         (into #{})))
        "The correct entities are returned.")))

(deftest update-an-entity
  (let [context (serialization/realize storage-spec entity-context)
        entity (find-entity context "Personal")
        response (with-authentication (-> context :users first)
                   (api/update {:params {:id (:id entity)
                                         :name "My Stuff"
                                         :settings {:inventory-method "fifo"}}}))
        retrieved (entities/reload storage-spec entity)]
    (is (= 200 (:status response))
        "The request is successful")
    (is (= "fifo" (-> response :body (json/parse-string true) :settings :inventory-method))
        "The response includes the updated inventory-method value")
    (is (= :fifo (-> retrieved :settings :inventory-method))
        "The record is updated")))

(deftest delete-an-entity
  (let [context (serialization/realize storage-spec entities-context)
        entity (find-entity context "Personal")
        user (find-user context "<EMAIL>")
        response (with-authentication user
                   (api/delete {:params {:entity-id (:id entity)}}))
        retrieved (entities/select storage-spec (:id user))]
    (is (= 202 (:status response)) "The response is successful and empty")
    (is (= #{"Business"} (->> retrieved
                              (map :name)
                              (into #{})))
        "The delete entity is no longer returned.")))
<commit_msg>correct entity delete test<commit_after>(ns clj-money.api.entities-test
  (:require [clojure.test :refer :all]
            [clojure.pprint :refer [pprint]]
            [environ.core :refer [env]]
            [cheshire.core :as json]
            [clj-factory.core :refer [factory]]
            [clj-money.factories.user-factory]
            [clj-money.serialization :as serialization]
            [clj-money.validation :as validation]
            [clj-money.test-helpers :refer [reset-db
                                            with-authentication
                                            find-user
                                            find-entity]]
            [clj-money.api.entities :as api]
            [clj-money.models.entities :as entities]))

(def storage-spec (env :db))

(use-fixtures :each (partial reset-db storage-spec))

(def ^:private entity-context
  {:users [(factory :user {:email "<EMAIL>"})
           (factory :user {:email "<EMAIL>"})]
   :entities [{:user-id "<EMAIL>"
               :name "Personal"}]})

(deftest create-an-entity
  (let [context (serialization/realize storage-spec entity-context)
        user (find-user context "<EMAIL>")
        response (with-authentication user
                   (api/create {:params {:name "Business"
                                         :settings {:inventory-method "fifo"}}}))
        retrieved (entities/select storage-spec (:id user))]
    (is (= 201 (:status response)) "The response is a successful creation")
    (is (= {:name "Business"
            :settings {:inventory-method :fifo}}
           (select-keys (:body response) [:name :settings]))
        "The new entity is returned in the response")
    (is (= #{"Personal" "Business"} (->> retrieved
                                         (map :name)
                                         (into #{})))
        "The new entity can be retrieved.")))

(def ^:private entities-context
  (-> entity-context
      (update-in [:entities] #(concat % [{:user-id "<EMAIL>"
                                          :name "Business"}
                                         {:user-id "<EMAIL>"
                                          :name "Other"}]))))

(deftest get-a-list-of-entities
  (let [context (serialization/realize storage-spec entities-context)
        response (with-authentication (-> context :users first)
                   (api/index {:params {}}))]
    (is (= 200 (:status response)) "The response is successful")
    (is (= #{"Business" "Personal"} (->> (:body response)
                                         (map :name)
                                         (into #{})))
        "The correct entities are returned.")))

(deftest update-an-entity
  (let [context (serialization/realize storage-spec entity-context)
        entity (find-entity context "Personal")
        response (with-authentication (-> context :users first)
                   (api/update {:params {:id (:id entity)
                                         :name "<NAME>"
                                         :settings {:inventory-method "fifo"}}}))
        retrieved (entities/reload storage-spec entity)]
    (is (= 200 (:status response))
        "The request is successful")
    (is (= "fifo" (-> response :body (json/parse-string true) :settings :inventory-method))
        "The response includes the updated inventory-method value")
    (is (= :fifo (-> retrieved :settings :inventory-method))
        "The record is updated")))

(deftest delete-an-entity
  (let [context (serialization/realize storage-spec entities-context)
        entity (find-entity context "Personal")
        user (find-user context "<EMAIL>")
        response (with-authentication user
                   (api/delete {:params {:id (:id entity)}}))
        retrieved (entities/select storage-spec (:id user))]
    (is (= 204 (:status response)) "The response is successful and empty")
    (is (= #{"Business"} (->> retrieved
                              (map :name)
                              (into #{})))
        "The delete entity is no longer returned.")))

{'fraction_non_alphanumeric': 0.13520470703603824, 'fraction_numerical': 0.0029418975239029173, 'mean_word_length': 1.7452893674293406, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 52, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2871562', 'n_tokens_mistral': 2178, 'n_tokens_neox': 2065, 'n_words': 592}