Class: Cequel::SpecSupport::Preparation

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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 = [], options = {})
  @model_dirs, @options = model_dirs, options
end

Class Method Details

.setup_database(*model_dirs) ⇒ void

This method returns an undefined value.

Provision and sync the database for a spec run.

Parameters:

  • model_dirs (Array<String,Pathname>)

    directories in which Cequel record classes reside. All files in these directories will be loaded before syncing the schema. Default: ‘Rails.root + “app/model”` if `Rails` is defined; otherwise no models will be autoloaded.



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)
  options = model_dirs.extract_options!

  model_dirs =
    if model_dirs.any? then model_dirs.flatten
    elsif defined? Rails then [Rails.root + "app/models"]
    else []
    end

  preparation = new(model_dirs, options)

  preparation.drop_keyspace
  preparation.create_keyspace
  preparation.sync_schema
end

Instance Method Details

#create_keyspacePreparation

Ensure that the necessary keyspace exists.

Returns:



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_keyspacePreparation

Ensure the current keyspace does not exist.

Returns:



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_schemaPreparation

Ensure that the necessary column families exist and match the models.

Returns:



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 options[: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 options[:quiet]
        STDERR.puts "Skipping anonymous record class without an " \
                    "explicit table name"
      end
    end
  end

  self
end