Beispiel #1
0
 def test_condens(self):
     entity = {
         'identificatie': '10281154',
         "ligtInBouwblok": {
             "edges": [{
                 "node": {
                     "identificatie": "03630012101200",
                     "volgnummer": 1
                 }
             }]
         }
     }
     request = {'condens': 'edges,node'}
     rb = GraphQLCustomStreamingResponseBuilder(None,
                                                None,
                                                None,
                                                request_args=request)
     self.assertTrue(rb.has_options)
     result = rb._customized_entity(entity)
     expect = {
         'identificatie': '10281154',
         'ligtInBouwblok': {
             'identificatie': '03630012101200',
             'volgnummer': 1
         }
     }
     self.assertEqual(result, expect)
Beispiel #2
0
 def test_geojson(self):
     entity = {
         "identificatie": "10281154",
         "geometrie1": "POINT(119411.7 487201.6)",
         "geometrie2": "POINT(119411.7 487201.6)",
         "geometrie3": "POINT(119411.7 487201.6)",
         "geometrie4": None,
     }
     request = {'geojson': 'geometrie1,geometrie3,geometrie4'}
     rb = GraphQLCustomStreamingResponseBuilder(None,
                                                None,
                                                None,
                                                request_args=request)
     self.assertTrue(rb.has_options)
     result = rb._customized_entity(entity)
     expect = {
         'identificatie': '10281154',
         'geometrie1': {
             "type": "Point",
             "coordinates": [119411.7, 487201.6]
         },
         'geometrie2': 'POINT(119411.7 487201.6)',
         'geometrie3': {
             "type": "Point",
             "coordinates": [119411.7, 487201.6]
         },
         'geometrie4': None,
     }
     self.assertEqual(result, expect)
Beispiel #3
0
 def test_schema(self, mock_super_build_entity):
     entity = {"identificatie": "10281154"}
     request = {'schema': 'schema name'}
     rb = GraphQLCustomStreamingResponseBuilder(None,
                                                None,
                                                None,
                                                request_args=request)
     self.assertTrue(rb.has_options)
     mock_super_build_entity.return_value = entity
     result = rb._build_entity(entity)
     expect = {'identificatie': '10281154', 'schema': 'schema name'}
     self.assertEqual(result, expect)
Beispiel #4
0
    def entrypoint(self):
        # Compatible with plain GraphQL endpoint
        query = request.args.get('query')
        if not query:
            # Compatible with existing GOB export code
            request_data = json.loads(request.data.decode('utf-8'))
            query = request_data['query']
        graphql2sql = GraphQL2SQL(query)
        try:
            sql = graphql2sql.sql()
        except NoAccessException as e:
            return "Forbidden", 403
        session = get_session()
        # use an ad-hoc Connection and stream results (instead of pre-buffered)
        result_rows = session.connection().execution_options(
            stream_results=True).execute(text(sql))

        response_builder = \
            GraphQLCustomStreamingResponseBuilder(result_rows,
                                                  graphql2sql.relations_hierarchy,
                                                  graphql2sql.selections,
                                                  request_args=request.args)

        return WorkerResponse.stream_with_context(
            response_builder, mimetype='application/x-ndjson')
Beispiel #5
0
    def entrypoint(self):
        query = self._get_query()
        if not query:
            logger.warning("No query passed in")
            return jsonify({'error': str("no query given")}), 400

        flat_query = query.replace("\n", "")
        logger.info(f"Running GraphQL for {get_request_id()}: {flat_query}")
        graphql2sql = GraphQL2SQL(query)
        try:
            sql = graphql2sql.sql()
        except NoAccessException:
            return jsonify({'error': 'Forbidden'}), 403
        except InvalidQueryException as e:
            return jsonify({'error': str(e)}), 400
        session = get_session()
        # use an ad-hoc Connection and stream results (instead of pre-buffered)
        result_rows = session.connection().execution_options(
            stream_results=True).execute(text(sql))
        response_builder = GraphQLCustomStreamingResponseBuilder(
            result_rows,
            graphql2sql.relations_hierarchy,
            graphql2sql.selections,
            request_args=request.args)

        return WorkerResponse.stream_with_context(
            response_builder, mimetype='application/x-ndjson')
Beispiel #6
0
 def test_flatten(self):
     entity = {
         "identificatie": "10281154",
         "status": {
             "code": 1,
             "omschrijving": "Actueel"
         }
     }
     request = {'flatten': 'true'}
     rb = GraphQLCustomStreamingResponseBuilder(None,
                                                None,
                                                None,
                                                request_args=request)
     self.assertTrue(rb.has_options)
     result = rb._customized_entity(entity)
     expect = {
         'identificatie': '10281154',
         'statuscode': 1,
         'statusomschrijving': 'Actueel'
     }
     self.assertEqual(result, expect)
Beispiel #7
0
 def test_init(self):
     rb = GraphQLCustomStreamingResponseBuilder(None, None, None)
     self.assertIsInstance(rb, GraphQLCustomStreamingResponseBuilder)
     self.assertEqual(rb.condens, [])
     self.assertEqual(rb.flatten, False)
Beispiel #8
0
    def test_build_entity(self, mock_super_build_entity):
        rb = GraphQLCustomStreamingResponseBuilder(None, None, None)
        rb._customized_entity = MagicMock()
        result = rb._build_entity('any entity')
        mock_super_build_entity.assert_called_with('any entity')
        rb._customized_entity.assert_not_called()
        self.assertEqual(result, mock_super_build_entity.return_value)

        request = {'condens': 'any condens'}
        rb = GraphQLCustomStreamingResponseBuilder(None,
                                                   None,
                                                   None,
                                                   request_args=request)
        rb._customized_entity = MagicMock()
        result = rb._build_entity('any entity')
        rb._customized_entity.assert_called_with(
            mock_super_build_entity.return_value)
        self.assertEqual(result, rb._customized_entity.return_value)

        request = {'flatten': 'true'}
        rb = GraphQLCustomStreamingResponseBuilder(None,
                                                   None,
                                                   None,
                                                   request_args=request)
        rb._customized_entity = MagicMock()
        rb._build_entity('any entity')
        rb._customized_entity.assert_called()