Home Page of Ron Star

Minitest and CouchDB

I ran into a problem recently where I needed to reset the DB when I ran my tests to avoid duplicate inserts. There are plenty of tools out there for other databases including MongoDB. But nothing for CouchDB using CouchRestModel. But it’s easy enough to just build something. After all, it’s just a DELETE/PUT http call to the db server.

In my test_helper.rb I added this:

def reset_test_db!
  puts "\n*** Reset Database\n\n"

  @host = 'localhost'
  @port = '5984'
  @path = "/[mydb]_test"

  # Delete database
  request = Net::HTTP::Delete.new(@path)
  response = Net::HTTP.new(@host, @port).start {|http| http.request(request) }

  # Create database
  request = Net::HTTP::Put.new(@path)
  response = Net::HTTP.new(@host, @port).start {|http| http.request(request) }
end

reset_test_db!

That’s all I needed.

Yeah, I know, I need to abstract out the database name. I’ll figure that out the next time.