How do I create a persistent table in a Rails app's test database?
I have a rails app that references the TMY3 meteorological dataset, which is a ton of different hourly weather observations for over 1000 different sites.
I downloaded the entire dataset and sucked the columns I need into a local table that is indexed by site, which makes it nice and fast in dev and in production.
However, the indexing takes a good 5 minutes, which makes it a HUGE pain to load in every time for tests. This table开发者_如何学编程 never changes, so I'd like to just load it once into my testing environment and then leave it there to run the rest of my tests against. All my other models and tables are standard rails dealies that get factoried or mocked as needed.
Any ideas?
For the model that you need to not get torn down at test time, put it in a separate database for testing purposes. Only the test database that is loaded from database.yml is cleared and rebuilt from the development database's schema.
class PersistModel < ActiveRecord::Base
PersistModel.establish_connection(
:adapter => "sqlite3",
:database => "db/persist.sqlite3"
)
end
This class will never have it's database cleared out.
You could probably get creative and have a test version of your real model that establishes a different connection.
class WeatherTest < Weather
WeatherTest.establish_connection(
:adapter => "sqlite3",
:database => "db/weather.sqlite3"
)
end
That way all of your functions from the "real" model are available to your test version.
You might see if this question about db:test:prepare applies to your situation.
Haven't tried it myself but could one of the caching plugins offer a solution ? Advanced Caching - from the Rails Guides
精彩评论