Module: Cassie::Testing

Extended by:
ActiveSupport::Concern
Defined in:
lib/cassie/testing.rb

Overview

This class provides helper methods for testing.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cleanup!Object

Wrap test cases as a block in this method. After the test case finishes, all tables that had data inserted into them will be truncated so that the data state will be clean for the next test case.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/cassie/testing.rb', line 23

def cleanup!
  begin
    yield
  ensure
    if Thread.current[:cassie_inserted].present?
      Cassie.instance.batch do
        Thread.current[:cassie_inserted].each do |table|
          keyspace, table = table.split('.', 2)
          schema = Cassie::Schema.find(keyspace)
          schema.truncate!(table) if schema
        end
      end
      Thread.current[:cassie_inserted] = nil
    end
  end
end

.prepare!Object

Prepare the test environment. This method must be called before running the test suite.



11
12
13
14
15
16
17
18
# File 'lib/cassie/testing.rb', line 11

def prepare!
  Cassie.send(:include, Cassie::Testing) unless Cassie.include?(Cassie::Testing)
  Cassie::Schema.all.each do |schema|
    schema.tables.each do |table|
      schema.truncate!(table)
    end
  end
end

Instance Method Details

#insert_with_testing(table, *args) ⇒ Object



41
42
43
44
45
# File 'lib/cassie/testing.rb', line 41

def insert_with_testing(table, *args)
  Thread.current[:cassie_inserted] ||= Set.new
  Thread.current[:cassie_inserted] << table
  insert_without_testing(table, *args)
end