spec/integration/repository_spec.rb in rom-repository-1.3.0 vs spec/integration/repository_spec.rb in rom-repository-1.3.1
- old
+ new
@@ -1,10 +1,11 @@
RSpec.describe 'ROM repository' do
include_context 'database'
include_context 'relations'
- include_context 'seeds'
+ include_context 'repo'
include_context 'structs'
+ include_context 'seeds'
it 'loads a single relation' do
expect(repo.all_users.to_a).to match_array([jane, joe])
end
@@ -299,17 +300,38 @@
def call(rel)
rel.map { |tuple| Hash(tuple).merge(mapped: true) }
end
end
+
+ define(:posts) do
+ register_as :nested_mapper
+
+ def call(rel)
+ rel.map { |tuple| Hash(tuple).tap { |h| h[:title] = h[:title].upcase } }
+ end
+ end
end
end
it 'auto-maps and applies a custom mapper' do
jane = repo.users.combine(:posts).map_with(:embed_address, auto_map: true).to_a.first
expect(jane).
- to eql(id:1, name: 'Jane', mapped: true, posts: [{ id: 1, author_id: 1, title: 'Hello From Jane', body: 'Jane Post' }])
+ to eql(id:1, name: 'Jane', mapped: true, posts: [
+ { id: 1, author_id: 1, title: 'Hello From Jane', body: 'Jane Post' }
+ ])
+ end
+
+ it 'applies a custom mapper inside #node' do
+ jane = repo.aggregate(:posts).node(:posts) { |posts| posts.as(:nested_mapper, auto_map: true) }.to_a.first
+
+ expect(jane).to be_a ROM::Struct
+
+ expect(jane.to_h).
+ to eql(id:1, name: 'Jane', posts: [
+ { id: 1, author_id: 1, title: 'HELLO FROM JANE', body: 'Jane Post' }
+ ])
end
end
describe 'using a custom model for a node' do
before do