instagram

RSpec Testing Rails With Elasticsearch

When your app is growing and you need to implement more complex queries, you will try to put load away from your database. Over at gigmit, we saw really long database times (~2 seconds) for a rather complex but often needed query. So I searched for an appropriate solution. Normally I try to avoid adding a new service to our infrastructure because it needs more administrative and monitoring work, so I checked out Postgresql’s view and materialized view functionality but wasn’t really satisfied. Also our already in used Redis server seemed to be too much to code to get what we needed. I know Elasticsearch for quite some time now and luckily there is a relatively new gem on the block which is called elasticsearch-rails. Its the official supported gem and the successor of the tire gem which isn’t maintained anymore. The development of the new gem is very active (they are doing a real persistence layer right now to use Elasticsearch as a full database replacement) and you should check it out and get involved, altough they are still far from a 1.0 release and there are still some rough parts. For me it was how we could integrate the Elasticsearch part in our testing infrastructure. So I asked them and got a few good responses. There are also some more advanced solutions mentioned like spinning up a test cluster, but we wanted to keep it simple in the beginning. This post covers how we did it for gigmit.

First, be sure to add the Rails environment to every index name in your models to avoid killing your dev data when testing.

class MyModel < ActiveRecord::Base
  include Elasticsearch::Model
  index_name ['gigmit', Rails.env, self.base_class.to_s.pluralize.underscore].join('_')
end

Next add this to your spec_helper.rb:

RSpec.configure do |config|
  # ... your code ...
  config.before(:each) do
    [MyModel, MyOtherModel].each do |model|
      model.__elasticsearch__.create_index!(force: true)
    end
  end
end

It means it will recreate all specified indexes and clean them all beforr every test. It’s rather fast and we use Elasticsearch mostly everywhere now, so this is sufficent for us. But if you want to use this only for specific tests you could do

RSpec.configure do |config|
  # ... your code ...
  config.before(:each, elasticsearch: true) do
    [MyModel, MyOtherModel].each do |model|
      model.__elasticsearch__.create_index!(force: true)
    end
  end
end

and let the tests where you need it run like this:

it 'should search with es', :elasticsearch do
  # your test code
end

Thats it. You now have a working setup for using Elasticsearch while testing with RSpec. Just be sure to have a running Elasticsearch node on your machine.

instagram

Test Driven Focaccia Development

Test Driven Focaccia Development

Teig mit Hefe war nie so meins. Zu viele Parameter in der Gleichung, um ein erfolgreiches Produkt zu bekommen. Eigentlich sollte es nur Pizza werden, aber das wurde dann verschoben und irgendwann musste der Hefewürfel weg.

Also mixe ich eine halbe Packung Mehl mit etwas Salz in einer großen Schüssel. In einer kleinen Schüssel zerbrösel ich einen halben Würfel frischer Hefe und gebe einen halben Teelöffel Honig, einen Teelöffel Olivenöl und 100ml lauwarmes Wasser dazu und verrüre es ordentlich. Das lass ich dann kurz stehen, um der Hefe einen guten Start zu ermöglichen. Dann einfach das Gebräu in das Mehl kippen und anfangen zu kneten. So lang lauwarmes Wasser dazugeben bis ein schöner Teig entsteht. Bei den aktuellen Temperaturen ist das Aufgehen auch noch um eingies einfacher: Man kann den Teig einfach hirnlos irgendwo mit einem Küchentuch abgedeckt stehen lassen. Nachdem der Teig sich in seiner Größe verdoppelt hat, ihn nochmal ordentlich durchkneten, in Form bringen, in die Form legen, mit den Fingern ein paar Mulden reindrücken und mit reichlich Olivenöl beträufeln. Für das finale Topping bediente ich mich von allem was da war, um eine breite Testabdeckung zu ermöglichen. Von links nach rechts: echter Schwarzkümmel, schwarze spanische Oliven, mexikanische Chia, Oregano.

Nächste geplante Testruns beinhalten verschieden Mehlsorten und Mischungen und andere Gewürze (gerade total angesagt bei mir: Zatar)

instagram