I'm having some issues with testing a clojure macro. When I put the code through the repl, it behaves as expected, but when I try to expect this behavior in a test, I'm getting back nil instead. I have a feeling it has to do with how the test runner handles macroexpansion, but I'm not sure what exactly is going on. Any advice/alternative ways to test this code is appreciated.
Here is a simplified example of the macro I'm trying to test
(defmacro macro-with-some-validation
[-name- & forms]
(assert-symbols [-name-])
`(defn ~-name- [] (println "You passed the validation")))
(macroexpand-1 (read-string "(macro-with-some-validation my-name (forms))"))
;; ->
(clojure.core/defn my-name [] (clojure.core/println "You passed the validation"))
When passed into the repl
(macroexpand-1 (read-string "(macro-with-some-validation 'not-symbol (forms))"))
;; ->
rulesets.core-test=> Exception Non-symbol passed in to function. chibi-1-0-0.core/assert-symbols (core.clj:140)
But when put through a test
(deftest macro-with-some-validation-bad
(testing "Passing in a non-symbol to the macro"
(is (thrown? Exception
(macroexpand-1 (read-string "(macro-with-some-validation 'not-symbol (forms))"))))))
;; after a lein test ->
FAIL in (macro-with-some-validation-bad) (core_test.clj:50)
Passing in a non-symbol to the macro
expected: (thrown? Exception (macroexpand-1 (read-string "(macro-with-some-validation 'not-symbol (forms))")))
actual: nil
Thanks.
Edit: forgot to include the source for assert-symbols in case it matters
(defn assert-symbol [symbol]
(if (not (instance? clojure.lang.Symbol symbol))
(throw (Exception. "Non-symbol passed in to function."))))
(defn assert-symbols [symbols]
(if (not (every? #(instance? clojure.lang.Symbol %) symbols))
(throw (Exception. "Non-symbol passed in to function."))))
After changing my read-strings to be ` instead, I'm able to get the code working again. Still strange that read-string wasn't working correctly, though. Thanks for the help.
Related
Question
I would like to black hole print like behaviors within my test bodies in order to keep my log output looking clean and tidy.
(deftest some-test
(testing "something"
(logless
(is (= 22 (test-thing 14))))))
I expect test-thing to call println and other similar calls to *out* and would like those to stop polluting my test output.
Is there a recognized way to do this in general?
I found this guy (with-out-str) but it's capturing the string, not quite what I'm looking for.
Background
I'm fairly new to Clojure, coming largely from a javascript world. Having a blast so far! But there's lots left for me to learn.
In Clojure, not Clojure.script (if it matters)
Just use with-out-str and then ignore the string.
Note that this will not capture error messages or messages from Java libraries. If you want to capture and/or suppress this output, I have written 3 additional functions in the Tupelo library that you may find useful:
with-err-str
with-system-err-str
with-system-out-str
The code looks like this:
(defmacro with-system-err-str
"Evaluates exprs in a context in which JVM System/err is bound to a fresh
PrintStream. Returns the string created by any nested printing calls."
[& body]
`(let [baos# (ByteArrayOutputStream.)
ps# (PrintStream. baos#)]
(System/setErr ps#)
~#body
(System/setErr System/err)
(.close ps#)
(.toString baos#)))
If you wanted, you could make a new macro like so:
(defmacro with-printing-suppressed
"Evaluates exprs in a context in which JVM System/err and System/out captured & discarded."
[& body]
`(let [baos# (ByteArrayOutputStream.)
ps# (PrintStream. baos#)
s# (new java.io.StringWriter)]
(System/setErr ps#)
(System/setOut ps#)
(binding [*err* s#
*out* s#]
(let [result# (do ~#body)]
(.close ps#)
(System/setErr System/err)
(System/setOut System/out)
result#))))
(defn stuff []
(println "***** doing stuff *****")
42)
and then test it:
(println "calling - before")
(is= 42 (with-printing-suppressed
(stuff)))
(println "calling - after")
with result:
calling - before
calling - after
Use default logging and logback.xml for output configuration.
default clojure logging
I'm running into some issues with reporting on failures using the clojure.test testing framework.
Now, I understand that I can override some functions for different reporting so that it prints out to the console or wherever I want it to print to. I also understand that I can save this output to a file.
My issue is the following... when I declare a deftest like this example:
(deftest test1
(is (= 1 1)
(is (= 2 1))
This test will run and if I do something like (run-tests) or (test-var #'test1) it will return nil but print the failures.
I decided to override the :fail method for reporting, because what I want is a map of the failures like this: {"expected" (:expected m), "actual" (:actual m)} and this kinda sorta works if I were to just use the reporting function.
The problem is that when you run tests through the Clojure.test framework there are many macro's that get called and it doesn't behave it exactly how I want to.
My end goal is: running the tests, and if there are any failures, instead of printing them, save them to a map and return the map to me. If they all pass, then I don't care what it returns to me.
Is this even possible? I don't want to stop testing if a certain test fails, I just want it to be logged somewhere, preferably a map.
Sources:
Clojure test with mutiple assertions
https://clojure.github.io/clojure/branch-1.1.x/clojure.test-api.html
https://groups.google.com/forum/#!topic/clojure/vCjso96wqps
I'm afraid there's no easy way how to do that.
You could provide a custom implementation of clojure.test/report :fail defmethod and store the result in an atom but it's hard to propage the result to outer layers.
If you just use test-var then it's doable but note that test fixtures aren't executed in this case - see test-vars source:
(:use clojure.test)
(deftest failing
(testing "fail me"
(is (= 1 0))
(is (= 2 1))
(is (= 3 2))))
(def test-failures (atom []))
(defmethod report :fail [m]
(swap! test-failures
(fn [previous-failures current-failure]
(conj previous-failures current-failure))
{:test-var-str (testing-vars-str m)
:expected (:expected m)
:actual (:actual m)}))
(defmethod report :end-test-var [m]
#test-failures)
(defn run-test-var [v]
(reset! test-failures [])
(test-var v))
;; in REPL:
(run-test-var #'failing)
;; =>
[{:test-var-str "(failing) (form-init4939336553149581727.clj:159)", :expected 1, :actual (0)}
{:test-var-str "(failing) (form-init4939336553149581727.clj:160)", :expected 2, :actual (1)}
{:test-var-str "(failing) (form-init4939336553149581727.clj:161)", :expected 3, :actual (2)}]
There's also defmethod report :end-test-ns but this one is not very useful because test-ns function returns #*report-counters*.
I use IntelliJ + Cursive.
I was doing some testing on a Genetic Algorithm I'm writing, and got a NPE. It wasn't clear where it was coming from, so I started up my debugger. As it was starting up however, a RuntimeException break was activated, and it pointed here in clojure.main:
(defn main
[& args]
(try
(if args
(loop [[opt arg & more :as args] args inits []]
(if (init-dispatch opt)
(recur more (conj inits [opt arg]))
((main-dispatch opt) args inits))) ; <---- Points Here
(repl-opt nil nil))
(finally
(flush))))
The exception reads:
Exception in thread "main" java.lang.RuntimeException: Unable to resolve symbol: create in this context, compiling:(C:\Users\slomi\AppData\Local\Temp\form-init2101329611502965041.clj:4223:33
I didn't see a create symbol anywhere in the code it pointed to (which would have been weird since it's clojure.main), and I checked my entire project and that symbol isn't used anywhere. I checked the path that the exception pointed to, and found this mess:
(.deleteOnExit (java.io.File. "C:\\Users\\slomi\\AppData\\Local\\Temp\\form-init2101329611502965041.clj")) (do (set! *warn-on-reflection* nil) (do (try (clojure.core/doto (quote ai-retry.genetic_algorithm.main) clojure.core/require clojure.core/in-ns) (catch java.lang.Exception e__11808__auto__ (clojure.core/println e__11808__auto__) (clojure.core/ns ai-retry.genetic_algorithm.main))) (try (clojure.core/require (quote clojure.tools.nrepl.server)) (catch java.lang.Throwable t__11809__auto__ (clojure.core/println "Error loading" (clojure.core/str (quote clojure.tools.nrepl.server) ":") (clojure.core/or (.getMessage t__11809__auto__) (clojure.core/type t__11809__auto__))))) (try (clojure.core/require (quote complete.core)) (catch java.lang.Throwable t__11809__auto__ (clojure.core/println "Error loading" (clojure.core/str (quote complete.core) ":") (clojure.core/or (.getMessage t__11809__auto__) (clojure.core/type t__11809__auto__))))) nil) (clojure.core/let [server__11804__auto__ (clojure.tools.nrepl.server/start-server :bind "127.0.0.1" :port 0 :ack-port 58282 :handler (clojure.tools.nrepl.server/default-handler (clojure.core/with-local-vars [wrap-init-ns__11765__auto__ (clojure.core/fn [h__11766__auto__] (clojure.core/with-local-vars [init-ns-sentinel__11767__auto__ nil] (clojure.core/fn [{:as msg__11768__auto__, :keys [session]}] (clojure.core/when-not ((clojure.core/deref session) init-ns-sentinel__11767__auto__) (clojure.core/swap! session clojure.core/assoc (var clojure.core/*ns*) (try (clojure.core/require (quote ai-retry.genetic_algorithm.main)) (clojure.core/create-ns (quote ai-retry.genetic_algorithm.main)) (catch java.lang.Throwable t__11769__auto__ (clojure.core/create-ns (quote user)))) init-ns-sentinel__11767__auto__ true)) (h__11766__auto__ msg__11768__auto__))))] (clojure.core/doto wrap-init-ns__11765__auto__ (clojure.tools.nrepl.middleware/set-descriptor! {:expects #{"eval"}, :requires #{(var clojure.tools.nrepl.middleware.session/session)}}) (clojure.core/alter-var-root (clojure.core/constantly (clojure.core/deref wrap-init-ns__11765__auto__))))))) port__11805__auto__ (:port server__11804__auto__) repl-port-file__11806__auto__ (clojure.core/apply clojure.java.io/file ["C:\\Users\\slomi\\IdeaProjects\\ai-retry" ".nrepl-port"]) legacy-repl-port__11807__auto__ (if (.exists (clojure.java.io/file "C:\\Users\\slomi\\IdeaProjects\\ai-retry\\target\\base+system+user+dev")) (clojure.java.io/file "C:\\Users\\slomi\\IdeaProjects\\ai-retry\\target\\base+system+user+dev" "repl-port"))] (clojure.core/when true (clojure.core/println "nREPL server started on port" port__11805__auto__ "on host" "127.0.0.1" (clojure.core/str "- nrepl://" "127.0.0.1" ":" port__11805__auto__))) (clojure.core/spit (clojure.core/doto repl-port-file__11806__auto__ .deleteOnExit) port__11805__auto__) (clojure.core/when legacy-repl-port__11807__auto__ (clojure.core/spit (clojure.core/doto legacy-repl-port__11807__auto__ .deleteOnExit) port__11805__auto__)) (clojure.core/deref (clojure.core/promise))))
Searching through it, there isn't any create symbol anywhere. The closest match is two instances of create-ns. I'm assuming this is a file auto-generated by the REPL for whatever reason, so I wouldn't expect it to be buggy anyways.
If I delete the file, a new one is created, and the same error happens in the new file.
Now, if I open a normal non-debugging REPL, I get the same error too. This happened completely out of nowhere. Before I started the debugger for the first time, I already had a REPL running, and it started fine. The error only started after I started the debugger, now it won't go away.
I tried lein clean, deleting the temp files manually, restarting IntelliJ and my computer, and ensuring none of my code has a weird compiler error that may be manifesting in an odd way.
Can anyone think of what this might be? Why is it thinking there's an unresolved create symbol in a file that doesn't even contain create?
I found out what it was. Thanks #Alan Thompson.
When I ran lein run, I got an error that it couldn't find anything to run in my main file, which was weird since my main had a proper main method. I looked over my project.clj, and realized that since I had started the REPL last, I had changed the main location, and I typed it wrong.
I had
:main ai-retry.genetic_algorithm.main
When it should have been
:main ai-retry.genetic-algorithm.main
Changing the underscore to a dash allowed everything to start properly.
How that possibly lead to the error it gave is beyond me though. I'm going to leave this here in case anyone has to deal with this in the future.
This happened to me. Same symptom, same cause (mangled namespaces), but slightly different manifestation:
(ns my-project.my-namespace=
^-- accidental errant character
If this happens to you, review all your namespace declarations.
I've got some fixtures that boot up and close the database in my project.
Now it looks something like this:
(use-fixtures :once with-embedded-db)
while in the fixture itself I've got a dynamic variable that I use in different places:
(def ^:dynamic *db*)
(defn with-embedded-db [f]
(binding [*db* (db/connect args)]
(f)
(finally
(db/clean-up *db)))
Now, assume that db/connect and db/clean-up take some time.
PROBLEM:
When I run tests using lein test, it takes very long time, unnecessarily spending time on connecting and disconnecting to the db for every namespace.
QUESTION:
Is there a way to set up global fixtures so that when I run lein test, it calls it only once for all the test namespaces?
Thanks!
It would have been better if that feature was added to leiningen itself. At least a ticket should be opened, if not a PR.
The following solution is dirty, but you can get the idea and transform it into something more intelligent.
;; profect.clj
:profiles
{:dev {:dependencies [[robert/hooke "1.1.2"]]
:injections [(require '[robert.hooke :as hooke])
(defn run-all-test-hook [f & nss]
(doall (map (fn [a]
(when (intern a '*db*)
(intern a '*db* "1234"))) nss))
(apply f nss))
(hooke/add-hook #'clojure.test/run-tests #'run-all-test-hook)
]}}
Note: leiningen itself uses robert/hooke in its core.
And then somewhere in tests:
(ns reagenttest.cli
(:require [clojure.test :refer :all]))
(def ^:dynamic *db*) ;; should be defined in every NS where it is needed
(deftest Again
(testing "new"
(prn *db*)))
Use circleci.test, it supports :global-fixtures:
... you can define global fixtures that are only run once for the entire test run, no matter how many namespaces you run.
I have a small program that is supposed to read SQL queries/commands one by one and execute them against a database.
If a query executes successfully, the next query is executed.
If there is an error executing one query, the program should stop executing all together.
I have the code, except that the query still continues execution even when there is an exception.
(defn main
[]
(loop [queries (get-all-queries)
querycount 1]
(let [q (first queries)]
(println (format "currently processing query %s", querycount))
(cond (nil? q) (println "All Queries ran successfully.")
:else (do
(cond (= (:status (process-query q querycount)) "OK")
(recur (rest queries) (+querycount 1)))
:else (println "An error occured while running queries")))))))
(defn process-query
[query query-count]
(let [{query-body :query-body, is-query-running? :is-query-running?} query
my-agent (agent
{:error false, :query-count query-count}
:error-handler handler-fn)]
(send my-agent (fn[_]
(execute-query! db query-body)))))
(loop [is-query-running? (is-query-running?)
error? (:error #my-agent)]
(cond error? (do (println "Error")
{:status "ERROR" :error-msg (:error-msg #my-agent)})
(and (not is-query-running?) (not error?)) (do (println "Success")
{:status "OK"})
(:else (do
(Thread/sleep 2000)
(recur (is-query-running?) (:error #my-agent)))))))
(defn handler-fn
[agent exception]
(println (format "an exception occured : %s" exception))
(if (instance? java.sql.BatchUpdateException exception)
(println (.getNextException exception)))
(send agent (? [_] {:error true, :error-message exception}))
(throw exception))
The reason why I'm using an agent is that I have some queries that take 4 hours to run.
and when that happens, the database does not notify the program that the query has been completed. instead, the program is stuck. so, instead, I constantly poll to check if the query is done already.
Is this the best way to accomplish what I'm trying to do ?
Should I be using any other concurrency primitives ?
Do I even need concurrency primitives ?
I've been thinking about this for a long time now.
I think you will need to use core.async to resolve this kind of workflow
Take a look at http://clojure.com/blog/2013/06/28/clojure-core-async-channels.html
This lib will let you to check your conditions with the related asynchronous tasks involved
A few resources that may help you
http://www.infoq.com/news/2013/07/core-async
https://www.youtube.com/watch?v=AhxcGGeh5ho
The main problem seems to be: on the one hand, you write that the long queries never return, i.e. they don't even throw exceptions. On the other hand, your error detection mechanism for the agent is based on catching an exception.
I think what you need to do is not check (primarily) whether an exception was caught, but whether execute-query has actually returned a valid result when is-query-running? returns false.
Regarding the right concurrency primitive, I would suggest using a future instead of an agent. They are simpler than agents, since they can only return a single value, instead of changing their state multiple times, and their way of error handling is to simply return the exception instead of the regular return value.
You can then follow this implementation idea: in the loop, do a deref with timeout on the future. If the return value of the deref is whatever execute-query! returns regularly, return "OK"(resp. add a second expression to the future body as a clearly identifiable return value, e.g. the keyword :ok). Otherwise, if the return value of the deref is an exception, return "ERROR" with the :error-msg from the exception like you do now. Finally, if the return value is the timeout value you gave to the deref, call is-query-running?. If it's true, loop another time, if it's false, return ERROR with a special :error-msg which communicates that your query ended without either returning nor throwing an exception. (And probably call future-cancel so you don't leak threads of never-ending execute-query! calls.)