believer 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. data/README.md +29 -2
  2. data/lib/believer.rb +5 -0
  3. data/lib/believer/base.rb +16 -4
  4. data/lib/believer/column.rb +74 -9
  5. data/lib/believer/columns.rb +18 -1
  6. data/lib/believer/command.rb +12 -1
  7. data/lib/believer/counter.rb +60 -0
  8. data/lib/believer/counting.rb +29 -0
  9. data/lib/believer/cql_helper.rb +22 -0
  10. data/lib/believer/create_table.rb +24 -0
  11. data/lib/believer/ddl.rb +2 -31
  12. data/lib/believer/drop_table.rb +9 -0
  13. data/lib/believer/environment/base_env.rb +5 -0
  14. data/lib/believer/insert.rb +3 -1
  15. data/lib/believer/persistence.rb +18 -2
  16. data/lib/believer/update.rb +52 -0
  17. data/lib/believer/values.rb +67 -0
  18. data/lib/believer/version.rb +1 -1
  19. data/spec/believer/base_spec.rb +9 -2
  20. data/spec/believer/callback_spec.rb +1 -1
  21. data/spec/believer/collection_columns_spec.rb +95 -0
  22. data/spec/believer/columns_spec.rb +21 -0
  23. data/spec/believer/counter_spec.rb +46 -0
  24. data/spec/believer/counting_spec.rb +31 -0
  25. data/spec/believer/delete_spec.rb +1 -1
  26. data/spec/believer/environment_spec.rb +4 -4
  27. data/spec/believer/finder_methods_spec.rb +9 -9
  28. data/spec/believer/insert_spec.rb +1 -1
  29. data/spec/believer/limit_spec.rb +1 -1
  30. data/spec/believer/order_by_spec.rb +2 -2
  31. data/spec/believer/query_spec.rb +2 -2
  32. data/spec/believer/querying_spec.rb +1 -1
  33. data/spec/believer/relation_spec.rb +6 -6
  34. data/spec/believer/test_run_life_cycle_spec.rb +2 -2
  35. data/spec/believer/time_series_spec.rb +23 -3
  36. data/spec/believer/update_spec.rb +71 -0
  37. data/spec/believer/where_spec.rb +4 -4
  38. data/spec/spec_helper.rb +6 -2
  39. data/spec/support/setup_database.rb +8 -8
  40. data/spec/support/test_classes.rb +22 -5
  41. metadata +17 -19
  42. data/lib/believer/extensions/will_paginate.rb +0 -173
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe Believer::Counting do
4
+
5
+ it 'should detect a counter table' do
6
+ expect(Test::Artist.is_counter_table?).to eql false
7
+ expect(Test::AlbumSales.is_counter_table?).to eql true
8
+ end
9
+
10
+ it 'should detect a changed counter instance' do
11
+ album_sales = Test::AlbumSales.new
12
+ expect(album_sales.has_counter_diffs?).to eql false
13
+ album_sales.sales.incr
14
+ expect(album_sales.has_counter_diffs?).to eql true
15
+ end
16
+
17
+ it 'should be able to set a counter using a number' do
18
+ album_sales = Test::AlbumSales.new
19
+ album_sales.sales = 2
20
+ expect(album_sales.sales.to_i).to eql 2
21
+ end
22
+
23
+ it 'should be able to reset a counter' do
24
+ album_sales = Test::AlbumSales.new
25
+ album_sales.sales.incr
26
+ expect(album_sales.has_counter_diffs?).to eql true
27
+ album_sales.sales.reset!
28
+ expect(album_sales.has_counter_diffs?).to eql false
29
+ end
30
+
31
+ end
@@ -5,7 +5,7 @@ describe Believer::Delete do
5
5
  it "create a valid delete statement" do
6
6
  del = Believer::Delete.new(:record_class => Test::Artist)
7
7
  del = del.where(:id => 1)
8
- del.to_cql.should == 'DELETE FROM artists WHERE id = 1'
8
+ expect(del.to_cql).to eql 'DELETE FROM artists WHERE id = 1'
9
9
  end
