<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>validates_presence_of :purpose : </title>
    <link>http://lenaherrmann.net</link>
    <language>en_US</language>
    <ttl>40</ttl>
    <description>Lena Herrmann</description>
    <item>
      <title>Thesis is done - thanks for your support!</title>
      <description>&lt;p&gt;I&amp;#8217;m done!! Three weeks ago &lt;a href="http://blog.couch.io/post/903223195/diplom-thesis-realisation-of-a-distributed-application"&gt;I handed in my diplom thesis&lt;/a&gt;. The practical part was the design and implementation of a distributed, replicatable &lt;a href="http://en.wikipedia.org/wiki/Outliner"&gt;outliner&lt;/a&gt; with the NoSQL database &lt;a href="http://couchdb.apache.org/"&gt;CouchDB&lt;/a&gt;. As theoretical work I evaluated Distributed Systems, CouchDB, various web technologies and programming strategies. I still have to pass the oral exam, but my degree is finally very close - soon I can call myself a graduated computer scientist. Yay!&lt;/p&gt;


&lt;p&gt;As soon as academic procedures allow it, I&amp;#8217;ll publish the text and the source code of the thesis here. Until then I&amp;#8217;d like to say thank you to a few people. As the foreword of thesis won&amp;#8217;t get as much attention as this blog, I&amp;#8217;ll do it here.&lt;/p&gt;


&lt;p&gt;First first first of all, my appreciation goes to my boys at home and my parents for backing me up by spending loads of time with my child. Not only my professional life is based upon their support. THANKS.&lt;/p&gt;


&lt;p&gt;Further thanks go to:&lt;/p&gt;


&lt;p&gt;&lt;a href="http://upstre.am/"&gt;Upstream&lt;/a&gt; for generously sponsoring a part of the time I spent researching - especially &lt;a href="http://www.twitter.com/langalex"&gt;Alex Lang&lt;/a&gt; for his support and coming up with the initial idea. Without them I probably would have done some usability study for the drawer, or I would have had lots of fun in some enterprisey Java company. Also I felt I wanted to strive hard to write good code in my thesis to match Upstream&amp;#8217;s high standards for code quality and test coverage - not a bad thing.&lt;/p&gt;


&lt;p&gt;Allround guru &lt;a href="http://jan.prima.de/"&gt;Jan Lehnardt&lt;/a&gt; - he not only knew and happily provided the answers to all questions I had about CouchDB, databases in general, JavaScript, LaTeX, typography and virtually everything else; he was also constantly encouraging and guiding me to contribute to open source projects, trusting in my skills and doing &lt;i&gt;awesome&lt;/i&gt; things. One day I&amp;#8217;ll also submit a talk to one of your conferences, word :-)&lt;/p&gt;


&lt;p style="font-size:0.9em;"&gt;&lt;img width="532"  src="/files/thesis_sofa.jpg" alt="Jan Lehnardt, myself and Alex Lang on the couch with thesis."/&gt;&lt;br/&gt;

Jan Lehnardt, myself and Alex Lang on the couch with thesis.&lt;/p&gt;


&lt;p&gt;My professor &lt;a href="http://prof.beuth-hochschule.de/edlich/"&gt;Stefan Edlich&lt;/a&gt; for doing a great job in mentoring me. He is also the university professor with the most phenomenal email response times I have ever heard of.&lt;/p&gt;


&lt;p&gt;&lt;a href="http://github.com/ur5"&gt;Urs&lt;/a&gt;, Andy and Ischa advised me on the structuring and helped to make the result look quite scientific. They patiently explained to me how to go about writing such a long academic text. Ischa also is one of the very few people who actually read the whole thing in one piece.&lt;/p&gt;


&lt;p&gt;Several lovely people who helped at certain points during the research or the programming, listened with interest and/or assisted figuring out specific problems: &lt;a href="http://freelancing-gods.com/"&gt;Pat Allan&lt;/a&gt; (getting started was the hardest part); Urs again (many small and big issues all along the way); &lt;a href="http://github.com/endor"&gt;Frank Pr&#246;&#223;dorf&lt;/a&gt; and &lt;a href="http://twitter.com/overbryd"&gt;Lukas Rieder&lt;/a&gt; (figuring out with me what a Couchapp actually is); &lt;a href="http://till.klampaeckel.de/"&gt;Till Klamp&#228;ckel&lt;/a&gt; (skilled advice about Lounge and EC2); &lt;a href="http://www.kristinaschneider.com/"&gt;Kristina Schneider&lt;/a&gt; (tricky CSS shit); &lt;a href="http://twitter.com/freaklikeme"&gt;Thilo Utke&lt;/a&gt; and &lt;a href="http://twitter.com/svenfuchs"&gt;Sven Fuchs&lt;/a&gt; (die HTMLUnit bug, die);  &lt;a href="http://twitter.com/i2w"&gt;Ian White&lt;/a&gt; (this one bug in the code you had no idea what it was for but figured it out anyway); and various people from the Ruby and Javascript communities who helped me to shape my ideas by asking basic or challenging questions.&lt;/p&gt;


&lt;p&gt;Some more people who wrote great open source programs, did great support and were always open to ideas: my thanks go to a good part of the CouchDB community here, and also to &lt;a href="http://quirkey.com/"&gt;Aaron Quint&lt;/a&gt; and &lt;a href="http://twitter.com/tjholowaychuk"&gt;TJ Holowaychuk&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Finally my army of proofreaders! I split the text up in very small parts so that also people with a limited time budget had no excuse - I can really recommend this approach ;-). The list includes almost everyone mentioned above, and also: &lt;a href="http://twitter.com/adrianlang"&gt;Adrian&lt;/a&gt;, Anne, &lt;a href="https://www.xing.com/profile/Birgit_Behringer"&gt;Birgit&lt;/a&gt;, &lt;a href="http://twitter.com/bumi"&gt;Bumi&lt;/a&gt;, Diana, &lt;a href="http://ekedase.blogspot.com/"&gt;Eike&lt;/a&gt;, &lt;a href="http://twitter.com/flofff"&gt;Flo&lt;/a&gt;, Grushi, &lt;a href="https://www.xing.com/profile/Helge_NeuschwanderLutz"&gt;Helge&lt;/a&gt;, Jan S., Micha, Neels, Ole and &lt;a href="http://www.huesler-informatik.ch/"&gt;Patrick&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;The very last one goes to my sweetheart for brightening my life in those last stressful months.&lt;/p&gt;


&lt;p&gt;Thank you everyone! I&amp;#8217;m glad to know you all!!&lt;/p&gt;
</description>
      <pubDate>Thu, 05 Aug 2010 10:41:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:8b192edb-c0eb-431c-ba7e-112050446cc0</guid>
      <comments>http://lenaherrmann.net/2010/08/05/thesis-is-done-thanks-for-your-support#comments</comments>
      <category>thesis</category>
      <category>thoughts</category>
      <category>couchdb</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=17</trackback:ping>
      <link>http://lenaherrmann.net/2010/08/05/thesis-is-done-thanks-for-your-support</link>
    </item>
    <item>
      <title>JavaScript syntax highlighting in the LaTeX listings package</title>
      <description>&lt;p&gt;I&amp;#8217;m currently writing my thesis, using &lt;a href="http://www.latex-project.org/"&gt;LaTeX&lt;/a&gt;. As the programming part of my thesis was mostly done in Javascript, I have to include some bits of Javascript source code in the LaTeX document. For this I&amp;#8217;m using the &lt;a href="http://en.wikibooks.org/wiki/LaTeX/Packages/Listings"&gt;listings package&lt;/a&gt;. With this package you can &amp;#8220;include the source code of any programming language within your document&amp;#8221;. Unfortunately Javascript is not in the list of supported programming languages. (In return, the list contains languages like Oberon-2, Promela or RSL, which are probably being used way more often than Javascript &amp;#8230; er.)&lt;/p&gt;


