|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | + |
| 5 | +RSpec.describe Grape::Entity::Preloader do |
| 6 | + class SQLCounter |
| 7 | + class << self |
| 8 | + attr_accessor :ignored_sql, :log, :log_all |
| 9 | + |
| 10 | + def clear_log |
| 11 | + self.log = [] |
| 12 | + self.log_all = [] |
| 13 | + end |
| 14 | + end |
| 15 | + clear_log |
| 16 | + |
| 17 | + def call(_name, _start, _finish, _message_id, values) |
| 18 | + return if values[:cached] |
| 19 | + |
| 20 | + sql = values[:sql] |
| 21 | + self.class.log_all << sql |
| 22 | + self.class.log << sql unless %w[SCHEMA TRANSACTION].include? values[:name] |
| 23 | + end |
| 24 | + |
| 25 | + ActiveSupport::Notifications.subscribe('sql.active_record', new) |
| 26 | + end |
| 27 | + |
| 28 | + class ApplicationRecord < ActiveRecord::Base |
| 29 | + self.abstract_class = true |
| 30 | + end |
| 31 | + |
| 32 | + class Tag < ApplicationRecord |
| 33 | + include Grape::Entity::DSL |
| 34 | + |
| 35 | + connection.create_table(:tags) do |t| |
| 36 | + t.string :name |
| 37 | + t.references :target, polymorphic: true |
| 38 | + end |
| 39 | + |
| 40 | + belongs_to :target, polymorphic: true |
| 41 | + |
| 42 | + entity do |
| 43 | + expose :name |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + class Book < ApplicationRecord |
| 48 | + include Grape::Entity::DSL |
| 49 | + |
| 50 | + connection.create_table(:books) do |t| |
| 51 | + t.string :name |
| 52 | + t.references :author |
| 53 | + end |
| 54 | + |
| 55 | + belongs_to :author, foreign_key: :author_id, class_name: 'User' |
| 56 | + has_many :tags, as: :target, dependent: :destroy |
| 57 | + |
| 58 | + entity do |
| 59 | + expose :name |
| 60 | + expose :tags, using: Tag::Entity, preload: :tags |
| 61 | + end |
| 62 | + end |
| 63 | + |
| 64 | + class User < ApplicationRecord |
| 65 | + include Grape::Entity::DSL |
| 66 | + |
| 67 | + connection.create_table(:users) do |t| |
| 68 | + t.string :name |
| 69 | + end |
| 70 | + |
| 71 | + has_many :books, foreign_key: :author_id, dependent: :destroy |
| 72 | + has_many :tags, as: :target, dependent: :destroy |
| 73 | + |
| 74 | + entity do |
| 75 | + expose :name |
| 76 | + expose :books, using: Book::Entity, preload: :books |
| 77 | + expose :tags, using: Tag::Entity, preload: :tags |
| 78 | + end |
| 79 | + end |
| 80 | + |
| 81 | + let!(:users) { [User.create(name: 'User1'), User.create(name: 'User2')] } |
| 82 | + let!(:user_tags) { [Tag.create(name: 'Tag1', target: users[0]), Tag.create(name: 'Tag2', target: users[1])] } |
| 83 | + let!(:books) { [Book.create(name: 'Book1', author: users[0]), Book.create(name: 'Book2', author: users[1])] } |
| 84 | + let!(:book_tags) { [Tag.create(name: 'Tag1', target: books[0]), Tag.create(name: 'Tag2', target: books[1])] } |
| 85 | + |
| 86 | + before { SQLCounter.clear_log } |
| 87 | + |
| 88 | + it 'preload associations through RepresentExposure' do |
| 89 | + User::Entity.preload_and_represent(users) |
| 90 | + |
| 91 | + expect(SQLCounter.log).to eq([ |
| 92 | + 'SELECT "books".* FROM "books" WHERE "books"."author_id" IN (?, ?)', |
| 93 | + 'SELECT "tags".* FROM "tags" WHERE "tags"."target_type" = ? AND "tags"."target_id" IN (?, ?)', |
| 94 | + 'SELECT "tags".* FROM "tags" WHERE "tags"."target_type" = ? AND "tags"."target_id" IN (?, ?)' |
| 95 | + ]) |
| 96 | + end |
| 97 | + |
| 98 | + it 'preload associations through NestingExposure' do |
| 99 | + Class.new(User::Entity) do |
| 100 | + unexpose :books |
| 101 | + expose :nesting do |
| 102 | + expose :books, using: Book::Entity, preload: :books |
| 103 | + end |
| 104 | + end.preload_and_represent(users) |
| 105 | + |
| 106 | + expect(SQLCounter.log).to eq([ |
| 107 | + 'SELECT "books".* FROM "books" WHERE "books"."author_id" IN (?, ?)', |
| 108 | + 'SELECT "tags".* FROM "tags" WHERE "tags"."target_type" = ? AND "tags"."target_id" IN (?, ?)', |
| 109 | + 'SELECT "tags".* FROM "tags" WHERE "tags"."target_type" = ? AND "tags"."target_id" IN (?, ?)' |
| 110 | + ]) |
| 111 | + end |
| 112 | + |
| 113 | + it 'preload same associations multiple times' do |
| 114 | + Class.new(User::Entity) do |
| 115 | + expose(:other_books, preload: :books) { :other_books } |
| 116 | + end.preload_and_represent(users) |
| 117 | + |
| 118 | + expect(SQLCounter.log).to eq([ |
| 119 | + 'SELECT "books".* FROM "books" WHERE "books"."author_id" IN (?, ?)', |
| 120 | + 'SELECT "tags".* FROM "tags" WHERE "tags"."target_type" = ? AND "tags"."target_id" IN (?, ?)', |
| 121 | + 'SELECT "tags".* FROM "tags" WHERE "tags"."target_type" = ? AND "tags"."target_id" IN (?, ?)' |
| 122 | + ]) |
| 123 | + end |
| 124 | + |
| 125 | + describe 'only preload associations that are specified in the options' do |
| 126 | + it 'through :only option' do |
| 127 | + User::Entity.preload_and_represent(users, only: [:tags]) |
| 128 | + |
| 129 | + expect(SQLCounter.log).to eq([ |
| 130 | + 'SELECT "tags".* FROM "tags" WHERE "tags"."target_type" = ? AND "tags"."target_id" IN (?, ?)' |
| 131 | + ]) |
| 132 | + end |
| 133 | + |
| 134 | + it 'through :except option' do |
| 135 | + User::Entity.preload_and_represent(users, except: [:tags]) |
| 136 | + |
| 137 | + expect(SQLCounter.log).to eq([ |
| 138 | + 'SELECT "books".* FROM "books" WHERE "books"."author_id" IN (?, ?)', |
| 139 | + 'SELECT "tags".* FROM "tags" WHERE "tags"."target_type" = ? AND "tags"."target_id" IN (?, ?)' |
| 140 | + ]) |
| 141 | + end |
| 142 | + end |
| 143 | +end |
0 commit comments