Files
tbd/test/tbd/todo/core_test.clj
ajet b1783e3f85
CI / Test + coverage (push) Failing after 22s
add todo api
2026-09-19 07:46:05 -09:00

42 lines
2.1 KiB
Clojure

(ns tbd.todo.core-test
(:require [clojure.test :refer [deftest is testing]]
[tbd.todo.core :as todo]))
(deftest validate-title-test
(testing "trims"
(is (= {:ok "buy milk"} (todo/validate-title " buy milk\n"))))
(testing "blank or missing"
(doseq [title [nil "" " " "\t\n"]]
(is (= {:error "title must not be blank"} (todo/validate-title title)) (pr-str title))))
(testing "length is checked after trimming"
(let [longest (apply str (repeat todo/max-title-length "x"))]
(is (= {:ok longest} (todo/validate-title (str " " longest " "))))
(is (contains? (todo/validate-title (str longest "x")) :error)))))
(deftest new-todo-test
(is (= {:ok {:owner-id 7 :title "buy milk" :done false :created-at 1000 :updated-at 1000}}
(todo/new-todo 7 " buy milk " 1000)))
(is (= {:error "title must not be blank"} (todo/new-todo 7 " " 1000))))
(def stored {:id 1 :owner-id 7 :title "buy milk" :done false :created-at 1000 :updated-at 1000})
(deftest apply-changes-test
(testing "title"
(is (= {:ok (assoc stored :title "buy oat milk" :updated-at 2000)}
(todo/apply-changes stored {:title " buy oat milk "} 2000))))
(testing "done, both ways"
(let [done (:ok (todo/apply-changes stored {:done true} 2000))]
(is (= (assoc stored :done true :updated-at 2000) done))
(is (= (assoc stored :updated-at 3000) (:ok (todo/apply-changes done {:done false} 3000))))))
(testing "both at once"
(is (= {:ok (assoc stored :title "x" :done true :updated-at 2000)}
(todo/apply-changes stored {:title "x" :done true} 2000))))
(testing "an invalid title changes nothing"
(is (= {:error "title must not be blank"} (todo/apply-changes stored {:title " " :done true} 2000))))
(testing "only :title and :done can change; the rest of the todo is untouched"
(is (= {:ok (assoc stored :done true :updated-at 2000)}
(todo/apply-changes stored {:done true :id 99 :owner-id 8 :created-at 0} 2000))))
(testing "no changes"
(doseq [changes [{} {:id 99} nil]]
(is (contains? (todo/apply-changes stored changes 2000) :error) (pr-str changes)))))