&lt;p&gt;Luckily &lt;a href="http://twitter.com/awendt"&gt;@awendt&lt;/a&gt; pointed me to these links: &lt;a href="http://vis4.net/blog/2009/08/javascript-quelltexte-in-latex/"&gt;Javascript Quelltexte in LaTeX&lt;/a&gt; and &lt;a href="http://mail.asta.uni-potsdam.de/pipermail/uplug/2006-June/003595.html"&gt;a thread on a mailinglist&lt;/a&gt;. I changed those examples only a bit, I&amp;#8217;m posting my result here mainly in the hope it will produce a Google hit for &amp;#8220;javascript latex source code&amp;#8221; :-)&lt;/p&gt;


&lt;p&gt;Put this at the beginning of your document. It will define support for the Javascript language. I included the keywords that show up in my code most frequently.

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="er"&gt;\&lt;/span&gt;usepackage{listings}
&lt;span class="er"&gt;\&lt;/span&gt;usepackage{color}
&lt;span class="er"&gt;\&lt;/span&gt;definecolor{lightgray}{rgb}{.&lt;span class="i"&gt;9&lt;/span&gt;,.&lt;span class="i"&gt;9&lt;/span&gt;,.&lt;span class="i"&gt;9&lt;/span&gt;}
&lt;span class="er"&gt;\&lt;/span&gt;definecolor{darkgray}{rgb}{.&lt;span class="i"&gt;4&lt;/span&gt;,.&lt;span class="i"&gt;4&lt;/span&gt;,.&lt;span class="i"&gt;4&lt;/span&gt;}
&lt;span class="er"&gt;\&lt;/span&gt;definecolor{purple}{rgb}{&lt;span class="fl"&gt;0.65&lt;/span&gt;, &lt;span class="fl"&gt;0.12&lt;/span&gt;, &lt;span class="fl"&gt;0.82&lt;/span&gt;}

&lt;span class="er"&gt;\&lt;/span&gt;lstdefinelanguage{&lt;span class="co"&gt;JavaScript&lt;/span&gt;}{
  keywords={typeof, new, &lt;span class="pc"&gt;true&lt;/span&gt;, &lt;span class="pc"&gt;false&lt;/span&gt;, catch, function, &lt;span class="r"&gt;return&lt;/span&gt;, null, catch, switch, var, &lt;span class="r"&gt;if&lt;/span&gt;, &lt;span class="r"&gt;in&lt;/span&gt;, &lt;span class="r"&gt;while&lt;/span&gt;, &lt;span class="r"&gt;do&lt;/span&gt;, &lt;span class="r"&gt;else&lt;/span&gt;, &lt;span class="r"&gt;case&lt;/span&gt;, &lt;span class="r"&gt;break&lt;/span&gt;},
  keywordstyle=&lt;span class="er"&gt;\&lt;/span&gt;color{blue}&lt;span class="er"&gt;\&lt;/span&gt;bfseries,
  ndkeywords={&lt;span class="r"&gt;class&lt;/span&gt;, export, boolean, throw, implements, import, this},
  ndkeywordstyle=&lt;span class="er"&gt;\&lt;/span&gt;color{darkgray}&lt;span class="er"&gt;\&lt;/span&gt;bfseries,
  identifierstyle=&lt;span class="er"&gt;\&lt;/span&gt;color{black},
  sensitive=&lt;span class="pc"&gt;false&lt;/span&gt;,
  comment=[l]{&lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt;},
  morecomment=[s]{&lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="k"&gt;*}{*&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt;},
  commentstyle=&lt;span class="er"&gt;\&lt;/span&gt;color{purple}&lt;span class="er"&gt;\&lt;/span&gt;ttfamily,
  stringstyle=&lt;span class="er"&gt;\&lt;/span&gt;color{red}&lt;span class="er"&gt;\&lt;/span&gt;ttfamily,
  morestring=[b]&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;,
  morestring=[b]&amp;quot;
}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;Here you define the style for the newly defined language. This also belongs in the preamble of your LaTeX file.
  
