Created
November 12, 2015 22:31
-
-
Save jindrichmynarz/25ac93304dbcec402b12 to your computer and use it in GitHub Desktop.
Example of generative testing of a SOAP API in Clojure
This file contains hidden or 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 soap-test.core-test | |
(:require [clojure.test :refer :all] | |
[clojure.test.check.generators :as gen] | |
[clojure.test.check.properties :as prop] | |
[clojure.test.check.clojure-test :refer [defspec]] | |
[clj-soap.core :as soap :refer [obj->soap-str]])) | |
; Boilerplace method required by the clj-soap library | |
(defmethod obj->soap-str :currency [obj argtype] (str obj)) | |
; Tested SOAP API's WSDL URL | |
(def service-url "http://test-testappcon.rhcloud.com/CurrencyConverter/services/CurrencyConverter?wsdl") | |
; SOAP client | |
(defonce client (soap/client-fn service-url)) | |
; Test if we get the same amount if we convert it from currency A to currency B and then back to currency A. | |
; Result: This test fails because of rounding errors. | |
(defspec there-and-back-again | |
100 | |
(let [convert-between-currencies (partial client :convertBetweenCurrencies)] | |
(prop/for-all [[source target] (gen/such-that (partial apply not=) | |
(gen/vector (gen/elements ["HRK" "USD" | |
"CZK" "EUR" | |
"IND"]) | |
2)) | |
amount (gen/such-that (some-fn zero? pos?) (gen/fmap float gen/ratio))] | |
(= amount (->> amount | |
(convert-between-currencies source target) | |
(convert-between-currencies target source)))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment