spec/integration/repository_spec.rb in rom-repository-0.3.1 vs spec/integration/repository_spec.rb in rom-repository-1.0.0.beta1

- old
+ new

@@ -45,13 +45,29 @@ jane_without_task, joe_with_task ]) end it 'loads nested combined relations' do - expect(repo.users_with_tasks_and_tags.first.to_h).to eql(user_with_task_and_tags.to_h) + user = repo.users_with_tasks_and_tags.first + + expect(user.id).to be(1) + expect(user.name).to eql('Jane') + expect(user.all_tasks.size).to be(1) + expect(user.all_tasks[0].id).to be(2) + expect(user.all_tasks[0].title).to eql('Jane Task') + expect(user.all_tasks[0].tags.size).to be(1) + expect(user.all_tasks[0].tags[0].name).to eql('red') end + it 'loads nested combined relations using configured associations' do + jane = repo.users_with_posts_and_their_labels.first + + expect(jane.posts.size).to be(1) + expect(jane.posts.map(&:title)).to eql(['Hello From Jane']) + expect(jane.posts.flat_map(&:labels).flat_map(&:name)).to eql(%w(red blue)) + end + it 'loads a wrapped relation' do expect(repo.tag_with_wrapped_task.first).to eql(tag_with_task) end it 'loads an aggregate via custom fks' do @@ -103,10 +119,26 @@ expect(post.title).to eql('Hello From Jane') expect(post.labels.size).to be(2) expect(post.labels.map(&:name)).to eql(%w(red blue)) end + it 'loads multiple child relations' do + user = repo.users.combine_children(many: [repo.posts, repo.tasks]).where(name: 'Jane').one + + expect(user.name).to eql('Jane') + expect(user.posts.size).to be(1) + expect(user.posts[0].title).to eql('Hello From Jane') + expect(user.tasks.size).to be(1) + expect(user.tasks[0].title).to eql('Jane Task') + end + + it 'loads multiple parent relations' do + post_label = repo.posts_labels.combine_parents(one: [repo.posts]).first + + expect(post_label.post.title).to eql('Hello From Jane') + end + context 'not common naming conventions' do it 'still loads nested relations' do comments = comments_repo.comments_with_likes.to_a expect(comments.size).to be(2) @@ -124,14 +156,38 @@ expect(comments[0].emotions[0].author).to eql('Joe') expect(comments[0].emotions[1].author).to eql('Anonymous') end end + describe 'projecting virtual attributes' do + it 'loads auto-mapped structs' do + user = repo.users. + inner_join(:posts, author_id: :id). + select_group { [id.qualified, name.qualified] }. + select_append { int::count(:posts).as(:post_count) }. + having { count(id.qualified) >= 1 }. + first + + expect(user.id).to be(1) + expect(user.name).to eql('Jane') + expect(user.post_count).to be(1) + end + end + + describe 'projecting aliased attributes' do + it 'loads auto-mapped structs' do + user = repo.users.select { [id.aliased(:userId), name.aliased(:userName)] }.first + + expect(user.userId).to be(1) + expect(user.userName).to eql('Jane') + end + end + context 'with a table without columns' do before { conn.create_table(:dummy) unless conn.table_exists?(:dummy) } it 'does not fail with a weird error when a relation does not have attributes' do - configuration.relation(:dummy) + configuration.relation(:dummy) { schema(infer: true) } repo = Class.new(ROM::Repository[:dummy]).new(rom) expect(repo.dummy.to_a).to eql([]) end end