&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="er"&gt;\&lt;/span&gt;lstset{
   language=&lt;span class="co"&gt;JavaScript&lt;/span&gt;,
   backgroundcolor=&lt;span class="er"&gt;\&lt;/span&gt;color{lightgray},
   extendedchars=&lt;span class="pc"&gt;true&lt;/span&gt;,
   basicstyle=&lt;span class="er"&gt;\&lt;/span&gt;footnotesize&lt;span class="er"&gt;\&lt;/span&gt;ttfamily,
   showstringspaces=&lt;span class="pc"&gt;false&lt;/span&gt;,
   showspaces=&lt;span class="pc"&gt;false&lt;/span&gt;,
   numbers=left,
   numberstyle=&lt;span class="er"&gt;\&lt;/span&gt;footnotesize,
   numbersep=&lt;span class="i"&gt;9&lt;/span&gt;pt,
   tabsize=&lt;span class="i"&gt;2&lt;/span&gt;,
   breaklines=&lt;span class="pc"&gt;true&lt;/span&gt;,
   showtabs=&lt;span class="pc"&gt;false&lt;/span&gt;,
   captionpos=b
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;The colours are just an example, read how to define more colours &lt;a href="http://en.wikibooks.org/wiki/LaTeX/Colors#Defining_new_colors"&gt;in the wikibook&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Now you can insert code like this. The example is nonsense, I tried to include as many keywords as possible. The &lt;i&gt;medskip&lt;/i&gt; command before the listing inserts a linebreak, a not very nice way to have some distance between the text and the listing.

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="er"&gt;\&lt;/span&gt;medskip
&lt;span class="er"&gt;\&lt;/span&gt;begin{lstlisting}[caption=My Javascript Example]
Name.prototype = {
  &lt;span class="ke"&gt;methodName&lt;/span&gt;: &lt;span class="kw"&gt;function&lt;/span&gt;(params){
    &lt;span class="kw"&gt;var&lt;/span&gt; doubleQuoteString = &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;some text&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;;
    &lt;span class="kw"&gt;var&lt;/span&gt; singleQuoteString = &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;some more text&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;;
    &lt;span class="c"&gt;// this is a comment&lt;/span&gt;
    &lt;span class="kw"&gt;if&lt;/span&gt;(&lt;span class="lv"&gt;this&lt;/span&gt;.confirmed != &lt;span class="kw"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; &lt;span class="kw"&gt;typeof&lt;/span&gt;(&lt;span class="lv"&gt;this&lt;/span&gt;.confirmed) == Boolean &amp;amp;&amp;amp; &lt;span class="lv"&gt;this&lt;/span&gt;.confirmed == &lt;span class="kw"&gt;true&lt;/span&gt;){
      document.createElement(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;h3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;);
      &lt;span class="pd"&gt;$&lt;/span&gt;(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;#system&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;).append(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;This looks great&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);
      &lt;span class="kw"&gt;return&lt;/span&gt; &lt;span class="kw"&gt;false&lt;/span&gt;;
    } &lt;span class="kw"&gt;else&lt;/span&gt; {
      &lt;span class="kw"&gt;throw&lt;/span&gt; &lt;span class="kw"&gt;new&lt;/span&gt; Error;
    }
  }
}
&lt;span class="er"&gt;\&lt;/span&gt;end{lstlisting}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;The result looks like this:&lt;/p&gt;


&lt;p&gt;&lt;img width="532"  src="/files/latex.png" alt="Javascript source code in LaTeX"/&gt;&lt;/p&gt;


&lt;p&gt;Update: &lt;a href="http://twitter.com/perkee"&gt;@perkee&lt;/a&gt; pointed out you can also use the &lt;a href="http://code.google.com/p/minted/"&gt;minted package&lt;/a&gt; for syntax highlighting.&lt;/p&gt; 
</description>
      <pubDate>Thu, 20 May 2010 11:20:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:53c2e2e5-5f54-4169-9b9a-d48e347b4cf3</guid>
      <comments>http://lenaherrmann.net/2010/05/20/javascript-syntax-highlighting-in-the-latex-listings-package#comments</comments>
      <category>javascript</category>
      <category>latex</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=16</trackback:ping>
      <link>http://lenaherrmann.net/2010/05/20/javascript-syntax-highlighting-in-the-latex-listings-package</link>
    </item>
    <item>
      <title>Security in CouchDB: changing the authentication db</title>
      <description>&lt;p&gt;I recently &lt;a href="http://lenaherrmann.net/2010/04/28/writing-a-testsuite-for-the-couchdb-api"&gt;wrote a testsuite for CouchDB&amp;#8217;s Javascript HTTP API&lt;/a&gt;. Among the things I tested were the security methods. That&amp;#8217;s those that deal with authentication - like signup, login, logout and so on. The challenge was to make sure my tests wouldn&amp;#8217;t interfere with the CouchDB setup of the person running the tests.&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;Background&lt;/h3&gt;


&lt;p&gt;CouchDB stores the information about the database users in a special database, the authentication_db. The default authentication_db is called &amp;#8220;_users&amp;#8221;. So this is where your user data is saved when you sign up without taking care about the details, e.g. when you use &lt;a href="http://github.com/lenalena/couchdb/blob/testsuite/share/www/script/jquery.couch.js#L112-119"&gt;the signup method provided by the JQuery HTTP API&lt;/a&gt;. Read more about the ways to interact with the authentication_db in &lt;a href="http://github.com/lenalena/couchdb-http-api-docs/blob/master/couch_js_security_methods.markdown"&gt;the HTTP API documentation&lt;/a&gt; (&lt;a href="http://github.com/lenalena/couchdb-http-api-docs/blob/master/jquery_couch_js_security_methods.markdown"&gt;JQuery methods&lt;/a&gt;), and read more about general CouchDB security &lt;a href="http://wiki.apache.org/couchdb/Security_Features_Overview"&gt;in the CouchDB Wiki&lt;/a&gt; or in &lt;a href="http://books.couchdb.org/relax/reference/security"&gt;The Definitive Guide&lt;/a&gt;.&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;How to set the authentication_db&lt;/h3&gt;


&lt;p&gt;When you e.g. create database users from within your tests, how can you make sure they don&amp;#8217;t mess up the actual authentication_db, the one the unsuspecting user might use in production? Of course you have to create a custom authentication_db for storing the user information, and make CouchDB use that one. And after your tests are finished, you want CouchDB to use the original authentication_db again, so that only your testsuite uses the custom users database.&lt;/p&gt;  


&lt;p&gt;So there is one function you call right at the beginning of the part of the testsuite that&amp;#8217;s concerned with security features:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="kw"&gt;function&lt;/span&gt; useTestUserDb(){
  test_users_db = &lt;span class="kw"&gt;new&lt;/span&gt; CouchDB(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;test_users_db&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);
  &lt;span class="kw"&gt;var&lt;/span&gt; xhr = CouchDB.request(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;PUT&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;/_config/couch_httpd_auth/authentication_db&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, {
    &lt;span class="ke"&gt;body&lt;/span&gt;: JSON.stringify(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;test_users_db&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)
  });
  &lt;span class="kw"&gt;if&lt;/span&gt;(&lt;span class="kw"&gt;typeof&lt;/span&gt;(old_auth_db) == &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;undefined&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;){
    old_auth_db = xhr.responseText.replace(&lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="ch"&gt;\n&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt;,&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;).replace(&lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="k"&gt;&amp;quot;&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="mod"&gt;g&lt;/span&gt;&lt;/span&gt;,&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;);
  }
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;We create a custom authentication_db (&amp;#8220;test_users_db&amp;#8221;) and write its name into the CouchDB configuration. If we don&amp;#8217;t have the name of the old authentication_db stored yet (the one that&amp;#8217;s called &amp;#8220;_users&amp;#8221; per default, but might be called differently), we strip this name from special characters and store it in a variable, so we can restore it later. This is the function we call at the very end of the testsuite:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="kw"&gt;function&lt;/span&gt; useOldUserDb(){
  CouchDB.request(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;PUT&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;/_config/couch_httpd_auth/authentication_db&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, {
    &lt;span class="ke"&gt;body&lt;/span&gt;: JSON.stringify(old_auth_db)
  });
  test_users_db.deleteDb();
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;In the last line, we delete our custom authentication_db. Everything is back to normal, there is no trace of our tests left. You can have a look at &lt;a href="http://github.com/lenalena/couchdb/blob/testsuite/share/www/spec/couch_js_class_methods_spec.js"&gt;how it looks like in production.&lt;/a&gt;&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;How to work with the authentication_db&lt;/h3&gt;


&lt;p&gt;You can create a new user like this:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="kw"&gt;var&lt;/span&gt; userDoc = CouchDB.prepareUserDoc({&lt;span class="ke"&gt;name&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;Gaius Baltar&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;roles&lt;/span&gt;: [&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;president&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;]}, &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;secretpassword&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);
test_users_db.save(userDoc);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;Then you are logged in and can do whatever you want. About working with users and roles, read the &lt;a href="http://books.couchdb.org/relax/reference/security"&gt;chapter in The Definitive Guide&lt;/a&gt;. When you&amp;#8217;re done, delete the userDoc from the authentication_db, as the authentication_db has to be empty before it can be deleted:
  
&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;test_users_db.deleteDoc({&lt;span class="ke"&gt;_id&lt;/span&gt; : userDoc.id, &lt;span class="ke"&gt;_rev&lt;/span&gt; : userDoc.rev})&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;
</description>
      <pubDate>Thu, 29 Apr 2010 10:40:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:f3260d78-d5a7-4750-9429-b21c2cef7f0a</guid>
      <comments>http://lenaherrmann.net/2010/04/29/security-in-couchdb-changing-the-authentication-db#comments</comments>
      <category>couchdb</category>
      <category>javascript</category>
      <category>authentication</category>
      <category>stubbing</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=14</trackback:ping>
      <link>http://lenaherrmann.net/2010/04/29/security-in-couchdb-changing-the-authentication-db</link>
    </item>
    <item>
      <title>Writing a testsuite for the CouchDB API</title>
      <description>&lt;p&gt;&lt;a href="http://couchdb.apache.org"&gt;CouchDB&lt;/a&gt; is a database that I quite like. In fact, I haven&amp;#8217;t done much else this winter, as the programming part of my thesis involved diving into many of CouchDB&amp;#8217;s features. I had just finished my little program and was desperately looking for an excuse not to start writing the thesis. Mercifully &lt;a href="http://twitter.com/janl"&gt;Jan&lt;/a&gt; from &lt;a href="http://couch.io"&gt;Couchio&lt;/a&gt; came along and asked if I wouldn&amp;#8217;t like to write a testsuite for CouchDB&amp;#8217;s Javascript HTTP API, and the documentation with it. I wanted!&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;What is this HTTP API?&lt;/h3&gt; 


&lt;p&gt;If you have worked with CouchDB from a Javascript program before (&lt;a href="http://www.quirkey.com/blog/2009/09/15/sammy-js-couchdb-and-the-new-web-architecture/"&gt;eg Sammy.js is a good companion for a simple web application&lt;/a&gt;), you have probably used the methods &lt;a href="http://github.com/apache/couchdb/blob/trunk/share/www/script/couch.js"&gt;a file named couch.js&lt;/a&gt; provides. With this interface you can easily access the most common CouchDB functionality without having to handle the XMLHttpRequest yourself. It allows you to create a &amp;#8220;database object&amp;#8221; and then perform things on this object, for example save or delete documents:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="kw"&gt;var&lt;/span&gt; db = &lt;span class="kw"&gt;new&lt;/span&gt; CouchDB(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;test_db&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);
db.createDb();

&lt;span class="kw"&gt;var&lt;/span&gt; doc = {&lt;span class="ke"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; : &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;Starbuck&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; : &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;Kara Thrace&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;};
&lt;span class="kw"&gt;var&lt;/span&gt; saved_doc = db.save(doc);

&lt;span class="c"&gt;// do something&lt;/span&gt;
db.deleteDoc({&lt;span class="ke"&gt;_id&lt;/span&gt; : &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;123&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;_rev&lt;/span&gt; : saved_doc.rev});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;If you&amp;#8217;re using JQuery, you can use the JQuery interface in &lt;a href="http://github.com/apache/couchdb/blob/trunk/share/www/script/jquery.couch.js"&gt;jquery.couch.js&lt;/a&gt;. It covers basically the same things as couch.js, but it uses, you guessed it, JQuery. That means you can send your requests asynchronously and handle the response in the callback. Same example:&lt;/p&gt;


&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="kw"&gt;var&lt;/span&gt; db = &lt;span class="pd"&gt;$&lt;/span&gt;.couch.db(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;spec_db&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;);
db.create();

&lt;span class="kw"&gt;var&lt;/span&gt; doc = {&lt;span class="ke"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;_id&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; : &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;Starbuck&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;name&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; : &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;Kara Thrace&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;};
db.saveDoc(doc, {
  &lt;span class="ke"&gt;success&lt;/span&gt;: &lt;span class="kw"&gt;function&lt;/span&gt;(response){
    &lt;span class="c"&gt;// do something&lt;/span&gt;
    db.removeDoc({&lt;span class="ke"&gt;_id&lt;/span&gt; : &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;123&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;_rev&lt;/span&gt; : response.rev});
  }
});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;The code looks a bit more complicated, but with this API you have the full power of &lt;a href="http://lenaherrmann.net/2010/02/15/refactoring-asynchronous-code"&gt;asynchronicity&lt;/a&gt; in your head, where spelling the word asynchronicity is the worst part. &lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;Writing the testsuite&lt;/h3&gt;


&lt;p&gt;I was free to choose which testing framework I wanted to use. The tests in CouchDB use a kind of home grown testing framework, which does its job, but is not really the state of the art of Unit Testing. The API tests aren&amp;#8217;t supposed to run within the normal CouchDB testsuite anyway. That is the one you always run in Futon when you installed CouchDB. So it was possible to use something different &amp;#8230; like &lt;a href="http://jspec.info/"&gt;JSpec&lt;/a&gt;. (I have also &lt;a href="http://lenaherrmann.net/2010/01/04/jspec-javascript-unit-testing-how-it-should-be"&gt;blogged&lt;/a&gt; and &lt;a href="http://lenaherrmann.net/2010/03/22/jspec-slides-of-my-talk-at-berlin-js-usergroup"&gt;talked&lt;/a&gt; about it in the past.)&lt;/p&gt;


&lt;p&gt;How to run JSpec tests? If you are familiar with JSpec tests, you know you define your tests in Javascript files, reference those from a HTML file and open that in a browser. When you want your tests to perform calls to the database, this won&amp;#8217;t work - you get the error message &amp;#8220;Access to restricted URI denied&amp;#8221;. Shortly this is because AJAX only allows connections to the same resource it is called from. Therefore you have to run the tests from within the database. CouchDB has a folder &lt;a href="http://github.com/apache/couchdb/blob/trunk/share/www/"&gt;share/www&lt;/a&gt;, which gets linked to the &amp;#8220;/_utils/&amp;#8221; path of your local installation. In this folder you find Futon. That&amp;#8217;s where you have to put your tests and all the helper scripts. Then you can run the tests directly from the couch by browsing to &lt;a href="http://127.0.0.1:5984/_utils/spec/run.html"&gt;/_utils/spec/run.html&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Writing the testsuite was pretty straightforward. Along the way I found some bugs in the code (no surprise as it was written untested). One of the problems I had to solve I&amp;#8217;ve explained in another blogpost: &lt;a href="http://lenaherrmann.net/2010/04/29/security-in-couchdb-changing-the-authentication-db"&gt;How to overwrite the authentication_db so the tests don&amp;#8217;t interfere with the CouchDB setup.&lt;/a&gt;&lt;/p&gt;


&lt;p&gt;There&amp;#8217;s one thing I&amp;#8217;m not completely happy with: The tests do really need kind of long to run, up to 3 minutes, depends a lot on the browser and CPU load. The other CouchDB Javascript tests are a lot faster. I think the new ones are a bit more comprehensive. And I preferred a very clean setup to how it&amp;#8217;s done in the old tests: I have a database created before every single assertion and have it dropped afterwards. So you know exactly in which state your database is during the test. I guess that&amp;#8217;s worth the longer runtime.&lt;/p&gt;


&lt;p&gt;So. If you want to see a lot of JSpec code in action, and want to learn more about CouchDB, &lt;a href="http://github.com/lenalena/couchdb/tree/testsuite/share/www/spec/"&gt;have a look at the code&lt;/a&gt;. At the moment it&amp;#8217;s in my fork only, the community (someone - you?) has yet to integrate it into Futon. For all the details, read &lt;a href="http://mail-archives.apache.org/mod_mbox/couchdb-dev/201004.mbox/%3C4BB74086.4010302@zeromail.org%3E"&gt;my rundown and left TODOs&lt;/a&gt;.&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;Documentation&lt;/h3&gt; 


&lt;p&gt;I also wrote API Docs for all the methods. It &lt;a href="http://github.com/lenalena/couchdb-http-api-docs"&gt;can be found here&lt;/a&gt;. Later you will find it nicely formatted in the &lt;a href="http://wiki.apache.org/couchdb/Reference"&gt;CouchDB Wiki&lt;/a&gt; and &lt;a href="http://www.couch.io/docs"&gt;on the Couchio website&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;If you find anything is missing or you don&amp;#8217;t understand it, feel free to drop me a line. I&amp;#8217;m happy to clarify it as long as it&amp;#8217;s all freshly in my brain.&lt;/p&gt;


&lt;p style="font-size:0.9em;"&gt;PS - I consider example data in tests important, so I took all the data from the SciFi series &lt;a href="http://en.battlestarwiki.org/wiki/Portal:Battlestar_Galactica_%28RDM%29"&gt;Battlestar Galactica&lt;/a&gt;, currently on my playlist and I luv it. And it gave me an excuse to read in the Battlestar Wiki every now and then when I was looking for correct spelling :)&lt;/p&gt;
</description>
      <pubDate>Wed, 28 Apr 2010 12:07:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:e603b6cb-97da-442f-91a0-7f91f70fb52d</guid>
      <comments>http://lenaherrmann.net/2010/04/28/writing-a-testsuite-for-the-couchdb-api#comments</comments>
      <category>jquery</category>
      <category>jspec</category>
      <category>couchdb</category>
      <category>testing</category>
      <category>documentation</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=13</trackback:ping>
      <link>http://lenaherrmann.net/2010/04/28/writing-a-testsuite-for-the-couchdb-api</link>
    </item>
    <item>
      <title>JSpec: Slides of my talk at Berlin JS Usergroup</title>
      <description>&lt;p&gt;Last thursday I gave a talk at the monthly meeting of the &lt;a href="http://twitter.com/berlinjs"&gt;Berlin JS Usergroup&lt;/a&gt;. I was talking about &lt;a href="http://github.com/visionmedia/jspec"&gt;JSpec&lt;/a&gt;, a Javascript Unit Testing framework. Thanks to all who were there to listen and ask questions!&lt;/p&gt;


&lt;p&gt;Here are &lt;a href="http://www.slideshare.net/kilaulena/jspec-a-javascript-unit-testing-framework"&gt;the slides&lt;/a&gt;. When you have a look at them, make sure to also read the notes (next to the comments), so the slides make sense to you.
&lt;/p&gt;


&lt;p&gt;To find more information about JSpec, check out &lt;a href="http://github.com/visionmedia/jspec"&gt;the README on github&lt;/a&gt;, or read the &lt;a href="http://lenaherrmann.net/2010/01/04/jspec-javascript-unit-testing-how-it-should-be"&gt;blog post I wrote about it recently&lt;/a&gt;.&lt;p&gt;

&lt;p&gt;Thanks also to &lt;a href="http://twitter.com/gisikw"&gt;@gisikw&lt;/a&gt; and &lt;a href="http://twitter.com/tjholowaychuk"&gt;@tjholowaychuk&lt;/a&gt; for &lt;a href="http://slides.kevingisi.com/TestYourJavaScript/"&gt;this talk&lt;/a&gt; from which I borrowed a few grammar examples :)&lt;/p&gt;
</description>
      <pubDate>Mon, 22 Mar 2010 12:52:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:8e64ebfd-22ee-4b7b-885d-96a488801c87</guid>
      <comments>http://lenaherrmann.net/2010/03/22/jspec-slides-of-my-talk-at-berlin-js-usergroup#comments</comments>
      <category>jspec</category>
      <category>talk</category>
      <category>slides</category>
      <category>testing</category>
      <category>javascript</category>
      <category>berlinjs</category>
      <category>usergroup</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=12</trackback:ping>
      <link>http://lenaherrmann.net/2010/03/22/jspec-slides-of-my-talk-at-berlin-js-usergroup</link>
    </item>
    <item>
      <title>Refactoring asynchronous code</title>
      <description>&lt;p&gt;How to refactor a long chunk of asynchronous code is one thing I learned during my Javascript &amp; CouchDB project. It&amp;#8217;s not a difficult thing, but I thought it was before I figured out how to do it, so I guess it might be interesting for Javascript newbies.&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;Asynchronous code&lt;/h3&gt;


&lt;p&gt;In the first couple of weeks I struggled with this new style of coding before I got the hang of it. If you&amp;#8217;re fairly new to Javascript, you&amp;#8217;re probably used to just assign a variable in one line and use it in the next line. In the asynchronous world you query for a value and do stuff with it &lt;i&gt;in the callback function&lt;/i&gt; of the query.&lt;/p&gt;


&lt;p&gt;To illustrate - &amp;#8220;normal&amp;#8221; code looks like this:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="kw"&gt;var&lt;/span&gt; result = db.query(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;select * from T&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);
&lt;span class="c"&gt;// use result&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;Although writing web applications just screams for doing it like this instead:
  
&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;
db.query(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;select..&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="kw"&gt;function&lt;/span&gt; (result) {
&lt;span class="c"&gt;// use result&lt;/span&gt;
});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p style="font-size:0.8em"&gt;(Examples stolen from &lt;a href="http://nodejs.org/"&gt;Ryan Dahl&amp;#8217;s Presentation about Node.js&lt;/a&gt;.)&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;The Problem&lt;/h3&gt;


&lt;p&gt;&lt;img style="float:left;margin-right:10px;" width="250"  src="http://lenaherrmann.net/files/async-bad.png" alt="Bad asynchronous code" /&gt;&lt;/p&gt;


&lt;p&gt;I have a method that checks if I have to display a notification message. For this it makes a lot of requests to the database, the received data has to be compared with other data, there&amp;#8217;s lots of if/else clauses to consider all kinds of conditions and edge cases. It started simple, but then more and more conditions came in, unless I had the &lt;a href="http://geekandpoke.typepad.com/.a/6a00d8341d3df553ef0128766398f7970c-pi"&gt;much feared diagonal closing bracket line&lt;/a&gt; on my screen. &lt;/p&gt;


&lt;p&gt;You don&amp;#8217;t need to know the details, just look at the picture and you&amp;#8217;ll get the impression.&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;clear:left"&gt;Refactoring&lt;/h3&gt;


&lt;p&gt;By way of illustration I made up a much shorter example:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;
checkForConflicts: &lt;span class="kw"&gt;function&lt;/span&gt;(){
  &lt;span class="c"&gt;//set arguments&lt;/span&gt;
  openDoc(id, &lt;span class="kw"&gt;function&lt;/span&gt;(result){
    &lt;span class="c"&gt;// do stuff with arguments&lt;/span&gt;
    &lt;span class="kw"&gt;if&lt;/span&gt;(result.bar == whatever){
      &lt;span class="c"&gt;// do more stuff with result&lt;/span&gt;
      openAnotherDoc(id2, &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
        &lt;span class="kw"&gt;if&lt;/span&gt;(result2.baz == &lt;span class="kw"&gt;true&lt;/span&gt;){
          &lt;span class="c"&gt;// show notification message with result2&lt;/span&gt;
        }
      });
    }
  });
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;It is time to refactor after you are sure this actually is the way to go and the code basically works (&amp;#8220;make it work, then make it pretty&amp;#8221;). First you group the code into methods: You split the lines by what they do, and then you name each section. I think around 8 LOC is a good length for a method. &lt;/p&gt;


&lt;p&gt;If this was synchronous code, you could just call these methods after each other. But this doesn&amp;#8217;t work here, because &lt;i&gt;result&lt;/i&gt; and &lt;i&gt;result2&lt;/i&gt; are not available outside the function that retrieves them. &lt;/p&gt;


&lt;p&gt;My first approach was to call the functions from within each other. This works - but to see what &lt;i&gt;checkForConflicts&lt;/i&gt; does at its very heart, you have to scroll down through all the methods - not much gained:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;checkForConflicts: &lt;span class="kw"&gt;function&lt;/span&gt;(){
  &lt;span class="c"&gt;//set arguments&lt;/span&gt;
  doSomething(&lt;span class="lv"&gt;arguments&lt;/span&gt;);
}

doSomething(&lt;span class="lv"&gt;arguments&lt;/span&gt;){
  openDoc(id, &lt;span class="kw"&gt;function&lt;/span&gt;(result){
    &lt;span class="c"&gt;// do stuff with arguments&lt;/span&gt;
    &lt;span class="kw"&gt;if&lt;/span&gt;(result.bar = whatever){
      doSomethingElse(result);
    }
  });
}

doSomethingElse(result){
  &lt;span class="c"&gt;// do more stuff with result&lt;/span&gt;
  openAnotherDoc(id2, &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
    &lt;span class="kw"&gt;if&lt;/span&gt;(result2.baz == &lt;span class="kw"&gt;true&lt;/span&gt;){
      showMessage(result2);
    }
  });
}

showMessage = &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
  &lt;span class="c"&gt;// show notification message with result2&lt;/span&gt;
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;The way to do it is to use &lt;i&gt;callbacks&lt;/i&gt;. Each method gets an anonymous function as (additional) argument: in the method declaration this function has the name &lt;i&gt;callback&lt;/i&gt;. This callback is called if all the conditions within the method are met. The callback gets its arguments from within the method that calls it. You have to specify these arguments again in &lt;i&gt;checkForConflicts&lt;/i&gt; to have a valid function definition.

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;checkForConflicts: &lt;span class="kw"&gt;function&lt;/span&gt;(){
  &lt;span class="c"&gt;//set arguments&lt;/span&gt;
  doSomething(&lt;span class="lv"&gt;arguments&lt;/span&gt;, &lt;span class="kw"&gt;function&lt;/span&gt;(result){
    doSomethingElse(result, &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
      showMessage(result2); 
    });
  });
}

doSomething(&lt;span class="lv"&gt;arguments&lt;/span&gt;, callback){
  openDoc(id, &lt;span class="kw"&gt;function&lt;/span&gt;(result){
    &lt;span class="c"&gt;// do stuff with arguments&lt;/span&gt;
    &lt;span class="kw"&gt;if&lt;/span&gt;(result.bar = whatever){
      callback(result);
    }
  });
}

doSomethingElse(result, callback){
  &lt;span class="c"&gt;// do more stuff with result&lt;/span&gt;
  openAnotherDoc(id2, &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
    &lt;span class="kw"&gt;if&lt;/span&gt;(result2.baz == &lt;span class="kw"&gt;true&lt;/span&gt;){
      callback(result2);
    }
  });
}

