Sha256: c6ddb9f2cb128b2328a576a8de443f6d0b4b3989199896e28cfa1faea023cc73

Contents?: true

Size: 1.38 KB

Versions: 9

Compression:

Stored size: 1.38 KB

Contents

# This class provides helper methods for testing.
module Cassie::Testing
  extend ActiveSupport::Concern
  
  included do
    alias_method_chain :insert, :testing
  end
  
  class << self
    # Prepare the test environment. This method must be called before running the test suite.
    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
    
    # 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.
    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
  end
  
  def insert_with_testing(table, *args)
    Thread.current[:cassie_inserted] ||= Set.new
    Thread.current[:cassie_inserted] << table
    insert_without_testing(table, *args)
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
whi-cassie-1.1.0 lib/cassie/testing.rb
whi-cassie-1.0.7 lib/cassie/testing.rb
whi-cassie-1.0.6 lib/cassie/testing.rb
whi-cassie-1.0.5 lib/cassie/testing.rb
whi-cassie-1.0.4 lib/cassie/testing.rb
whi-cassie-1.0.3 lib/cassie/testing.rb
whi-cassie-1.0.2 lib/cassie/testing.rb
whi-cassie-1.0.1 lib/cassie/testing.rb
whi-cassie-1.0.0 lib/cassie/testing.rb