Skip to content

Commit a0da47d

Browse files
committed
Add unit tests for service_extension backend_api logic
1 parent 8e93ab0 commit a0da47d

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

test/unit/backend_api_logic/service_extension_test.rb

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,84 @@ class BackendApiProxy < ActiveSupport::TestCase
3636

3737
assert_equal backend_api_config, service.backend_api_proxy.backend_api_config
3838
end
39+
40+
test '#backend_api returns backend_api from the first backend_api_configs, if persisted' do
41+
service = FactoryBot.create(:simple_service)
42+
backend_api = FactoryBot.create(:backend_api, account: service.account)
43+
another_backend_api = FactoryBot.create(:backend_api, account: service.account)
44+
FactoryBot.create(:backend_api_config, service: service, backend_api: backend_api, path: '/one')
45+
FactoryBot.create(:backend_api_config, service: service, backend_api: another_backend_api, path: '/two')
46+
47+
assert_equal 2, service.reload.backend_api_configs.count
48+
49+
first_result = service.backend_api
50+
assert_equal backend_api, first_result
51+
52+
# The second call returns the memoized instance
53+
second_result = service.backend_api
54+
assert_same first_result, second_result
55+
end
56+
57+
test '#update! saves backend_api_config and backend_api for the service' do
58+
service = FactoryBot.create(:simple_service)
59+
60+
# built on demand
61+
backend_api = service.backend_api
62+
assert_not backend_api.persisted?
63+
assert_not service.backend_api_proxy.backend_api_config.persisted?
64+
65+
service.backend_api_proxy.update!(private_endpoint: 'https://api.example.com', path: '/backend-path')
66+
67+
# memoized instances, but now persisted
68+
persisted_backend_api = service.backend_api
69+
70+
assert persisted_backend_api.persisted?
71+
assert service.backend_api_proxy.backend_api_config.persisted?
72+
73+
assert_same backend_api, persisted_backend_api
74+
end
75+
76+
test '#backend_api builds and memoizes unpersisted backend_api' do
77+
service = FactoryBot.create(:simple_service)
78+
proxy = service.backend_api_proxy
79+
80+
# First call builds unpersisted backend_api
81+
first_call = proxy.backend_api
82+
assert_not first_call.persisted?
83+
assert_equal service.account, first_call.account
84+
assert_equal service.system_name, first_call.system_name
85+
86+
# Second call returns the same memoized unpersisted instance
87+
second_call = proxy.backend_api
88+
assert_same first_call, second_call
89+
assert_not second_call.persisted?
90+
end
91+
92+
test '#backend_api_config memoizes across multiple calls' do
93+
service = FactoryBot.create(:simple_service)
94+
proxy = service.backend_api_proxy
95+
96+
first_call = proxy.backend_api_config
97+
second_call = proxy.backend_api_config
98+
99+
assert_not first_call.persisted?
100+
assert_not second_call.persisted?
101+
assert_same first_call, second_call
102+
end
103+
104+
test '#backend_api does not pollute account.backend_apis association with unpersisted records' do
105+
service = FactoryBot.create(:simple_service)
106+
account = service.account
107+
108+
initial_count = account.backend_apis.count
109+
110+
# Call backend_api which builds an unpersisted backend_api
111+
backend_api = service.backend_api_proxy.backend_api
112+
assert_not backend_api.persisted?
113+
114+
# Association should not include unpersisted record
115+
assert_equal initial_count, account.backend_apis.count
116+
assert_not_includes account.backend_apis, backend_api
117+
end
39118
end
40119
end

0 commit comments

Comments
 (0)