Skip to content
This repository has been archived by the owner on Jul 28, 2020. It is now read-only.

Commit

Permalink
Add graphql and client scheduling specs.
Browse files Browse the repository at this point in the history
  • Loading branch information
MrYawe committed Aug 27, 2019
1 parent 66e9af1 commit 0871c41
Show file tree
Hide file tree
Showing 7 changed files with 310 additions and 12 deletions.
12 changes: 6 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ GEM
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.5.2)
addressable (2.6.0)
public_suffix (>= 2.0.2, < 4.0)
appraisal (2.2.0)
bundler
Expand All @@ -26,7 +26,7 @@ GEM
safe_yaml (~> 1.0.0)
diff-lcs (1.3)
docile (1.3.1)
hashdiff (0.3.7)
hashdiff (1.0.0)
i18n (1.6.0)
concurrent-ruby (~> 1.0)
jaro_winkler (1.5.1)
Expand All @@ -40,7 +40,7 @@ GEM
pry (0.11.3)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
public_suffix (3.0.2)
public_suffix (3.1.1)
rainbow (3.0.0)
rake (10.5.0)
rspec (3.7.0)
Expand Down Expand Up @@ -69,7 +69,7 @@ GEM
rubocop-rspec (1.27.0)
rubocop (>= 0.56.0)
ruby-progressbar (1.9.0)
safe_yaml (1.0.4)
safe_yaml (1.0.5)
simplecov (0.16.1)
docile (~> 1.1)
json (>= 1.8, < 3)
Expand All @@ -84,10 +84,10 @@ GEM
tzinfo (>= 1.0.0)
unicode-display_width (1.4.0)
vcr (4.0.0)
webmock (3.4.2)
webmock (3.6.2)
addressable (>= 2.3.6)
crack (>= 0.3.2)
hashdiff
hashdiff (>= 0.4.0, < 2.0.0)

PLATFORMS
ruby
Expand Down
10 changes: 6 additions & 4 deletions lib/zenaton/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,23 @@ def start_workflow(flow)
end

def start_scheduled_task(task, cron)
@graphql.request(
res = @graphql.request(
gateway_url,
Services::GraphQL::CREATE_TASK_SCHEDULE,
create_task_schedule_input(task, cron),
gateway_headers
)['createTaskSchedule']
)
res && res['createTaskSchedule']
end

def start_scheduled_workflow(flow, cron)
@graphql.request(
res = @graphql.request(
gateway_url,
Services::GraphQL::CREATE_WORKFLOW_SCHEDULE,
create_workflow_schedule_input(flow, cron),
gateway_headers
)['createWorkflowSchedule']
)
res && res['createWorkflowSchedule']
end

# rubocop:disable Metrics/MethodLength
Expand Down
4 changes: 2 additions & 2 deletions lib/zenaton/services/graphql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ def handle_response_body(response_body)
raise Zenaton::ExternalError, format_external_error(response_body)
end

response_body['data']
response_body && response_body['data']
end

def external_error?(response_body)
response_body.key?('errors')
response_body&.key?('errors')
end

def format_external_error(response_body)
Expand Down
49 changes: 49 additions & 0 deletions spec/fixtures/vcr_cassettes/grahpql_ok.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions spec/fixtures/vcr_cassettes/grahpql_validation_errors.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

103 changes: 103 additions & 0 deletions spec/zenaton/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,19 @@
get: workflow_data
)
end
let(:graphql) do
instance_double(
Zenaton::Services::GraphQL,
request: nil
)
end
let(:workflow) { FakeWorkflow1.new(1, 2) }
let(:task) { FakeTask3.new(1, 2) }
let(:event) { FakeEvent.new }
let(:version) { FakeVersion.new(1, 2) }
let(:workflow_data) { { 'name' => 'Zenaton::Interfaces::Workflow' } }
let(:uuid) { 'some-very-valid-uuid4' }
let(:cron) { '* * * * * *' }

before do
setup_client
Expand Down Expand Up @@ -61,6 +68,10 @@
it 'stores an instance of the http service as an instance variable' do
expect(client.instance_variable_get(:@http)).to eq(http)
end

