spec/trackoid_spec.rb in trackoid-0.1.5 vs spec/trackoid_spec.rb in trackoid-0.1.6
- old
+ new
@@ -7,11 +7,10 @@
field :name # Dummy field
track :visits
end
describe Mongoid::Tracking do
-
before(:all) do
@trackoid_version = File.read(File.expand_path("../VERSION", File.dirname(__FILE__)))
end
it "should expose the same version as the VERSION file" do
@@ -36,11 +35,10 @@
end
}.should_not raise_error
end
describe "when creating a new field with stats" do
-
before(:all) do
@mock = Test.new
end
it "should deny access to the underlying mongoid field" do
@@ -94,15 +92,13 @@
end
it "should not be aggregated" do
@mock.aggregated?.should be_false
end
-
end
describe "when using a model in the database" do
-
before(:all) do
Test.delete_all
Test.create(:name => "test")
@object_id = Test.first.id
end
@@ -141,14 +137,88 @@
it "should correctly handle the last 7 days" do
@mock.visits.last_days.should == [0, 0, 0, 0, 0, 2, 2]
end
+ it "string dates should work" do
+ @mock.visits.inc("2010-07-11")
+ @mock.visits.on("2010-07-11").should == 1
+ end
+
+ it "should give the first date with first_date" do
+ @mock.visits.first_date.should == Date.parse("2010-07-11")
+ end
+
+ it "should give the last date with last_date" do
+ @mock.visits.last_date.should == Date.today
+ end
+
+ it "should give the first value" do
+ @mock.visits.first.should == 1
+ end
+
+ it "should give the last value" do
+ @mock.visits.last.should == 2
+ end
end
- context "testing accessor operations without reloading models" do
+ describe "Testing accessors with an empty model" do
+ before do
+ Test.delete_all
+ Test.create(:name => "test")
+ @object_id = Test.first.id
+ @mock = Test.first
+ end
+ it "should return nil for .first_date" do
+ @mock.visits.first_date.should be_nil
+ end
+
+ it "should return nil for .last_date" do
+ @mock.visits.last_date.should be_nil
+ end
+
+ it "should return nil for .first" do
+ @mock.visits.first.should be_nil
+ end
+
+ it "should return nil for .last" do
+ @mock.visits.last.should be_nil
+ end
+
+ it "should return nil for .all" do
+ @mock.visits.all.should be_nil
+ end
+ end
+
+ describe "Testing new range accessors with an empty model" do
+ before do
+ Test.delete_all
+ Test.create(:name => "test")
+ @object_id = Test.first.id
+ @mock = Test.first
+ end
+
+ it "should return the correct values for .all" do
+ @mock.visits.set(1, "2010-07-11")
+ @mock.visits.set(2, "2010-07-12")
+ @mock.visits.set(3, "2010-07-13")
+
+ @mock.visits.all.should == [1, 2, 3]
+ end
+
+ it "should return the correct values for .all (Take II)" do
+ @mock.visits.set(5, "2010-07-01")
+ @mock.visits.set(10, "2010-07-30")
+
+ @mock.visits.all.should == [5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10]
+ @mock.visits.last.should == 10
+ @mock.visits.first.should == 5
+ end
+ end
+
+ context "testing accessor operations without reloading models" do
before(:all) do
Test.delete_all
Test.create(:name => "test")
@object_id = Test.first.id
end
@@ -193,19 +263,14 @@
it "on() accessor must work on dates as Ranges" do
# We have data for today as previous tests populated the visits field
@mock.visits.on(Date.parse("2010-04-30")..Date.parse("2010-05-02")).should == [0, 10, 0]
end
-
-
end
-
-
context "regression test for github issues" do
-
- it "should not raise undefined method [] for nil:NilClass for objects already saved" do
+ it "should not raise undefined method [] for nil:NilClass when adding a new track into an existing object" do
class TestModel
include Mongoid::Document
include Mongoid::Tracking
field :name
end
@@ -218,9 +283,7 @@
tm = TestModel.first
tm.something.today.should == 0
tm.something.inc
tm.something.today.should == 1
end
-
end
-
end