10
10
 
11
11
 
@@ -17,8 +17,8 @@ describe Believer::Environment do
17
17
  end
18
18
  Rails = Struct.new(:root, :env, :logger).new(File.join(RSpec.configuration.test_files_dir, 'rails'), :test, nil)
19
19
  env = Believer::Base.environment
20
- env.class.should == Believer::Environment::RailsEnv
21
- env.configuration[:host].should == '123.456.789.0'
20
+ expect(env.class).to eql Believer::Environment::RailsEnv
21
+ expect(env.configuration[:host]).to eql '123.456.789.0'
22
22
  Object.instance_eval { remove_const :Rails}
23
23
  end
24
24
 
@@ -28,8 +28,8 @@ describe Believer::Environment do
28
28
  end
29
29
  Merb = Struct.new(:root, :environment, :logger).new(File.join(RSpec.configuration.test_files_dir, 'merb'), :test, nil)
30
30
  env = Believer::Base.environment
31
- env.class.should == Believer::Environment::MerbEnv
32
- env.configuration[:host].should == 'merb.test.local'
31
+ expect(env.class).to eql Believer::Environment::MerbEnv
32
+ expect(env.configuration[:host]).to eql 'merb.test.local'
33
33
  Object.instance_eval { remove_const :Merb}
34
34
  end
35
35
 
@@ -11,29 +11,29 @@ describe Believer::FinderMethods do
11
11
 
12
12
  it 'exists? should return true for an existing object' do
13
13
  # Test all variants
14
- Test::Artist.exists?(:name => 'U2').should == true
15
- Test::Artist.exists?('name = ?', 'U2').should == true
14
+ expect(Test::Artist.exists?(:name => 'U2')).to eql true
15
+ expect(Test::Artist.exists?('name = ?', 'U2')).to eql true
16
16
  end
17
17
 
18
18
  it 'exists? should return false for an non-existing object' do
19
19
  # Test all variants
20
- Test::Artist.exists?(:name => 'Genesis').should == false
21
- Test::Artist.exists?('name = ?', 'Genesis').should == false
20
+ expect(Test::Artist.exists?(:name => 'Genesis')).to eql false
21
+ expect(Test::Artist.exists?('name = ?', 'Genesis')).to eql false
22
22
  end
23
23
 
24
24
  it 'find using a hash' do
25
- Test::Artist.find(:name => 'U2').should == @u2
25
+ expect(Test::Artist.find(:name => 'U2')).to eql @u2
26
26
  end
27
27
 
28
28
  it 'find using an array' do
29
29
  res = Test::Artist.find('U2', 'UB 40')
30
- res.size.should == 2
31
- res.include?(@u2).should == true
32
- res.include?(@ub_40).should == true
30
+ expect(res.size).to eql 2
31
+ expect(res.include?(@u2)).to eql true
32
+ expect(res.include?(@ub_40)).to eql true
33
33
  end
34
34
 
35
35
  it 'find using single primary key value' do
36
- Test::Artist.find('U2').should == @u2
36
+ expect(Test::Artist.find('U2')).to eql @u2
37
37
  end
38
38
 
39
39
  end
@@ -4,6 +4,6 @@ describe Believer::Insert do
4
4
 
5
5
  it "create statement based on hash" do
6
6
  insert = Believer::Insert.new(:record_class => Test::Artist, :values => {:id => 1, :name => 'Beatles'})
7
- insert.to_cql.should == "INSERT INTO artists (id, name) VALUES (1, 'Beatles')"
7
+ expect(insert.to_cql).to eql "INSERT INTO artists (id, name) VALUES (1, 'Beatles')"
8
8
  end
9
9
  end
@@ -2,6 +2,6 @@ require 'spec_helper'
2
2
 
3
3
  describe Believer::Limit do
4
4
  it 'create CQL' do
5
- Believer::Limit.new(23).to_cql.downcase.should == 'limit 23'
5
+ expect(Believer::Limit.new(23).to_cql.downcase).to eql 'limit 23'
6
6
  end
