|
| 1 | +# frozen_string_literal: true |
| 2 | +require "spec_helper" |
| 3 | + |
| 4 | +describe GraphQL::Dataloader::ActiveRecordAssociationSource do |
| 5 | + if testing_rails? |
| 6 | + it_dataloads "queries for associated records when the association isn't already loaded" do |d| |
| 7 | + my_first_car = ::Album.find(2) |
| 8 | + homey = ::Album.find(4) |
| 9 | + log = with_active_record_log(colorize: false) do |
| 10 | + vulfpeck, chon = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :band).load_all([my_first_car, homey]) |
| 11 | + assert_equal "Vulfpeck", vulfpeck.name |
| 12 | + assert_equal "Chon", chon.name |
| 13 | + end |
| 14 | + |
| 15 | + assert_includes log, '[["id", 1], ["id", 3]]' |
| 16 | + |
| 17 | + toms_story = ::Album.find(3) |
| 18 | + log = with_active_record_log(colorize: false) do |
| 19 | + vulfpeck, chon, toms_story_band = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :band).load_all([my_first_car, homey, toms_story]) |
| 20 | + assert_equal "Vulfpeck", vulfpeck.name |
| 21 | + assert_equal "Chon", chon.name |
| 22 | + assert_equal "Tom's Story", toms_story_band.name |
| 23 | + end |
| 24 | + |
| 25 | + assert_includes log, '[["id", 2]]' |
| 26 | + end |
| 27 | + |
| 28 | + it_dataloads "doesn't load records that are already cached by ActiveRecordSource" do |d| |
| 29 | + d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load_all([1,2,3]) |
| 30 | + |
| 31 | + my_first_car = ::Album.find(2) |
| 32 | + homey = ::Album.find(4) |
| 33 | + toms_story = ::Album.find(3) |
| 34 | + |
| 35 | + log = with_active_record_log(colorize: false) do |
| 36 | + vulfpeck, chon, toms_story_band = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :band).load_all([my_first_car, homey, toms_story]) |
| 37 | + assert_equal "Vulfpeck", vulfpeck.name |
| 38 | + assert_equal "Chon", chon.name |
| 39 | + assert_equal "Tom's Story", toms_story_band.name |
| 40 | + end |
| 41 | + |
| 42 | + assert_equal "", log |
| 43 | + end |
| 44 | + |
| 45 | + it_dataloads "warms the cache for ActiveRecordSource" do |d| |
| 46 | + my_first_car = ::Album.find(2) |
| 47 | + homey = ::Album.find(4) |
| 48 | + toms_story = ::Album.find(3) |
| 49 | + d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :band).load_all([my_first_car, homey, toms_story]) |
| 50 | + |
| 51 | + log = with_active_record_log(colorize: false) do |
| 52 | + d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load_all([1,2,3]) |
| 53 | + end |
| 54 | + |
| 55 | + assert_equal "", log |
| 56 | + end |
| 57 | + |
| 58 | + it_dataloads "doesn't warm the cache when a scope is given" do |d| |
| 59 | + my_first_car = ::Album.find(2) |
| 60 | + homey = ::Album.find(4) |
| 61 | + summerteeth = ::Album.find(6) |
| 62 | + results = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :band, ::Band.country).load_all([my_first_car, homey, summerteeth]) |
| 63 | + assert_equal [nil, nil, ::Band.find(4)], results |
| 64 | + |
| 65 | + log = with_active_record_log(colorize: false) do |
| 66 | + d.with(GraphQL::Dataloader::ActiveRecordSource, Band).load_all([1,2,4]) |
| 67 | + end |
| 68 | + |
| 69 | + assert_includes log, "SELECT \"bands\".* FROM \"bands\" WHERE \"bands\".\"id\" IN (?, ?, ?) [[\"id\", 1], [\"id\", 2], [\"id\", 4]]" |
| 70 | + end |
| 71 | + |
| 72 | + it_dataloads "doesn't pause when the association is already loaded" do |d| |
| 73 | + source = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :band) |
| 74 | + assert_equal 0, source.results.size |
| 75 | + assert_equal 0, source.pending.size |
| 76 | + |
| 77 | + my_first_car = ::Album.find(2) |
| 78 | + vulfpeck = my_first_car.band |
| 79 | + |
| 80 | + vulfpeck2 = source.load(my_first_car) |
| 81 | + |
| 82 | + assert_equal vulfpeck, vulfpeck2 |
| 83 | + |
| 84 | + assert_equal 0, source.results.size |
| 85 | + assert_equal 0, source.pending.size |
| 86 | + |
| 87 | + my_first_car.reload |
| 88 | + vulfpeck3 = source.load(my_first_car) |
| 89 | + assert_equal vulfpeck, vulfpeck3 |
| 90 | + |
| 91 | + assert_equal 1, source.results.size |
| 92 | + assert_equal 0, source.pending.size |
| 93 | + end |
| 94 | + |
| 95 | + it_dataloads "raises an error with a non-existent association" do |d| |
| 96 | + my_first_car = ::Album.find(2) |
| 97 | + source = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :tour_bus) |
| 98 | + assert_raises ActiveRecord::AssociationNotFoundError do |
| 99 | + source.load(my_first_car) |
| 100 | + end |
| 101 | + end |
| 102 | + |
| 103 | + it_dataloads "works with polymorphic associations" do |d| |
| 104 | + wilco = ::Band.find(4) |
| 105 | + vulfpeck = d.with(GraphQL::Dataloader::ActiveRecordAssociationSource, :thing).load(wilco) |
| 106 | + assert_equal ::Band.find(1), vulfpeck |
| 107 | + end |
| 108 | + end |
| 109 | +end |
0 commit comments