incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Vlad GURDIGA <gurd...@gmail.com>
Subject Testing shows and lists
Date Tue, 03 Nov 2009 12:05:32 GMT
Hello!

How would someone test the application code inside shows and lists? Is
there any recommended way or some best-practice?

The actual page is probably more practical to test inside the browser
with something like QUnit. But, before uploading the show to CouchDB
I'd like to make sure it does not throw syntactical or other errors
like undefined variable/function/whatever.

If you get it to CouchDB in an "unstable" state it's pretty
uncomfortable to find what's broken even from the logs when you have a
page built up from more than a couple page components (with CouchApp
macro-instructions like // !code and // !json).

You can TDD the code you use all over the application by separating it
into a library*. But how would you make sure that the page components
and the other code that build up the page do actually glue up together
nicely before uploading it?

* http://github.com/gurdiga/cozy/tree/master/lib/

Why before? Because it is an earlier feedback, and it is probably *a
lot* faster to run it from command line with SpiderMonkey.

Now,if you feed the show, which is an anonymous function to js, it
only checks for syntactic correctness, because it does not actually
run that function. You can try to kinda monkey-patch the function by
adding "(" before and ")()" after the function body with a little
shell script like this:

#!/bin/sh

echo "("
cat $1
echo ")();"

and then use it like:

$ ./mokey-patch.sh ./shows/page.js | js -w -s

This way you would have that function ran and you'd find out that you
need to mock up the "doc" and "req" variables if you have references
to them in your code.

When it comes to lists you get one more layer to penetrate: the code
that you'll write is given as a closure to the "provides" function,
and running the main list function with js does not actually run the
code in the closure unless you will try to import somehow the original
"provides" function from the CouchDB code, which seamed kinda scary to
me and I abandoned this idea.

The way which I ended up with was to kinda "export" the application
code into a separate file named like "page.app.js" in which I have all
my application code and which I inject back to the show file before
"couchapp push" to CouchDB. Having it separate gives me the ability to
make sure it runs before I get it on-line. Of course I have to fake
all the data I get as arguments to the main function, but this turned
out not to be that hard*.

* http://github.com/gurdiga/cozy/blob/master/shows/book.app.js#L1

The bad thing about my "approach" is that I get almost *twice as much
code* uploaded to CouchDB as is actually ran by the show/list because
the *.app.js is preprocessed by CouchApp and all the "// !code" macros
are expanded.

It is functional, it works, but it does not feel quite right. =(

I'm kinda stuck here for now, so any good ideas are welcome. :)

Mime
View raw message