Sha256: 997c9809784085f56b6082af8a8e5b75aacde34aa58d69918eae5ef78e6bf8e8

Contents?: true

Size: 1.34 KB

Versions: 5

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

# This class provides helper methods for testing.
module Cassie::Testing
  extend ActiveSupport::Concern

  included do
    prepend OverrideMethods
  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!
      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)
          end
        end
        Thread.current[:cassie_inserted] = nil
      end
    end
  end

  module OverrideMethods
    def insert(table, *args)
      Thread.current[:cassie_inserted] ||= Set.new
      Thread.current[:cassie_inserted] << table
      super(table, *args)
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
whi-cassie-1.3.4 lib/cassie/testing.rb
whi-cassie-1.3.2 lib/cassie/testing.rb
whi-cassie-1.3.1 lib/cassie/testing.rb
whi-cassie-1.3.0 lib/cassie/testing.rb
whi-cassie-1.2.0 lib/cassie/testing.rb