showMessage = &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
  &lt;span class="c"&gt;// show notification message with result2&lt;/span&gt;
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;When you look at &lt;i&gt;checkForConflicts&lt;/i&gt;, you now immediately see what it does, without being bothered by the details. As a side effect, deciding which code needs access to which arguments helps you to understand your code better. In my case I was able to optimize the use of passed variables a lot.&lt;/p&gt;


&lt;p&gt;&lt;img style="float:left;margin-right:10px;" width="250"  src="http://lenaherrmann.net/files/async-good.png" alt="Refactored asynchronous code" /&gt;&lt;/p&gt;


&lt;p&gt;You might argue that the code is much longer now. First, longer is not worse if it is more readable. Second, this is only an example - if there was code instead of &lt;i&gt;//do stuff&lt;/i&gt; the few extra lines wouldn&amp;#8217;t carry weight.&lt;/p&gt;


&lt;p&gt;Finally here is a screenshot of my refactored code. All the methods don&amp;#8217;t fit on one screen, but you can see what &lt;i&gt;checkForConflicts&lt;/i&gt; does very quickly.&lt;/p&gt;


&lt;p style="clear:left"/&gt;
</description>
      <pubDate>Mon, 15 Feb 2010 16:17:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:8e9117d9-b37a-467b-99ac-b6f035ed1a9e</guid>
      <comments>http://lenaherrmann.net/2010/02/15/refactoring-asynchronous-code#comments</comments>
      <category>javascript</category>
      <category>refactoring</category>
      <category>ajax</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=11</trackback:ping>
      <link>http://lenaherrmann.net/2010/02/15/refactoring-asynchronous-code</link>
    </item>
    <item>
      <title>Testing PUT requests with Culerity</title>
      <description>&lt;p&gt;&lt;img style="float:left;margin-right:10px;" width="250"  src="/files/Image/Bildschirmfoto%202010-02-02%20um%2012.38.41.png" alt="All green!" /&gt;&lt;/p&gt;


