<commit_before>;; Copyright (c) <NAME>. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file COPYING at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.

(ns sandbar.dev.test-validation
  (:use (clojure test)
        (sandbar.dev validation
                     [test :only (t)])))

(deftest test-add-validation-errors
  (t "add validation error"
     (t "when errors are empt"
        (is (= (add-validation-error {} :a "a")
               {:_validation-errors {:a ["a"]}})))
     (t "when no key is passed, error goes in :form key"
        (is (= (add-validation-error {} "a")
               {:_validation-errors {:form ["a"]}})))
     (t "when error key exists in form data"
        (is (= (add-validation-error {:a "a"} :a "a")
               {:a "a" :_validation-errors {:a ["a"]}} )))
     (t "when validation errors exist and putting new value in same key"
        (is (= (add-validation-error {:a "a" :_validation-errors {:a ["a"]}}
                                     :a "b")
               {:a "a" :_validation-errors {:a ["a" "b"]}})))
     (t "when validation errors exist and adding new error"
        (is (= (add-validation-error {:a "a" :_validation-errors {:a ["a"]}}
                                     :b "b")
               {:a "a" :_validation-errors {:a ["a"] :b ["b"]}})))))

(defn example-validator
  "A validator takes a map as an argument and then returns the map with a
   key named :_validation-errors that contains a map or error messages."
  [m]
  (if (= (:a m) "a")
    m
    (add-validation-error m :a ":a does not contain the letter a")))

(deftest test-if-valid
  (t "testing if-valid"
     (t "with built-in functions and validation is true"
          (is (= (if-valid identity {:a "a"} identity list)
                 {:a "a"})))
     (t "with custom functions"
        (let [success (fn [a] (:a a))
              failure (fn [a b] (first (:a b)))]
          (t "when validation is true"
             (is (= (if-valid example-validator {:a "a"}
                              success
                              failure)
                    "a")))
          (t "when validation is false"
             (is (= (if-valid example-validator {:a "b"}
                              success
                              failure)
                    ":a does not contain the letter a")))))))

(deftest test-non-empty-string
  (t "test non-empty-string"
     (t "when string is empty"
        (is (= (non-empty-string {:a ""} :a "error")
               {:a "" :_validation-errors {:a ["error"]}}))
        (t "with map message"
           (is (= (non-empty-string {:a ""} :a {:a "Username"})
                  {:a "" :_validation-errors
                   {:a ["Username cannot be blank!"]}})))
        (t "with empty message"
           (is (= (non-empty-string {:a ""} :a)
                  {:a "" :_validation-errors
                   {:a ["a cannot be blank!"]}})))
        (t "with custom error message"
           (is (= (non-empty-string {:a ""} :a {:a-validation-error "x"})
                  {:a "" :_validation-errors
                   {:a ["x"]}}))))
     (t "when nil instead of string"
        (is (= (non-empty-string {:a nil} :a "error")
               {:a nil :_validation-errors {:a ["error"]}})))
     (t "when value is non-empty string"
        (is (= (non-empty-string {:a "a"} :a "error")
               {:a "a"})))))

(deftest test-build-validator
  (t "build validator"
     (t "out of one validator fn and validation passes"
        (is (= ((build-validator (non-empty-string :a)) {:a "a"})
               {:a "a"})))
     (t "out of one validator fn and validation fails"
        (is (= ((build-validator (non-empty-string :a)) {:a ""})
               {:a ""
                :_validation-errors {:a ["a cannot be blank!"]}})))
     (t "out of two validators fns and validation passes"<commit_msg>Added unit tests for modified non-empty-string function.<commit_after>;; Copyright (c) <NAME>. All rights reserved.
;; The use and distribution terms for this software are covered by the
;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file COPYING at the root of this distribution.
;; By using this software in any fashion, you are agreeing to be bound by
;; the terms of this license.
;; You must not remove this notice, or any other, from this software.

(ns sandbar.dev.test-validation
  (:use (clojure test)
        (sandbar.dev validation
                     [test :only (t)])))

(deftest test-add-validation-errors
  (t "add validation error"
     (t "when errors are empt"
        (is (= (add-validation-error {} :a "a")
               {:_validation-errors {:a ["a"]}})))
     (t "when no key is passed, error goes in :form key"
        (is (= (add-validation-error {} "a")
               {:_validation-errors {:form ["a"]}})))
     (t "when error key exists in form data"
        (is (= (add-validation-error {:a "a"} :a "a")
               {:a "a" :_validation-errors {:a ["a"]}} )))
     (t "when validation errors exist and putting new value in same key"
        (is (= (add-validation-error {:a "a" :_validation-errors {:a ["a"]}}
                                     :a "b")
               {:a "a" :_validation-errors {:a ["a" "b"]}})))
     (t "when validation errors exist and adding new error"
        (is (= (add-validation-error {:a "a" :_validation-errors {:a ["a"]}}
                                     :b "b")
               {:a "a" :_validation-errors {:a ["a"] :b ["b"]}})))))

(defn example-validator
  "A validator takes a map as an argument and then returns the map with a
   key named :_validation-errors that contains a map or error messages."
  [m]
  (if (= (:a m) "a")
    m
    (add-validation-error m :a ":a does not contain the letter a")))

(deftest test-if-valid
  (t "testing if-valid"
     (t "with built-in functions and validation is true"
          (is (= (if-valid identity {:a "a"} identity list)
                 {:a "a"})))
     (t "with custom functions"
        (let [success (fn [a] (:a a))
              failure (fn [a b] (first (:a b)))]
          (t "when validation is true"
             (is (= (if-valid example-validator {:a "a"}
                              success
                              failure)
                    "a")))
          (t "when validation is false"
             (is (= (if-valid example-validator {:a "b"}
                              success
                              failure)
                    ":a does not contain the letter a")))))))

(deftest test-non-empty-string
  (t "test non-empty string"
     (t "when string is empty"
        (is (= (non-empty-string {:a ""} :a "error")
               {:a "" :_validation-errors {:a ["error"]}}))
        (t "with map message"
           (is (= (non-empty-string {:a ""} :a {:a "Username"})
                  {:a "" :_validation-errors
                   {:a ["Username cannot be blank!"]}})))
        (t "with empty message"
           (is (= (non-empty-string {:a ""} :a)
                  {:a "" :_validation-errors
                   {:a ["a cannot be blank!"]}})))
        (t "with custom error message"
           (is (= (non-empty-string {:a ""} :a {:a-validation-error "x"})
                  {:a "" :_validation-errors
                   {:a ["x"]}}))))
     (t "when nil instead of string"
        (is (= (non-empty-string {:a nil} :a "error")
               {:a nil :_validation-errors {:a ["error"]}})))
     (t "when value is non-empty string"
        (is (= (non-empty-string {:a "a"} :a "error")
               {:a "a"})))
     (t "with multiple keywords"
        (t "where validation passes"
           (is (= (non-empty-string {:a "a" :b "b"} :a :b "error")
                  {:a "a" :b "b"})))
        (t "where validation fails for one keyword"
           (is (= (non-empty-string {:a "a" :b ""} :a :b "error")
                  {:a "a" :b "" :_validation-errors {:b ["error"]}})))
        (t "where validation fails for one keyword with properties"
           (is (= (non-empty-string {:a "a" :b ""} :a :b {:b "b"})
                  {:a "a" :b ""
                   :_validation-errors {:b ["b cannot be blank!"]}})))
        (t "where validation fails for all keywords with properties"
           (is (= (non-empty-string {:a "" :b ""} :a :b {:a "a" :b "b"})
                  {:a "" :b ""
                   :_validation-errors {:b ["b cannot be blank!"]
                                        :a ["a cannot be blank!"]}}))))))

(deftest test-build-validator
  (t "build validator"
     (t "out of one validator fn and validation passes"
        (is (= ((build-validator (non-empty-string :a)) {:a "a"})
               {:a "a"})))
     (t "out of one validator fn and validation fails"
        (is (= ((build-validator (non-empty-string :a)) {:a ""})
               {:a ""
                :_validation-errors {:a ["a cannot be blank!"]}})))
     (t "out of two validators fns and validation passes"
{'fraction_non_alphanumeric': 0.1653908355795148, 'fraction_numerical': 0.0008625336927223719, 'mean_word_length': 1.8603145235892693, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4211977', 'n_tokens_mistral': 2898, 'n_tokens_neox': 2752, 'n_words': 1012}