spec/integration/repository_spec.rb in rom-repository-1.0.2 vs spec/integration/repository_spec.rb in rom-repository-1.1.0
- old
+ new
@@ -68,10 +68,17 @@
it 'loads a wrapped relation' do
expect(repo.tag_with_wrapped_task.first).to eql(tag_with_task)
end
+ it 'loads multiple wraps' do
+ post_label = repo.posts_labels.wrap(:post).wrap(:label).to_a.first
+
+ expect(post_label.label_id).to be(post_label.label.id)
+ expect(post_label.post_id).to be(post_label.post.id)
+ end
+
it 'loads an aggregate via custom fks' do
jane = repo.aggregate(many: repo.posts).where(name: 'Jane').one
expect(jane.posts.size).to be(1)
expect(jane.posts.first.title).to eql('Hello From Jane')
@@ -133,21 +140,30 @@
expect(jane.labels.size).to be(2)
expect(jane.labels[0].name).to eql('red')
expect(jane.labels[1].name).to eql('blue')
end
- it 'loads children and its parents via wrap' do
+ it 'loads children and its parents via wrap_parent' do
posts = repo.posts.wrap_parent(author: repo.users)
label = repo.labels.combine(many: { posts: posts }).first
expect(label.name).to eql('red')
expect(label.posts.size).to be(1)
expect(label.posts[0].title).to eql('Hello From Jane')
expect(label.posts[0].author.name).to eql('Jane')
end
+ it 'loads children and its parents via wrap and association name' do
+ label = repo.labels.combine(many: { posts: repo.posts.wrap(:author) }).first
+
+ expect(label.name).to eql('red')
+ expect(label.posts.size).to be(1)
+ expect(label.posts[0].title).to eql('Hello From Jane')
+ expect(label.posts[0].author.name).to eql('Jane')
+ end
+
it 'loads a parent via custom fks' do
post = repo.posts.combine(:author).where(title: 'Hello From Jane').one
expect(post.title).to eql('Hello From Jane')
expect(post.author.name).to eql('Jane')
@@ -239,8 +255,85 @@
it 'does not fail with a weird error when a relation does not have attributes' do
configuration.relation(:dummy) { schema(infer: true) }
repo = Class.new(ROM::Repository[:dummy]).new(rom)
expect(repo.dummy.to_a).to eql([])
+ end
+ end
+
+ describe 'mapping without structs' do
+ shared_context 'plain hash mapping' do
+ describe '#one' do
+ it 'returns a hash' do
+ expect(repo.users.limit(1).one).to eql(id: 1, name: 'Jane')
+ end
+
+ it 'returns a nested hash for an aggregate' do
+ expect(repo.aggregate(:posts).limit(1).one).
+ to eql(id: 1, name: 'Jane', posts: [{ id: 1, author_id: 1, title: 'Hello From Jane', body: 'Jane Post'}])
+ end
+ end
+ end
+
+ context 'with auto_struct disabled upon initialization' do
+ subject(:repo) do
+ repo_class.new(rom, auto_struct: false)
+ end
+
+ include_context 'plain hash mapping'
+ end
+
+ context 'with auto_struct disabled at the class level' do
+ before do
+ repo_class.auto_struct(false)
+ end
+
+ include_context 'plain hash mapping'
+ end
+ end
+
+ describe 'using custom mappers along with auto-mapping' do
+ before do
+ configuration.mappers do
+ define(:users) do
+ register_as :embed_address
+
+ def call(rel)
+ rel.map { |tuple| Hash(tuple).merge(mapped: true) }
+ 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' }])
+ end
+ end
+
+ describe 'using a custom model for a node' do
+ before do
+ class Test::Post < OpenStruct; end
+ end
+
+ it 'uses provided model for the member type' do
+ jane = repo.users.combine(many: repo.posts.as(Test::Post)).where(name: 'Jane').one
+
+ expect(jane.name).to eql('Jane')
+ expect(jane.posts.size).to be(1)
+ expect(jane.posts[0]).to be_instance_of(Test::Post)
+ expect(jane.posts[0].title).to eql('Hello From Jane')
+ expect(jane.posts[0].body).to eql('Jane Post')
+ end
+
+ it 'uses provided model for the attribute type' do
+ jane = repo.users.combine(one: repo.posts.as(Test::Post)).where(name: 'Jane').one
+
+ expect(jane.name).to eql('Jane')
+ expect(jane.post).to be_instance_of(Test::Post)
+ expect(jane.post.title).to eql('Hello From Jane')
+ expect(jane.post.body).to eql('Jane Post')
end
end
end