7
7
  end
@@ -4,11 +4,11 @@ describe Believer::OrderBy do
4
4
 
5
5
  it "create a CQL expression" do
6
6
  ob = Believer::OrderBy.new(:field)
7
- ob.to_cql.downcase.should == 'order by field asc'
7
+ expect(ob.to_cql.downcase).to eql 'order by field asc'
8
8
  end
9
9
 
10
10
  it "reverse order" do
11
11
  ob = Believer::OrderBy.new(:field).inverse
12
- ob.to_cql.downcase.should == 'order by field desc'
12
+ expect(ob.to_cql.downcase).to eql 'order by field desc'
13
13
  end
14
14
  end
@@ -32,7 +32,7 @@ describe Believer::Query do
32
32
  where(:release_date => Time.utc(2013)).
33
33
  order(:name, :desc).
34
34
  limit(10)
35
- q.to_cql.should == "SELECT name, artist FROM albums WHERE name = 'Revolver' AND release_date = '2013-01-01 00:00:00+0000' ORDER BY name DESC LIMIT 10"
35
+ expect(q.to_cql).to eql "SELECT name, artist FROM albums WHERE name = 'Revolver' AND release_date = '2013-01-01 00:00:00+0000' ORDER BY name DESC LIMIT 10"
36
36
  end
37
37
 
38
38
  it 'should include allow filtering clause' do
@@ -48,7 +48,7 @@ describe Believer::Query do
48
48
  it 'should behave like an Enumerable' do
49
49
  q = Believer::Query.new(:record_class => Test::Artist).where(:name => artists.map { |o| o.name })
50
50
  Enumerable.instance_methods(false).each do |enum_method|
51
- q.respond_to?(enum_method.to_sym).should == true
51
+ expect(q.respond_to?(enum_method.to_sym)).to eql true
52
52
  end
53
53
  end
54
54
 
@@ -9,7 +9,7 @@ describe Believer::Querying do
9
9
  {:method => :limit, :args => 10},
10
10
  ].each do |scenario|
11
11
  it "#{scenario[:method]} call should return a query object" do
12
- Test::Artist.send(scenario[:method], scenario[:args]).class.should == Believer::Query
12
+ expect(Test::Artist.send(scenario[:method], scenario[:args]).class).to eql Believer::Query
13
13
  end
14
14
  end
15
15
 
@@ -21,28 +21,28 @@ describe Believer::Relation do
21
21
 
22
22
  it "report correct size of one to many relation" do
23
23
  a = Test::Artist.where(:name => 'Beatles').first
24
- a.albums.size.should == 3
24
+ expect(a.albums.size).to eql 3
25
25
  end
26
26
 
27
27
  it "one to many relation collection should support exists? method" do
28
28
  a = Test::Artist.where(:name => 'Beatles').first
29
- a.albums.exists?(:name => 'Help').should == true
29
+ expect(a.albums.exists?(:name => 'Help')).to eql true
30
30
  end
31
31
 
32
32
  it "one to many relation collection should support clear method" do
33
33
  a = Test::Artist.where(:name => 'Beatles').first
34
- a.albums.size.should == 3
34
+ expect(a.albums.size).to eql 3
35
35
  a.albums.clear
36
- a.albums.size.should == 0
36
+ expect(a.albums.size).to eql 0
37
37
  end
38
38
 
39
39
  it "one to many relation collection should support find method" do
40
40
  a = Test::Artist.where(:name => 'Beatles').first
41
- a.albums.find(:name => 'Help').should == @help
41
+ expect(a.albums.find(:name => 'Help')).to eql @help
42
42
  end
43
43
 
44
44
  it "one to one relation" do
45
- @have_a_cigar.album.should == Test::Album.where(:artist_name => 'Pink Floyd', :name => 'Wish you were here').first
45
+ expect(@have_a_cigar.album).to eql Test::Album.where(:artist_name => 'Pink Floyd', :name => 'Wish you were here').first
46
46
  end
47
47
 
48
48
  end
