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

Merged
merged 36 commits into from Nov 24, 2012

3 participants

@adinapoli
Snap Framework member

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 Sep 19, 2012
@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 Nov 24, 2012
@gregorycollins gregorycollins commented on the diff Nov 25, 2012
snap.cabal.orig
@@ -0,0 +1,233 @@
+name: snap
@gregorycollins
Snap Framework member
gregorycollins added a line comment Nov 25, 2012

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
Snap Framework member

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

Snap Framework member

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

@gregorycollins gregorycollins commented on the diff Nov 25, 2012
src/Snap/Snaplet/Test.hs
+ 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
Snap Framework member
gregorycollins added a line comment Nov 25, 2012

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 Nov 25, 2012
src/Snap/Snaplet/Test.hs
+ 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
Snap Framework member
gregorycollins added a line comment Nov 25, 2012

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 Nov 25, 2012
src/Snap/Snaplet/Test.hs
+ (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
Snap Framework member
gregorycollins added a line comment Nov 25, 2012

Maybe you can just let it throw instead?

@adinapoli
Snap Framework member
adinapoli added a line comment Nov 25, 2012

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 Nov 25, 2012
src/Snap/Snaplet/Test.hs
+ -> 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
Snap Framework member
gregorycollins added a line comment Nov 25, 2012

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 Nov 25, 2012
test/suite/Snap/Snaplet/Auth/Handlers/Tests.hs
------------------------------------------------------------------------------
tests :: Test
tests = testGroup "Snap.Snaplet.Auth.Handlers"
- [testGroup "createUser tests"
+ [mutuallyExclusive $ testGroup "createUser tests"
@gregorycollins
Snap Framework member
gregorycollins added a line comment Nov 25, 2012

What's the mutual exclusion here for?

@adinapoli
Snap Framework member
adinapoli added a line comment Nov 25, 2012

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 Nov 25, 2012
test/suite/Snap/Snaplet/Auth/Handlers/Tests.hs
------------------------------------------------------------------------------
tests :: Test
tests = testGroup "Snap.Snaplet.Auth.Handlers"
- [testGroup "createUser tests"
+ [mutuallyExclusive $ testGroup "createUser tests"
[testCreateUserGood
@gregorycollins
Snap Framework member
gregorycollins added a line comment Nov 25, 2012

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
Snap Framework member

Thanks for writing all of these tests!

@mightybyte
Snap Framework member

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