nulogy_message_bus_producer 1.0.4 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,4 +13,110 @@ RSpec.describe NulogyMessageBusProducer::PostgresPublicSubscriptions do
13
13
  end.to raise_error(KeyError, /The schema registry did not contain an entry/)
14
14
  end
15
15
  end
16
+
17
+ context "when subscription is triggered" do
18
+ before do
19
+ NulogyMessageBusProducer.config.register_schema(
20
+ schema: "NulogyMessageBusProducer::Specs::TestSchema",
21
+ key: "test"
22
+ )
23
+ end
24
+
25
+ it "generates events" do
26
+ create_subscription(event_type: "testCreated")
27
+ create_subscription(event_type: "testCreated")
28
+
29
+ uuid = SecureRandom.uuid
30
+
31
+ expect do
32
+ NulogyMessageBusProducer::Specs::TestSchema.subscriptions.trigger(
33
+ "testCreated",
34
+ {},
35
+ { uuid: uuid, tenant_id: SecureRandom.uuid }
36
+ )
37
+ end.to change(NulogyMessageBusProducer::PublicSubscriptionEvent, :count).by(2)
38
+
39
+ event_data = NulogyMessageBusProducer::PublicSubscriptionEvent.pluck(:event_json)
40
+ expect(event_data).to all(include("foo" => { "id" => uuid }))
41
+ end
42
+ end
43
+
44
+ context "when a subscription error occurs" do
45
+ context "when failing with raise" do
46
+ before do
47
+ NulogyMessageBusProducer.config.producing_events_fails_with(:raise)
48
+ end
49
+
50
+ it "raises an exception" do
51
+ expect { trigger_erroneous_subscription }.to raise_error(/A subscription event could not be produced/)
52
+ end
53
+ end
54
+
55
+ context "when failing with a soft fail" do
56
+ before do
57
+ NulogyMessageBusProducer.config.producing_events_fails_with(:soft_fail)
58
+ end
59
+
60
+ it "does not raise an error" do
61
+ trigger_erroneous_subscription
62
+ end
63
+
64
+ it "calls the provided block" do
65
+ called = false
66
+
67
+ NulogyMessageBusProducer.config.producing_events_fails_with(:soft_fail) do |_|
68
+ called = true
69
+ end
70
+
71
+ trigger_erroneous_subscription
72
+
73
+ expect(called).to be(true)
74
+ end
75
+ end
76
+ end
77
+
78
+ def trigger_erroneous_subscription
79
+ NulogyMessageBusProducer.config.register_schema(schema: "NulogyMessageBusProducer::Specs::TestSchema", key: "test")
80
+
81
+ event_type = "testCreated"
82
+
83
+ subscription = create_subscription(event_type: event_type)
84
+ simulate_invalid_query(subscription)
85
+
86
+ NulogyMessageBusProducer::Specs::TestSchema.subscriptions.trigger(
87
+ event_type,
88
+ {},
89
+ { uuid: SecureRandom.uuid, tenant_id: SecureRandom.uuid }
90
+ )
91
+ end
92
+
93
+ def create_subscription(**overrides) # rubocop:disable Metrics/MethodLength
94
+ args = {
95
+ id: SecureRandom.uuid,
96
+ subscription_group_id: SecureRandom.uuid,
97
+ topic_name: "test_topic",
98
+ schema_key: "test",
99
+ event_type: "test_event_type",
100
+ query: <<~QUERY
101
+ query ($id: UUID!) {
102
+ foo (id: $id) {
103
+ id
104
+ }
105
+ }
106
+ QUERY
107
+ }.merge(overrides)
108
+
109
+ NulogyMessageBusProducer::PublicSubscription.create_or_update(args)
110
+ end
111
+
112
+ def simulate_invalid_query(subscription)
113
+ subscription.query = <<~QUERY
114
+ query ($id: UUID!) {
115
+ foo (id: $id) {
116
+ a_field_that_does_not_exist
117
+ }
118
+ }
119
+ QUERY
120
+ subscription.save(validate: false)
121
+ end
16
122
  end
@@ -4,7 +4,7 @@ RSpec.describe NulogyMessageBusProducer::SubscriberGraphqlSchemaValidator do
4
4
  subject(:validator) { NulogyMessageBusProducer::SubscriberGraphqlSchemaValidator.new }
5
5
 
6
6
  before do
7
- NulogyMessageBusProducer.register_schema("test", "NulogyMessageBusProducer::Specs::TestSchema")
7
+ NulogyMessageBusProducer.config.register_schema(schema: "NulogyMessageBusProducer::Specs::TestSchema", key: "test")
8
8
  end
9
9
 
10
10
  describe "#validate" do
