Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changing module structure to conform with Mongoid 2.4.0
- Loading branch information
Showing
3 changed files
with
53 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,70 @@ | ||
# -*- encoding: utf-8 -*- | ||
require 'helper' | ||
require 'duration/mongoid' | ||
require 'mongoid' | ||
|
||
describe "mongoid support" do | ||
before do | ||
@mongoid_field = Mongoid::Fields::Serializable::Duration.new | ||
class MyModel | ||
This comment has been minimized.
Sorry, something went wrong. |
||
include Mongoid::Document | ||
field :duration, type: Duration | ||
end | ||
@model = MyModel.new | ||
end | ||
|
||
describe "#get - Mongoid's deserialization" do | ||
describe "assigning an integer value" do | ||
it "should return the duration given the total in seconds" do | ||
assert_equal Duration.new(90), @mongoid_field.deserialize(90) | ||
@model.duration = 90 | ||
assert_equal @model.duration, Duration.new(90) | ||
end | ||
|
||
it "should return nil for nil serialized values" do | ||
assert_nil @mongoid_field.deserialize(nil) | ||
it "assigning a nil value" do | ||
@model.duration = nil | ||
assert_nil @model.duration | ||
end | ||
end | ||
|
||
describe "#set - Mongoid's serialization" do | ||
it "should serialize to nil given an invalid serialized value or nil" do | ||
assert_nil @mongoid_field.serialize([1,2,3]) | ||
assert_nil @mongoid_field.serialize(nil) | ||
assert_nil @mongoid_field.serialize("") | ||
assert_nil @mongoid_field.serialize({}) | ||
assert_nil @mongoid_field.serialize({:seconds => "", :hours => ""}) | ||
assert_nil @mongoid_field.serialize({:x => 100, :seconds => ""}) | ||
|
||
describe "assigning an array" do | ||
it "should return nil" do | ||
@model.duration = [1,2,3] | ||
assert_nil @model.duration | ||
end | ||
|
||
it "should return total seconds given a duration" do | ||
assert_equal 90, @mongoid_field.serialize(Duration.new(:minutes => 1, :seconds => 30)) | ||
end | ||
|
||
describe "assigning a valid hash" do | ||
it "should return total seconds given a duration in hash" do | ||
@model.duration = { :minutes => 1, :seconds => 30 } | ||
assert_equal Duration.new({ :minutes => 1, :seconds => 30 }), @model.duration | ||
end | ||
end | ||
|
||
it "should return total seconds given a duration in seconds" do | ||
assert_equal 10, @mongoid_field.serialize(10) | ||
end | ||
|
||
it "should return total seconds given a duration in string" do | ||
assert_equal 10, @mongoid_field.serialize("10") | ||
assert_equal 10, @mongoid_field.serialize("10string") | ||
assert_equal 0, @mongoid_field.serialize("string") # not blank | ||
describe "assigning invalid hashes" do | ||
it "should return nil" do | ||
[{}, {:seconds => "", :hours => ""}, {:x => 100, :seconds => ""}].each do |value| | ||
@model.duration = value | ||
assert_nil @model.duration | ||
end | ||
end | ||
end | ||
|
||
it "should return total seconds given a duration in hash" do | ||
assert_equal 90, @mongoid_field.serialize(:minutes => 1, :seconds => 30) | ||
describe "assigning a Duration object" do | ||
it "should return the duration value" do | ||
duration = Duration.new(:minutes => 1, :seconds => 30) | ||
@model.duration = duration | ||
assert_equal duration, @model.duration | ||
end | ||
end | ||
|
||
describe "assigning a string" do | ||
it "should return total seconds given a duration in string" do | ||
@model.duration = "10" | ||
assert_equal Duration.new(10), @model.duration | ||
|
||
@model.duration = "10string" | ||
assert_equal Duration.new(10), @model.duration | ||
|
||
@model.duration = "string" | ||
assert_equal Duration.new(0), @model.duration | ||
end | ||
end | ||
end |
It's not a good idea to define a class inside the before block. This way the test suite will try to redefine the class for each "it" spec.