Class: Cequel::SpecSupport::Preparation
- Inherits:
-
Object
- Object
- Cequel::SpecSupport::Preparation
- Defined in:
- lib/cequel/spec_support/preparation.rb
Overview
Provide database preparation behavior that is useful for spec/test suites.
For Rails apps adding the following code to the bottom of one’s ‘spec_helper.rb` (below the `RSpec.configure` block) ensures a clean and fully synced test db before each test run.
# one time database setup
Cequel::SpecSupport::Preparation.setup_database
For non-rails apps adding the following code to the bottom of one’s ‘spec_helper.rb` (below the `RSpec.configure` block) ensures a clean and fully synced test db before each test run.
# one time database setup
Cequel::SpecSupport::Preparation
.setup_database(App.root + "lib/models",
App.root + "lib/other-models")
Class Method Summary collapse
-
.setup_database(*model_dirs) ⇒ void
Provision and sync the database for a spec run.
Instance Method Summary collapse
-
#create_keyspace ⇒ Preparation
Ensure that the necessary keyspace exists.
-
#drop_keyspace ⇒ Preparation
Ensure the current keyspace does not exist.
-
#initialize(model_dirs = [], options = {}) ⇒ Preparation
constructor
A new instance of Preparation.
-
#sync_schema ⇒ Preparation
Ensure that the necessary column families exist and match the models.
Constructor Details
#initialize(model_dirs = [], options = {}) ⇒ Preparation
Returns a new instance of Preparation.
48 49 50 |
# File 'lib/cequel/spec_support/preparation.rb', line 48 def initialize(model_dirs = [], = {}) @model_dirs, @options = model_dirs, end |
Class Method Details
.setup_database(*model_dirs) ⇒ void
This method returns an undefined value.
Provision and sync the database for a spec run.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/cequel/spec_support/preparation.rb', line 32 def self.setup_database(*model_dirs) = model_dirs. model_dirs = if model_dirs.any? then model_dirs.flatten elsif defined? Rails then [Rails.root + "app/models"] else [] end preparation = new(model_dirs, ) preparation.drop_keyspace preparation.create_keyspace preparation.sync_schema end |
Instance Method Details
#create_keyspace ⇒ Preparation
Ensure that the necessary keyspace exists.
70 71 72 73 74 75 76 |
# File 'lib/cequel/spec_support/preparation.rb', line 70 def create_keyspace keyspace = Cequel::Record.connection.schema keyspace.create! unless keyspace.exists? self end |
#drop_keyspace ⇒ Preparation
Ensure the current keyspace does not exist.
57 58 59 60 61 62 63 |
# File 'lib/cequel/spec_support/preparation.rb', line 57 def drop_keyspace keyspace = Cequel::Record.connection.schema keyspace.drop! if keyspace.exists? self end |
#sync_schema ⇒ Preparation
Ensure that the necessary column families exist and match the models.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/cequel/spec_support/preparation.rb', line 84 def sync_schema record_classes.each do |record_class| begin record_class.synchronize_schema unless [:quiet] puts "Synchronized schema for #{record_class.name}" end rescue Record::MissingTableNameError # It is obviously not a real record class if it doesn't have a # table name. unless [:quiet] STDERR.puts "Skipping anonymous record class without an " \ "explicit table name" end end end self end |