&lt;p&gt;One major pain at testing with culerity so far was a HtmlUnit bug that causes a ScriptException with PUT AJAX requests in jQuery. When you tried to test an update you got a &amp;#8220;java.lang.IllegalArgumentException: The content cannot be null&amp;#8221;. Fortunately, this bug is fixed now. With latest HtmlUnit build culerity should run fine, and I tell you how.&lt;/p&gt;


&lt;p&gt;I assume you have culerity installed already. If not, follow &lt;a href="http://github.com/langalex/culerity"&gt;these instructions&lt;/a&gt; for testing Rails apps &lt;a href="http://upstre.am/2009/10/25/testing-couchapps-with-cucumber-and-culerity/"&gt;or these&lt;/a&gt; for testing Couchapps.&lt;/p&gt;


&lt;p&gt;1. Make sure you have up-to-date versions of celerity and culerity:&lt;br/&gt;
$ jruby -S gem install celerity #(gives you celerity-0.7.8)&lt;br/&gt;
$ gem install culerity &amp;#8211;source http://gemcutter.org #(gives you culerity-0.2.7)&lt;/p&gt;


&lt;p&gt;2. Get the latest HtmlUnit nightly build:&lt;br/&gt;
$ wget http://build.canoo.com/htmlunit/artifacts/htmlunit-2.7-SNAPSHOT-with-dependencies.zip&lt;br/&gt;
$ unzip htmlunit-2.7-SNAPSHOT-with-dependencies.zip &lt;br/&gt;
$ cd htmlunit-2.7-SNAPSHOT-with-dependencies/lib&lt;br/&gt;
These are the jar files you need to copy into your celerity gem.&lt;/p&gt;


