forked from elliotcm/light_mongo
/
serialization_spec.rb
75 lines (62 loc) · 1.97 KB
/
serialization_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
require File.dirname(__FILE__) + '/../../lib/light_mongo'
describe LightMongo::Document::Serialization do
before(:each) do
LightMongo.stub!(:database => mock(:database, :connection => nil), :slow_serialization => true)
@test_class_collection = mock(:test_class_collection)
Mongo::Collection.stub!(:new => @test_class_collection)
class TestClass
include LightMongo::Document
attr_accessor :test_attribute
end
end
describe "#export" do
before(:each) do
@test_object = TestClass.new
@id = mock(:id)
@test_class_collection.stub!(:save => @id)
end
context "if Persistence has been included" do
it "saves itself" do
@test_object.should_receive(:save)
@test_object.export
end
it "generates a hash of its class name, id, and embed status" do
@test_object.export.should == {'_class_name' => 'TestClass', '_id' => @id, '_embed' => true}
end
end
context "if Persistence hasn't been included" do
before(:each) do
class NoPersistence
include LightMongo::Document::Serialization
end
@no_persistence = NoPersistence.new
end
it "returns self" do
@no_persistence.export.should == @no_persistence
end
end
end
describe "#initialize(params)" do
before(:each) do
@test_value = mock(:test_value).to_s
end
context "when given a hash" do
before(:each) do
@params = {:test_attribute => @test_value}
end
it "converts the hash to attributes" do
@test_object = TestClass.new(@params)
@test_object.test_attribute.should == @test_value
end
end
end
describe "#from_hash(hash)" do
before(:each) do
@test_object = TestClass.new
end
it "parses a hash into instance attributes" do
@test_object.from_hash(:test_attribute => 'Test value')
@test_object.test_attribute.should == 'Test value'
end
end
end