I want to write my own test cases in Haskell modules. Lets say I made a "range" module. I might want to check:
(range 1 2) + (range 3 5) == (range 1 5)
(range 1 4) + empty == (range 1 4)
etc.
I'd like to put these tests in modules, and perhaps some way to turn them on/off using a compiler flag.
I'm not currently really interested in some framework generating test cases for me, I'm happy to do that myself.
Why not try the new hotness that is Tasty. Tasty can be integrated with cabal for integrated building/testing.
import Test.Tasty
import Test.Tasty.HUnit
import My.Range.Module
main = defaultMain unitTests
unitTests = testGroup "Unit tests" [
testCase "Adding Continuous Ranges" $
(range 1 2) + (range 3 5) #?= (range 1 5),
testCase "Adding an empty Range" $
(range 1 4) + empty #?= (range 1 4)]
There are command line options
% ./test --help
Mmm... tasty test suite
Usage: ex [-p|--pattern ARG] [-l|--list-tests] [-j|--num-threads ARG]
[-q|--quiet] [--hide-successes] [--smallcheck-depth ARG]
[--quickcheck-tests ARG] [--quickcheck-replay ARG]
[--quickcheck-max-size ARG] [--quickcheck-max-ratio ARG]
Available options:
-h,--help Show this help text
-p,--pattern ARG Select only tests that match pattern
-l,--list-tests Do not run the tests; just print their names
-j,--num-threads ARG Number of threads to use for tests execution
-q,--quiet Do not produce any output; indicate success only by
the exit code
--hide-successes Do not print tests that passed successfully
--smallcheck-depth ARG Depth to use for smallcheck tests
--quickcheck-tests ARG Number of test cases for QuickCheck to generate
--quickcheck-replay ARG Replay token to use for replaying a previous test run
--quickcheck-max-size ARG
Size of the biggest test cases quickcheck generates
--quickcheck-max-ratio ARG
Maximum number of discared tests per successful test
before giving up
You can use the --pattern option to select only specific tests to run
Related
I'm trying to create my first tests on Clojure. I wrote this file. When I run:
lein test
I got:
ERROR in (update-question) (core.clj:32)
Uncaught exception, not in assertion.
expected: nil
actual:
clojure.core/require core.clj: 6007 (repeats 2 times)
clojure.core/load-libs core.clj: 5969
clojure.core/load-libs core.clj: 5985
clojure.test/run-tests test.clj: 768 (repeats 2 times)
clojure.core/apply core.clj: 667
clojure.lang.ExceptionInfo: Parameter Mismatch: :id parameter data not found.
java.lang.Exception: Exception in :get-one-question
FAIL in (create-answer) (tests_test.clj:63)
Create a new answer
expected: (not (nil? (:ordnen answer)))
actual: (not (not true))
FAIL in (create-answer) (tests_test.clj:64)
Create a new answer
expected: nil
actual: "Some not so cool answer"
diff: + "Some not so cool answer"
2020-03-05 17:24:12,124 [main] INFO zentaur.models.tests-test - >>> PA #first-user #first-user >>>>> {:email "grayson#me.com", :fname "Grayson", :lname "Barr", :active true, :id 72, :role_id 1, :uuid "efc8c516-04f1-4b22-a983-ed1e9c4f0da7"}
Ran 5 tests containing 7 assertions.
**2 failures, 1 errors.**
Testing zentaur.models.tests-test
2020-03-05 17:24:12,233 [main] INFO zentaur.models.tests-test - >>> PARAM #first-question#first-question#first-question >>>>> {:explanation "Explanation", :reviewed_fact false, :question "Some cool Question", :points 2, :hint "Some hint", :qtype 2, :updated_at #time/instant "2020-03-05T23:24:12Z", :reviewed_cr false, :active true, :id 71, :user_id 1, :origin 0, :fulfill nil, :created_at #time/instant "2020-03-05T23:24:12Z", :reviewed_lang false}
2020-03-05 17:24:12,849 [main] INFO zentaur.models.tests-test - >>> PA #first-user #first-user >>>>> {:email "samantha#yahoo.com", :fname "Samantha", :lname "Good", :active true, :id 73, :role_id 1, :uuid "16d3eea4-3ed2-4636-a753-7fc0ec381e77"}
Ran 5 tests containing 8 assertions.
0 failures, 0 errors.
So, it looks like the test is executed twice but the first time with errors and the second correctly. I noticed the line:
clojure.test/run-tests test.clj: 768 (repeats 2 times)
but I don't why the file is called twice and why the first time some tests are "skipped". If I remove the line:
(run-tests)
from the file, only the first (and wrong) time is executed.
You don't need (run-tests) in the file. The command lein test will find all tests defined via deftest and run them for you.
Here is a sample project you can clone and modify (try it w/o changes first to see an example of how it should look).
It is possible that your mount is doing something strange. I personally don't like the "silent magic" that mount uses. Also, I'm surprised it works for zentaur.config, zentaur.handler, and zentaur.db.core when they are not part of the (:require ...) expression.
As Alan mentioned, (run-tests) is not necessary, that option usually is used in combination with "with-test".
As far as I could understand, before execute them, "lein test" gathers all the "deftest" in a vector that doesn't reflect the order in the file, so the last "deftest" are executed first. My solution was to nest the three "(testing " sections that I need to be executed sequentially in a "parent" deftest.
Overtone seems to be ignoring the mult and add arguments to sin-osc. Wanted to make sure I'm not missing something silly before filing a bug report.
This snippet should vary the amplitude from 0.6 to 1.0:
(definst sin-test [freq 440]
(* (sin-osc:kr 0.5 0 0.2 0.8)
(sin-osc:ar freq)))
Instead its clearly multiplying by -1 to 1 (going totally silent in the middle). Setting the mult on the sin-osc:ar in this snippet also has no effect.
The equivalent in straight supercollider behaves as expected:
{ SinOsc.kr(0.5,0,0.2,0.8) * SinOsc.ar(440) }.play;
I notice that the the tremolo example on the overtone getting started page does the mult and add manually, but figured they were just being explanatory:
(definst trem [freq 440 depth 10 rate 6 length 3]
(* 0.3
(line:kr 0 1 length FREE)
(saw (+ freq (* depth (sin-osc:kr rate))))))
Given the following code:
defmodule Pullapi.Workout do
import Pullapi.Numbers
#moduledoc """
Functions that generate a workout representation
"""
#doc """
Returns a pullup set defined by the number of `max_reps` a user can do, a `percentage`, and the
number of maximum additional or decremented reps, `rep_bound`.
## Examples
iex> Pullapi.Workout.pullup_set(20, 60, 5)
%{"Action" => "Pullups", "Units" => "14"}
"""
#spec pullup_set(integer, integer, integer) :: map()
def pullup_set(max_reps, percentage, rep_bound) do
median = max_reps * (percentage / 100)
unit_range = Pullapi.Numbers.median_range(round(median), rep_bound)
units = Enum.random(unit_range)
%{"Action" => "Pullups", "Units" => "#{units}"}
end
end
The doctest fails with:
1) test doc at Pullapi.Workout.pullup_set/3 (1) (PullapiTest)
test/pullapi_test.exs:4
Doctest failed
code: Pullapi.Workout.pullup_set(20, 60, 5) === %{"Action" => "Pullups", "Units" => "14"}
left: %{"Action" => "Pullups", "Units" => "8"}
stacktrace:
lib/pullapi/workout.ex:13: Pullapi.Workout (module)
Is there a way of specifying that the "Units" value is randomly generated? It looks like I'm following the way Enum.random is doctested
Enum.random's doctest is explicitly setting a seed value for the test, which makes the result of future calls to :rand functions deterministic.
iex(1)> for _ <- 1..10 do
...(1)> :rand.seed(:exsplus, {101, 102, 103})
...(1)> Enum.random([1, 2, 3])
...(1)> end
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2]
The person who wrote the tests most likely ran the functions once to check what values are returned after those seed values are set and then put them in the doctest. Unless the inner workings of :rand change, those seeds will keep producing the same values, which is good enough for doctests (you can always fix the tests if it breaks in future versions of Erlang).
So, to fix your doctest, you should execute this code once in iex (you can change the seed values if you want):
:rand.seed(:exsplus, {101, 102, 103})
Pullapi.Workout.pullup_set(20, 60, 5)
And then hardcode the returned values in your doctest. Your tests should now pass until Erlang's rand module's internals change.
I am getting logs from logstash and sending messages which contains ERROR to riemann server.
logsash message : message" => "Jul 10 04:34:47 : [ERROR] [host] Sample abc123"
In riemann I need to write a logic to find if the same ERROR occurs more than 2 times for the same sample(i.e sample abc 123) in 5min then I have to alert /print on the console(currently I am trying to print to test)
below is my logic to achieve this, currently it prints only nil and not the message, can some one please help me here.
(by [:message]
;; over time windows of 5 seconds...
(fixed-time-window 5
;; calculate the average value of the metric and emit an average (summary) event
(combine folds/mean
;; collect the summary event over the last 3 fixed-time-windows
(moving-event-window 2
;;find the summary event with the minimum average metric
(combine folds/minimum
(where (> metric 2.0)
prn
;;(email "xyz")
)
)
)
)
)
)
(It prints message every 2sec, if I use just fixed-time-window and nothing else but I want to print only if same sample occurs 2times in 5min)
Riemann event for example:
:service "logstash",
:time 1468920656,
:state "critical",
:host "0.0.0.0",
:ttl nil,
:message "Jul 11 04:34:47 <hostname>: [ERROR] [xyz] - Sample abc123 causes problem for ...""
I am using clj-plaza (0.0.5-SNAPSHOT) to query a Sesame/Jena Model. The function model-query does not appear to execute the query. It returns the internal representation of a clj-plaza query instead.
(init-jena-framework)
(def *m* (build-model))
(with-model *m*
(model-add-triples
(model-to-triples
(document-to-model "http://www.rdfdata.org/dat/rdfdata.rdf"
:rdf))))
(def all-subjects-query
(defquery
(query-set-vars [:?subject])
(query-set-pattern (make-pattern [[:?subject ?p ?o]]))
(query-set-type :select))
;; As expected
(model-query-triples *m* all-subjects-query)
=> clojure.lang.LazySeq#2e1e8502
;; Does not execute query (?)
(model-query *m* all-subjects-query)
=> {:kind :select, :pattern [[:?object :?p :?o]], :vars [:?object]}
The official tutorial claims model-query returns a list of bindings from the query:
({:?object "http://randomurl.com/asdf"}
{:?object "http://asdf.com/qwer"})
This is a bug.
Here is a fix. Until it is merged back and updated on clojars, feel free to use my fork.
A workaround would be to use (query model query) (instead of model-query)after importing the corresponding Jena or Sesame implementation.
For Sesame:
(use 'plaza.rdf.implementations.sesame)
(init-sesame-framework)
(def *m* (build-model))
(with-model *m*
(model-add-triples
(model-to-triples
(document-to-model "http://www.rdfdata.org/dat/rdfdata.rdf"
:rdf))))
(def all-subjects-query
(defquery
(query-set-vars [:?subject])
(query-set-pattern (make-pattern [[:?subject ?p ?o]]))
(query-set-type :select))
(query *m* all-subjects-query)
=> [{:?s #<SesameResource http://www.rdfdata.org/dat/rdfdata.rdf>}
{:?s #<SesameResource http://www.rdfdata.org/dat/rdfdata.rdf>}
{:?s #<SesameResource http://www.rdfdata.org/dat/rdfdata.rdf>}
{:?s #<SesameResource http://www.rdfdata.org/dat/rdfdata.rdf>}
{:?s #<SesameResource http://rdfweb.org/topic/FOAFBulletinBoard>}
{:?s #<SesameResource http://rdfweb.org/topic/FOAFBulletinBoard>} ...