&lt;p&gt;3. Find your celerity gem and copy the files:&lt;br/&gt;
$ jruby -S gem which celerity&lt;br/&gt;
$ cp * PATH_OF_JRUBY_INSTALLATION/lib/ruby/gems/1.8/gems/celerity-0.7.7/lib/celerity/htmlunit&lt;/p&gt;


&lt;p&gt;4. The new HtmlUnit needs JQuery 1.4, so you &lt;a href="http://docs.jquery.com/Downloading_jQuery#Current_Release"&gt;need to upgrade&lt;/a&gt; if you haven&amp;#8217;t done already. There&amp;#8217;s a &lt;a href="http://github.com/jquery/jquery-compat-1.3"&gt;backwards compatibility plugin for 1.3&lt;/a&gt; if this gives you trouble.
&lt;/p&gt;


&lt;p&gt;Have fun!&lt;/p&gt;
</description>
      <pubDate>Sun, 31 Jan 2010 21:22:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:40dcb9ab-9661-4888-bdb1-d9ac36fb7fc2</guid>
      <comments>http://lenaherrmann.net/2010/01/31/testing-put-requests-with-culerity#comments</comments>
      <category>testing</category>
      <category>culerity</category>
      <category>ajax</category>
      <category>htmlunit</category>
      <category>bug</category>
      <category>celerity</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=10</trackback:ping>
      <link>http://lenaherrmann.net/2010/01/31/testing-put-requests-with-culerity</link>
    </item>
    <item>
      <title>JSpec - JavaScript Unit testing how it should be</title>
      <description>&lt;p&gt;Selecting a unit test framework for a JavaScript-only application - not a task with an obvious answer! Coming from the Rails world, where most people work with either TestUnit or RSpec, I was a bit lost when I started researching. &lt;a href="http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#JavaScript"&gt;This table on wikipedia&lt;/a&gt; was quite helpful for a first overview. But: JsUnit, JSTest, jsUnitTest, jsUnity - these are actually different frameworks, and there&amp;#8217;s even many more &amp;#8230;&lt;/p&gt;


&lt;p&gt;I narrowed the selection down to those being currently developed, or are in use in &amp;#8220;big&amp;#8221; projects. &lt;a href="http://www.jsunit.net/"&gt;JsUnit&lt;/a&gt; seems to be one of the major players, but I found almost no documentation, and the code base hasn&amp;#8217;t changed a lot in the last years. That&amp;#8217;s not a negative thing in itself, but having an active community around is a good thing. &lt;a href="http://docs.jquery.com/QUnit"&gt;QUnit&lt;/a&gt; is used for testing JQuery, it looked like you could work with it as well, but its syntax and setup didn&amp;#8217;t make me go &amp;#8220;wow&amp;#8221; exactly. Same goes for &lt;a href="http://code.google.com/p/jqunit/"&gt;JQUnit&lt;/a&gt;, about which I had read good stuff in several blog posts. It&amp;#8217;s also what Sammy.js is tested with.&lt;/p&gt;


&lt;p&gt;I like RSpec, so when I had my first look at &lt;a href="http://github.com/nkallen/screw-unit"&gt;ScrewUnit&lt;/a&gt;, I was delighted to find there&amp;#8217;s BDD syntax for JavaScript too. ScrewUnit is also a part of &lt;a href="http://github.com/relevance/blue-ridge"&gt;Blue Ridge&lt;/a&gt;, which is sadly still only available as a Rails plugin. So, ScrewUnit looked nice.&lt;/p&gt;


