Skip to content

Commit

Permalink
Merge pull request #221 from GNS-Science/fix/220_nodes_query_error
Browse files Browse the repository at this point in the history
Fix/220 nodes query error
  • Loading branch information
chrisbc authored Jun 26, 2024
2 parents e13a619 + abf5c42 commit 2f65546
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 16 deletions.
4 changes: 3 additions & 1 deletion graphql_api/schema/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ def resolve_nodes(root, info, id_in, **kwargs):
_type, _id = from_global_id(gid)
if _type in ['RuptureGenerationTask', 'StrongMotionStation', 'GeneralTask', 'AutomationTask']:
result.append(db_root.thing.get_one(_id))
elif _type in ['File', 'SmsFile', 'InversionSolution']:
elif _type in ['File', 'SmsFile']:
result.append(db_root.file.get_one(_id))
elif "InversionSolution" in _type:
result.append(db_root.file.get_one(_id))
elif _type in ['Table']:
result.append(db_root.table.get_one(_id))
Expand Down
17 changes: 2 additions & 15 deletions graphql_api/tests/test_general_task_bugfix_217.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,23 +63,10 @@ def setUp(self):

self._data_manager = data_manager.DataManager(search_manager=SearchManager('test', 'test', {'fake': 'auth'}))

def test_create_two_gts_and_link_them(self):
# the first GT
def test_create_one_gt(self):

gt1_result = self.client.execute(CREATE_GT, variable_values=dict(created=dt.datetime.now(tzutc())))
print(gt1_result)
assert gt1_result['data']['create_general_task']['general_task']['id'] == 'R2VuZXJhbFRhc2s6MTAwMDAw'
assert gt1_result['data']['create_general_task']['general_task']['subtask_type'] == 'OPENQUAKE_HAZARD'
assert gt1_result['data']['create_general_task']['general_task']['model_type'] == 'COMPOSITE'

# # the second
# gt2_result = self.client.execute(CREATE_GT, variable_values=dict(created=dt.datetime.now(tzutc())))
# print(gt2_result)
# assert gt2_result['data']['create_general_task']['general_task']['id'] == 'R2VuZXJhbFRhc2s6MQ=='

# # finally the relation
# gt_link_result = self.client.execute(
# CREATE_GT_RELATION, variable_values=dict(parent_id='R2VuZXJhbFRhc2s6MA==', child_id='R2VuZXJhbFRhc2s6MQ==')
# )

# print('GTLINK ', gt_link_result)
# assert gt_link_result['data']['create_task_relation']['ok'] == True
200 changes: 200 additions & 0 deletions graphql_api/tests/test_nodes_bugfix_220.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
"""
Test API function for GeneralTask
using moto mocking
"""

import datetime as dt
import unittest

import boto3
from dateutil.tz import tzutc
from graphene.test import Client
from graphql_relay import from_global_id
from moto import mock_dynamodb, mock_s3
from pynamodb.connection.base import Connection # for mocking

from graphql_api.config import REGION, S3_BUCKET_NAME
from graphql_api.data import data_manager
from graphql_api.dynamodb.models import ToshiFileObject, ToshiIdentity, ToshiThingObject
from graphql_api.schema import root_schema
from graphql_api.schema.search_manager import SearchManager

from .hazard.setup_helpers import SetupHelpersMixin


@mock_dynamodb
@mock_s3
class TestScaledInversionSolution(unittest.TestCase, SetupHelpersMixin):
def setUp(self):
self.client = Client(root_schema)

# S3
self._s3 = boto3.resource('s3', region_name=REGION)
self._s3.create_bucket(Bucket=S3_BUCKET_NAME)

# Dynamo
self._connection = Connection(region=REGION)

ToshiThingObject.create_table()
ToshiFileObject.create_table()
ToshiIdentity.create_table()

self._data_manager = data_manager.DataManager(search_manager=SearchManager('test', 'test', {'fake': 'auth'}))

upstream_sid = self.create_source_solution()
self.new_gt = self.create_general_task()
self.at_id = self.create_automation_task("SCALE_SOLUTION")
self.create_gt_relation(self.new_gt, self.at_id)

result = self.create_scaled_solution(upstream_sid, self.at_id)

ss = result['data']['create_scaled_inversion_solution']['solution']
# self.assertEqual(ss['source_solution']['id'], upstream_sid)

# print(ToshiFileObject.get("100001").object_content)

# # object ID is stored internally as an INT
# self.assertEqual(ToshiFileObject.get("100001").object_content['id'], int(from_global_id(ss['id'])[1]))

self.scaled_solution_id = ss['id']

def test_nodes_query(self):
print("self.scaled_solution_id", self.scaled_solution_id)
qry = (
'''
query q0 {
nodes(id_in: ["%s"]) {
ok
result {
edges {
node {
__typename
... on Node {
id
}
}
}
}
}
}'''
% self.scaled_solution_id
)

print(qry)
executed = self.client.execute(qry)
print(executed)

node = executed['data']['nodes']['result']['edges'][0]['node']
assert node['id'] == self.scaled_solution_id
assert node['__typename'] == "ScaledInversionSolution"

def test_nodes_query_expand_solution(self):
print("self.scaled_solution_id", self.scaled_solution_id)
qry = (
'''
query q0 {
nodes(id_in: ["%s"]) {
ok
result {
edges {
node {
__typename
... on Node {
id
}
... on InversionSolutionInterface {
produced_by { ... on Node{id} }
}
# ... on PredecessorsInterface {
# predecessors {
# typename
# relationship
# node {
# __typename
# ... on Node{ id }
# }
# depth
# }
# }
... on FileInterface {
file_name
file_size
}
... on ScaledInversionSolution {
source_solution { ... on Node{id} }
}
}
}
}
}
}'''
% self.scaled_solution_id
)

print(qry)
executed = self.client.execute(qry)
print(executed)

node = executed['data']['nodes']['result']['edges'][0]['node']
assert node['id'] == self.scaled_solution_id
assert node['__typename'] == "ScaledInversionSolution"
assert node['source_solution']
assert node['source_solution']['id']
assert node['produced_by']
assert node['produced_by']['id']

def test_nodes_query_expand_solution_task_hierarchy(self):
print("self.scaled_solution_id", self.scaled_solution_id)
qry = (
'''
query q0 {
nodes(id_in: ["%s"]) {
ok
result {
edges {
node {
__typename
... on Node {
id
}
... on InversionSolutionInterface {
produced_by {
__typename
... on Node{id} # the AutomationTask
... on AutomationTaskInterface {
parents {
edges {
node {
parent {
... on Node { id } # the GT id
... on GeneralTask {
meta {k v}
title
description
created
}
}
}
}
}
}
}
}
}
}
}
}
}'''
% self.scaled_solution_id
)

print(qry)
executed = self.client.execute(qry)
print(executed)

node = executed['data']['nodes']['result']['edges'][0]['node']
assert node['id'] == self.scaled_solution_id
assert node['__typename'] == "ScaledInversionSolution"
assert node['produced_by']
assert node['produced_by']['id']
assert node['produced_by']['parents']['edges'][0]['node']['parent']['title']

0 comments on commit 2f65546

Please sign in to comment.