from dagster_graphql.test.utils import execute_dagster_graphql

from .setup import define_context

RESOURCE_QUERY = '''
{
  pipeline(params: { name: "multi_mode_with_resources" }) {
    __typename
    ... on Pipeline {
      modes {
        name
        resources {
          name
          description
          configField {
            configType {
              name
              ... on CompositeConfigType {
                fields {
                  name
                  configType {
                    name
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
'''

REQUIRED_RESOURCE_QUERY = '''{
  pipeline(params: { name:"required_resource_pipeline" }){
    name
    solids {
      definition {
          requiredResources {
            resourceKey
          }
      }
    }
  }
}
'''


def test_mode_fetch_resources(snapshot):
    result = execute_dagster_graphql(define_context(), RESOURCE_QUERY)

    assert not result.errors
    assert result.data
    assert result.data['pipeline']
    assert result.data['pipeline']['modes']
    for mode_data in result.data['pipeline']['modes']:
        assert mode_data['resources']

    snapshot.assert_match(result.data)


def test_required_resources(snapshot):
    result = execute_dagster_graphql(define_context(), REQUIRED_RESOURCE_QUERY)

    assert not result.errors
    assert result.data
    assert result.data['pipeline']['solids']
    [solid] = result.data['pipeline']['solids']
    assert solid
    assert solid['definition']['requiredResources']
    assert solid['definition']['requiredResources'] == [{'resourceKey': 'R1'}]

    snapshot.assert_match(result.data)