&lt;p&gt;And THEN &amp;#8230; I found &lt;a href="http://visionmedia.github.com/jspec/"&gt;JSpec&lt;/a&gt;. I was thrilled! It&amp;#8217;s a JavaScript framework, but it doesn&amp;#8217;t look like JavaScript at all: no curly brackets, no semicolons, just plain domain language. It just looks like RSpec. Here is a random snippet from my test suite, just have a look:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;describe &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;NoteCollection&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
  before_each
    note3 = &lt;span class="kw"&gt;new&lt;/span&gt; Note({&lt;span class="ke"&gt;id&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;8c8&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;text&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;three&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;});
    note1 = &lt;span class="kw"&gt;new&lt;/span&gt; Note({&lt;span class="ke"&gt;id&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;ae9&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;text&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;one&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;next_id&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;107&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;});
    note2 = &lt;span class="kw"&gt;new&lt;/span&gt; Note({&lt;span class="ke"&gt;id&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;107&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;text&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;two&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;next_id&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;8c8&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;});
      
    notes = &lt;span class="kw"&gt;new&lt;/span&gt; NoteCollection([note3, note1, note2]);
  end
  
  describe &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;firstNote&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
    it &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;should return a note&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      notes.firstNote().should.be_an_instance_of Note
    end
    
    it &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;should return the first note&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      notes.firstNote().id.should.eql note1.id
    end

    it &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;should throw an error if there is more than one note that could be the first note&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      note4 = &lt;span class="kw"&gt;new&lt;/span&gt; Note({&lt;span class="ke"&gt;id&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;fff&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;rev&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;2-420&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;});
      notes.notes.push(note4);
      -{notes.firstNote()}.should.throw_error &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;More than one first note found&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
    end  
  end
end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;And that are just 3 out of over 45 core matchers. There are also matchers for JQuery, see the next example, where I put the DOM I wanted to test in a fixture called outline.html. In Rails I don&amp;#8217;t use fixtures, but if you want to test an application where all the markup is generated out of many different sources, it makes sense to test little pieces of the DOM in isolation. In ScrewUnit you have only one file for your all your mocked DOM.

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;describe &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;NoteElement&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
  before_each
    outline = elements(fixture(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;outline&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;))
    notes = outline.find(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;li&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;)
  
    parent_note = &lt;span class="kw"&gt;new&lt;/span&gt; NoteElement(&lt;span class="pd"&gt;$&lt;/span&gt;(notes.get(&lt;span class="i"&gt;0&lt;/span&gt;)))
    child_note = &lt;span class="kw"&gt;new&lt;/span&gt; NoteElement(&lt;span class="pd"&gt;$&lt;/span&gt;(notes.get(&lt;span class="i"&gt;1&lt;/span&gt;)))
  end
  
  describe &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;focusNextTextarea&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
    it &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;should focus the first child note if there is one&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      parent_note.focusNextTextarea();
      parent_note.noteLi().should.not.have_attr(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;data-focus&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)
      child_note.noteLi().attr(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;data-focus&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;).should.eql &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
    end
  end

  describe &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;insertUpdateNotePointers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;    
    it &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;should call setNextPointerToNewlyInsertedNote&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      inserted_note = {&lt;span class="ke"&gt;id&lt;/span&gt;:&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;4&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;text&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;inserted!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;};
      parent_note.should.receive(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;setNextPointerToNewlyInsertedNote&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;).with_args(inserted_note)
      parent_note.insertUpdateNotePointers(inserted_note);
    end
  end
end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;Note, if you don&amp;#8217;t like the RSpec-like Syntax, you can also skip the JSpec DSL and just use plain Javascript syntax.&lt;/p&gt;


&lt;p&gt;More JSpec goodness: You can either run the tests in the browser, or in the terminal, so you can include it in your CI. In the terminal you can specify the browsers it should be tested with: The browsers will be opened in the background, but you won&amp;#8217;t see your stuff being run like in Selenium. You can mock timers to test asynchronous calls. You can stub methods, mock AJAX requests, specify shared behaviours, hook into JSpec to specify your own matchers or do whatever else, and &amp;#8230; just &lt;a href="http://github.com/visionmedia/jspec/"&gt;see for yourself&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Installation is easy too: just add JSpec.js and JSpec.css to your application. If you need them, add the files for jquery, timers or xhr support too. In spec/index.html you include your application files and specify which tests you want to run. Then open it in the browser, wait a few milliseconds, and see the result. If you want to run JSpec tests in the terminal, just install the JSpec Ruby gem. For this you also have to install &lt;a href="http://www.mozilla.org/rhino/"&gt;Rhino&lt;/a&gt; (a Java-based JavaScript interpreter) - when you are on OS X, &lt;a href="http://peter.michaux.ca/articles/installing-rhino-on-os-x"&gt;this should get you started&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;What I don&amp;#8217;t like about JSpec &amp;#8230; the BlueRidge browser output is much more clear and pretty. I thought really hard about more things but that&amp;#8217;s it.&lt;/p&gt;


&lt;p&gt;Make sure to read the documentation on &lt;a href="http://jspec.info/"&gt;the JSpec website&lt;/a&gt; as well as the &lt;a href="http://github.com/visionmedia/jspec/"&gt;github Readme&lt;/a&gt;. Both ressources look similar, but at the time of writing there were different things on the two pages.&lt;/p&gt;
</description>
      <pubDate>Mon, 04 Jan 2010 12:07:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:c6222c7f-ab27-4e0b-a744-09dde4f35970</guid>
      <comments>http://lenaherrmann.net/2010/01/04/jspec-javascript-unit-testing-how-it-should-be#comments</comments>
      <category>javascript</category>
      <category>jspec</category>
      <category>jquery</category>
      <category>testing</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=9</trackback:ping>
      <link>http://lenaherrmann.net/2010/01/04/jspec-javascript-unit-testing-how-it-should-be</link>
    </item>
    <item>
      <title>Bulk deletion of documents in CouchDB</title>
      <description>&lt;p&gt;For the couchapp I'm writing, I recently wanted to do a bulk delete operation with CouchDB, but found very little about it on the web. I don't know if my approach is the best way to go, but well, it works - if you know a better way, I'm happy to learn about it.&lt;/p&gt;

&lt;p&gt;In the file &lt;a href="http://github.com/lenalena/couchdb/blob/trunk/share/www/script/jquery.couch.js"&gt;jquery.couch.js&lt;/a&gt; that comes with CouchDB there are a couple of convenience methods to help you dealing with the database, documents and views. There is a method for deletion too (removeDoc), but it only can handle one document at a time. In the style of bulkSave, I added a bulkRemove method:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;notextile&gt;&lt;span class="CodeRay"&gt;bulkRemove: &lt;span class="kw"&gt;function&lt;/span&gt;(docs, options){
  docs.docs = &lt;span class="pd"&gt;$&lt;/span&gt;.each(docs.docs, &lt;span class="kw"&gt;function&lt;/span&gt;(i, doc){doc._deleted = &lt;span class="kw"&gt;true&lt;/span&gt;});
  &lt;span class="pd"&gt;$&lt;/span&gt;.extend(options, {&lt;span class="ke"&gt;successStatus&lt;/span&gt;: &lt;span class="i"&gt;201&lt;/span&gt;});
  ajax({
      &lt;span class="ke"&gt;type&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;,
      &lt;span class="ke"&gt;contentType&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;application/json&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;,
      &lt;span class="ke"&gt;dataType&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;json&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, 
      &lt;span class="ke"&gt;data&lt;/span&gt;: toJSON(docs),
      &lt;span class="ke"&gt;url&lt;/span&gt;: &lt;span class="lv"&gt;this&lt;/span&gt;.uri + &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;_bulk_docs&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + encodeOptions(options)
    },
    options,
    &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;The documents could not be deleted&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
  );
},&lt;/span&gt;&lt;/notextile&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;In here I add the attribute "_deleted = true" to every document I want to delete, and then I do a bulk update with the documents. That's it.&lt;/p&gt;

