Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

0.10: Updated code with the new Snaplet.Test and Auth.Handlers Tests #56

Merged
merged 36 commits into from

3 participants

@adinapoli

Hi guys,

I think time has come, because I don't know if until the end of the year I will be able to keep the same pace (big relocation in progress).
Here are the results of my dev effort of the last month or so: the new Snaplet.Test allows the programmer to test his Snaplet(s) with an easy interface, very similar to Snap.Test one.

I've used the same code I've developed to test Auth.Handlers.
We have now 85% code coverage on that module, which is pretty good. I had to comment the markAuthFailLocketOut test (see bug report I've raised).

Thank you all and Doug for helping me with the code base :)

Cheers,
A.

adinapoli and others added some commits
@adinapoli adinapoli Added Snaplet.Test basic stuff 95c8b8d
@adinapoli adinapoli Fixed the cabal manifest to include Snaplet.Test and HUnit f4add17
Alfredo Added a preliminary snaplet to test HUnit assertions 8a5a8bb
Alfredo Merge remote-tracking branch 'upstream/0.10' into snaplet-test 7dcac7d
@adinapoli adinapoli Removed snaplest.Test from launcher 300101f
Alfredo Merge remote-tracking branch 'upstream/0.10' into snaplet-test 01f2715
Alfredo Merge branch 'snaplet-test' of http://github.com/adinapoli/snap into …
…snaplet-test
660a6c9
Alfredo Removed good.tpl from heist/templates 625d502
Alfredo Implemented a simple test which uses a Snaplet.Test assertion under t…
…he hood
770ffe2
@adinapoli adinapoli Merge remote-tracking branch 'upstream/0.10' into snaplet-test 743313a
@adinapoli adinapoli Removed MusicStoreSnaplet and its deps 8cf3acd
@adinapoli adinapoli Removed MusicStore 1561648
Alfredo Removed Assertion, Types and any trace of previous work. Focusing on …
…runHandler
a87ee1a
Your Name Merge remote-tracking branch 'upstream/0.10' into snaplet-test 343d8e3
@adinapoli adinapoli Merge remote-tracking branch 'upstream/0.10' into snaplet-test 37a71a9
@adinapoli adinapoli Merge branch 'snaplet-test' of http://github.com/adinapoli/snap into …
…snaplet-test
0760995
@adinapoli adinapoli Used undefined as placeholder for (Snaplet b), one test passes 9074a84
@adinapoli adinapoli Damn close. 3b24629
@adinapoli adinapoli Used Initializer in RunHandler. Still not completely satisfied, though c25330f
Your Name First full-fledged implementation of runHandler. Hooray 2043a56
Your Name Minor tweak c99c0a2
Your Name Added tests for createUser. They pass but I need to find a way to flu…
…sh users.json at every test
b478854
Your Name Added the withTemporaryFile function to implement a poor-man-setUp me…
…chanism
dc258d3
Your Name Added tests for createUser and usernameExists 78d57c5
Your Name Added more test, polished existing ones. 2f28ebf
Your Name Added two more tests aed6e04
Your Name Minor tweak d99040f
Your Name Add other tests 4c64575
Your Name Added more tests 16a3883
Your Name Added more test, ignoring json files in test 6d01d04
Your Name Merged conflicts from upstream/0.10 1a4a64f
Your Name Polished a bit the code, added a couple of more tests 984b58c
Your Name Need to fix the locked out test ad60dbf
Your Name Merge remote-tracking branch 'upstream/0.10' into snaplet-test 9e9f11b
Your Name Merge 0.10 into snaplet-test 2e136c3
Your Name Merged upstream/0.10, aligned the test code with the new lenses 2391a69
@mightybyte mightybyte merged commit 2391a69 into snapframework:0.10
@gregorycollins gregorycollins commented on the diff
snap.cabal.orig
@@ -0,0 +1,233 @@
+name: snap
@gregorycollins Owner

This looks like an accidental inclusion, could you zap this file please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@gregorycollins

You accidentally committed a .swo file here, whatever that is.

Hi Greg, sorry for that! Probably it was a Vim-related thing :)

@gregorycollins gregorycollins commented on the diff
src/Snap/Snaplet/Test.hs
((44 lines not shown))
+ catchNonExistence :: IO a -> a -> IO a
+ catchNonExistence job nonexistval =
+ E.catch job $
+ \e -> if isDoesNotExistError e then return nonexistval
+ else ioError e
+
+
+------------------------------------------------------------------------------
+-- | Given a Snaplet Handler and a 'RequestBuilder' defining
+-- a test request, runs the Handler, producing an HTTP 'Response'.
+--
+-- Note that the output of this function is slightly different from
+-- 'runHandler' defined in Snap.Test, because due to the fact running
+-- the initializer inside 'SnapletInit' can throw an exception.
+runHandler :: MonadIO m =>
+ RequestBuilder m ()
@gregorycollins Owner

Wibble: alignment here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@gregorycollins gregorycollins commented on the diff
src/Snap/Snaplet/Test.hs
((69 lines not shown))
+ return $ Right res
+
+
+------------------------------------------------------------------------------
+-- | Given a Snaplet Handler, a 'SnapletInit' specifying the initial state,
+-- and a 'RequestBuilder' defining a test request, runs the handler,
+-- returning the monadic value it produces.
+--
+-- Throws an exception if the 'Snap' handler early-terminates with 'finishWith'
+-- or 'mzero'.
+--
+-- Note that the output of this function is slightly different from
+-- 'evalHandler defined in Snap.Test, because due to the fact running
+-- the initializer inside 'SnapletInit' can throw an exception.
+evalHandler :: MonadIO m =>
+ RequestBuilder m ()
@gregorycollins Owner

Also here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@gregorycollins gregorycollins commented on the diff
src/Snap/Snaplet/Test.hs
((67 lines not shown))
+ (Right (a,_)) -> do
+ res <- ST.runHandler rq $ runPureBase h a
+ return $ Right res
+
+
+------------------------------------------------------------------------------
+-- | Given a Snaplet Handler, a 'SnapletInit' specifying the initial state,
+-- and a 'RequestBuilder' defining a test request, runs the handler,
+-- returning the monadic value it produces.
+--
+-- Throws an exception if the 'Snap' handler early-terminates with 'finishWith'
+-- or 'mzero'.
+--
+-- Note that the output of this function is slightly different from
+-- 'evalHandler defined in Snap.Test, because due to the fact running
+-- the initializer inside 'SnapletInit' can throw an exception.
@gregorycollins Owner

Maybe you can just let it throw instead?

This is somewhat we discussed with Doug, as far as I could remember.
The API is not so terrible, so I think we could leave things as they are, or we can come up with a different implementation :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@gregorycollins gregorycollins commented on the diff
src/Snap/Snaplet/Test.hs
((87 lines not shown))
+ -> m (Either Text a)
+evalHandler rq h s = do
+ app <- getSnaplet s
+ case app of
+ (Left e) -> return $ Left e
+ (Right (a,_)) -> do
+ res <- ST.evalHandler rq $ runPureBase h a
+ return $ Right res
+
+
+------------------------------------------------------------------------------
+-- | Run the given initializer, yielding a tuple where the first element is
+-- a @Snaplet b@, or an error message whether the initializer threw an
+-- exception.
+getSnaplet :: MonadIO m =>
+ SnapletInit b b
@gregorycollins Owner

Alignment here also

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@gregorycollins gregorycollins commented on the diff
test/suite/Snap/Snaplet/Auth/Handlers/Tests.hs
((23 lines not shown))
------------------------------------------------------------------------------
tests :: Test
tests = testGroup "Snap.Snaplet.Auth.Handlers"
- [testGroup "createUser tests"
+ [mutuallyExclusive $ testGroup "createUser tests"
@gregorycollins Owner

What's the mutual exclusion here for?

as far as I remember, mutuallyExclusive was to enforce that these tests were not run concurrently.
In this particular case, the order is important, because I'm testing the Test T things with some preconditions that the test T-1 set up. In absence of a fixture-like mechanism, this was the only way I've found :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@gregorycollins gregorycollins commented on the diff
test/suite/Snap/Snaplet/Auth/Handlers/Tests.hs
((23 lines not shown))
------------------------------------------------------------------------------
tests :: Test
tests = testGroup "Snap.Snaplet.Auth.Handlers"
- [testGroup "createUser tests"
+ [mutuallyExclusive $ testGroup "createUser tests"
[testCreateUserGood
@gregorycollins Owner

This list should have a space between the punctuation and the names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@gregorycollins

Thanks for writing all of these tests!

@mightybyte
Owner

Ok, I fixed most of these issues. The only ones left that I'm not sure about are the comment about evalHandler and throwing and the mutualExclusion one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 19, 2012
  1. @adinapoli
  2. @adinapoli
Commits on Sep 20, 2012
Commits on Sep 21, 2012
Commits on Sep 22, 2012
  1. @adinapoli
Commits on Sep 24, 2012
  1. Merge branch 'snaplet-test' of http://github.com/adinapoli/snap into …

    Alfredo authored
    …snaplet-test
  2. Removed good.tpl from heist/templates

    Alfredo authored
Commits on Sep 25, 2012
  1. @adinapoli
  2. @adinapoli
  3. @adinapoli

    Removed MusicStore

    adinapoli authored
Commits on Sep 26, 2012
Commits on Oct 5, 2012
Commits on Oct 7, 2012
  1. @adinapoli
  2. @adinapoli
Commits on Oct 11, 2012
  1. @adinapoli
Commits on Oct 14, 2012
  1. @adinapoli

    Damn close.

    adinapoli authored
Commits on Oct 15, 2012
  1. @adinapoli
Commits on Oct 16, 2012
  1. Minor tweak

    Your Name authored
Commits on Oct 18, 2012
  1. Added tests for createUser. They pass but I need to find a way to flu…

    Your Name authored
    …sh users.json at every test
  2. Added tests for createUser and usernameExists

    Your Name authored
  3. Added more test, polished existing ones.

    Your Name authored
  4. Added two more tests

    Your Name authored
  5. Minor tweak

    Your Name authored
Commits on Oct 19, 2012
  1. Add other tests

    Your Name authored
  2. Added more tests

    Your Name authored
  3. Added more test, ignoring json files in test

    Your Name authored
Commits on Oct 22, 2012
  1. Merged conflicts from upstream/0.10

    Your Name authored
Commits on Oct 30, 2012
  1. Need to fix the locked out test

    Your Name authored
Commits on Nov 9, 2012
  1. Merge 0.10 into snaplet-test

    Your Name authored
Something went wrong with that request. Please try again.