it 'stores an instance of the graphql service as an instance variable' do
expect(client.instance_variable_get(:@graphql)).to eq(graphql)
end
end

describe '#worker_url' do
Expand Down Expand Up @@ -288,6 +299,95 @@
end
end

# rubocop:disable Metrics/LineLength
describe '#start_scheduled_workflow' do
let(:start_scheduled_workflow) { client.start_scheduled_workflow(workflow, cron) }
let(:start_scheduled_version_workflow) { client.start_scheduled_workflow(version, cron) }
let(:expected_url) { 'https://gateway.zenaton.com/api' }
let(:expected_query) { Zenaton::Services::GraphQL::CREATE_WORKFLOW_SCHEDULE }
let(:expected_headers) { { 'app-id' => nil, 'api-token' => nil } }
let(:expected_variables) do
{
'createWorkflowScheduleInput' => {
'intentId' => uuid,
'environmentName' => nil,
'cron' => cron,
'workflowName' => 'FakeWorkflow1',
'canonicalName' => 'FakeWorkflow1',
'programmingLanguage' => 'RUBY',
'properties' => {
'o' => '@zenaton#0',
's' => [{ 'a' => { :@first => 1, :@second => 2 } }]
}.to_json
}
}
end

let(:expected_variables_version_workflow) do
{
'createWorkflowScheduleInput' => {
'intentId' => uuid,
'environmentName' => nil,
'cron' => cron,
'workflowName' => 'FakeWorkflow2',
'canonicalName' => 'FakeVersion',
'programmingLanguage' => 'RUBY',
'properties' => {
'o' => '@zenaton#0',
's' => [{ 'a' => { :@args => '@zenaton#1' } }, { 'a' => [1, 2] }]
}.to_json
}
}
end

context 'with a valid workflow' do
it 'calls the graphQL request' do
e = [expected_url, expected_query, expected_variables, expected_headers]
start_scheduled_workflow
expect(graphql).to have_received(:request).with(*e)
end
end

context 'with a valid version workflow' do
it 'calls the graphQL request' do
e = [expected_url, expected_query, expected_variables_version_workflow, expected_headers]
start_scheduled_version_workflow
expect(graphql).to have_received(:request).with(*e)
end
end
end
# rubocop:enable Metrics/LineLength

describe '#start_scheduled_task' do
let(:start_scheduled_task) { client.start_scheduled_task(task, cron) }
let(:expected_url) { 'https://gateway.zenaton.com/api' }
let(:expected_query) { Zenaton::Services::GraphQL::CREATE_TASK_SCHEDULE }
let(:expected_headers) { { 'app-id' => nil, 'api-token' => nil } }
let(:expected_variables) do
{
'createTaskScheduleInput' => {
'intentId' => uuid,
'environmentName' => nil,
'cron' => cron,
'taskName' => 'FakeTask3',
'programmingLanguage' => 'RUBY',
'properties' => {
'o' => '@zenaton#0',
's' => [{ 'a' => { :@arg1 => 1, :@arg2 => 2 } }]
}.to_json
}
}
end

context 'with a valid workflow' do
it 'calls the graphQL request' do
e = [expected_url, expected_query, expected_variables, expected_headers]
start_scheduled_task
expect(graphql).to have_received(:request).with(*e)
end
end
end

describe '#kill_workflow' do
let(:expected_url) do
'http://localhost:4001/api/v_newton/instances?custom_id=MyCustomId'
Expand Down Expand Up @@ -443,9 +543,12 @@
end
end

# rubocop:disable Metrics/AbcSize
def setup_client
Singleton.__init__(described_class)
allow(Zenaton::Services::Http).to receive(:new).and_return(http)
allow(Zenaton::Services::GraphQL).to receive(:new).and_return(graphql)
allow(SecureRandom).to receive(:uuid).and_return(uuid)
end
# rubocop:enable Metrics/AbcSize
end
Loading

0 comments on commit 0871c41

Please sign in to comment.