rom 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- 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,87 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
describe 'Session' do
|
|
6
|
+
let(:users) { TEST_ENV.schema[:users] }
|
|
7
|
+
let(:relation) { TEST_ENV[:users] }
|
|
8
|
+
let(:mapper) { relation.mapper }
|
|
9
|
+
let(:model) { mapper.model }
|
|
10
|
+
|
|
11
|
+
before do
|
|
12
|
+
users.insert([[1, 'John'], [2, 'Jane']])
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
after do
|
|
16
|
+
users.delete([[1, 'John'], [2, 'Jane'], [2, 'Jane Doe'], [3, 'Piotr']])
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
specify 'fetching an object from a relation' do
|
|
20
|
+
TEST_ENV.session do |session|
|
|
21
|
+
# fetch user for the first time
|
|
22
|
+
jane1 = session[:users].restrict(name: 'Jane').one
|
|
23
|
+
|
|
24
|
+
expect(jane1).to eq(model.new(id: 2, name: 'Jane'))
|
|
25
|
+
|
|
26
|
+
# here IM-powered loader kicks in
|
|
27
|
+
jane2 = session[:users].restrict(name: 'Jane').one
|
|
28
|
+
|
|
29
|
+
expect(jane1).to be(jane2)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
specify 'deleting an object from a relation' do
|
|
34
|
+
TEST_ENV.session do |session|
|
|
35
|
+
jane = session[:users].restrict(name: 'Jane').one
|
|
36
|
+
|
|
37
|
+
session[:users].delete(jane)
|
|
38
|
+
|
|
39
|
+
session.flush
|
|
40
|
+
|
|
41
|
+
expect(relation.to_a).not_to include(jane)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
specify 'saving an object to a relation' do
|
|
46
|
+
TEST_ENV.session do |session|
|
|
47
|
+
piotr = session[:users].new(id: 3, name: 'Piotr')
|
|
48
|
+
|
|
49
|
+
session[:users].save(piotr)
|
|
50
|
+
|
|
51
|
+
session.flush
|
|
52
|
+
|
|
53
|
+
expect(relation.to_a).to include(piotr)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
specify 'updating an object in a relation' do
|
|
58
|
+
TEST_ENV.session do |session|
|
|
59
|
+
jane = session[:users].restrict(id: 2).one
|
|
60
|
+
jane.name = 'Jane Doe'
|
|
61
|
+
|
|
62
|
+
session[:users].save(jane)
|
|
63
|
+
|
|
64
|
+
session.flush
|
|
65
|
+
|
|
66
|
+
expect(relation.count).to be(2)
|
|
67
|
+
|
|
68
|
+
expect(relation.to_a.last).to eql(jane)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
specify 'updating an immutable object in a relation' do
|
|
73
|
+
TEST_ENV.session do |session|
|
|
74
|
+
jane = session[:users].restrict(id: 2).one
|
|
75
|
+
|
|
76
|
+
jane.freeze
|
|
77
|
+
|
|
78
|
+
session[:users].update_attributes(jane, name: 'Jane Doe')
|
|
79
|
+
|
|
80
|
+
session.flush
|
|
81
|
+
|
|
82
|
+
expect(relation.count).to be(2)
|
|
83
|
+
|
|
84
|
+
expect(relation.to_a.last).to eql(jane.update(name: 'Jane Doe'))
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
require 'spec_helper'
|
|
4
|
+
|
|
5
|
+
describe 'Wrapped mappers' do
|
|
6
|
+
let(:env) do
|
|
7
|
+
Environment.setup(test: 'memory://test') do
|
|
8
|
+
schema do
|
|
9
|
+
base_relation :users do
|
|
10
|
+
repository :test
|
|
11
|
+
|
|
12
|
+
attribute :id, Integer
|
|
13
|
+
attribute :name, String
|
|
14
|
+
|
|
15
|
+
key :id
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
base_relation :tasks do
|
|
19
|
+
repository :test
|
|
20
|
+
|
|
21
|
+
attribute :id, Integer
|
|
22
|
+
attribute :title, String
|
|
23
|
+
|
|
24
|
+
key :id
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
mapping do
|
|
29
|
+
relation(:users) do
|
|
30
|
+
model User
|
|
31
|
+
map :id, :name
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
relation(:tasks) do
|
|
35
|
+
model Task
|
|
36
|
+
map :id, :title
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
before do
|
|
43
|
+
User = mock_model(:id, :name)
|
|
44
|
+
Task = mock_model(:id, :title, :user)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
after do
|
|
48
|
+
Object.send(:remove_const, :User) if defined?(User)
|
|
49
|
+
Object.send(:remove_const, :Task) if defined?(Task)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
subject(:mapper) { env[:tasks].mapper.wrap(:user => env[:users].mapper) }
|
|
53
|
+
|
|
54
|
+
specify 'loading wrapped tuples' do
|
|
55
|
+
tuple = { :id => 1, :title => 'Task 1', :user => { :id => 2, :name => 'Jane' } }
|
|
56
|
+
|
|
57
|
+
user = User.new(tuple[:user])
|
|
58
|
+
task = Task.new(:id => tuple[:id], :title => tuple[:title], :user => user)
|
|
59
|
+
|
|
60
|
+
expect(mapper.load(tuple)).to eql(task)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
specify 'dumping wrapped tuples' do
|
|
64
|
+
mapper = env[:tasks].mapper.wrap(:user => env[:users].mapper)
|
|
65
|
+
|
|
66
|
+
tuple = { :id => 1, :title => 'Task 1', :user => { :id => 2, :name => 'Jane' } }
|
|
67
|
+
|
|
68
|
+
user = User.new(tuple[:user])
|
|
69
|
+
task = Task.new(:id => tuple[:id], :title => tuple[:title], :user => user)
|
|
70
|
+
|
|
71
|
+
expect(mapper.dump(task)).to eql([1, 'Task 1', [2, 'Jane']])
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
shared_examples_for 'Mapper::Loader' do
|
|
4
|
+
subject(:loader) { described_class.new(header, model, transformer) }
|
|
5
|
+
|
|
6
|
+
let(:header) { Mapper::Header.build([[:id, type: Integer, key: true], [:name, type: String]]) }
|
|
7
|
+
let(:tuple) { Hash[id: 1, name: 'Jane', something: 'foo'] }
|
|
8
|
+
let(:model) { mock_model(:id, :name) }
|
|
9
|
+
let(:object) { model.new(id: 1, name: 'Jane') }
|
|
10
|
+
let(:transformer) { fake('transformer') { Morpher::Evaluator::Transformer } }
|
|
11
|
+
end
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
shared_examples_for 'Mapper::Loader#identity' do
|
|
4
|
+
subject(:loader) { described_class.build(header, model) }
|
|
5
|
+
|
|
6
|
+
let(:header) { Mapper::Header.build([[:id, Integer], [:name, String]], keys: [:id]) }
|
|
7
|
+
let(:tuple) { Hash[id: 1, name: 'Jane'] }
|
|
8
|
+
let(:model) { mock_model(:id, :name) }
|
|
9
|
+
|
|
10
|
+
it "returns object's identity" do
|
|
11
|
+
expect(loader.identity(tuple)).to eq([1])
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
shared_context 'Mapper' do
|
|
4
|
+
let(:mapper) { described_class.build([[:id, key: true], [:name]], model: model) }
|
|
5
|
+
|
|
6
|
+
let(:header) { fake(:header) { Mapper::Header } }
|
|
7
|
+
let(:data) { [1, 'Jane'] }
|
|
8
|
+
let(:tuple) { Hash[id: 1, name: 'Jane'] }
|
|
9
|
+
let(:object) { model.new(id: 1, name: 'Jane') }
|
|
10
|
+
let(:model) { mock_model(:id, :name) }
|
|
11
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
shared_context 'Relation' do
|
|
4
|
+
subject(:relation) { described_class.new(users, mapper) }
|
|
5
|
+
|
|
6
|
+
let(:header) {
|
|
7
|
+
Axiom::Relation::Header.coerce([[:id, Integer], [:name, String]], keys: [:id])
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
let(:users) {
|
|
11
|
+
Axiom::Relation.new(header, [
|
|
12
|
+
[1, 'John'], [2, 'Jane'], [3, 'Jack'], [4, 'Jade']
|
|
13
|
+
])
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
let(:model) { mock_model(:id, :name) }
|
|
17
|
+
let(:mapper) { TestMapper.new(users.header, model) }
|
|
18
|
+
|
|
19
|
+
let(:john) { model.new(id: 1, name: 'John') }
|
|
20
|
+
let(:jane) { model.new(id: 2, name: 'Jane') }
|
|
21
|
+
let(:jack) { model.new(id: 3, name: 'Jack') }
|
|
22
|
+
let(:jade) { model.new(id: 4, name: 'Jade') }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
shared_context 'Project with tasks' do
|
|
26
|
+
subject(:relation) { described_class.new(projects_with_tasks, mapper) }
|
|
27
|
+
|
|
28
|
+
let(:header) {
|
|
29
|
+
Axiom::Relation::Header.coerce(
|
|
30
|
+
[
|
|
31
|
+
[:id, Integer],
|
|
32
|
+
[:name, String ],
|
|
33
|
+
[:task_id, Integer],
|
|
34
|
+
[:task_name, String ],
|
|
35
|
+
]
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
let(:projects_with_tasks) {
|
|
40
|
+
Axiom::Relation.new(header, [
|
|
41
|
+
[1, 'rom-relation', 1, 'Add ROM::Relation#group' ],
|
|
42
|
+
[1, 'rom-relation', 2, 'Add ROM::Relation#ungroup'],
|
|
43
|
+
])
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
let(:project_with_tasks_model) { mock_model(:id, :name, :tasks) }
|
|
47
|
+
let(:task_model) { mock_model(:task_id, :task_name) }
|
|
48
|
+
|
|
49
|
+
let(:mapper) { ProjectWithTasksMapper.new(header, project_with_tasks_model, task_model) }
|
|
50
|
+
|
|
51
|
+
let(:project_with_tasks) {
|
|
52
|
+
project_with_tasks_model.new(id: 1, name: 'rom-relation', tasks: tasks)
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
let(:tasks) {[
|
|
56
|
+
# TODO add renaming support: [[:id, Integer], [:name, String]]
|
|
57
|
+
task_model.new(task_id: 1, task_name: 'Add ROM::Relation#group'),
|
|
58
|
+
task_model.new(task_id: 2, task_name: 'Add ROM::Relation#ungroup')
|
|
59
|
+
]}
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
shared_context 'City with location' do
|
|
63
|
+
subject(:relation) { described_class.new(city_relation, mapper) }
|
|
64
|
+
|
|
65
|
+
let(:header) {
|
|
66
|
+
Axiom::Relation::Header.coerce([
|
|
67
|
+
[:id, Integer], [:name, String], [:location_id, Integer], [:lat, Float], [:lng, Float]
|
|
68
|
+
])
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
let(:city_relation) {
|
|
72
|
+
Axiom::Relation.new(header, [[1, 'Krakow', 1, 2.0, 3.0]])
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
let(:city_model) { mock_model(:id, :name, :location) }
|
|
76
|
+
let(:location_model) { mock_model(:lat, :lng) }
|
|
77
|
+
|
|
78
|
+
let(:mapper) { CityWithLocationMapper.new(header, city_model, location_model) }
|
|
79
|
+
|
|
80
|
+
let(:city) { city_model.new(id: 1, name: 'Krakow', location: location) }
|
|
81
|
+
let(:location) { location_model.new(lat: 2.0, lng: 3.0) }
|
|
82
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
shared_context 'Session::Environment' do
|
|
4
|
+
let(:object) { described_class.new({ users: users }, Session::Tracker.new) }
|
|
5
|
+
|
|
6
|
+
let(:users) {
|
|
7
|
+
relation = TEST_ENV.repository(:test)[:users]
|
|
8
|
+
mapper = Mapper.build([[:id], [:name]], model: mock_model(:id, :name))
|
|
9
|
+
Relation.new(relation, mapper)
|
|
10
|
+
}
|
|
11
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
shared_context 'Session::Relation' do
|
|
4
|
+
let(:users) { session[:users] }
|
|
5
|
+
let(:object) { users }
|
|
6
|
+
|
|
7
|
+
let(:session) { Session.new(env) }
|
|
8
|
+
let(:env) { Session::Environment.new({ users: relation }, tracker) }
|
|
9
|
+
let(:tracker) { Session::Tracker.new }
|
|
10
|
+
|
|
11
|
+
let(:mapper) { Mapper.build([[:id, type: Integer, key: true], [:name, type: String]], model: model) }
|
|
12
|
+
let(:model) { mock_model(:id, :name) }
|
|
13
|
+
let(:header) { TEST_ENV.schema[:users].header }
|
|
14
|
+
let(:axiom) { Axiom::Relation::Variable.new(Axiom::Relation.new(header, [[1, 'John'], [2, 'Jane']])) }
|
|
15
|
+
let(:relation) { Relation.new(axiom, mapper) }
|
|
16
|
+
|
|
17
|
+
let(:user) { session[:users].to_a.first }
|
|
18
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
if ENV['COVERAGE'] == 'true'
|
|
4
|
+
require 'simplecov'
|
|
5
|
+
require 'coveralls'
|
|
6
|
+
|
|
7
|
+
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
|
|
8
|
+
SimpleCov::Formatter::HTMLFormatter,
|
|
9
|
+
Coveralls::SimpleCov::Formatter
|
|
10
|
+
]
|
|
11
|
+
|
|
12
|
+
SimpleCov.start do
|
|
13
|
+
command_name 'spec:unit'
|
|
14
|
+
|
|
15
|
+
add_filter 'config'
|
|
16
|
+
add_filter 'lib/rom/support'
|
|
17
|
+
add_filter 'spec'
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
require 'devtools/spec_helper'
|
|
22
|
+
|
|
23
|
+
require 'rom'
|
|
24
|
+
|
|
25
|
+
require 'bogus/rspec'
|
|
26
|
+
|
|
27
|
+
include ROM
|
|
28
|
+
include SpecHelper
|
|
29
|
+
include Morpher::NodeHelpers
|
|
30
|
+
|
|
31
|
+
TEST_ENV = Environment.setup(test: 'memory://test') do
|
|
32
|
+
schema do
|
|
33
|
+
base_relation :users do
|
|
34
|
+
repository :test
|
|
35
|
+
|
|
36
|
+
attribute :id, Integer
|
|
37
|
+
attribute :name, String
|
|
38
|
+
|
|
39
|
+
key :id
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
mapping do
|
|
44
|
+
relation(:users) do
|
|
45
|
+
model mock_model(:id, :name)
|
|
46
|
+
map :id, :name
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
module SpecHelper
|
|
4
|
+
|
|
5
|
+
def mock_model(*attributes, &block)
|
|
6
|
+
model = Class.new {
|
|
7
|
+
include Equalizer.new(*attributes)
|
|
8
|
+
|
|
9
|
+
const_set(:ATTRIBUTES, attributes)
|
|
10
|
+
|
|
11
|
+
attributes.each { |name| attr_accessor name }
|
|
12
|
+
|
|
13
|
+
def initialize(attrs = {}, &block)
|
|
14
|
+
attrs.each { |name, value| send("#{name}=", value) }
|
|
15
|
+
instance_eval(&block) if block
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def update(tuple)
|
|
19
|
+
self.class.new(to_h.update(tuple))
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def attribute_names
|
|
23
|
+
self.class.const_get(:ATTRIBUTES)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def to_h
|
|
27
|
+
attribute_names.each_with_object({}) { |name, h| h[name] = send(name) }
|
|
28
|
+
end
|
|
29
|
+
}
|
|
30
|
+
model.class_eval(&block) if block
|
|
31
|
+
model
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
end
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# encoding: utf-8
|
|
2
|
+
|
|
3
|
+
class TestMapper < Struct.new(:header, :model)
|
|
4
|
+
|
|
5
|
+
def call(relation)
|
|
6
|
+
relation
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def load(tuple)
|
|
10
|
+
model.new(
|
|
11
|
+
Hash[
|
|
12
|
+
header.map { |attribute| [attribute.name, tuple[attribute.name]] }
|
|
13
|
+
]
|
|
14
|
+
)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def dump(object)
|
|
18
|
+
header.each_with_object([]) { |attribute, tuple|
|
|
19
|
+
tuple << object.send(attribute.name)
|
|
20
|
+
}
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
class CityWithLocationMapper < Struct.new(:header, :model, :location_model)
|
|
26
|
+
attr_reader :city_header
|
|
27
|
+
attr_reader :location_header
|
|
28
|
+
|
|
29
|
+
def initialize(header, model, location_model)
|
|
30
|
+
super
|
|
31
|
+
@city_header = header.project(city_header_names)
|
|
32
|
+
@location_header = header.project(location_header_names)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def call(relation)
|
|
36
|
+
relation
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def load(tuple)
|
|
40
|
+
model.new(city_attributes(tuple).merge(location: location_object(tuple[:location])))
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def dump(object)
|
|
44
|
+
raise NotImplementedError, "#{self.class}##{__method__} must be implemented"
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
private
|
|
48
|
+
|
|
49
|
+
def city_attributes(tuple)
|
|
50
|
+
Hash[city_header.map { |attribute| [attribute.name, tuple[attribute.name]] }]
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def location_object(tuple)
|
|
54
|
+
TestMapper.new(location_header, location_model).load(tuple)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def city_header_names
|
|
58
|
+
[:id, :name]
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def location_header_names
|
|
62
|
+
[:lat, :lng]
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
class ProjectWithTasksMapper < Struct.new(:header, :model, :task_model)
|
|
68
|
+
attr_reader :task_header
|
|
69
|
+
attr_reader :project_header
|
|
70
|
+
|
|
71
|
+
def initialize(header, model, task_model)
|
|
72
|
+
super
|
|
73
|
+
@task_header = header.project(task_header_names)#.rename(task_aliases)
|
|
74
|
+
@project_header = header.project(project_header_names)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def call(relation)
|
|
78
|
+
relation
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def load(tuple)
|
|
82
|
+
model.new(project_attributes(tuple).merge(tasks: task_collection(tuple)))
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def dump(object)
|
|
86
|
+
raise NotImplementedError, "#{self.class}##{__method__} must be implemented"
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
private
|
|
90
|
+
|
|
91
|
+
def project_attributes(tuple)
|
|
92
|
+
Hash[project_header.map { |attribute| [attribute.name, tuple[attribute.name]] }]
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def task_collection(tuple)
|
|
96
|
+
tuple[:tasks].map(&method(:task_object))
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def task_object(task_tuple)
|
|
100
|
+
TestMapper.new(task_header, task_model).load(task_tuple)#.rename(task_aliases))
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def task_header_names
|
|
104
|
+
[:task_id, :task_name]
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def project_header_names
|
|
108
|
+
[:id, :name]
|
|
109
|
+
end
|
|
110
|
+
end
|