cassandra_cleaner 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d447cc067301e38bd4fd54d0d9be23806e069428
4
+ data.tar.gz: 7570639ddb01f7288681410f7663e2b9812a29a0
5
+ SHA512:
6
+ metadata.gz: 4df431d18048cbf39bcfe6b8db6188bf3732710aa8739c43069bf22ff850ddf20b3c919865d6f62a8db48d3fed409dcee82d30c516ea4b6cbc834b4f2601f661
7
+ data.tar.gz: 751b95f3efc685be6f524c606265bf8b7d18c3c0d18b5e0c011d19c7f652da0a3e1aa8092932f7c4fcfda1f04ca93270c697a0c1eed00bcfc65d922d51bde0d6
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ -f d
2
+ --colour
data/Gemfile ADDED
@@ -0,0 +1,18 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cassandra_cleaner.gemspec
4
+ gemspec
5
+
6
+ gem 'cql-rb', '~> 2.0.0'
7
+
8
+ group :test do
9
+ gem 'rspec', '>= 3.0'
10
+ end
11
+
12
+ group :development do
13
+ gem "guard-rspec", "~> 4.4.2", require: false
14
+ end
15
+
16
+ group :development, :test do
17
+ gem "guard", "~> 2.11"
18
+ end
data/Guardfile ADDED
@@ -0,0 +1,83 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ ## Uncomment and set this to only include directories you want to watch
5
+ # directories %w(app lib config test spec feature)
6
+
7
+ ## Uncomment to clear the screen before every task
8
+ # clearing :on
9
+
10
+ ## Guard internally checks for changes in the Guardfile and exits.
11
+ ## If you want Guard to automatically start up again, run guard in a
12
+ ## shell loop, e.g.:
13
+ ##
14
+ ## $ while bundle exec guard; do echo "Restarting Guard..."; done
15
+ ##
16
+ ## Note: if you are using the `directories` clause above and you are not
17
+ ## watching the project directory ('.'), the you will want to move the Guardfile
18
+ ## to a watched dir and symlink it back, e.g.
19
+ #
20
+ # $ mkdir config
21
+ # $ mv Guardfile config/
22
+ # $ ln -s config/Guardfile .
23
+ #
24
+ # and, you'll have to watch "config/Guardfile" instead of "Guardfile"
25
+
26
+ # Note: The cmd option is now required due to the increasing number of ways
27
+ # rspec may be run, below are examples of the most common uses.
28
+ # * bundler: 'bundle exec rspec'
29
+ # * bundler binstubs: 'bin/rspec'
30
+ # * spring: 'bin/rspec' (This will use spring if running and you have
31
+ # installed the spring binstubs per the docs)
32
+ # * zeus: 'zeus rspec' (requires the server to be started separately)
33
+ # * 'just' rspec: 'rspec'
34
+
35
+ guard :rspec, cmd: "bundle exec rspec" do
36
+ require "ostruct"
37
+
38
+ # Generic Ruby apps
39
+ rspec = OpenStruct.new
40
+ rspec.spec = ->(m) { "spec/#{m}_spec.rb" }
41
+ rspec.spec_dir = "spec"
42
+ rspec.spec_helper = "spec/spec_helper.rb"
43
+
44
+ watch(%r{^spec/.+_spec\.rb$})
45
+ watch(%r{^lib/(.+)\.rb$}) { |m| rspec.spec.("lib/#{m[1]}") }
46
+ watch(rspec.spec_helper) { rspec.spec_dir }
47
+
48
+ # Rails example
49
+ rails = OpenStruct.new
50
+ rails.app = %r{^app/(.+)\.rb$}
51
+ rails.views_n_layouts = %r{^app/(.*)(\.erb|\.haml|\.slim)$}
52
+ rails.controllers = %r{^app/controllers/(.+)_controller\.rb$}
53
+ rails.routes = "config/routes.rb"
54
+ rails.app_controller = "app/controllers/application_controller.rb"
55
+ rails.spec_helper = "spec/rails_helper.rb"
56
+ rails.spec_support = %r{^spec/support/(.+)\.rb$}
57
+ rails.views = %r{^app/views/(.+)/.*\.(erb|haml|slim)$}
58
+
59
+ watch(rails.app) { |m| rspec.spec.(m[1]) }
60
+ watch(rails.views_n_layouts) { |m| rspec.spec.("#{m[1]}#{m[2]}") }
61
+ watch(rails.controllers) do |m|
62
+ [
63
+ rspec.spec.("routing/#{m[1]}_routing"),
64
+ rspec.spec.("controllers/#{m[1]}_controller"),
65
+ rspec.spec.("acceptance/#{m[1]}")
66
+ ]
67
+ end
68
+
69
+ watch(rails.spec_support) { rspec.spec_dir }
70
+ watch(rails.spec_helper) { rspec.spec_dir }
71
+ watch(rails.routes) { "spec/routing" }
72
+ watch(rails.app_controller) { "spec/controllers" }
73
+
74
+ # Capybara features specs
75
+ watch(rails.views) { |m| rspec.spec.("features/#{m[1]}") }
76
+
77
+ # Turnip features and steps
78
+ watch(%r{^spec/acceptance/(.+)\.feature$})
79
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
80
+ Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
81
+ end
82
+
83
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 artemk
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,54 @@
1
+ # CassandraCleaner
2
+
3
+ it is like database_cleaner but for cql-rb (cassandra)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'cassandra_cleaner'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install cassandra_cleaner
20
+
21
+ ## Usage
22
+
23
+ Add config in rspec_helper.rb
24
+
25
+ ```ruby
26
+ require 'cassandra_cleaner'
27
+ CassandraCleaner.configure do |config|
28
+ config.cleaner_tag = :cassandra_clean
29
+ config.debug = false
30
+ config.client = CQL_CLIENT #aimed that CQL_CLIENT is some constanst set before, i.e. CQL_CLIENT = Cql::Client.connect(hosts: ['cassandra.example.com'])
31
+ config.enable
32
+ end
33
+ ```
34
+
35
+ And mark some test with a tag you set in config section
36
+
37
+ ```ruby
38
+ it "should test something", :cassandra_clean do
39
+ #test me
40
+ end
41
+ ```
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it ( https://github.com/[my-github-username]/cassandra_cleaner/fork )
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create a new Pull Request
50
+
51
+
52
+ #TODO
53
+ 1. add warning if update or insert not in CassandraCleaner.configured_column_families
54
+ 2. more specs
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cassandra_cleaner/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cassandra_cleaner"
8
+ spec.version = CassandraCleaner::VERSION
9
+ spec.authors = ["artemk"]
10
+ spec.email = ["me@artemk.name"]
11
+ spec.summary = %q{it is like database_cleaner but for cql-rb (cassandra)}
12
+ spec.description = %q{it is like database_cleaner but for cql-rb (cassandra)}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
@@ -0,0 +1,17 @@
1
+ module CassandraCleaner
2
+ class Configuration
3
+ attr_accessor :cleaner_tag, :debug, :client
4
+
5
+ def initialize
6
+ @cleaner_tag = :clean_cassandra
7
+ @debug = false
8
+ @client = nil
9
+ end
10
+
11
+ def enable
12
+ RSpec.configure do |config|
13
+ config.include CassandraCleaner::RspecHelper
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,25 @@
1
+ # monkey patching cql-rb classes
2
+ # todo: add batch
3
+ module Cql
4
+ module Client
5
+ class SynchronousClient
6
+ include CassandraCleaner::CqlRspecNotifications::Client
7
+ include CassandraCleaner::CqlRspecNotifications::Base
8
+ end
9
+
10
+ class AsynchronousClient
11
+ include CassandraCleaner::CqlRspecNotifications::Client
12
+ include CassandraCleaner::CqlRspecNotifications::Base
13
+ end
14
+
15
+ class AsynchronousPreparedStatement
16
+ include CassandraCleaner::CqlRspecNotifications::PreparedStatement
17
+ include CassandraCleaner::CqlRspecNotifications::Base
18
+ end
19
+
20
+ class SynchronousPreparedStatement
21
+ include CassandraCleaner::CqlRspecNotifications::PreparedStatement
22
+ include CassandraCleaner::CqlRspecNotifications::Base
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,44 @@
1
+ require 'cql/client/client'
2
+
3
+ module CassandraCleaner
4
+ module CqlRspecNotifications
5
+
6
+ module Base
7
+ def self.included(host)
8
+ host.class_eval do
9
+ alias_method :execute_without_notify, :execute
10
+ alias_method :execute, :execute_with_notify
11
+ end
12
+
13
+ end
14
+
15
+ def notify_observers(table_name)
16
+ notifiers = CassandraCleaner.rspec_notifiers
17
+ notifiers.each{|n| n.receive(table_name)} if notifiers && table_name
18
+ end
19
+ end
20
+
21
+ module Client
22
+ def execute_with_notify(*args)
23
+ res = execute_without_notify(*args)
24
+ table_name = grap_table_name(args)
25
+ notify_observers(table_name)
26
+ res
27
+ end
28
+
29
+ def grap_table_name(args)
30
+ $1.split('.').last if args && args.first.match(/(?:INSERT INTO|UPDATE)\s([\w\.]*)\s.*/)
31
+ end
32
+ end
33
+
34
+ module PreparedStatement
35
+ def execute_with_notify(*args)
36
+ res = execute_without_notify(*args)
37
+ table_name = self.metadata.instance_variable_get(:@metadata).try(:first).try(:last).try(:table)
38
+ notify_observers(table_name)
39
+ res
40
+ end
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,28 @@
1
+ require 'rspec'
2
+
3
+ module CassandraCleaner
4
+ module RspecHelper
5
+ def self.included(rspec_config)
6
+ rspec_config.before(:each, CassandraCleaner.configuration.cleaner_tag => true) do
7
+ CassandraCleaner.add_rspec_notifier(CassandraCleaner::RspecNotifier.new)
8
+ end
9
+
10
+ rspec_config.after(:each, CassandraCleaner.configuration.cleaner_tag => true) do
11
+ begin
12
+ CassandraCleaner.rspec_notifiers.each do |n|
13
+ n.show_info if CassandraCleaner.configuration.debug
14
+
15
+ table_for_truncation = n.tables & CassandraCleaner.configured_column_families
16
+ unless table_for_truncation.empty?
17
+ table_for_truncation.each do |column_family|
18
+ CassandraCleaner.configuration.client.execute("TRUNCATE #{column_family}")
19
+ end
20
+ end
21
+ end
22
+ ensure
23
+ CassandraCleaner.unregister_notifiers
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,20 @@
1
+ module CassandraCleaner
2
+ class RspecNotifier
3
+ def initialize
4
+ @column_families = []
5
+ end
6
+
7
+ def receive(table_name)
8
+ @column_families << table_name
9
+ end
10
+
11
+ def show_info
12
+ puts "\nColumn Families used in tests"
13
+ pp @column_families.uniq
14
+ end
15
+
16
+ def tables
17
+ @column_families.uniq
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,3 @@
1
+ module CassandraCleaner
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,40 @@
1
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
2
+
3
+ require "cql"
4
+
5
+ require_relative "cassandra_cleaner/version"
6
+ require_relative 'cassandra_cleaner/configuration'
7
+
8
+ module CassandraCleaner
9
+ class << self
10
+ def configure(&block)
11
+ yield configuration
12
+ end
13
+
14
+ def configuration
15
+ @configuration ||= Configuration.new
16
+ end
17
+
18
+ def configured_column_families
19
+ configuration.client.execute("select columnfamily_name from system.schema_columnfamilies where keyspace_name = '#{configuration.client.keyspace}'").to_a.map(&:values).flatten
20
+ end
21
+
22
+ def add_rspec_notifier(rspec_notifier)
23
+ @@rspec_notifiers ||= []
24
+ @@rspec_notifiers << rspec_notifier
25
+ end
26
+
27
+ def unregister_notifiers
28
+ @@rspec_notifiers = []
29
+ end
30
+
31
+ def rspec_notifiers
32
+ @@rspec_notifiers ||= []
33
+ end
34
+ end
35
+ end
36
+
37
+ require_relative 'cassandra_cleaner/rspec_helper'
38
+ require_relative 'cassandra_cleaner/rspec_notifier'
39
+ require_relative 'cassandra_cleaner/cql_rspec_notifications'
40
+ require_relative 'cassandra_cleaner/cql'
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe CassandraCleaner do
4
+ let(:cql_client) { CQL_CLIENT }
5
+ let(:keyspace) { ENV['CASSANDRA_CLEANER_KEYSPACE'] }
6
+
7
+ before(:all) do
8
+ CQL_CLIENT.execute("CREATE TABLE #{ENV['CASSANDRA_CLEANER_KEYSPACE']}.users (id INT, name TEXT, PRIMARY KEY (id))")
9
+ CQL_CLIENT.execute("CREATE TABLE #{ENV['CASSANDRA_CLEANER_KEYSPACE']}.posts (id INT, title TEXT, PRIMARY KEY (id))")
10
+ end
11
+
12
+ after(:all) do
13
+ CQL_CLIENT.execute("DROP TABLE #{ENV['CASSANDRA_CLEANER_KEYSPACE']}.users")
14
+ CQL_CLIENT.execute("DROP TABLE #{ENV['CASSANDRA_CLEANER_KEYSPACE']}.posts")
15
+ end
16
+
17
+ describe "truncation" do
18
+ it "should not truncate anything after this spec" do
19
+ cql_client.execute("INSERT INTO #{keyspace}.users (id, name) VALUES (1, 'Artem K')")
20
+ expect(CassandraCleaner.rspec_notifiers.map(&:tables)).to eq([])
21
+ end
22
+
23
+ it "should truncate users after this spec", :cassandra_clean do
24
+ cql_client.execute("INSERT INTO #{keyspace}.users (id, name) VALUES (1, 'Artem K')")
25
+ expect(CassandraCleaner.rspec_notifiers.map(&:tables)).to eq([["users"]])
26
+ end
27
+ end
28
+
29
+ describe ".configured_column_families" do
30
+ it "should return column families that are part of keyspace" do
31
+ expect(CassandraCleaner.configured_column_families).to eq(["posts", "users"])
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,12 @@
1
+ # encoding: utf-8
2
+
3
+ ENV['CASSANDRA_HOST'] ||= 'localhost'
4
+ ENV['CASSANDRA_CLEANER_KEYSPACE'] ||= 'cassandra_cleaner_test'
5
+
6
+ require "bundler"
7
+ Bundler.setup
8
+
9
+ require 'cassandra_cleaner'
10
+ CQL_CLIENT = Cql::Client.connect(hosts: [ENV['CASSANDRA_HOST']])
11
+
12
+ Dir['./spec/support/**/*.rb'].sort.each { |f| require f }
@@ -0,0 +1,28 @@
1
+ RSpec.configure do |config|
2
+ config.before(:suite) do
3
+
4
+ #create test keyspace
5
+ keyspace_definition = <<-KSDEF
6
+ CREATE KEYSPACE #{ENV['CASSANDRA_CLEANER_KEYSPACE']}
7
+ WITH replication = {
8
+ 'class': 'SimpleStrategy',
9
+ 'replication_factor': 1
10
+ }
11
+ KSDEF
12
+ CQL_CLIENT.execute("DROP KEYSPACE #{ENV['CASSANDRA_CLEANER_KEYSPACE']}") rescue nil
13
+ CQL_CLIENT.execute(keyspace_definition)
14
+ CQL_CLIENT.use(ENV['CASSANDRA_CLEANER_KEYSPACE'])
15
+ end
16
+
17
+ config.after(:suite) do
18
+
19
+ #drop keyspace
20
+ CQL_CLIENT.execute("DROP KEYSPACE #{ENV['CASSANDRA_CLEANER_KEYSPACE']}") rescue nil
21
+
22
+ #close test connection
23
+ CQL_CLIENT.close
24
+ end
25
+ end
26
+
27
+
28
+
@@ -0,0 +1,6 @@
1
+ CassandraCleaner.configure do |config|
2
+ config.cleaner_tag = :cassandra_clean
3
+ config.debug = false
4
+ config.client = CQL_CLIENT
5
+ config.enable
6
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cassandra_cleaner
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - artemk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: it is like database_cleaner but for cql-rb (cassandra)
42
+ email:
43
+ - me@artemk.name
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".rspec"
50
+ - Gemfile
51
+ - Guardfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - cassandra_cleaner.gemspec
56
+ - lib/cassandra_cleaner.rb
57
+ - lib/cassandra_cleaner/configuration.rb
58
+ - lib/cassandra_cleaner/cql.rb
59
+ - lib/cassandra_cleaner/cql_rspec_notifications.rb
60
+ - lib/cassandra_cleaner/rspec_helper.rb
61
+ - lib/cassandra_cleaner/rspec_notifier.rb
62
+ - lib/cassandra_cleaner/version.rb
63
+ - spec/cassandra_cleaner_spec.rb
64
+ - spec/spec_helper.rb
65
+ - spec/support/1_cql_client.rb
66
+ - spec/support/2_config.rb
67
+ homepage: ''
68
+ licenses:
69
+ - MIT
70
+ metadata: {}
71
+ post_install_message:
72
+ rdoc_options: []
73
+ require_paths:
74
+ - lib
75
+ required_ruby_version: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ required_rubygems_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - ">="
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 2.2.2
88
+ signing_key:
89
+ specification_version: 4
90
+ summary: it is like database_cleaner but for cql-rb (cassandra)
91
+ test_files:
92
+ - spec/cassandra_cleaner_spec.rb
93
+ - spec/spec_helper.rb
94
+ - spec/support/1_cql_client.rb
95
+ - spec/support/2_config.rb