&lt;p&gt;This is one possible use case: In the delete action of my controller I first fetch an array with the post and its comments from the couch (how to do this see below), and then I call the bulkRemove action on that array.

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;notextile&gt;&lt;span class="CodeRay"&gt;couchapp.design.view(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;post_with_comments&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;, {
  &lt;span class="ke"&gt;startkey&lt;/span&gt;: [params[&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;]],
  &lt;span class="ke"&gt;endkey&lt;/span&gt;: [params[&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;], {}],
  &lt;span class="ke"&gt;success&lt;/span&gt;: &lt;span class="kw"&gt;function&lt;/span&gt;(json) {
    &lt;span class="kw"&gt;if&lt;/span&gt; (json.rows.length &amp;gt; &lt;span class="i"&gt;0&lt;/span&gt;) { 
      &lt;span class="kw"&gt;var&lt;/span&gt; post_and_comments = json.rows.map(&lt;span class="kw"&gt;function&lt;/span&gt;(row) {&lt;span class="kw"&gt;return&lt;/span&gt; row.value}); 
      couchapp.db.bulkRemove({&lt;span class="ke"&gt;docs&lt;/span&gt;: post_and_comments}, {
        &lt;span class="ke"&gt;success&lt;/span&gt;: &lt;span class="kw"&gt;function&lt;/span&gt;() {
          flash = {&lt;span class="ke"&gt;message&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;Post deleted.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;type&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;notice&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;}
          redirect(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;#/outlines&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;, flash);
        },
        &lt;span class="ke"&gt;error&lt;/span&gt;: &lt;span class="kw"&gt;function&lt;/span&gt;(response_code, msg) {
          flash = {&lt;span class="ke"&gt;message&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;Error deleting post: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt; + msg, &lt;span class="ke"&gt;type&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;};
        }
      });
    }      
  }
});&lt;/span&gt;&lt;/notextile&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;Note that the "flash" object I'm using is my own Sammy.js implementation of a Rails-like flash to show a message after a redirect. Maybe I'll share that in another blog post.&lt;/p&gt;

&lt;p&gt;As a bonus, this is the view I'm using to retrieve a post and its comments in one request. I'm following the pattern Christopher Lenz is recommending in his article on &lt;a href="http://www.cmlenz.net/archives/2007/10/couchdb-joins"&gt;CouchDB Joins&lt;/a&gt;.

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;notextile&gt;&lt;span class="CodeRay"&gt;&lt;span class="kw"&gt;function&lt;/span&gt;(doc) {
  &lt;span class="kw"&gt;if&lt;/span&gt; (doc.type == &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;Post&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;) {
    emit([doc._id, &lt;span class="i"&gt;0&lt;/span&gt;], doc);
  } &lt;span class="kw"&gt;else&lt;/span&gt; &lt;span class="kw"&gt;if&lt;/span&gt; (doc.type == &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;Comment&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;) {
    emit([doc.post_id, &lt;span class="i"&gt;1&lt;/span&gt;, Date.parse(doc.created_at)], doc);
  }
}&lt;/span&gt;&lt;/notextile&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;When you call this view with

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;notextile&gt;&lt;span class="CodeRay"&gt;startkey: [params[&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;]],
&lt;span class="ke"&gt;endkey&lt;/span&gt;: [params[&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;], {}]&lt;/span&gt;&lt;/notextile&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;you get an array with the post as the first element, and then all the comments that have the post's ID as post_id.&lt;/p&gt;

&lt;p&gt;This can of course also be used for things other than deleting: to show all the comments for a post, I get the array's first element and extract the title and content. Then I remove it from the array with

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;notextile&gt;&lt;span class="CodeRay"&gt;json.rows.splice(&lt;span class="i"&gt;0&lt;/span&gt;,&lt;span class="i"&gt;1&lt;/span&gt;);        &lt;/span&gt;&lt;/notextile&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;You now have an array with only the post's comments and you can display them somehow.&lt;/p&gt;

</description>
      <pubDate>Tue, 22 Dec 2009 17:37:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:150befc7-1e89-47d5-ae0c-47d803336e73</guid>
      <comments>http://lenaherrmann.net/2009/12/22/bulk-deletion-of-documents-in-couchdb#comments</comments>
      <category>couchdb</category>
      <category>javascript</category>
      <category>json</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=8</trackback:ping>
      <link>http://lenaherrmann.net/2009/12/22/bulk-deletion-of-documents-in-couchdb</link>
    </item>
    <item>
      <title>Testing key events with Culerity</title>
      <description>&lt;p&gt;The application I'm writing depends heavily on key events. When you press enter in a specific text field, some other field opens, when you press tab, the text fields moves, etc. Of course I need to cover this in my cucumber integration tests. The &lt;a href="http://celerity.rubyforge.org/yard/Celerity/Element.html#fire_event-instance_method"&gt;Celerity API&lt;/a&gt; is a bit thin for this purpose: You can fire a, let's say, keydown event - but it doesn't pass through the specific key code.&lt;/p&gt;


&lt;p&gt;Actually it's easy when you know how. You can execute any javascript directly on the Culerity RemoteBrowserProxy object. In order to fire a specific key event, you have to create a KeyboardEvent, initialize it with whatever you want, and dispatch it on the element.&lt;/p&gt;


&lt;p&gt;Read up the details, especially the meaning of initKeyEvent's parameters &lt;a href="http://help.dottoro.com/ljbwbehw.php"&gt;in this Javascript API&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;I ended up with this cucumber step:&lt;/p&gt;


&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="co"&gt;When&lt;/span&gt; &lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="k"&gt;^I hit &amp;quot;([^&lt;/span&gt;&lt;span class="ch"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;]*)&amp;quot; in a text_field with id &amp;quot;([^&lt;/span&gt;&lt;span class="ch"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;]*)&amp;quot;$&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt; &lt;span class="r"&gt;do&lt;/span&gt; |key, id|
  key_code = &lt;span class="r"&gt;case&lt;/span&gt; key
    &lt;span class="r"&gt;when&lt;/span&gt; &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;enter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      &lt;span class="i"&gt;13&lt;/span&gt;
    &lt;span class="r"&gt;when&lt;/span&gt; &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;up&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      &lt;span class="i"&gt;38&lt;/span&gt;
    &lt;span class="r"&gt;when&lt;/span&gt; &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;down&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      &lt;span class="i"&gt;40&lt;/span&gt;
    &lt;span class="r"&gt;else&lt;/span&gt;
      &lt;span class="i"&gt;0&lt;/span&gt;
    &lt;span class="r"&gt;end&lt;/span&gt;
  &lt;span class="gv"&gt;$browser&lt;/span&gt;.execute_script(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;event = document.createEvent(&amp;quot;KeyboardEvent&amp;quot;);&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;)
  &lt;span class="gv"&gt;$browser&lt;/span&gt;.execute_script(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;event.initKeyEvent(&amp;quot;keydown&amp;quot;, true, false, document.window, false, false, false, false, &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt; + key_code.to_s + &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;, 0)&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;)
  &lt;span class="gv"&gt;$browser&lt;/span&gt;.execute_script(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;document.getElementById('&lt;/span&gt;&lt;span class="il"&gt;&lt;span class="idl"&gt;#{&lt;/span&gt;id&lt;span class="idl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="k"&gt;').dispatchEvent(event)&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)
  &lt;span class="co"&gt;When&lt;/span&gt; &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;I wait for the AJAX call to finish&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Note that this is meant to work for Firefox only. For IE you can do a similar thing with createEventObject, &lt;a href="http://help.dottoro.com/ljhlvomw.php"&gt;see here how&lt;/a&gt;.&lt;/p&gt;
</description>
      <pubDate>Sun, 22 Nov 2009 20:43:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:0bb79202-702f-46ab-a4c1-4a9aecaf11b8</guid>
      <comments>http://lenaherrmann.net/2009/11/22/testing-key-events-with-culerity#comments</comments>
      <category>cucumber</category>
      <category>culerity</category>
      <category>celerity</category>
      <category>testing</category>
      <category>javascript</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=7</trackback:ping>
      <link>http://lenaherrmann.net/2009/11/22/testing-key-events-with-culerity</link>
    </item>
  </channel>
</rss>