@@ -22,7 +22,7 @@ describe Believer::Test::TestRunLifeCycle do
22
22
 
23
23
  after :all do
24
24
  Fly.drop_table
25
- @monitor.kill_count.should == @created.size
25
+ expect(@monitor.kill_count).to eql @created.size
26
26
  end
27
27
 
28
28
  after :each do
@@ -32,7 +32,7 @@ describe Believer::Test::TestRunLifeCycle do
32
32
  end
33
33
 
34
34
  it "should clean all created objects, even after a fail" do
35
- Believer::Test::TestRunLifeCycle::Destructor.instance.observed_models.size.should == CREATE_COUNT
35
+ expect(Believer::Test::TestRunLifeCycle::Destructor.instance.observed_models.size).to eql CREATE_COUNT
36
36
  end
37
37
 
38
38
  class Monitor
@@ -6,7 +6,7 @@ describe 'Time series' do
6
6
  before :each do
7
7
  @interval = 1.minute
8
8
  @start = Time.utc(2012)
9
- @count = 10
9
+ @count = 5
10
10
  @count.times do |i|
11
11
  attrs = {:computer_id => 'ABC', :event_type => 1, :time => @start + (@interval * i)}
12
12
  Test::Event::PARAMETER_NAMES.each do |param|
@@ -16,8 +16,28 @@ describe 'Time series' do
16
16
  end
17
17
  end
18
18
 
19
- it 'load all' do
20
- Test::Event.where(:computer_id => 'ABC', :event_type => 1).size.should == @count
19
+ it 'should load all' do
20
+ expect(Test::Event.where(:computer_id => 'ABC', :event_type => 1).size).to eql @count
21
21
  end
22
22
 
23
+ it 'should load after specific time' do
24
+ events = Test::Event.where(:computer_id => 'ABC', :event_type => 1).where('time >= ?', @start + @interval)
25
+ expect(events.size).to eql(@count - 1)
26
+ end
27
+
28
+ it 'should load between to date' do
29
+ events = Test::Event.where(:computer_id => 'ABC', :event_type => 1).
30
+ where('time >= ?', @start + @interval).
31
+ where('time <= ?', @start + (@count-2) * @interval)
32
+ expect(events.size).to eql(@count - 2)
33
+ end
34
+
35
+ #it 'should load between to date' do
36
+ # events = Test::Event.where(:computer_id => 'ABC', :event_type => 1).select(:computer_id, :event_type).
37
+ # where('time >= ?', @start + @interval).
38
+ # where('time >= ?', @start + @interval*2).
39
+ # where('time <= ?', @start + (@count-2) * @interval)
40
+ # expect(events.size).to eql(@count - 2)
41
+ #end
42
+
23
43
  end
