|
| 1 | +#!/usr/bin/env tarantool |
| 2 | +local fio = require('fio') |
| 3 | +local yaml = require('yaml') |
| 4 | +local avro = require('avro_schema') |
| 5 | +local test = require('tap').test('to avro schema') |
| 6 | +local testdata = require('test.testdata.common_testdata') |
| 7 | +local graphql = require('graphql') |
| 8 | + |
| 9 | +-- require in-repo version of graphql/ sources despite current working directory |
| 10 | +package.path = fio.abspath(debug.getinfo(1).source:match("@?(.*/)") |
| 11 | + :gsub('/./', '/'):gsub('/+$', '')) .. '/../../?.lua' .. ';' .. package.path |
| 12 | + |
| 13 | +test:plan(5) |
| 14 | + |
| 15 | +box.cfg{} |
| 16 | + |
| 17 | +testdata.init_spaces() |
| 18 | +local meta = testdata.get_test_metadata() |
| 19 | +testdata.fill_test_data(box.space, meta) |
| 20 | + |
| 21 | +local accessor = graphql.accessor_space.new({ |
| 22 | + schemas = meta.schemas, |
| 23 | + collections = meta.collections, |
| 24 | + service_fields = meta.service_fields, |
| 25 | + indexes = meta.indexes, |
| 26 | +}) |
| 27 | + |
| 28 | +local gql_wrapper = graphql.new({ |
| 29 | + schemas = meta.schemas, |
| 30 | + collections = meta.collections, |
| 31 | + accessor = accessor, |
| 32 | +}) |
| 33 | + |
| 34 | +local query = [[ |
| 35 | + query order_by_id($order_id: String, $include_description: Boolean, |
| 36 | + $skip_discount: Boolean, $include_user: Boolean, |
| 37 | + $include_user_first_name: Boolean, $user_id: String, |
| 38 | + $include_order_meta: Boolean) { |
| 39 | + order_collection(order_id: $order_id) { |
| 40 | + order_id |
| 41 | + description @include(if: $include_description) |
| 42 | + discount @skip(if: $skip_discount) |
| 43 | + user_connection(user_id: "user_id_1") |
| 44 | + @include(if: $include_user) { |
| 45 | + user_id |
| 46 | + first_name @include(if: $include_user_first_name) |
| 47 | + } |
| 48 | + order_metainfo_connection(order_id: $order_id) |
| 49 | + @skip(if: $include_order_meta) { |
| 50 | + order_metainfo_id |
| 51 | + } |
| 52 | + } |
| 53 | + user_collection(user_id: $user_id) @include(if: $include_user) { |
| 54 | + user_id |
| 55 | + first_name |
| 56 | + } |
| 57 | + } |
| 58 | +]] |
| 59 | + |
| 60 | +local expected_avro_schema = [[ |
| 61 | +type: record |
| 62 | +name: Query |
| 63 | +fields: |
| 64 | +- name: order_collection |
| 65 | + type: |
| 66 | + type: array |
| 67 | + items: |
| 68 | + type: record |
| 69 | + fields: |
| 70 | + - name: order_id |
| 71 | + type: string |
| 72 | + - name: description |
| 73 | + type: string* |
| 74 | + - name: discount |
| 75 | + type: double* |
| 76 | + - name: user_connection |
| 77 | + type: |
| 78 | + type: record* |
| 79 | + fields: |
| 80 | + - name: user_id |
| 81 | + type: string |
| 82 | + - name: first_name |
| 83 | + type: string* |
| 84 | + name: user_collection |
| 85 | + namespace: Query.order_collection |
| 86 | + - name: order_metainfo_connection |
| 87 | + type: |
| 88 | + type: record* |
| 89 | + fields: |
| 90 | + - name: order_metainfo_id |
| 91 | + type: string |
| 92 | + name: order_metainfo_collection |
| 93 | + namespace: Query.order_collection |
| 94 | + name: order_collection |
| 95 | + namespace: Query |
| 96 | +- name: user_collection |
| 97 | + type: |
| 98 | + type: array* |
| 99 | + items: |
| 100 | + type: record |
| 101 | + fields: |
| 102 | + - name: user_id |
| 103 | + type: string |
| 104 | + - name: first_name |
| 105 | + type: string |
| 106 | + name: user_collection |
| 107 | + namespace: Query |
| 108 | +]] |
| 109 | + |
| 110 | +local gql_query = gql_wrapper:compile(query) |
| 111 | + |
| 112 | +local avro_from_query = gql_query:avro_schema() |
| 113 | +expected_avro_schema = yaml.decode(expected_avro_schema) |
| 114 | +test:is_deeply(avro_from_query, expected_avro_schema, |
| 115 | + 'comparision between expected and generated (from query) avro-schemas') |
| 116 | + |
| 117 | +local ok, compiled_schema = avro.create(avro_from_query) |
| 118 | +assert(ok, tostring(compiled_schema)) |
| 119 | + |
| 120 | +local variables_1 = { |
| 121 | + order_id = 'order_id_1', |
| 122 | + user_id = 'user_id_1', |
| 123 | + include_description = true, |
| 124 | + skip_discount = false, |
| 125 | + include_user = true, |
| 126 | + include_user_first_name = false, |
| 127 | + include_order_meta = true |
| 128 | +} |
| 129 | + |
| 130 | +local result_1 = gql_query:execute(variables_1) |
| 131 | +result_1 = result_1.data |
| 132 | +local result_expected_1 = yaml.decode([[ |
| 133 | + user_collection: |
| 134 | + - user_id: user_id_1 |
| 135 | + first_name: Ivan |
| 136 | + order_collection: |
| 137 | + - order_id: order_id_1 |
| 138 | + discount: 0 |
| 139 | + description: first order of Ivan |
| 140 | + user_connection: |
| 141 | + user_id: user_id_1 |
| 142 | +]]) |
| 143 | + |
| 144 | +test:is_deeply(result_1, result_expected_1, |
| 145 | + 'comparision between expected and actual query response') |
| 146 | + |
| 147 | +local ok, err = avro.validate(compiled_schema, result_1) |
| 148 | +assert(ok, tostring(err)) |
| 149 | +test:is(ok, true, 'query response validation by avro') |
| 150 | + |
| 151 | +local variables_2 = { |
| 152 | + order_id = 'order_id_1', |
| 153 | + user_id = 'user_id_1', |
| 154 | + include_description = false, |
| 155 | + skip_discount = true, |
| 156 | + include_user = true, |
| 157 | + include_user_first_name = true, |
| 158 | + include_order_meta = false |
| 159 | +} |
| 160 | + |
| 161 | +local result_2 = gql_query:execute(variables_2) |
| 162 | +result_2 = result_2.data |
| 163 | +local result_expected_2 = yaml.decode([[ |
| 164 | + user_collection: |
| 165 | + - user_id: user_id_1 |
| 166 | + first_name: Ivan |
| 167 | + order_collection: |
| 168 | + - order_id: order_id_1 |
| 169 | + user_connection: |
| 170 | + user_id: user_id_1 |
| 171 | + first_name: Ivan |
| 172 | + order_metainfo_connection: |
| 173 | + order_metainfo_id: order_metainfo_id_1 |
| 174 | +]]) |
| 175 | + |
| 176 | +test:is_deeply(result_2, result_expected_2, |
| 177 | + 'comparision between expected and actual query response') |
| 178 | + |
| 179 | +local ok, err = avro.validate(compiled_schema, result_2) |
| 180 | +assert(ok, tostring(err)) |
| 181 | +test:is(ok, true, 'query response validation by avro') |
| 182 | + |
| 183 | +assert(test:check(), 'check plan') |
| 184 | + |
| 185 | +os.exit() |
0 commit comments