|
3 | 3 | require "spec_helper"
|
4 | 4 |
|
5 | 5 | require "redis"
|
| 6 | +require "connection_pool" |
6 | 7 |
|
7 | 8 | RSpec.describe GraphQL::PersistedQueries::StoreAdapters::RedisStoreAdapter do
|
8 |
| - describe "#initialize" do |
9 |
| - let(:options) { {} } |
| 9 | + subject { described_class.new(redis_client: redis_client) } |
10 | 10 |
|
11 |
| - subject { described_class.new(options) } |
| 11 | + context "when Hash instance is passed" do |
| 12 | + let(:redis_client) { { redis_url: "redis://127.0.0.3:8791/3" } } |
12 | 13 |
|
13 |
| - context "when client is passed" do |
14 |
| - let(:options) { { client: Redis.new(url: "redis://127.0.0.3:8791/3") } } |
15 |
| - |
16 |
| - it "uses client" do |
17 |
| - expect(subject.storage.connection[:id]).to eq("redis://127.0.0.3:8791/3") |
18 |
| - end |
19 |
| - |
20 |
| - context "when passed along with other parameters" do |
21 |
| - let(:options) do |
22 |
| - { |
23 |
| - client: Redis.new(url: "redis://127.0.0.3:8791/3"), |
24 |
| - redis_url: "redis://127.0.0.1:6379", |
25 |
| - redis_host: "127.0.0.1", |
26 |
| - redis_port: "6379", |
27 |
| - redis_db_name: "0" |
28 |
| - } |
29 |
| - end |
30 |
| - |
31 |
| - it "raises error" do |
32 |
| - expect { subject }.to raise_error( |
33 |
| - ArgumentError, |
34 |
| - "client cannot be passed along with redis_url, redis_host, " \ |
35 |
| - "redis_port or redis_db_name options" |
36 |
| - ) |
37 |
| - end |
38 |
| - end |
| 14 | + it "wraps with proc" do |
| 15 | + expect(subject.instance_variable_get("@redis_proc")).to be_kind_of(Proc) |
39 | 16 | end
|
| 17 | + end |
40 | 18 |
|
41 |
| - context "when redis_host, redis_port and redis_db_name are passed" do |
42 |
| - let(:options) do |
43 |
| - { redis_host: "127.0.0.2", redis_port: "2214", redis_db_name: "7" } |
44 |
| - end |
| 19 | + context "when Proc instance is passed" do |
| 20 | + let(:redis_client) { proc {} } |
45 | 21 |
|
46 |
| - it "builds redis URL" do |
47 |
| - expect(subject.storage.connection[:id]).to eq("redis://127.0.0.2:2214/7") |
48 |
| - end |
| 22 | + it "wraps with proc" do |
| 23 | + expect(subject.instance_variable_get("@redis_proc")).to be_kind_of(Proc) |
49 | 24 | end
|
| 25 | + end |
50 | 26 |
|
51 |
| - context "when REDIS_URL is configured" do |
52 |
| - let(:redis_url) { "redis://127.0.0.1:6379" } |
53 |
| - |
54 |
| - before do |
55 |
| - allow(ENV).to receive(:[]).with("REDIS_URL").and_return(redis_url) |
56 |
| - end |
57 |
| - |
58 |
| - it "uses default db" do |
59 |
| - expect(subject.storage.connection[:id]).to eq("redis://127.0.0.1:6379/0") |
60 |
| - end |
61 |
| - |
62 |
| - context "when redis_db_name is configured" do |
63 |
| - let(:options) { { redis_db_name: "42" } } |
| 27 | + context "when Redis instance is passed" do |
| 28 | + let(:redis_client) { Redis.new(url: "redis://127.0.0.3:8791/3") } |
64 | 29 |
|
65 |
| - it "uses configured db" do |
66 |
| - expect(subject.storage.connection[:id]).to eq("redis://127.0.0.1:6379/42") |
67 |
| - end |
68 |
| - end |
| 30 | + it "wraps with proc" do |
| 31 | + expect(subject.instance_variable_get("@redis_proc")).to be_kind_of(Proc) |
69 | 32 | end
|
| 33 | + end |
70 | 34 |
|
71 |
| - context "when redis_url is passed" do |
72 |
| - let(:options) { { redis_url: "redis://127.0.0.4:2177/22" } } |
| 35 | + context "when ConnectionPool instance is passed" do |
| 36 | + let(:redis_client) { ConnectionPool.new { Redis.new(url: "redis://127.0.0.3:8791/3") } } |
73 | 37 |
|
74 |
| - it "uses passed redis_url" do |
75 |
| - expect(subject.storage.connection[:id]).to eq("redis://127.0.0.4:2177/22") |
76 |
| - end |
| 38 | + it "wraps with proc" do |
| 39 | + expect(subject.instance_variable_get("@redis_proc")).to be_kind_of(Proc) |
| 40 | + end |
| 41 | + end |
77 | 42 |
|
78 |
| - context "when passed along with other parameters" do |
79 |
| - let(:options) do |
80 |
| - { |
81 |
| - redis_url: "redis://127.0.0.1:6379", |
82 |
| - redis_host: "127.0.0.1", |
83 |
| - redis_port: "6379", |
84 |
| - redis_db_name: "0" |
85 |
| - } |
86 |
| - end |
| 43 | + context "when not supported object is passed" do |
| 44 | + let(:redis_client) { 42 } |
87 | 45 |
|
88 |
| - it "raises error" do |
89 |
| - expect { subject }.to raise_error( |
90 |
| - ArgumentError, |
91 |
| - "redis_url cannot be passed along with redis_host, redis_port or redis_db_name options" |
92 |
| - ) |
93 |
| - end |
94 |
| - end |
| 46 | + it "raises error" do |
| 47 | + expect { subject }.to raise_error( |
| 48 | + ArgumentError, |
| 49 | + ":redis_client accepts Redis, ConnectionPool, Hash or Proc only" |
| 50 | + ) |
95 | 51 | end
|
96 | 52 | end
|
97 | 53 | end
|
0 commit comments