rom 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +2 -0
- data/.travis.yml +22 -0
- data/Changelog.md +16 -0
- data/Gemfile +13 -6
- data/Gemfile.devtools +71 -0
- data/Guardfile +19 -0
- data/LICENSE +1 -1
- data/README.md +52 -30
- data/Rakefile +3 -0
- data/config/devtools.yml +4 -0
- data/config/flay.yml +3 -0
- data/config/flog.yml +2 -0
- data/config/mutant.yml +5 -0
- data/config/reek.yml +103 -0
- data/config/rubocop.yml +62 -0
- data/config/yardstick.yml +2 -0
- data/lib/rom.rb +13 -5
- data/lib/rom/constants.rb +16 -0
- data/lib/rom/environment.rb +105 -0
- data/lib/rom/environment/builder.rb +71 -0
- data/lib/rom/mapper.rb +176 -0
- data/lib/rom/mapper/attribute.rb +108 -0
- data/lib/rom/mapper/builder.rb +58 -0
- data/lib/rom/mapper/builder/definition.rb +162 -0
- data/lib/rom/mapper/header.rb +103 -0
- data/lib/rom/mapper/loader_builder.rb +26 -0
- data/lib/rom/relation.rb +375 -0
- data/lib/rom/repository.rb +71 -0
- data/lib/rom/schema.rb +21 -0
- data/lib/rom/schema/builder.rb +59 -0
- data/lib/rom/schema/definition.rb +84 -0
- data/lib/rom/schema/definition/relation.rb +80 -0
- data/lib/rom/schema/definition/relation/base.rb +27 -0
- data/lib/rom/session.rb +111 -0
- data/lib/rom/session/environment.rb +67 -0
- data/lib/rom/session/identity_map.rb +43 -0
- data/lib/rom/session/mapper.rb +62 -0
- data/lib/rom/session/relation.rb +140 -0
- data/lib/rom/session/state.rb +59 -0
- data/lib/rom/session/state/created.rb +22 -0
- data/lib/rom/session/state/deleted.rb +25 -0
- data/lib/rom/session/state/persisted.rb +34 -0
- data/lib/rom/session/state/transient.rb +20 -0
- data/lib/rom/session/state/updated.rb +29 -0
- data/lib/rom/session/tracker.rb +62 -0
- data/lib/rom/support/axiom/adapter.rb +111 -0
- data/lib/rom/support/axiom/adapter/data_objects.rb +38 -0
- data/lib/rom/support/axiom/adapter/memory.rb +25 -0
- data/lib/rom/support/axiom/adapter/postgres.rb +19 -0
- data/lib/rom/support/axiom/adapter/sqlite3.rb +20 -0
- data/lib/version.rb +1 -1
- data/rom.gemspec +7 -3
- data/spec/integration/environment_setup_spec.rb +24 -0
- data/spec/integration/grouped_mappers_spec.rb +87 -0
- data/spec/integration/join_and_group_spec.rb +76 -0
- data/spec/integration/join_and_wrap_spec.rb +68 -0
- data/spec/integration/mapping_embedded_relations_spec.rb +73 -0
- data/spec/integration/mapping_relations_spec.rb +120 -0
- data/spec/integration/schema_definition_spec.rb +152 -0
- data/spec/integration/session_spec.rb +87 -0
- data/spec/integration/wrapped_mappers_spec.rb +73 -0
- data/spec/shared/unit/environment_context.rb +6 -0
- data/spec/shared/unit/loader.rb +11 -0
- data/spec/shared/unit/loader_identity.rb +13 -0
- data/spec/shared/unit/mapper_context.rb +11 -0
- data/spec/shared/unit/relation_context.rb +82 -0
- data/spec/shared/unit/session_environment_context.rb +11 -0
- data/spec/shared/unit/session_relation_context.rb +18 -0
- data/spec/spec_helper.rb +49 -0
- data/spec/support/helper.rb +34 -0
- data/spec/support/ice_nine_config.rb +10 -0
- data/spec/support/test_mapper.rb +110 -0
- data/spec/unit/rom/environment/builder/mapping_spec.rb +24 -0
- data/spec/unit/rom/environment/builder/schema_spec.rb +33 -0
- data/spec/unit/rom/environment/class_methods/setup_spec.rb +18 -0
- data/spec/unit/rom/environment/repository_spec.rb +21 -0
- data/spec/unit/rom/mapper/attribute/embedded_collection/to_ast_spec.rb +18 -0
- data/spec/unit/rom/mapper/attribute/embedded_value/to_ast_spec.rb +16 -0
- data/spec/unit/rom/mapper/attribute/rename_spec.rb +9 -0
- data/spec/unit/rom/mapper/attribute/to_ast_spec.rb +9 -0
- data/spec/unit/rom/mapper/builder/class_methods/call_spec.rb +61 -0
- data/spec/unit/rom/mapper/class_methods/build_spec.rb +55 -0
- data/spec/unit/rom/mapper/dump_spec.rb +11 -0
- data/spec/unit/rom/mapper/group_spec.rb +35 -0
- data/spec/unit/rom/mapper/header/each_spec.rb +26 -0
- data/spec/unit/rom/mapper/header/element_reader_spec.rb +21 -0
- data/spec/unit/rom/mapper/header/group_spec.rb +18 -0
- data/spec/unit/rom/mapper/header/join_spec.rb +14 -0
- data/spec/unit/rom/mapper/header/keys_spec.rb +29 -0
- data/spec/unit/rom/mapper/header/project_spec.rb +13 -0
- data/spec/unit/rom/mapper/header/rename_spec.rb +11 -0
- data/spec/unit/rom/mapper/header/to_ast_spec.rb +11 -0
- data/spec/unit/rom/mapper/header/wrap_spec.rb +18 -0
- data/spec/unit/rom/mapper/identity_from_tuple_spec.rb +11 -0
- data/spec/unit/rom/mapper/identity_spec.rb +11 -0
- data/spec/unit/rom/mapper/join_spec.rb +15 -0
- data/spec/unit/rom/mapper/load_spec.rb +11 -0
- data/spec/unit/rom/mapper/new_object_spec.rb +14 -0
- data/spec/unit/rom/mapper/project_spec.rb +11 -0
- data/spec/unit/rom/mapper/rename_spec.rb +16 -0
- data/spec/unit/rom/mapper/wrap_spec.rb +35 -0
- data/spec/unit/rom/relation/delete_spec.rb +15 -0
- data/spec/unit/rom/relation/drop_spec.rb +11 -0
- data/spec/unit/rom/relation/each_spec.rb +23 -0
- data/spec/unit/rom/relation/first_spec.rb +19 -0
- data/spec/unit/rom/relation/group_spec.rb +29 -0
- data/spec/unit/rom/relation/inject_mapper_spec.rb +17 -0
- data/spec/unit/rom/relation/insert_spec.rb +13 -0
- data/spec/unit/rom/relation/last_spec.rb +19 -0
- data/spec/unit/rom/relation/one_spec.rb +49 -0
- data/spec/unit/rom/relation/rename_spec.rb +21 -0
- data/spec/unit/rom/relation/replace_spec.rb +13 -0
- data/spec/unit/rom/relation/restrict_spec.rb +25 -0
- data/spec/unit/rom/relation/sort_by_spec.rb +25 -0
- data/spec/unit/rom/relation/take_spec.rb +11 -0
- data/spec/unit/rom/relation/to_a_spec.rb +20 -0
- data/spec/unit/rom/relation/update_spec.rb +25 -0
- data/spec/unit/rom/relation/wrap_spec.rb +29 -0
- data/spec/unit/rom/repository/class_methods/build_spec.rb +27 -0
- data/spec/unit/rom/repository/element_reader_spec.rb +21 -0
- data/spec/unit/rom/repository/element_writer_spec.rb +18 -0
- data/spec/unit/rom/schema/builder/class_methods/build_spec.rb +103 -0
- data/spec/unit/rom/schema/element_reader_spec.rb +15 -0
- data/spec/unit/rom/session/class_methods/start_spec.rb +23 -0
- data/spec/unit/rom/session/clean_predicate_spec.rb +21 -0
- data/spec/unit/rom/session/environment/element_reader_spec.rb +13 -0
- data/spec/unit/rom/session/flush_spec.rb +58 -0
- data/spec/unit/rom/session/mapper/load_spec.rb +47 -0
- data/spec/unit/rom/session/relation/delete_spec.rb +28 -0
- data/spec/unit/rom/session/relation/dirty_predicate_spec.rb +35 -0
- data/spec/unit/rom/session/relation/identity_spec.rb +11 -0
- data/spec/unit/rom/session/relation/new_spec.rb +50 -0
- data/spec/unit/rom/session/relation/save_spec.rb +50 -0
- data/spec/unit/rom/session/relation/state_spec.rb +23 -0
- data/spec/unit/rom/session/relation/track_spec.rb +23 -0
- data/spec/unit/rom/session/relation/tracking_predicate_spec.rb +23 -0
- data/spec/unit/rom/session/relation/update_attributes_spec.rb +45 -0
- data/spec/unit/rom/session/state_spec.rb +79 -0
- metadata +212 -11
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Relation, '#sort_by' do
|
6
|
+
include_context 'Relation'
|
7
|
+
|
8
|
+
share_examples_for 'sorted relation' do
|
9
|
+
specify do
|
10
|
+
should eql([jack, jade, jane, john])
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'with a list of attribute names' do
|
15
|
+
subject { relation.sort_by([:name]).to_a }
|
16
|
+
|
17
|
+
it_behaves_like 'sorted relation'
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'with a block' do
|
21
|
+
subject { relation.sort_by { |r| [r.name] }.to_a }
|
22
|
+
|
23
|
+
it_behaves_like 'sorted relation'
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Relation, '#to_a' do
|
6
|
+
subject(:relation) { described_class.new(axiom_relation, mapper) }
|
7
|
+
|
8
|
+
let(:axiom_relation) { [1, 2] }
|
9
|
+
let(:loaded_objects) { %w(1 2) }
|
10
|
+
fake(:mapper)
|
11
|
+
|
12
|
+
before do
|
13
|
+
stub(mapper).load(1) { '1' }
|
14
|
+
stub(mapper).load(2) { '2' }
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'gets all tuples and loads them via mapper' do
|
18
|
+
expect(relation.to_a).to eql(loaded_objects)
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Relation, '#update' do
|
6
|
+
include_context 'Relation'
|
7
|
+
|
8
|
+
subject { relation.update(john, old_tuple) }
|
9
|
+
|
10
|
+
let!(:old_tuple) { relation.mapper.dump(john) }
|
11
|
+
|
12
|
+
it { should be_instance_of(Relation) }
|
13
|
+
|
14
|
+
before do
|
15
|
+
john.name = 'John Doe'
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'replaces old object with the new one' do
|
19
|
+
expect(subject.restrict(name: 'John Doe').one).to eq(john)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'removes old object' do
|
23
|
+
expect(subject.restrict(name: 'John').count).to be(0)
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Relation, '#wrap' do
|
6
|
+
let(:users) { Relation.new(user_relation, user_mapper) }
|
7
|
+
let(:tasks) { Relation.new(task_relation, task_mapper) }
|
8
|
+
|
9
|
+
fake(:user_relation) { Axiom::Relation }
|
10
|
+
fake(:user_mapper) { Mapper }
|
11
|
+
|
12
|
+
fake(:task_relation) { Axiom::Relation }
|
13
|
+
fake(:task_header) { Axiom::Relation::Header }
|
14
|
+
fake(:task_mapper) { Mapper }
|
15
|
+
|
16
|
+
fake(:wrapped_relation) { Axiom::Relation }
|
17
|
+
fake(:wrapped_mapper) { Mapper }
|
18
|
+
|
19
|
+
it "wraps relation and mapper" do
|
20
|
+
stub(task_relation).header { task_header }
|
21
|
+
stub(user_relation).wrap(:tasks => task_header) { wrapped_relation }
|
22
|
+
stub(user_mapper).wrap(:tasks => task_mapper) { wrapped_mapper }
|
23
|
+
|
24
|
+
expect(users.wrap(:tasks => tasks)).to eql(Relation.new(wrapped_relation, wrapped_mapper))
|
25
|
+
|
26
|
+
expect(user_relation).to have_received.wrap(:tasks => task_header)
|
27
|
+
expect(user_mapper).to have_received.wrap(:tasks => task_mapper)
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Repository, '.build' do
|
6
|
+
subject { described_class.build(name, uri) }
|
7
|
+
|
8
|
+
let(:name) { :test }
|
9
|
+
|
10
|
+
context 'with a registered uri scheme' do
|
11
|
+
let(:uri) { Addressable::URI.parse('memory://test') }
|
12
|
+
|
13
|
+
it { should be_instance_of(described_class) }
|
14
|
+
|
15
|
+
its(:name) { should be(name) }
|
16
|
+
its(:adapter) { should eq(Axiom::Adapter.build(uri)) }
|
17
|
+
end
|
18
|
+
|
19
|
+
context 'with an unregistered uri scheme' do
|
20
|
+
let(:uri) { Addressable::URI.parse('unregistered://test') }
|
21
|
+
let(:msg) { "#{uri.scheme.inspect} is no registered uri scheme" }
|
22
|
+
|
23
|
+
specify do
|
24
|
+
expect { subject }.to raise_error(Axiom::UnknownAdapterError, msg)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Repository, '#[]' do
|
6
|
+
subject { object[relation_name] }
|
7
|
+
|
8
|
+
let(:object) { described_class.build(name, uri) }
|
9
|
+
let(:name) { :bigdata }
|
10
|
+
let(:uri) { Addressable::URI.parse('memory://test') }
|
11
|
+
|
12
|
+
let(:relation) { Axiom::Relation::Base.new(relation_name, header) }
|
13
|
+
let(:relation_name) { :test }
|
14
|
+
let(:header) { [] }
|
15
|
+
|
16
|
+
before do
|
17
|
+
object[relation_name] = relation
|
18
|
+
end
|
19
|
+
|
20
|
+
it { should eq(relation) }
|
21
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Repository, '#[]=' do
|
6
|
+
subject { object[:users] }
|
7
|
+
|
8
|
+
before do
|
9
|
+
object[:users] = relation
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:object) { Repository.build(:test, Addressable::URI.parse('memory://test')) }
|
13
|
+
let(:relation) { Axiom::Relation::Base.new(:users, []) }
|
14
|
+
|
15
|
+
it { should eq(relation) }
|
16
|
+
|
17
|
+
it { should be_instance_of(Axiom::Relation::Variable::Materialized) }
|
18
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Schema::Builder, '.build' do
|
6
|
+
include_context 'Environment'
|
7
|
+
|
8
|
+
let(:registry) { Hash[test: repository] }
|
9
|
+
|
10
|
+
let(:keys) { [:id] }
|
11
|
+
|
12
|
+
let(:schema) {
|
13
|
+
key_args = keys
|
14
|
+
|
15
|
+
described_class.build(registry) do
|
16
|
+
base_relation :users do
|
17
|
+
repository :test
|
18
|
+
|
19
|
+
attribute :id, Integer
|
20
|
+
attribute :name, String
|
21
|
+
|
22
|
+
key(*key_args)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
}
|
26
|
+
|
27
|
+
let(:header) {
|
28
|
+
Axiom::Relation::Header.coerce([[:id, Integer], [:name, String]], keys: keys)
|
29
|
+
}
|
30
|
+
|
31
|
+
let(:relation) {
|
32
|
+
Axiom::Relation::Base.new(:users, header)
|
33
|
+
}
|
34
|
+
|
35
|
+
fake(:repository)
|
36
|
+
fake(:gateway) { Axiom::Relation }
|
37
|
+
|
38
|
+
before do
|
39
|
+
stub(repository).[]=(:users, relation) { gateway }
|
40
|
+
stub(repository).[](:users) { gateway }
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.it_registers_relation
|
44
|
+
it 'registers base relation in the repository' do
|
45
|
+
expect(subject[:users]).to be(gateway)
|
46
|
+
expect(repository).to have_received.[]=(:users, relation)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context 'defining base relations' do
|
51
|
+
subject { schema }
|
52
|
+
|
53
|
+
context 'with a single key' do
|
54
|
+
it_registers_relation
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'with multiple keys' do
|
58
|
+
let(:keys) { [:id, :name] }
|
59
|
+
|
60
|
+
it_registers_relation
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'defining relations' do
|
65
|
+
subject do
|
66
|
+
schema.call do
|
67
|
+
relation :restricted_users do
|
68
|
+
users.restrict(name: 'Jane')
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
it 'registers restricted relation' do
|
74
|
+
stub(gateway).restrict(name: 'Jane') { gateway }
|
75
|
+
|
76
|
+
expect(subject[:restricted_users]).to be(gateway)
|
77
|
+
|
78
|
+
expect(gateway).to have_received.restrict(name: 'Jane')
|
79
|
+
end
|
80
|
+
|
81
|
+
context 'when invalid relation name is used' do
|
82
|
+
subject do
|
83
|
+
schema.call do
|
84
|
+
relation :restricted_users do
|
85
|
+
not_here.restrict
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'raises error' do
|
91
|
+
expect { subject }.to raise_error(
|
92
|
+
NameError, /method `not_here'/
|
93
|
+
)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context 'without block' do
|
99
|
+
subject { Schema::Builder.build({}) }
|
100
|
+
|
101
|
+
it { should be_instance_of(Schema::Builder) }
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Schema, "#[]" do
|
4
|
+
subject(:schema) { Schema.new(users: relation) }
|
5
|
+
|
6
|
+
fake(:relation)
|
7
|
+
|
8
|
+
it "returns registered relation" do
|
9
|
+
expect(schema[:users]).to be(relation)
|
10
|
+
end
|
11
|
+
|
12
|
+
it "raises error when relation is missing" do
|
13
|
+
expect { schema[:not_here] }.to raise_error
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Session, '.start' do
|
6
|
+
include_context 'Session::Relation'
|
7
|
+
|
8
|
+
let(:user) { model.new(id: 3, name: 'Piotr') }
|
9
|
+
|
10
|
+
it 'starts a new session' do
|
11
|
+
Session.start(users: relation) do |session|
|
12
|
+
expect(session).to be_clean
|
13
|
+
expect(session).to be_instance_of(Session)
|
14
|
+
expect(session[:users]).to be_instance_of(Session::Relation)
|
15
|
+
|
16
|
+
session[:users].track(user).save(user)
|
17
|
+
|
18
|
+
session.flush
|
19
|
+
end
|
20
|
+
|
21
|
+
expect(relation.to_a).to include(user)
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Session, '#clean?' do
|
6
|
+
subject { session.clean? }
|
7
|
+
|
8
|
+
include_context 'Session::Relation'
|
9
|
+
|
10
|
+
context 'when tracker has no pending state changes' do
|
11
|
+
it { should be_true }
|
12
|
+
end
|
13
|
+
|
14
|
+
context 'when tracker has pending state changes' do
|
15
|
+
before do
|
16
|
+
session[:users].delete(user)
|
17
|
+
end
|
18
|
+
|
19
|
+
it { should be_false }
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Session::Environment, '#[]' do
|
6
|
+
subject { object[:users] }
|
7
|
+
|
8
|
+
include_context 'Session::Environment'
|
9
|
+
|
10
|
+
it 'returns session relation proxy' do
|
11
|
+
expect(subject).to be_kind_of(Session::Relation)
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Session, '#flush' do
|
6
|
+
subject { session.flush }
|
7
|
+
|
8
|
+
include_context 'Session::Relation'
|
9
|
+
|
10
|
+
let(:object) { session }
|
11
|
+
|
12
|
+
let(:john) { session[:users].to_a.first }
|
13
|
+
let(:jane) { session[:users].to_a.last }
|
14
|
+
let(:piotr) { session[:users].new(id: 3, name: 'Piotr') }
|
15
|
+
|
16
|
+
before do
|
17
|
+
session[:users].delete(john)
|
18
|
+
|
19
|
+
jane.name = 'Jane Doe'
|
20
|
+
session[:users].save(jane)
|
21
|
+
|
22
|
+
session[:users].save(piotr)
|
23
|
+
end
|
24
|
+
|
25
|
+
it_behaves_like 'a command method'
|
26
|
+
|
27
|
+
it { should be_clean }
|
28
|
+
|
29
|
+
it 'commits all deletes' do
|
30
|
+
expect(subject[:users].to_a).to_not include(john)
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'commits all updates' do
|
34
|
+
expect(subject[:users].to_a.first).to eq(relation.to_a.first)
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'commits all inserts' do
|
38
|
+
expect(subject[:users].to_a).to include(piotr)
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'sets correct state for created objects' do
|
42
|
+
expect(subject[:users].state(piotr)).to be_persisted
|
43
|
+
expect(subject[:users].dirty?(piotr)).to be(false)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'registers newly created object in the IM' do
|
47
|
+
expect(subject[:users].restrict { |r| r.name.eq('Piotr') }.to_a.first).to be(piotr)
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'sets correct state for updated objects' do
|
51
|
+
expect(subject[:users].state(jane)).to be_persisted
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'sets correct state for deleted objects' do
|
55
|
+
expect(subject[:users].state(john)).to be_frozen
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe Session::Mapper, '#load' do
|
6
|
+
subject { object.load(tuple) }
|
7
|
+
|
8
|
+
let(:object) { described_class.new(mapper, tracker, im) }
|
9
|
+
|
10
|
+
let(:mapper) { fake(:mapper) { ROM::Mapper } }
|
11
|
+
|
12
|
+
let(:tuple) { Hash[id: identity, name: 'Jane'] }
|
13
|
+
let(:identity) { [1] }
|
14
|
+
let(:user) { model.new(tuple) }
|
15
|
+
let(:model) { mock_model(:id, :name) }
|
16
|
+
let(:im) { Session::IdentityMap.build }
|
17
|
+
let(:tracker) { Session::Tracker.new }
|
18
|
+
|
19
|
+
before do
|
20
|
+
stub(mapper).identity_from_tuple(tuple) { identity }
|
21
|
+
stub(mapper).identity(user) { identity }
|
22
|
+
stub(mapper).load(tuple) { user }
|
23
|
+
end
|
24
|
+
|
25
|
+
context 'when IM does not include the loaded object' do
|
26
|
+
it 'loads the object' do
|
27
|
+
expect(subject).to be(user)
|
28
|
+
mapper.should have_received.load(tuple)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'stores persisted state in the tracker' do
|
32
|
+
expect(subject).to be(user)
|
33
|
+
expect(tracker.fetch(identity)).to be_persisted
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context 'when IM includes the loaded object' do
|
38
|
+
before do
|
39
|
+
im.store(identity, user, tuple)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'returns already loaded object' do
|
43
|
+
expect(subject).to be(user)
|
44
|
+
mapper.should_not have_received.load(tuple)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|