Revision 87234768
Added by Kamil Szubrycht about 5 years ago
test/graphql/queries/location_query_test.rb | ||
---|---|---|
require 'test_helper'
|
||
|
||
class Queries::LocationQueryTest < ActiveSupport::TestCase
|
||
test 'fetching location attributes' do
|
||
environment = FactoryBot.create(:environment)
|
||
FactoryBot.create(:puppetclass, :environments => [environment])
|
||
location = FactoryBot.create(:location, environments: [environment])
|
||
|
||
query = <<-GRAPHQL
|
||
class Queries::LocationQueryTest < GraphQLQueryTestCase
|
||
let(:query) do
|
||
<<-GRAPHQL
|
||
query (
|
||
$id: String!
|
||
) {
|
||
... | ... | |
}
|
||
}
|
||
}
|
||
hosts {
|
||
totalCount
|
||
edges {
|
||
node {
|
||
id
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
GRAPHQL
|
||
end
|
||
|
||
location_global_id = Foreman::GlobalId.for(location)
|
||
variables = { id: location_global_id }
|
||
context = { current_user: FactoryBot.create(:user, :admin) }
|
||
let(:environment) { FactoryBot.create(:environment) }
|
||
let(:hosts) { FactoryBot.create_list(:host, 2) }
|
||
let(:location_object) { FactoryBot.create(:location, hosts: hosts, environments: [environment]) }
|
||
|
||
result = ForemanGraphqlSchema.execute(query, variables: variables, context: context)
|
||
expected = {
|
||
'location' => {
|
||
'id' => location_global_id,
|
||
'createdAt' => location.created_at.utc.iso8601,
|
||
'updatedAt' => location.updated_at.utc.iso8601,
|
||
'name' => location.name,
|
||
'title' => location.title,
|
||
'environments' => {
|
||
'totalCount' => location.environments.count,
|
||
'edges' => location.environments.sort_by(&:id).map do |env|
|
||
{
|
||
'node' => {
|
||
'id' => Foreman::GlobalId.for(env)
|
||
}
|
||
}
|
||
end
|
||
},
|
||
'puppetclasses' => {
|
||
'totalCount' => location.puppetclasses.count,
|
||
'edges' => location.puppetclasses.sort_by(&:id).map do |puppetclass|
|
||
{
|
||
'node' => {
|
||
'id' => Foreman::GlobalId.for(puppetclass)
|
||
}
|
||
}
|
||
end
|
||
}
|
||
}
|
||
}
|
||
let(:global_id) { Foreman::GlobalId.for(location_object) }
|
||
let(:variables) {{ id: global_id }}
|
||
let(:data) { result['data']['location'] }
|
||
|
||
setup do
|
||
FactoryBot.create(:puppetclass, :environments => [environment])
|
||
end
|
||
|
||
test 'fetching location attributes' do
|
||
assert_empty result['errors']
|
||
assert_equal expected, result['data']
|
||
|
||
assert_equal global_id, data['id']
|
||
assert_equal location_object.created_at.utc.iso8601, data['createdAt']
|
||
assert_equal location_object.updated_at.utc.iso8601, data['updatedAt']
|
||
assert_equal location_object.name, data['name']
|
||
assert_equal location_object.title, data['title']
|
||
|
||
assert_collection location_object.environments, data['environments']
|
||
assert_collection location_object.puppetclasses, data['puppetclasses']
|
||
assert_collection location_object.hosts, data['hosts'], type_name: 'Host'
|
||
end
|
||
end
|
Also available in: Unified diff
Fixes #26297 - graphql: add Host queries