Skip to content

Instantly share code, notes, and snippets.

@olivergeorge
Created Jan 3, 2020
Embed
What would you like to do?
WIP generating relational db content using clojure.test.check
(ns v1
(:require [clojure.test.check :as tc]
[clojure.test.check.generators :as gen]
[clojure.test.check.properties :as prop]
[clojure.spec.alpha :as s]))
(defn gen-char [n]
(gen/fmap #(apply str %) (gen/vector gen/char-alphanumeric n)))
(defn gen-varchar [n]
(gen/fmap #(apply str %) (gen/vector gen/char-alphanumeric 0 n)))
(defmacro CHAR [n]
`(s/with-gen (s/and string? #(<= (count %) ~n)) #(gen-char ~n)))
(defmacro VARCHAR [n]
`(s/with-gen (s/and string? #(<= (count %) ~n)) #(gen-varchar ~n)))
(defmacro NVARCHAR [n]
`(s/with-gen (s/and string? #(<= (count %) ~n)) #(gen-varchar ~n)))
(s/def ::DATETIME2 string?)
(s/def ::DATETIME string?)
(s/def ::BIT boolean?)
(s/def :dbo/CM_PERSON
(s/keys :req-un [:dbo.CM_PERSON/CODE
:dbo.CM_PERSON/LASTNAME
:dbo.CM_PERSON/FIRSTNAME
:dbo.CM_PERSON/UPDATE_NUMBER
:dbo.CM_PERSON/ADDRESS
:dbo.CM_PERSON/SUBURB
:dbo.CM_PERSON/STATE
:dbo.CM_PERSON/POSTCODE
:dbo.CM_PERSON/PHONE
:dbo.CM_PERSON/FACSIMILE
:dbo.CM_PERSON/MOBILE
:dbo.CM_PERSON/EMAIL
:dbo.CM_PERSON/AG_LICENSE_ID]))
(s/def :dbo.CM_PERSON/ID int?)
(s/def :dbo.CM_PERSON/CODE (CHAR 10))
(s/def :dbo.CM_PERSON/LASTNAME (CHAR 50))
(s/def :dbo.CM_PERSON/FIRSTNAME (CHAR 50))
(s/def :dbo.CM_PERSON/ADDRESS (s/nilable (CHAR 255)))
(s/def :dbo.CM_PERSON/SUBURB (s/nilable (CHAR 50)))
(s/def :dbo.CM_PERSON/STATE (s/nilable (CHAR 3)))
(s/def :dbo.CM_PERSON/POSTCODE (s/nilable (CHAR 4)))
(s/def :dbo.CM_PERSON/PHONE (s/nilable (CHAR 20)))
(s/def :dbo.CM_PERSON/FACSIMILE (s/nilable (CHAR 20)))
(s/def :dbo.CM_PERSON/MOBILE (s/nilable (CHAR 20)))
(s/def :dbo.CM_PERSON/EMAIL (s/nilable (CHAR 50)))
(s/def :dbo.CM_PERSON/UPDATE_NUMBER nat-int?)
(s/def :dbo.CM_PERSON/AG_LICENSE_ID (s/nilable :dbo.AG_LICENSE/ID))
(s/def :dbo.CM_PERSON/IS_LICENSE_OWNER #{0 1})
(s/def :dbo/AG_LICENSE
(s/keys :req-un [:dbo.AG_LICENSE/ID
:dbo.AG_LICENSE/CODE
:dbo.AG_LICENSE/UPDATE_NUMBER]
:opt-un [:dbo.AG_LICENSE/DESCRIPTION]))
(s/def :dbo.AG_LICENSE/ID pos-int?)
(s/def :dbo.AG_LICENSE/CODE (VARCHAR 10))
(s/def :dbo.AG_LICENSE/DESCRIPTION (CHAR 50))
(s/def :dbo.AG_LICENSE/UPDATE_NUMBER nat-int?)
(s/def :dbo/AP_TODO_STATUS
(s/keys :req-un [:dbo.AP_TODO_STATUS/ID
:dbo.AP_TODO_STATUS/UPDATE_NUMBER
:dbo.AP_TODO_STATUS/DESCRIPTION]))
(s/def :dbo.AP_TODO_STATUS/ID pos-int?)
(s/def :dbo.AP_TODO_STATUS/DESCRIPTION (s/nilable (CHAR 50)))
(s/def :dbo.AP_TODO_STATUS/UPDATE_NUMBER nat-int?)
(s/def :dbo/auth_user
(s/keys :req-un [:dbo.auth_user/id
:dbo.auth_user/password
:dbo.auth_user/username
:dbo.auth_user/first_name
:dbo.auth_user/last_name
:dbo.auth_user/email
:dbo.auth_user/is_superuser
:dbo.auth_user/is_staff
:dbo.auth_user/is_active]
:opt-un [:dbo.auth_user/last_login]))
(s/def :dbo.auth_user/id pos-int?)
(s/def :dbo.auth_user/password (NVARCHAR 128))
(s/def :dbo.auth_user/last_login ::DATETIME2)
(s/def :dbo.auth_user/is_superuser ::BIT)
(s/def :dbo.auth_user/username (NVARCHAR 30))
(s/def :dbo.auth_user/first_name (NVARCHAR 30))
(s/def :dbo.auth_user/last_name (NVARCHAR 30))
(s/def :dbo.auth_user/email (NVARCHAR 254))
(s/def :dbo.auth_user/is_staff ::BIT)
(s/def :dbo.auth_user/is_active ::BIT)
(s/def :dbo.auth_user/date_joined ::DATETIME2)
(s/def :dbo/AP_TODO
(s/keys :req-un [:dbo.AP_TODO/ID
:dbo.AP_TODO/USER_NOTE
:dbo.AP_TODO/UPDATE_NUMBER
:dbo.AP_TODO/OBJECT_ID
:dbo.AP_TODO/CONTENT_TYPE_ID
:dbo.AP_TODO/AP_APPLICATION_ID
:dbo.AP_TODO/FORM_NAME
:dbo.AP_TODO/TABLE_NAME
:dbo.AP_TODO/FORM_ID
:dbo.AP_TODO/NOTE
:dbo.AP_TODO/USERNAME
:dbo.AP_TODO/AP_TODO_STATUS_ID
:dbo.AP_TODO/CM_PERSON_ID
:dbo.AP_TODO/DATE_CREATED
:dbo.AP_TODO/USER_CREATED
:dbo.AP_TODO/DATE_UPDATED
:dbo.AP_TODO/USER_UPDATED
:dbo.AP_TODO/USER_ID
:dbo.AP_TODO/USER_CREATED_ID
:dbo.AP_TODO/USER_UPDATED_ID]))
(s/def :dbo.AP_TODO/ID pos-int?)
(s/def :dbo.AP_TODO/AP_APPLICATION_ID (s/nilable int?))
(s/def :dbo.AP_TODO/FORM_NAME (s/nilable (VARCHAR 50)))
(s/def :dbo.AP_TODO/TABLE_NAME (s/nilable (VARCHAR 50)))
(s/def :dbo.AP_TODO/FORM_ID (s/nilable int?))
(s/def :dbo.AP_TODO/NOTE (s/nilable (VARCHAR 128)))
(s/def :dbo.AP_TODO/USERNAME (s/nilable (VARCHAR 50)))
(s/def :dbo.AP_TODO/USER_NOTE (VARCHAR 4000))
(s/def :dbo.AP_TODO/UPDATE_NUMBER nat-int?)
(s/def :dbo.AP_TODO/AP_TODO_STATUS_ID (s/nilable int?))
(s/def :dbo.AP_TODO/CM_PERSON_ID (s/nilable int?))
(s/def :dbo.AP_TODO/DATE_CREATED (s/nilable ::DATETIME))
(s/def :dbo.AP_TODO/USER_CREATED (s/nilable (VARCHAR 50)))
(s/def :dbo.AP_TODO/DATE_UPDATED (s/nilable ::DATETIME))
(s/def :dbo.AP_TODO/USER_UPDATED (s/nilable (VARCHAR 50)))
(s/def :dbo.AP_TODO/OBJECT_ID pos-int?)
(s/def :dbo.AP_TODO/CONTENT_TYPE_ID int?)
(s/def :dbo.AP_TODO/USER_ID (s/nilable :dbo.auth_user/id))
(s/def :dbo.AP_TODO/USER_CREATED_ID (s/nilable :dbo.auth_user/id))
(s/def :dbo.AP_TODO/USER_UPDATED_ID (s/nilable :dbo.auth_user/id))
(defn gen-person
[licenses]
(s/gen :dbo/CM_PERSON
{:dbo.AG_LICENSE/ID #(if (seq licenses)
(gen/elements (map :ID licenses))
(gen/return nil))}))
(defn gen-todo
[auth_users]
(s/gen :dbo/AP_TODO
{:dbo.auth_user/id #(if (seq auth_users)
(gen/elements (map :id auth_users))
(gen/return nil))}))
(def db-gen
(gen/let [AG_LICENSEs (gen/vector-distinct-by :ID (s/gen :dbo/AG_LICENSE))
CM_PERSONs (gen/vector-distinct-by :ID (gen-person AG_LICENSEs))
AP_TODO_STATUSs (gen/vector-distinct-by :ID (s/gen :dbo/AP_TODO_STATUS))
auth_users (gen/vector-distinct-by :ID (s/gen :dbo/auth_user))
AP_TODOs (gen/vector-distinct-by :ID (gen-todo auth_users))
]
{:AG_LICENSEs AG_LICENSEs
:CM_PERSONs CM_PERSONs
:AP_TODO_STATUSs AP_TODO_STATUSs
:AP_TODOs AP_TODOs
:auth_users auth_users}))
(comment
(:AP_TODOs (gen/generate db-gen)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment