File: has_dataloader_spec.rb

package info (click to toggle)
ruby-graphql 2.5.19-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 13,868 kB
  • sloc: ruby: 80,420; ansic: 1,808; yacc: 845; javascript: 480; makefile: 6
file content (48 lines) | stat: -rw-r--r-- 1,565 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true
require "spec_helper"

if testing_rails?
  describe GraphQL::Schema::Member::HasDataloader do
    class DataloaderExample
      include GraphQL::Schema::Member::HasDataloader
      def initialize(dataloader, object = nil)
        @context = OpenStruct.new(dataloader: dataloader)
        @object = object
      end

      attr_reader :context, :object
    end

    class PlusSource < GraphQL::Dataloader::Source
      def fetch(keys)
        res = keys.reduce(&:+)
        keys.map { |k| res }
      end
    end

    it_dataloads "loads records with dataload_record" do |d|
      example = DataloaderExample.new(d)
      assert_equal "Homey", example.dataload_record(Album, 4).name
      assert_equal 4, example.dataload_record(Album, "Homey", find_by: :name).id
    end

    it_dataloads "loads association with dataload_association" do |d|
      album1 = Album.find(1)
      example = DataloaderExample.new(d, album1)
      assert_equal "Vulfpeck", example.dataload_association(:band).name, "Defaults to record = object"

      album = Album.find(4)
      assert_equal "Chon", example.dataload_association(album, :band).name
      album.reload
      assert_nil example.dataload_association(album, :band, scope: Band.country)
    end

    it_dataloads "calls any source with dataload..." do |d|
      example = DataloaderExample.new(d)
      d.with(PlusSource).request(2)
      d.with(PlusSource).request(3)
      assert_equal 9, example.dataload(PlusSource, 4)
      assert_equal 5, example.dataload(PlusSource, 5)
    end
  end
end