@@ -0,0 +1,71 @@
1
+ require 'spec_helper'
2
+
3
+ describe Believer::Update do
4
+
5
+ let :artist do
6
+ Test::Artist.create(:name => 'Beatles', :label => 'Apple')
7
+ end
8
+
9
+ let :album_sales do
10
+ Test::AlbumSales.create(:artist_name => 'Beatles', :name => 'Revolver')
11
+ end
12
+
13
+ it "should create statement based on hash" do
14
+ update = Believer::Update.new(:record_class => artist.class, :values => {:label => 'Apple'}).where(:name => artist.name)
15
+ expect(update.to_cql).to eql "UPDATE artists SET label = '#{artist.label}' WHERE name = '#{artist.name}'"
16
+ end
17
+
18
+ it "should create statement based on an object" do
19
+ update = Believer::Update.create(artist)
20
+ expect(update.to_cql).to eql "UPDATE artists SET label = '#{artist.label}' WHERE name = '#{artist.name}'"
21
+ end
22
+
23
+ it "should create create a correct statement for a counter increment" do
24
+ album_sales.sales.incr(2)
25
+ update = Believer::Update.create(album_sales)
26
+ expect(update.to_cql).to eql "UPDATE album_sales SET sales = sales + 2 WHERE artist_name = '#{album_sales.artist_name}' AND name = '#{album_sales.name}'"
27
+ end
28
+
29
+ it "should create create a correct statement for a counter decrement" do
30
+ album_sales.sales.decr(2)
31
+ update = Believer::Update.create(album_sales)
32
+ expect(update.to_cql).to eql "UPDATE album_sales SET sales = sales - 2 WHERE artist_name = '#{album_sales.artist_name}' AND name = '#{album_sales.name}'"
33
+ end
34
+
35
+ it "should execute a counter change" do
36
+ cur_val = album_sales.sales.to_i
37
+ album_sales.sales.incr(2)
38
+ update = Believer::Update.create(album_sales)
39
+ update.execute
40
+ cur_album_sales = Test::AlbumSales.where(album_sales.key_values).first
41
+ expect(cur_album_sales.sales.to_i).to eql (cur_val + 2)
42
+ end
43
+
44
+ it "should not execute if no changes are detected in any counter" do
45
+ cur_val = album_sales.reload!.sales.to_i
46
+ puts "Cur val: #{cur_val}"
47
+ album_sales.sales.incr(2)
48
+ album_sales.sales.decr(2)
49
+ expect(album_sales.sales.to_i).to eql cur_val
50
+ update = Believer::Update.create(album_sales)
51
+ expect(update.execute).to eql false
52
+
53
+ cur_album_sales = Test::AlbumSales.where(album_sales.key_values).first
54
+ expect(cur_album_sales.sales.to_i).to eql cur_val
55
+ end
56
+
57
+ it "should update the values" do
58
+ artist.label = 'Chrysalis'
59
+ Believer::Update.create(artist).execute
60
+ loaded_artist = Test::Artist.where(:name => 'Beatles').first
61
+ expect(loaded_artist.label).to eql('Chrysalis')
62
+ end
63
+
64
+ it "should update the values" do
65
+ artist.label = 'Chrysalis'
66
+ Believer::Update.create(artist).execute
67
+ loaded_artist = Test::Artist.where(:name => 'Beatles').first
68
+ expect(loaded_artist.label).to eql('Chrysalis')
69
+ end
70
+
71
+ end
@@ -9,20 +9,20 @@ describe Believer::WhereClause do
9
9
  [:timestamp, Time.utc(2013), "field = '2013-01-01 00:00:00+0000'"],
10
10
  ].each do |scenario|
11
11
  it "simple #{scenario[0]} parameter " do
12
- Believer::WhereClause.new('field = ?', scenario[1]).to_cql.downcase.should == scenario[2]
12
+ expect(Believer::WhereClause.new('field = ?', scenario[1]).to_cql.downcase).to eql scenario[2]
13
13
  end
14
14
  end
15
15
 
16
16
  it 'multiple parameters' do
17
- Believer::WhereClause.new('a = ? AND b = ? AND c = ?', 1, 2, 3).to_cql.downcase.should == 'a = 1 and b = 2 and c = 3'
17
+ expect(Believer::WhereClause.new('a = ? AND b = ? AND c = ?', 1, 2, 3).to_cql.downcase).to eql 'a = 1 and b = 2 and c = 3'
18
18
  end
19
19
 
20
20
  it 'simple hash parameter' do
21
- Believer::WhereClause.new(:a => 1, :b => 2).to_cql.downcase.should == 'a = 1 and b = 2'
21
+ expect(Believer::WhereClause.new(:a => 1, :b => 2).to_cql.downcase).to eql 'a = 1 and b = 2'
22
22
  end
23
23
 
24
24
  it 'enumerable hash parameter' do
25
- Believer::WhereClause.new(:a => [1, 2, 3]).to_cql.downcase.should == 'a in (1,2,3)'
25
+ expect(Believer::WhereClause.new(:a => [1, 2, 3]).to_cql.downcase).to eql 'a in (1,2,3)'
26
26
  end
27
27
 
28
28
  end
@@ -16,8 +16,12 @@ Dir[File.expand_path('../support/*.rb', __FILE__)].each {|f| require f}
16
16
 
17
17
  setup_database
18
18
 