@@ -7,6 +7,7 @@ require File.expand_path("dummy/config/environment.rb", __dir__)
7
7
 
8
8
  require "rspec/rails"
9
9
  require "active_record"
10
+ require "spec_helper"
10
11
 
11
12
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].sort.each { |f| require f }
12
13
 
@@ -13,12 +13,27 @@ module NulogyMessageBusProducer
13
13
  field :foo, TestObject, null: false do
14
14
  argument :id, type: NulogyGraphqlApi::Types::UUID, required: false
15
15
  end
16
+
17
+ def foo(id:)
18
+ OpenStruct.new(id: id)
19
+ end
20
+ end
21
+
22
+ class TestCreated < NulogyMessageBusProducer::BasePublicSubscription
23
+ field :description, String, null: false
24
+ end
25
+
26
+ class TestSubscription < GraphQL::Schema::Object
27
+ extend GraphQL::Subscriptions::SubscriptionRoot
28
+
29
+ field :test_created, subscription: TestCreated
16
30
  end
17
31
 
18
32
  class TestSchema < GraphQL::Schema
19
33
  use NulogyMessageBusProducer::PostgresPublicSubscriptions
20
34
 
21
35
  query TestQuery
36
+ subscription TestSubscription
22
37
  end
23
38
  end
24
39
  end
@@ -3,7 +3,7 @@ require "unit_spec_helper"
3
3
  RSpec.describe NulogyMessageBusProducer::PublicSubscription do
4
4
  context "when validating" do
5
5
  before do
6
- NulogyMessageBusProducer.register_schema("test", "NulogyMessageBusProducer::Specs::TestSchema")
6
+ NulogyMessageBusProducer.config.register_schema schema: "NulogyMessageBusProducer::Specs::TestSchema", key: "test"
7
7
  end
8
8
 
9
9
  it "is invalid with a blank query" do
@@ -18,6 +18,14 @@ RSpec.describe NulogyMessageBusProducer::PublicSubscription do
18
18
  expect(model.errors[:query]).to contain_exactly("can't be blank")
19
19
  end
20
20
 
21
+ it "is invalid with blank schema_key" do
22
+ model = NulogyMessageBusProducer::PublicSubscription.new(schema_key: "")
23
+
24
+ model.validate
25
+
26
+ expect(model.errors[:schema_key]).to be_present
27
+ end
28
+
21
29
  it "is invalid with an invalid query" do
22
30
  model = NulogyMessageBusProducer::PublicSubscription.new(
23
31
  schema_key: "test",
@@ -33,7 +41,9 @@ RSpec.describe NulogyMessageBusProducer::PublicSubscription do
33
41
  model.validate
34
42
 
35
43
  expect(model).to_not be_valid
36
- expect(model.errors[:query]).to contain_exactly("Field 'a_field_that_does_not_exist' doesn't exist on type 'testObject' (id: <new_record>)")
44
+ expect(model.errors[:query]).to contain_exactly(
45
+ "Field 'a_field_that_does_not_exist' doesn't exist on type 'testObject' (id: <new_record>)"
46
+ )
37
47
  end
38
48
 
39
49
  it "valid with a valid query" do
metadata CHANGED
@@ -1,43 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nulogy_message_bus_producer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nulogy
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-13 00:00:00.000000000 Z
11
+ date: 2020-07-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 5.2.4.2
19
+ version: '5'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - "~>"
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 5.2.4.2
26
+ version: '5'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: graphql
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 1.9.12
33
+ version: '1.9'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 1.9.12
40
+ version: '1.9'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: strong_migrations
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -191,6 +191,7 @@ files:
191
191
  - lib/nulogy_message_bus_producer.rb
192
192
  - lib/nulogy_message_bus_producer/application_record.rb
193
193
  - lib/nulogy_message_bus_producer/base_public_subscription.rb
194
+ - lib/nulogy_message_bus_producer/config.rb
194
195
  - lib/nulogy_message_bus_producer/engine.rb
195
196
  - lib/nulogy_message_bus_producer/postgres_public_subscriptions.rb
196
197
  - lib/nulogy_message_bus_producer/public_subscription.rb
@@ -260,7 +261,7 @@ files:
260
261
  homepage: https://github.com/nulogy/message-bus/tree/master/gems/nulogy_message_bus_producer
261
262
  licenses: []
262
263
  metadata: {}
263
- post_install_message:
264
+ post_install_message:
264
265
  rdoc_options: []
265
266
  require_paths:
266
267
  - lib
@@ -276,7 +277,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
276
277
  version: '0'
277
278
  requirements: []
278
279
  rubygems_version: 3.0.3
279
- signing_key:
280
+ signing_key:
280
281
  specification_version: 4
281
282
  summary: Nulogy's code for producing to the Message Bus
282
283
  test_files: