graphql-engine/server/tests-py/test_compression.py
Brandon Simmons 91aee7fdeb Test result ordering, add --accept test mode to automatically accept changed test cases
We add a new pytest flag `--accept` that will automatically write back
yaml files with updated responses. This makes it much easier and less
error-prone to update test cases when we expect output to change, or
when authoring new tests.

Second we make sure to test that we actually preserve the order of the
selection set when returning results. This is a "SHOULD" part of the
spec but seems pretty important and something that users will rely on.

To support both of the above we use ruamel.yaml which preserves a
certain amount of formatting and comments (so that --accept can work in
a failry ergonomic way), as well as ordering (so that when we write yaml
the order of keys has meaning that's preserved during parsing).

Use ruamel.yaml everywhere for consistency (since both libraries have
different quirks).

Quirks of ruamel.yaml:
- trailing whitespace in multiline strings in yaml files isn't written
  back out as we'd like: https://bitbucket.org/ruamel/yaml/issues/47/multiline-strings-being-changed-if-they
- formatting is only sort of preserved; ruamel e.g. normalizes
  indentation. Normally the diff is pretty clean though, and you can
  always just check in portions of your test file after --accept

fixup
2019-11-05 15:15:25 -06:00

79 lines
2.7 KiB
Python

#!/usr/bin/env python3
import pytest
import ruamel.yaml as yaml
import jsondiff
from super_classes import DefaultTestSelectQueries
class TestCompression(DefaultTestSelectQueries):
gzip_header = {'Accept-Encoding': 'gzip'}
brotli_header = {'Accept-Encoding': 'br'}
gzip_brotli_header = {'Accept-Encoding': 'gzip, br'}
def _make_post(self, hge_ctx, u, q, h):
if hge_ctx.hge_key is not None:
h['X-Hasura-Admin-Secret'] = hge_ctx.hge_key
resp = hge_ctx.http.post(
hge_ctx.hge_url + u,
json=q,
headers=h
)
return resp
def _get_config(self, f):
with open(f) as c:
conf = yaml.safe_load(c)
return conf['url'], conf['query'], conf['response']
def _assert_status_code_200(self, resp):
assert resp.status_code == 200, resp.json()
def _assert_encoding(self, headers, encoding):
assert 'Content-Encoding' in headers, headers
assert headers['Content-Encoding'] == encoding, headers
def _assert_resp(self, resp, exp_resp):
json_resp = resp.json()
assert json_resp == exp_resp, yaml.dump({
'response': json_resp,
'expected': exp_resp,
'diff': jsondiff.diff(exp_resp, json_resp)
})
def _assert_gzip(self, resp, exp_resp):
self._assert_status_code_200(resp)
self._assert_encoding(resp.headers, 'gzip')
self._assert_resp(resp, exp_resp)
def _assert_brotli(self, resp, exp_resp):
self._assert_status_code_200(resp)
self._assert_encoding(resp.headers, 'br')
self._assert_resp(resp, exp_resp)
def test_gzip_compression_graphql(self, hge_ctx):
url, q, exp_resp = self._get_config(self.dir() + '/graphql_query.yaml')
resp = self._make_post(hge_ctx, url, q, self.gzip_header)
self._assert_gzip(resp, exp_resp)
def test_gzip_compression_v1_query(self, hge_ctx):
url, q, exp_resp = self._get_config(self.dir() + '/v1_query.yaml')
resp = self._make_post(hge_ctx, url, q, self.gzip_header)
self._assert_gzip(resp, exp_resp)
def test_gzip_brotli_graphql_query(self, hge_ctx):
url, q, exp_resp = self._get_config(self.dir() + '/graphql_query.yaml')
resp = self._make_post(hge_ctx, url, q, self.gzip_brotli_header)
self._assert_gzip(resp, exp_resp)
def test_gzip_brotli_v1_query(self, hge_ctx):
url, q, exp_resp = self._get_config(self.dir() + '/v1_query.yaml')
resp = self._make_post(hge_ctx, url, q, self.gzip_brotli_header)
self._assert_gzip(resp, exp_resp)
@classmethod
def dir(cls):
return 'queries/compression'