19
- RSpec.configure do |c|
20
- c.add_setting :test_files_dir, :default => File.expand_path('../test_files/', __FILE__)
19
+ RSpec.configure do |config|
20
+ config.add_setting :test_files_dir, :default => File.expand_path('../test_files/', __FILE__)
21
+ config.expect_with :rspec do |c|
22
+ # Enable only the `expect` sytax...
23
+ c.syntax = :expect
24
+ end
21
25
  end
22
26
 
23
27
  File.expand_path('../test_files/', __FILE__)
@@ -5,16 +5,16 @@ def setup_database
5
5
 
6
6
  connection = env.create_connection(:connect_to_keyspace => false)
7
7
  begin
8
- connection.use(env.connection_configuration[:keyspace])
8
+ puts "Dropping keyspace"
9
+ env.drop_keyspace
9
10
  rescue Cql::QueryError
11
+ end
12
+ env.create_keyspace({}, connection)
13
+ connection.use(env.connection_configuration[:keyspace])
10
14
 
11
- env.create_keyspace({}, connection)
12
- connection.use(env.connection_configuration[:keyspace])
13
-
14
- Test.classes.each do |cl|
15
- puts "Creating table #{cl.table_name}"
16
- cl.create_table
17
- end
15
+ Test.classes.each do |cl|
16
+ puts "Creating table #{cl.table_name}"
17
+ cl.create_table
18
18
  end
19
19
 
20
20
  end
@@ -34,6 +34,15 @@ module Test
34
34
  has_single :album, :class => 'Test::Album', :key => [:artist_name, :album_name], :foreign_key => [:artist_name, :name]
35
35
  end
36
36
 
37
+ class AlbumSales < Believer::Base
38
+ column :artist_name
39
+ column :name
40
+
41
+ column :sales, :type => :counter
42
+
43
+ primary_key :artist_name, :name
44
+ end
45
+
37
46
  class Person < Believer::Base
38
47
  self.table_name= 'people'
39
48
 
@@ -41,16 +50,15 @@ module Test
41
50
  column :name, :type => :string
42
51
 
43
52
  primary_key :id
44
-
45
53
  end
46
54
 
47
55
  class Event < Believer::Base
48
56
  column :computer_id, :type => :string
49
57
  column :event_type, :type => :integer
50
- column :time, :type => :integer, :key => true
58
+ column :time, :type => :timestamp, :key => true
51
59
  column :description, :type => :string
52
60
 
53
- PARAMETER_NAMES = (1..50).map { |index| "parameter_#{index}".to_sym }
61
+ PARAMETER_NAMES = (1..5).map { |index| "parameter_#{index}".to_sym }
54
62
  PARAMETER_NAMES.each do |param|
55
63
  column param, :type => :float
56
64
  end
@@ -59,13 +67,22 @@ module Test
59
67
 
60
68
  end
61
69
 
70
+ class Child < Believer::Base
71
+ column :name, :type => :string
72
+ column :marbles, :type => :set, :element_type => :symbol
73
+ column :soccer_cards, :type => :array, :element_type => :string
74
+ column :family, :type => :hash, :key_type => :symbol, :value_type => :string
75
+
76
+ primary_key :name
77
+ end
78
+
62
79
  class Environment < Believer::Environment::BaseEnv
63
80
  def configuration
64
81
  {
65
82
  :host => '127.0.0.1',
66
83
  :keyspace => 'believer_test_space',
67
84
  :believer => {
68
- :logger => {:level => ::Logger::INFO}
85
+ :logger => {:level => ::Logger::DEBUG}
69
86
  }
70
87
  }
71
88
  end
@@ -76,7 +93,7 @@ module Test
76
93
  end
77
94
 
78
95
  Believer::Base.environment = test_environment
79
- CLASSES = [Artist, Album, Song, Event, Person]
96
+ CLASSES = [Artist, Album, Song, AlbumSales, Event, Person, Child]
80
97
  #CLASSES.each {|cl| cl.environment = test_environment}
81
98
 
82
99
  def self.classes