Skip to content

Commit

Permalink
Fix/transaction log tests (#119)
Browse files Browse the repository at this point in the history
* fix(transaction-log-tests): run all tests

* fix(transaction-log-tests): fix transaction_log response

* fix(transaction-log-tests): fix transaction_log doc_size
  • Loading branch information
paulineribeyre authored Mar 7, 2019
1 parent 797558c commit 982750b
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 160 deletions.
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ before_script:

# command to run tests
script:
- py.test -vv --cov=peregrine --cov-report xml tests/system_test.py tests/graphql/test_graphql.py tests/graphql/test_datasets.py
- py.test -vv --cov=peregrine --cov-report xml tests

after_script:
- python-codacy-coverage -r coverage.xml

env:
global:
- GDC_ES_HOST=localhost

17 changes: 11 additions & 6 deletions peregrine/resources/submission/graphql/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
)

def filter_to_cls_fields(cls, doc):
fields = {f.attname for f in cls._meta.fields}
fields = set(cls._meta.fields.keys())
doc = {
key: val
for key, val in doc.iteritems()
Expand Down Expand Up @@ -76,7 +76,7 @@ def resolve_dependents(self, info, **args):
try:
return [
GenericEntity(**dependent)
for dependent in self.dependents
for dependent in self.dependents or []
]
except AttributeError:
# graphene does unsightly things, if there are no
Expand Down Expand Up @@ -137,7 +137,7 @@ def resolve_unique_keys(self, info, **args):
return []

def resolve_type(self, info, **args):
return lambda: self.type
return self.type

def resolve_related_cases(self, info, **args):
if case_cache_enabled():
Expand Down Expand Up @@ -165,7 +165,7 @@ class TransactionResponse(graphene.ObjectType):
entities = graphene.List(TransactionResponseEntity)

@classmethod
def resolve_entities(cls, response, **args):
def resolve_entities(cls, response, *args, **kwargs):
try:
return [
instantiate_safely(TransactionResponseEntity, entity)
Expand All @@ -189,12 +189,17 @@ class TransactionDocument(graphene.ObjectType):
response_json = graphene.String()
response = graphene.Field(TransactionResponse)

# These fields depend on these columns being loaded
fields_depend_on_columns = {
"doc_size": {"doc"},
}

@classmethod
def resolve_doc_size(cls, document, *args, **kwargs):
return len(document.doc)

@classmethod
def resolve_response(cls, document, *arg, **kwargss):
def resolve_response(cls, document, *arg, **kwargs):
try:
response_json = json.loads(document.response_json)
return instantiate_safely(TransactionResponse, response_json)
Expand Down Expand Up @@ -244,7 +249,7 @@ def resolve_project_id(self, info, **args):

def resolve_documents(self, info, **args):
return [TransactionDocument(**dict(
filtered_column_dict(r, info),
filtered_column_dict(r, info, TransactionDocument.fields_depend_on_columns),
**{'response_json': json.dumps(r.response_json)}
)) for r in self.documents]

Expand Down
2 changes: 1 addition & 1 deletion run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ userdatamodel-init --db test_userapi
python bin/setup_test_database.py
mkdir -p tests/resources/keys; cd tests/resources/keys; openssl genrsa -out test_private_key.pem 2048; openssl rsa -in test_private_key.pem -pubout -out test_public_key.pem; cd -

py.test -vv --cov=peregrine --cov-report xml tests/system_test.py tests/graphql/test_graphql.py tests/graphql/test_datasets.py
py.test -vv --cov=peregrine --cov-report xml tests
57 changes: 29 additions & 28 deletions tests/graphql/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@

@pytest.fixture
def graphql_client(client, submitter):
def execute(query, variables=None):
if variables is None:
variables = {}
data = json.dumps({'query': query, 'variables': variables})
return client.post(path, headers=submitter(path, 'post'), data=data)
def execute(query, variables={}):
return client.post(path, headers=submitter, data=json.dumps({
'query': query,
'variables': variables,
}))
return execute


Expand Down Expand Up @@ -87,10 +87,10 @@ def put_tcga_brca(admin, client):


@pytest.fixture
def mock_tx_log(pg_driver):
utils.reset_transactions(pg_driver)
with pg_driver.session_scope() as session:
return session.merge(TransactionLog(
def mock_tx_log(pg_driver_clean):
utils.reset_transactions(pg_driver_clean)
with pg_driver_clean.session_scope() as session:
return session.merge(models.submission.TransactionLog(
is_dry_run=True,
program='CGCI',
project='BLGSP',
Expand All @@ -102,34 +102,35 @@ def mock_tx_log(pg_driver):


@pytest.fixture
def populated_blgsp(client, submitter, pg_driver, cgci_blgsp):
post_example_entities_together(client, pg_driver, submitter)
def populated_blgsp(client, submitter, pg_driver_clean):
utils.reset_transactions(pg_driver_clean)
post_example_entities_together(client, pg_driver_clean, submitter)


@pytest.fixture
def failed_deletion_transaction(client, submitter, pg_driver, populated_blgsp):
with pg_driver.session_scope():
node_id = pg_driver.nodes(models.Sample).first().node_id
def failed_deletion_transaction(client, submitter, pg_driver_clean, populated_blgsp):
with pg_driver_clean.session_scope():
node_id = pg_driver_clean.nodes(models.Sample).first().node_id
delete_path = '/v0/submission/CGCI/BLGSP/entities/{}'.format(node_id)
r = client.delete(
'/v0/submission/CGCI/BLGSP/entities/{}'.format(node_id),
headers=submitter(path, 'delete')
)
delete_path,
headers=submitter)
assert r.status_code == 400, r.data
return str(r.json['transaction_id'])


@pytest.fixture
def failed_upload_transaction(client, submitter, pg_driver):
data = json.dumps({
'type': 'sample',
'cases': [{'id': 'no idea'}],
'sample_type': 'teapot',
'how_heavy': 'no',
}),
def failed_upload_transaction(client, submitter, pg_driver_clean):
put_path = '/v0/submission/CGCI/BLGSP/'
r = client.put(
'/v0/submission/CGCI/BLGSP/',
headers=submitter(path, 'put'),
data=data,
)
put_path,
data=json.dumps({
'type': 'sample',
'cases': [{'id': 'no idea'}],
'sample_type': 'teapot',
'how_heavy': 'no',
}),
headers=submitter)
assert r.status_code == 400, r.data
return str(r.json['transaction_id'])

63 changes: 0 additions & 63 deletions tests/graphql/test_graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,69 +18,6 @@

path = '/v0/submission/graphql'

# ======================================================================
# Fixtures

@pytest.fixture
def graphql_client(client, submitter):
def execute(query, variables={}):
return client.post(path, headers=submitter, data=json.dumps({
'query': query,
'variables': variables,
}))
return execute


@pytest.fixture
def mock_tx_log(pg_driver_clean):
utils.reset_transactions(pg_driver_clean)
with pg_driver_clean.session_scope() as session:
return session.merge(models.submission.TransactionLog(
is_dry_run=True,
program='CGCI',
project='BLGSP',
role='create',
state='SUCCEEDED',
committed_by=12345,
closed=False,
))


@pytest.fixture
def populated_blgsp(client, submitter, pg_driver_clean):
utils.reset_transactions(pg_driver_clean)
post_example_entities_together(client, pg_driver_clean, submitter)


@pytest.fixture
def failed_deletion_transaction(client, submitter, pg_driver_clean, populated_blgsp):
with pg_driver_clean.session_scope():
node_id = pg_driver_clean.nodes(models.Sample).first().node_id
delete_path = '/v0/submission/CGCI/BLGSP/entities/{}'.format(node_id)
r = client.delete(
delete_path,
headers=submitter)
assert r.status_code == 400, r.data
return str(r.json['transaction_id'])


@pytest.fixture
def failed_upload_transaction(client, submitter, pg_driver_clean):
put_path = '/v0/submission/CGCI/BLGSP/'
r = client.put(
put_path,
data=json.dumps({
'type': 'sample',
'cases': [{'id': 'no idea'}],
'sample_type': 'teapot',
'how_heavy': 'no',
}),
headers=submitter)
assert r.status_code == 400, r.data
return str(r.json['transaction_id'])

# ======================================================================
# Tests

def post_example_entities_together(
client, pg_driver_clean, submitter, data_fnames=data_fnames):
Expand Down
Loading

0 comments on commit 982750b

Please sign in to comment.