Skip to content

Commit

Permalink
Convert RSpec to modern syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
dlackty committed Jun 22, 2018
1 parent 3fd4669 commit 62cb417
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion forgetsy.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency 'redis', '> 2.0'
gem.add_runtime_dependency 'redis-namespace', '>= 1.1.0'
gem.add_runtime_dependency 'activesupport', '>= 3.2.0'
gem.add_development_dependency 'rspec', '~> 2.13.0'
gem.add_development_dependency 'rspec', '~> 2.14.0'
gem.add_development_dependency 'rake', '~> 0.9'
end
42 changes: 21 additions & 21 deletions spec/delta_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@
describe 'creation' do
it 'creates two set instances with appropriate keys' do
delta = Forgetsy::Delta.create('foo', t: 1.week)
delta.should be_kind_of(Forgetsy::Delta)
expect(delta).to be_kind_of(Forgetsy::Delta)
set = delta.primary_set
set.should be_kind_of(Forgetsy::Set)
@redis.exists(set.name).should == true
expect(set).to be_kind_of(Forgetsy::Set)
expect(@redis.exists(set.name)).to eq(true)
set = delta.secondary_set
set.should be_kind_of(Forgetsy::Set)
@redis.exists(set.name).should == true
expect(set).to be_kind_of(Forgetsy::Set)
expect(@redis.exists(set.name)).to eq(true)
end
end

describe 'retrospective creation' do
it 'sets last decay date of secondary set to older than that of the primary' do
delta = Forgetsy::Delta.create('foo', t: 1.week, replay: true)
delta.should be_kind_of(Forgetsy::Delta)
expect(delta).to be_kind_of(Forgetsy::Delta)
primary_set = delta.primary_set
secondary_set = delta.secondary_set
secondary_set.last_decayed_date.should < primary_set.last_decayed_date
expect(secondary_set.last_decayed_date).to be < primary_set.last_decayed_date
end
end

Expand All @@ -35,23 +35,23 @@
delta.incr('foo_bin')
delta.incr('foo_bin')
delta.incr('bar_bin')
delta.fetch(bin: 'foo_bin').values.first.round(1).should == 1.0
delta.fetch(bin: 'bar_bin').values.first.round(1).should == 1.0
expect(delta.fetch(bin: 'foo_bin').values.first.round(1)).to eq(1.0)
expect(delta.fetch(bin: 'bar_bin').values.first.round(1)).to eq(1.0)
end

it 'passes options on to sets' do
opts = { decay: false }
mock_set = double()
mock_set.should_receive(:fetch).with(opts) { [] }
expect(mock_set).to receive(:fetch).with(opts) { [] }
delta = Forgetsy::Delta.create('foo', t: 1.week)
delta.incr('foo_bin')
delta.stub(:primary_set) { mock_set }
allow(delta).to receive(:primary_set) { mock_set }
delta.fetch(opts)
end

it 'returns nil when trying to fetch a non-existent bin' do
delta = Forgetsy::Delta.create('foo', t: 1.week)
delta.fetch(bin: 'foo_bin').should == {'foo_bin' => nil }
expect(delta.fetch(bin: 'foo_bin')).to eq({'foo_bin' => nil })
end

it 'raises a value error if a delta with that name does not exist' do
Expand All @@ -61,7 +61,7 @@
rescue NameError
error = true
end
error.should == true
expect(error).to eq(true)
end

it 'fetches normalised counts when fetching all scores' do
Expand All @@ -70,10 +70,10 @@
delta.incr('foo_bin')
delta.incr('bar_bin')
all_scores = delta.fetch()
all_scores.keys[0].should == 'foo_bin'
all_scores.keys[1].should == 'bar_bin'
all_scores.values[0].round(1).should == 1.0
all_scores.values[1].round(1).should == 1.0
expect(all_scores.keys[0]).to eq('foo_bin')
expect(all_scores.keys[1]).to eq('bar_bin')
expect(all_scores.values[0].round(1)).to eq(1.0)
expect(all_scores.values[1].round(1)).to eq(1.0)
end

it 'limits results when using :n option' do
Expand All @@ -82,9 +82,9 @@
delta.incr_by('bar_bin', 2)
delta.incr('quux_bin')
all_scores = delta.fetch(n: 2)
all_scores.length.should == 2
expect(all_scores.length).to eq(2)
all_scores = delta.fetch()
all_scores.length.should == 3
expect(all_scores.length).to eq(3)
end

