<commit_before>require 'chronologic'
require 'webmock/rspec'
require 'cassandra/mock'

module ChronologicHelpers

  def simple_event
    Chronologic::Event.new.tap do |event|
      event.key = "checkin_1"
      event.timestamp = Time.now.utc
      event.data = {"type" => "checkin", "message" => "I'm here!"}
      event.objects = {"user" => "user_1", "spot" => "spot_1"}
      event.timelines = ["user_1", "spot_1"]
    end
  end

  def nested_event
    Chronologic::Event.new.tap do |event|
      event.key = "comment_1"
      event.timestamp = Time.now.utc
      event.data = {"type" => "comment", "message" => "Me too!", "parent" => "checkin_1"}
      event.objects = {"user" => "user_2", "checkin" => "checkin_1"}
      event.timelines = ["checkin_1"]
    end
  end

  def populate_timeline
    jp = {"name" => "<NAME>"}
    protocol.record("spot_1", jp)

    uuids = []
    %w{sco jc am pb mt rm ak ad rs bf}.each_with_index do |u, i|
      record = {"name" => u}
      key = "user_#{i}"
      protocol.record(key, record)

      protocol.subscribe("user_1_home", "user_#{i}")

      event = simple_event
      event.key = "checkin_#{i}"
      event.objects["user"] = key
      event.timelines = [key, "spot_1"]
      uuids << protocol.publish(event)
    end

    return uuids
  end

  # Cassandra#truncate isn't reliable against cassandra-0.7.4, but
  # this gets the job done. It's a hack, so look for a better way to do this
  # every now and then
  def clean_up_keyspace!(conn)
    conn.schema.cf_defs.each do |cf|
      conn.send(:each_key, cf.name) do |k|
        conn.remove(cf.name, k)
      end
    end
  end

end

RSpec.configure do |config|
  config.include(ChronologicHelpers)
  config.include(WebMock::API)

  config.before do
    if ENV['CASSANDRA']
      Chronologic::Schema.write_opts = {
        :consistency => Cassandra::Consistency::ONE
      }
      Chronologic.connection = Cassandra.new('ChronologicTest')
      clean_up_keyspace!(Chronologic.connection)
    else
      schema = {
        'Chronologic' => {
          'Object' => {},
          'Subscription' => {},
          'Event' => {},
          'Timeline' => {}
        }
      }
      Chronologic.connection = Cassandra::Mock.new('Chronologic', schema)
    end
  end

  config.before do
    WebMock.disable_net_connect!
    WebMock.reset!
  end

end
<commit_msg>Attempt to make running tests against a real Cassandra node more reliable.<commit_after>require 'chronologic'
require 'webmock/rspec'
require 'cassandra/mock'

module ChronologicHelpers

  def simple_event
    Chronologic::Event.new.tap do |event|
      event.key = "checkin_1"
      event.timestamp = Time.now.utc
      event.data = {"type" => "checkin", "message" => "I'm here!"}
      event.objects = {"user" => "user_1", "spot" => "spot_1"}
      event.timelines = ["user_1", "spot_1"]
    end
  end

  def nested_event
    Chronologic::Event.new.tap do |event|
      event.key = "comment_1"
      event.timestamp = Time.now.utc
      event.data = {"type" => "comment", "message" => "Me too!", "parent" => "checkin_1"}
      event.objects = {"user" => "user_2", "checkin" => "checkin_1"}
      event.timelines = ["checkin_1"]
    end
  end

  def populate_timeline
    jp = {"name" => "<NAME>"}
    protocol.record("spot_1", jp)

    uuids = []
    %w{sco jc am pb mt rm ak ad rs bf}.each_with_index do |u, i|
      record = {"name" => u}
      key = "user_#{i}"
      protocol.record(key, record)

      protocol.subscribe("user_1_home", "user_#{i}")

      event = simple_event
      event.key = "checkin_#{i}"
      event.objects["user"] = key
      event.timelines = [key, "spot_1"]
      uuids << protocol.publish(event)
    end

    return uuids
  end

  # Cassandra#truncate isn't reliable against cassandra-0.7.4, but
  # this gets the job done. It's a hack, so look for a better way to do this
  # every now and then
  def clean_up_keyspace!(conn)
    conn.schema.cf_defs.each do |cf|
      conn.send(:each_key, cf.name) do |k|
        conn.remove(cf.name, k)
      end
    end
  end

end

RSpec.configure do |config|
  config.include(ChronologicHelpers)
  config.include(WebMock::API)

  config.before do
    if ENV['CASSANDRA']
      Chronologic::Service::Schema.write_opts = {
        :consistency => Cassandra::Consistency::ONE
      }
      Chronologic.connection = Cassandra.new(
        'ChronologicTest',
        ['127.0.0.1:9160'],
        :connection_timeout => 3,
        :retries => 2,
        :timeout => 3
      )
      clean_up_keyspace!(Chronologic.connection)
    else
      schema = {
        'Chronologic' => {
          'Object' => {},
          'Subscription' => {},
          'Event' => {},
          'Timeline' => {}
        }
      }
      Chronologic.connection = Cassandra::Mock.new('Chronologic', schema)
    end
  end

  config.before do
    WebMock.disable_net_connect!
    WebMock.reset!
  end

end

{'fraction_non_alphanumeric': 0.1427402862985685, 'fraction_numerical': 0.009202453987730062, 'mean_word_length': 2.6069321533923304, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5858673', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1681, 'n_words': 377}