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.
Related
I want to fetch 10 latest entries from elasticsearch database.
For this, I am using:
(require '[clojurewerkz.elastisch.rest.document :as esd])
(esd/search es-conn
index_name
mapping
:query (q/prefix :column value)
:from 0 :size 10)
This only fetches the oldest 10 entries from the document.
I want to know how to fetch the latest entries....
I mean which parameter should be passed.
You need to have a _timestamp fields on your document:
https://www.elastic.co/search?q=timestamp
Then you can sort on that and limit the size to 10.
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 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
I was wondering what would be the best way of specifying a default value when doing an outer-join in cascalog for field that could be null.
(def example-query
(<- [?id ?fname ?lname !days-active]
(users :> ?id ?fname ?lname)
(active :> ?fname ?lname !days-active))
In this example users and active would be previously defined queries and I'm just looking to correlate active user information (?fname ?lname !days-active) and regular user information (?id ?fname ?lname)
So when the join happened if there was no corresponding information for !days-active it would output 0 instead of nil
i.e.
392393 john smith 3
003030 jane doe 0
instead of
392393 john smith 3
003030 jane doe null
Updated Example
(<- [!!database-id ?feature !!user-clicks !!engaged-users ?application-id ?active-users]
(app-id-db-id-feature-clicks-engaged :> ?application-id !!database-id ?feature !!user-clicks !!engaged-users )
(user-info :> ?application-id ?feature ?active-users))]
example output would look something roughly like
4234 search null null 222 5000
3232 profile 500 400 331 6000
with the filtering that I'm interested I could change the fields that would be !!engaged-users and !!user-clicks to have 0 instead of null. Would using multiple Or predicates work?
I think what you want to do is add an or predicate:
(def example-query
(<- [?id ?fname ?lname !days-active]
(users :> ?id ?fname ?lname)
(active :> ?fname ?lname !days-active)
(or !days-active 0 :> ?active-days)))
That's not an outer join, by the way, it's just not filtering out null variables in the !days-active position.