it "works with retroactive events" do
Expand All @@ -95,8 +95,8 @@
follows_delta.incr('UserBar', date: 1.week.ago)
follows_delta.incr('UserFoo', date: 1.day.ago)
follows_delta.incr('UserFoo')
follows_delta.fetch['UserFoo'].round(2).should == 0.67
follows_delta.fetch['UserBar'].round(2).should == 0.50
expect(follows_delta.fetch['UserFoo'].round(2)).to eq(0.67)
expect(follows_delta.fetch['UserBar'].round(2)).to eq(0.50)
end
end

Expand Down
26 changes: 13 additions & 13 deletions spec/set_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@

describe 'creation' do
it 'creates a redis set with the appropriate name and stores metadata' do
@redis.zcount('foo', '-inf', '+inf').should == 2
expect(@redis.zcount('foo', '-inf', '+inf')).to eq(2)
end

it 'stores the last decayed date in a special key upon creation' do
manual_date = 3.weeks.ago
a = Forgetsy::Set.create('bar', t: 1.week, date: manual_date)
a.last_decayed_date.should == manual_date.to_f.round(7)
expect(a.last_decayed_date).to eq(manual_date.to_f.round(7))
end

it 'stores the mean lifetime in a special key upon creation' do
@set.lifetime.should == 1.week.to_f
expect(@set.lifetime).to eq(1.week.to_f)
end

it 'fails with an argument error when no :t option is supplied' do
Expand All @@ -29,46 +29,46 @@
rescue ArgumentError
error = true
end
error.should == true
expect(error).to eq(true)
end
end

describe 'increments' do
it 'increments counters correctly' do
@set.incr('foo_bin')
@redis.zscore('foo', 'foo_bin').should == 1.0
expect(@redis.zscore('foo', 'foo_bin')).to eq(1.0)
end

it 'increments in batches' do
@set.incr_by('foo_bin', 5)
@redis.zscore('foo', 'foo_bin').should == 5.0
expect(@redis.zscore('foo', 'foo_bin')).to eq(5.0)
end

it 'ignores an increment with a date older than the last decay date' do
manual_date = 2.weeks.ago
lifetime = 2.weeks
@set = Forgetsy::Set.create('foo', t: lifetime, date: manual_date)
@set.incr('foo_bin', date: 3.weeks.ago)
@set.fetch(bin: 'foo_bin').values.first.should == nil
expect(@set.fetch(bin: 'foo_bin').values.first).to eq(nil)
end
end

describe 'fetch' do
it 'allows fetch by bin name' do
@set.incr_by('foo_bin', 2)
@set.fetch(bin: 'foo_bin', decay: false).should == { 'foo_bin' => 2.0 }
expect(@set.fetch(bin: 'foo_bin', decay: false)).to eq({ 'foo_bin' => 2.0 })
end

it 'can fetch top n bins' do
@set.incr_by('foo_bin', 2)
@set.incr_by('bar_bin', 1)
@set.fetch(n: 2, decay: false).should == { 'foo_bin' => 2.0, 'bar_bin' => 1.0 }
expect(@set.fetch(n: 2, decay: false)).to eq({ 'foo_bin' => 2.0, 'bar_bin' => 1.0 })
end

it 'can fetch a whole set' do
@set.incr_by('foo_bin', 2)
@set.incr_by('bar_bin', 1)
@set.fetch(decay: false).should == { 'foo_bin' => 2.0, 'bar_bin' => 1.0 }
expect(@set.fetch(decay: false)).to eq({ 'foo_bin' => 2.0, 'bar_bin' => 1.0 })
end
end

Expand All @@ -89,8 +89,8 @@
decayed_bar = bar * Math.exp(- rate * time_delta)

@set.decay(date: now)
@set.fetch(bin: 'foo_bin').values.first.round(3).should == decayed_foo.round(3)
@set.fetch(bin: 'bar_bin').values.first.round(3).should == decayed_bar.round(3)
expect(@set.fetch(bin: 'foo_bin').values.first.round(3)).to eq(decayed_foo.round(3))
expect(@set.fetch(bin: 'bar_bin').values.first.round(3)).to eq(decayed_bar.round(3))
end
end

Expand All @@ -100,7 +100,7 @@
lifetime = 1.week
@set = Forgetsy::Set.create('foo', t: lifetime, date: manual_date)
@set.incr('foo_bin')
@set.fetch.values.length.should == 0
expect(@set.fetch.values.length).to eq(0)
end
end
end

0 comments on commit 62cb417

Please sign in to comment.