Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

use fakeredis for testing

  • Loading branch information...
commit c1ef1887ddac96fb9895208c9a7b19c460b50dca 1 parent dc08261
@blahed authored
View
11 lib/von/counter.rb
@@ -2,6 +2,8 @@ module Von
class Counter
PARENT_REGEX = /:?[^:]+\z/
+ attr_reader :field
+
# Initialize a new Counter
#
# field - counter field name
@@ -28,9 +30,9 @@ def increment
# Increment periods associated with this key
def increment_periods
- return unless Von.config.periods.has_key?(@field.to_sym)
+ return unless Von.config.periods.has_key?(@field)
- Von.config.periods[@field.to_sym].each do |key, period|
+ Von.config.periods[@field].each do |key, period|
Von.connection.hincrby(period.hash_key, period.field, 1)
unless Von.connection.lrange(period.list_key, 0, -1).include?(period.field)
Von.connection.rpush(period.list_key, period.field)
@@ -63,7 +65,8 @@ def self.increment(field)
#
# Returns an Integer count
def count
- Von.connection.hget(hash_key, 'total')
+ count = Von.connection.hget(hash_key, 'total')
+ count.nil? ? 0 : count.to_i
end
# Count the fields for the given time period for this Counter.
@@ -83,7 +86,7 @@ def count_period(period)
end
keys = Von.connection.hgetall("#{hash_key}:#{period}")
- _counts.map { |date| { date => keys.fetch(date, 0) }}
+ _counts.map { |date| { date => keys.fetch(date, 0).to_i }}
end
# Lookup the count for this Counter in Redis.
View
8 test/config_test.rb
@@ -33,8 +33,12 @@
config.counter 'foo', :best => [ :month, :year ]
end
- Von.config.bests[:bar].must_equal [ :day ]
- Von.config.bests[:foo].must_equal [ :month, :year ]
+ Von.config.bests[:bar].first.must_be_instance_of Von::Period
+ Von.config.bests[:bar].first.period.must_equal :daily
+ Von.config.bests[:foo].first.must_be_instance_of Von::Period
+ Von.config.bests[:foo].first.period.must_equal :monthly
+ Von.config.bests[:foo].last.must_be_instance_of Von::Period
+ Von.config.bests[:foo].last.period.must_equal :yearly
end
end
View
92 test/counter_test.rb
@@ -6,106 +6,100 @@
before :each do
Timecop.freeze(Time.local(2013, 01, 01, 01, 01))
Von.config.init!
- mock_connection!
+ @redis = Redis.new
+ @redis.flushall
end
it "increments the total counter if given a single key" do
Counter.increment('foo')
-
- @store.has_key?('von:counters:foo').must_equal true
- @store['von:counters:foo']['total'].must_equal 1
+ @redis.hget('von:counters:foo', 'total').must_equal '1'
Counter.increment('foo')
- @store['von:counters:foo']['total'].must_equal 2
+ @redis.hget('von:counters:foo', 'total').must_equal '2'
end
it "increments the total counter for a key and it's parent keys" do
Counter.increment('foo:bar')
-
- @store.has_key?('von:counters:foo').must_equal true
- @store['von:counters:foo']['total'].must_equal 1
- @store.has_key?('von:counters:foo:bar').must_equal true
- @store['von:counters:foo:bar']['total'].must_equal 1
-
+
+ @redis.hget('von:counters:foo', 'total').must_equal '1'
+ @redis.hget('von:counters:foo:bar', 'total').must_equal '1'
+
Counter.increment('foo:bar')
- @store['von:counters:foo']['total'].must_equal 2
- @store['von:counters:foo:bar']['total'].must_equal 2
+ @redis.hget('von:counters:foo', 'total').must_equal '2'
+ @redis.hget('von:counters:foo:bar', 'total').must_equal '2'
end
-
+
it "increments a month counter" do
Von.configure do |config|
config.counter 'foo', :monthly => 1
end
-
+
Counter.increment('foo')
Counter.increment('foo')
-
- @store.has_key?('von:counters:foo').must_equal true
- @store.has_key?('von:counters:foo:monthly').must_equal true
- @store['von:counters:foo']['total'].must_equal 2
- @store['von:counters:foo:monthly']['2013-01'].must_equal 2
- @store['von:lists:foo:monthly'].size.must_equal 1
+
+ @redis.hget('von:counters:foo', 'total').must_equal '2'
+ @redis.hget('von:counters:foo:monthly', '2013-01').must_equal '2'
+ @redis.lrange('von:lists:foo:monthly', 0, -1).size.must_equal 1
+ @redis.lrange('von:lists:foo:monthly', 0, -1).first.must_equal '2013-01'
end
-
+
it 'increments a minute counter' do
Von.configure do |config|
config.counter 'foo', :minutely => 60
end
-
+
Counter.increment('foo')
Counter.increment('foo')
-
- @store.has_key?('von:counters:foo').must_equal true
- @store.has_key?('von:counters:foo:minutely').must_equal true
- @store['von:counters:foo']['total'].must_equal 2
- @store['von:counters:foo:minutely']['2013-01-01 01:01'].must_equal 2
- @store['von:lists:foo:minutely'].size.must_equal 1
+
+ @redis.hget('von:counters:foo', 'total').must_equal '2'
+ @redis.hget('von:counters:foo:minutely', '2013-01-01 01:01').must_equal '2'
+ @redis.lrange('von:lists:foo:minutely', 0, -1).size.must_equal 1
+ @redis.lrange('von:lists:foo:minutely', 0, -1).first.must_equal '2013-01-01 01:01'
end
-
+
it "expires counters past the limit" do
Von.configure do |config|
config.counter 'foo', :monthly => 1
end
-
+
Counter.increment('foo')
Timecop.freeze(Time.local(2013, 02))
Counter.increment('foo')
-
- @store.has_key?('von:counters:foo').must_equal true
- @store.has_key?('von:counters:foo:monthly').must_equal true
- @store['von:counters:foo']['total'].must_equal 2
- @store['von:counters:foo:monthly'].has_key?('2013-02').must_equal true
- @store['von:lists:foo:monthly'].size.must_equal 1
+
+ @redis.hget('von:counters:foo', 'total').must_equal '2'
+ @redis.hget('von:counters:foo:monthly', '2013-02').must_equal '1'
+ @redis.lrange('von:lists:foo:monthly', 0, -1).size.must_equal 1
+ @redis.lrange('von:lists:foo:monthly', 0, -1).first.must_equal '2013-02'
end
-
+
it "gets a total count for a counter" do
Counter.increment('foo')
Counter.increment('foo')
Counter.increment('foo')
-
+
Von.count('foo').must_equal 3
end
-
+
it "gets a count for a time period and 0s missing entries" do
Von.configure do |config|
config.counter 'foo', :monthly => 1, :hourly => 6
end
-
+
Timecop.freeze(Time.local(2013, 02, 01, 05))
Counter.increment('foo')
Timecop.freeze(Time.local(2013, 02, 01, 07))
Counter.increment('foo')
-
+
Von.count('foo').must_equal 2
-
+
Von.count('foo', :monthly).must_equal [{"2013-02" => 2}]
Von.count('foo', :hourly).must_equal [
- {"2013-02-01 02:00"=>0},
- {"2013-02-01 03:00"=>0},
- {"2013-02-01 04:00"=>0},
- {"2013-02-01 05:00"=>1},
- {"2013-02-01 06:00"=>0},
- {"2013-02-01 07:00"=>1}
+ { "2013-02-01 02:00" => 0 },
+ { "2013-02-01 03:00" => 0 },
+ { "2013-02-01 04:00" => 0 },
+ { "2013-02-01 05:00" => 1 },
+ { "2013-02-01 06:00" => 0 },
+ { "2013-02-01 07:00" => 1 }
]
end
View
64 test/test_helper.rb
@@ -6,68 +6,8 @@
require 'minitest/autorun'
require 'minitest/pride'
+require 'fakeredis'
require 'mocha'
require 'timecop'
-require 'von'
-
-module Von
- class TestConnection
- attr_reader :store
-
- def initialize
- @store = {}
- end
-
- def hincrby(hash, key, counter)
- @store[hash] ||= {}
-
- if @store[hash].has_key?(key)
- @store[hash][key] += counter
- else
- @store[hash][key] = counter
- end
-
- @store[hash][key]
- end
-
- def hget(hash, key)
- @store[hash][key]
- end
-
- def hgetall(hash)
- @store.fetch(hash, {})
- end
-
- def hdel(hash, key)
- @store[hash].delete(key)
- end
-
- def rpush(list, member)
- @store[list] ||= []
- @store[list] << member
- end
-
- def lpop(list)
- @store[list].shift
- end
-
- def lrange(list, start, stop)
- @store[list] ||= []
- @store[list][start..stop]
- end
-
- def llen(list)
- @store[list].length
- end
-
- end
-end
-
-module MiniTest::Expectations
- def mock_connection!
- connection = Von::TestConnection.new
- @store = connection.store
- Von.expects(:connection).returns(connection).at_least_once
- end
-end
+require 'von'
View
5 test/von_test.rb
@@ -7,8 +7,9 @@
end
it "increments a counter and counts it" do
- mock_connection!
-
+ @redis = Redis.new
+ @redis.flushall
+
Von.increment('foo')
Von.increment('foo')
Von.count('foo').must_equal 2
View
1  von.gemspec
@@ -22,6 +22,7 @@ Gem::Specification.new do |gem|
gem.add_development_dependency 'rake', '~> 10.0.3'
gem.add_development_dependency 'minitest', '~> 3.0.0'
+ gem.add_development_dependency 'fakeredis', '~> 0.4.1'
gem.add_development_dependency 'mocha', '~> 0.11.4'
gem.add_development_dependency 'timecop', '~> 0.5.9.1'
end
Please sign in to comment.
Something went wrong with that request. Please try again.