Created
November 22, 2022 15:10
-
-
Save practicalli-johnny/4f19153a18f95fa7236c05b11b6d8724 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns mock-data-experiments | |
"Experimenting with specifications for data structures used in the Fraud API") | |
;; -------------------------------------------------- | |
;; Data generators | |
(comment | |
;; (require '[clojure.spec.alpha :as spec]) | |
;; ;; Generate an email using a regular expression | |
;; (def w3c-html5-spec-email-regex | |
;; "^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$") | |
;; (def email-regex #"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$") | |
;; (spec/def ::email-type (spec/and string? #(re-matches email-regex %))) | |
;; (clojure.spec.alpha/gen ::email-type) | |
;; ;; Spec2 Example of another approach - also not working with generators | |
;; (def email-regex #"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$") | |
;; (s/def ::firstname string?) | |
;; (s/def ::lastname string?) | |
;; (s/def ::email (s/and string? #(re-matches email-regex %))) | |
;; (s/def ::contact (s/schema [::firstname, ::lastname, ::email])) | |
;; (def contact-test {:firstname "Ricardo" :lastname "Cabral" :email "ricardo@ricardo.com" :extra 1}) | |
;; (s/valid? ::contact contact-test) | |
;; (gen/generate (s/gen ::contact)) . ;;error here | |
;; (gen/sample (s/gen ::contact)) ;; error here as well | |
;; ;; spec2 - possibly a fixed version although no guarantee if spec2 changes | |
;; (def email-regex #"^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,63}$") | |
;; (s/def ::email-type (s/and string? #(re-matches email-regex %))) | |
;; defn- email-generator | |
;; "Generates sample emails" [] | |
;; (gen/fmap #(str % "@" % ".com") (gen/string-alphanumeric)) | |
;; (s/def ::sample-email (s/with-gen ::email-type email-generator)) | |
;; (defn generate-contact-sample | |
;; "Generate sample contact to test" | |
;; [] | |
;; {:firstname (gen/generate (gen/string)) | |
;; :lastname (gen/generate (gen/string)) | |
;; :email (gen/generate (email-generator))}) | |
;; (generate-contact-sample) | |
;; (s/def ::firstname string?) | |
;; (s/def ::lastname string?) | |
;; (s/def ::email ::email-type) | |
;; (s/def ::contact (s/schema [::firstname ::lastname ::email])) | |
;; (s/valid? ::contact (generate-contact-sample)) | |
#_()) ; End of rich comment | |
;; End of Data generators | |
;; -------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment