Skip to content

Commit

Permalink
Add generative tests (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilmoraunio authored Jun 6, 2024
1 parent 303b8ce commit 130d9d5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 1 deletion.
28 changes: 28 additions & 0 deletions .github/workflows/gen-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
name: Run generative tests

on:
push:
branches: [main]
schedule:
- cron: "0 0 1 * *" # every month

jobs:
gen-tests:
name: Run generative tests

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Setup latest Java
uses: actions/setup-java@v3.11.0
with:
distribution: zulu
java-version: 21
- name: Setup Clojure
uses: DeLaGuardo/setup-clojure@bc7570e912b028bbcc22f457adec7fdf98e2f4ed # 12.5
with:
cli: 1.10.1.693
- name: Run tests
run: CLOJURE=clojure-11 bin/run-gen-tests
2 changes: 1 addition & 1 deletion bin/kaocha
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env bash
# Should work if the env var is empty
clojure -A:"$CLOJURE" -M:test -m kaocha.runner "$@"
clojure -A:"$CLOJURE" -M:test -m kaocha.runner --skip-meta :generative "$@"
3 changes: 3 additions & 0 deletions bin/run-gen-tests
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
# Should work if the env var is empty
clojure -A:"$CLOJURE" -M:test -m kaocha.runner --focus oksa.generator-test "$@"
36 changes: 36 additions & 0 deletions test/oksa/generator_test.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
(ns ^:generative oksa.generator-test
(:require [#?(:clj clojure.test
:cljs cljs.test) :as t]
[malli.generator :as mg]
[malli.core :as m]
[oksa.parse]
[oksa.test-util :refer [unparse-and-validate]]))

#?(:bb nil
:clj (require '[kaocha.config]
'[kaocha.testable]))

(def document-schema (oksa.parse/-graphql-dsl-lang {:oksa/strict true} :oksa.parse/Document))
(def document-parser (m/parser document-schema))

#?(:bb nil
:clj (t/deftest generator-test
(let [seed (or (:kaocha.plugin.randomize/seed kaocha.testable/*config*)
(.nextLong (java.util.Random.)))
samples (loop [seed seed
samples nil
tries 10]
(cond (some? samples) samples
(zero? tries) (throw (ex-info "could not generate examples" {}))
:else (recur
(inc seed)
(try (mg/generate document-schema {:size 10 :seed seed})
(catch Exception _ nil))
(dec tries))))]
(prn ::seed seed)
(doseq [sample samples]
(t/is (true? (or (= :malli.core/invalid (document-parser sample))
(string? (try (unparse-and-validate sample)
(catch Throwable e
(prn (ex-info "invalid sample" {:sample sample} e))
nil))))))))))

0 comments on commit 130d9d5

Please sign in to comment.