1
\$\begingroup\$

Is this code a good test for a testable Clojure code?

(ns github.amexboy.v2-test
  (:require [github.amexboy.v2 :as t])
  (:use [clojure.test]))

(deftest test-module-name-default
  (let [ 
        repository-root "/some/path/to/git"
        project-root "/some/path/to/project"
        services [{:input "service|origin/develop" :service-name "service" :ex-branch "origin/develop" :ex-service-name "service-develop"}
                  {:input "service" :service-name "service" :ex-branch "origin/master" :ex-service-name "service-master"}
                  {:input "service|develop" :service-name "service" :ex-branch "develop" :ex-service-name "service-develop"}
                  {:input "" :ex-failure {:error "Service name required"}}]]

    (run! (fn [test-map]
            (let [expected-service-name (:ex-service-name test-map)
                  expected-branch (:ex-branch test-map)
                  service-name (:service-name test-map)

                  ; Passing a mock function that validates the inputs
                  git-funcs {:git-worktree 
                             (fn [repo worktree branch]
                               (is (= (str repository-root "/" service-name ) repo))
                               (is (= (str project-root "/" expected-service-name ) worktree))
                               (is (= expected-branch branch)))}]

              (let [res (t/gen-worktree repository-root project-root (:input test-map) git-funcs)
                    failure (:ex-failure test-map)]
                (if failure 
                  (is (= (:error res) (:error failure) ))
                  (is (= (not (:error res))))))))
          services)))

; Test parse input
; Testing passing differenet parmeters to the entry point
;; Over my head - Classic - MTKO
(deftest test-command-line-parse
  (let [ 
        git-funcs {}
        test-input [
                    {
                     :input "-b TEST-branch-name -B origin/develop -m service-one,service-two|master -n TEST-one -r /path/to/root -p /pat/to/projects --no-start" 
                     :ex-base-branch "origin/develop" 
                     :ex-branch "TEST-branch-name"
                     :ex-project-root "/path/to/projects" 
                     :ex-repo-root "/path/to/root" 
                     :ex-name "TEST-one"
                     :ex-services ["service-one" "service-two"]}
                    {
                     :input "-b TEST-branch-name -m service-one,service-two -n TEST-one" 
                     :ex-base-branch "origin/master" 
                     :ex-branch "TEST-branch-name"
                     :ex-project-root "./projects" 
                     :ex-repo-root "." 
                     :ex-name "TEST-one"
                     :ex-services ["service-one" "service-two"]}
                    {
                     :input "-b TEST-branch -n test-one" 
                     :ex-failure {:error "Invalid parameter: module name is required"}}
                    {
                     :input "-b TEST-branch -m service-one,service-two|master" 
                     :ex-failure {:error "Invalid parameter: project name is required"}}
                    {
                     :input "-m service-one,service-two|master -n test-one" 
                     :ex-failure {:error "Invalid parameter: branch is required"}}]]

    (run! (fn [t] 
            (let [
                  gen-project (fn [project-def  git-funcs]
                                (is (= (:base-branch project-def) (:ex-base-branch t) ))
                                (is (= (:branch project-def) (:ex-branch t) ))
                                (is (= (:project-root project-def) (:ex-project-root t) ))
                                (is (= (:repo-root project-def) (:ex-repo-root t) ))
                                (is (= (:name project-def) (:ex-name t) ))
                                (is (= (:services project-def) (:ex-services t) )))
                  ]

              (let [res (t/gen-project(:input test-map) git-funcs)
                    failure (:ex-failure test-map)]
                (if failure 
                  (is (= (:error res) (:error failure) ))
                  (is (= (not (:error res))))))))
          test-input)))

The tests are awefully verbose. What I'm trying to do on the first function is to pass a mocked method and validate the paramters.

Are you able to tell what the code (if implemented) might do?

\$\endgroup\$
  • 1
    \$\begingroup\$ -B orign/develop looks like a typo? \$\endgroup\$ – 200_success Jul 15 at 23:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.