In order to dummy up a test in node.js, I need data to populate a testing CouchDB database. Specifically, I am testing some code that creates statistics plots (in R) and then saves them to a doc as attachments. So for my tests, I need at least one document with its PNG attachments already in place.
I couldn’t find a simple “howto” for this on the Internet, so here’s a note to my future self.
First of all, the CouchDB docs are great, and curl is your friend. Curl lets you set the headers. In this case, I don’t want HTML to come back, I want a valid JSON document, so (in typical belt-and-suspenders style) I specify both the content type and the accept header parameters to be application/json
as follows:
curl -H 'Content-Type: application/json' \ -H 'Accept: application/json' \ 127.0.0.1:5984/my%2freal%2fdatabase/801447?attachments=true> 801447.json
The returned document has encoded the binary PNG files as JSON fields, in accordance with the CouchDB specs:
{"_id":"801447","_rev":"55-8e15623f21dce9ed556cfe96b9c85a8e", "2012":{"properties":[ {"name":"SERFAS CLUB", "cal_pm":"R3.688", "abs_pm":40.920000000000001705, "latitude_4269":"33.880712", "longitude_4269":"-117.613596", "lanes":1, "segment_length":"0.316", "freeway":91, "direction":"E", "vdstype":"ML", "district":8, "versions":["2012-12-04","2012-12-12"], "geojson":{"type":"Point", "crs":{"type":"name", "properties":{"name":"EPSG:4326"}}, "coordinates":[-117.62000000000000455, 33.881000000000000227]} } ]}, "_attachments":{ "801447_2012_raw_004.png":{ "content_type":"image/png", "revpos":53,"digest":"md5-tF2vnhvNw7pLHlK31DVNUw==", "data":"iVBORw0KGgoAAAANSUhEUgAABkAAAAGQCAIAAAB59ztRAAAgAElEQVR4 nOzdeYAUxd038Opr7tmbXVYRXEQEOeSSy3greK1sIJqIRImaRONLPBJDTFBRDGp4Dl GjicYj4oEJyimsyHItyiWPoCAYjQQQuZZll71m5+r3jwrtOEd1z0xP9TDz/fzDzNBb v6ruquqemupqQVVVAgAAAAAAAAAAkK1EqzMAAAAAAAAAAADAggEsAAAAAAAAAADIah jAAgAAAAAAAACArIYBLAAAAAAAAAAAyGoYwAIAAAAAAAAAgKyGASwAAAAAAAAAAMhq GMACAAAAAAAAAICshgEsAAAAAAAAAADIahjAAgAAAAAAAACArIYBLAAAAAAAAAAAyG oYwAIAAAAAAAAAgKyGASwAAAAAAAAAAMhqGMACAAAAAAAAAICshgEsAAAAAAAAAADI ahjAAgAAAAAAAACArIYBLAAAAAAAAAAAyGoYwAIAAAAAAAAAgKyGASwAAAAAAAAAAM hqGMACAAAAAAAAAICshgEsAAA ..."
Lovely binary-to-hex, looking good.
To verify that the returned document is actually valid json, I use the command line some more (and I’m not sure which Linux library installed this, but there are several JSON pretty printers and verifiers out there):
james@emma files[bug/fixplots]$ json_verify< 801451.json JSON is valid
Then to use the document in my test, all I have to do is read it in and send it off:
function put_json_file(file,couchurl,cb){ var db_dump = require(file) // in node you can require json too! superagent.post(couchurl) .type('json') .send(db_dump) .end(function(e,r){ should.not.exist(e) should.exist(r) return cb(e) }) return null }
To see that in action, I put my various CouchDB-related utilities in a file here, and then my actual test has a before
job that creates the CouchDB database and populates it, and a corresponding after
task that deletes the temporary database.