|
1 |
| -import pytest |
2 | 1 | import json
|
| 2 | +from io import BytesIO |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +from flask import url_for |
| 7 | +from flask_graphql.graphqlview import place_files_in_operations |
| 8 | +from werkzeug.test import EnvironBuilder |
| 9 | + |
| 10 | +from .app import create_app |
3 | 11 |
|
4 | 12 | try:
|
5 | 13 | from StringIO import StringIO
|
6 | 14 | except ImportError:
|
7 | 15 | from io import StringIO
|
8 | 16 |
|
| 17 | + |
| 18 | + |
9 | 19 | try:
|
10 | 20 | from urllib import urlencode
|
11 | 21 | except ImportError:
|
12 | 22 | from urllib.parse import urlencode
|
13 | 23 |
|
14 |
| -from .app import create_app |
15 |
| -from flask import url_for |
16 | 24 |
|
17 | 25 |
|
18 | 26 | @pytest.fixture
|
@@ -465,18 +473,93 @@ def test_supports_pretty_printing(client):
|
465 | 473 |
|
466 | 474 |
|
467 | 475 | def test_post_multipart_data(client):
|
468 |
| - query = 'mutation TestMutation { writeTest { test } }' |
| 476 | + query = """ |
| 477 | + mutation TestMutation($file: FileUpload!) { |
| 478 | + fileUploadTest(file: $file) { |
| 479 | + data, name, type |
| 480 | + } |
| 481 | + } |
| 482 | + """ |
| 483 | + |
469 | 484 | response = client.post(
|
470 | 485 | url_string(),
|
471 |
| - data= { |
472 |
| - 'query': query, |
473 |
| - 'file': (StringIO(), 'text1.txt'), |
474 |
| - }, |
475 |
| - content_type='multipart/form-data' |
476 |
| - ) |
| 486 | + method='POST', |
| 487 | + data={ |
| 488 | + # Form data |
| 489 | + 'operations': json.dumps({ |
| 490 | + 'query': query, |
| 491 | + 'variables': {'file': None}, |
| 492 | + }), |
| 493 | + 'map': json.dumps({ |
| 494 | + '0': ['variables.file'], |
| 495 | + }), |
| 496 | + '0': (BytesIO(b'FILE-DATA-HERE'), 'hello.txt', 'text/plain'), |
| 497 | + }) |
477 | 498 |
|
478 | 499 | assert response.status_code == 200
|
479 |
| - assert response_json(response) == {'data': {u'writeTest': {u'test': u'Hello World'}}} |
| 500 | + assert response_json(response) == { |
| 501 | + 'data': {u'fileUploadTest': { |
| 502 | + 'data': u'FILE-DATA-HERE', |
| 503 | + 'name': 'hello.txt', |
| 504 | + 'type': 'text/plain', |
| 505 | + }}, |
| 506 | + } |
| 507 | + |
| 508 | + |
| 509 | +def test_can_place_file_in_flat_variable(): |
| 510 | + operations = { |
| 511 | + 'variables': {'myfile': None}, |
| 512 | + "query": "QUERY", |
| 513 | + } |
| 514 | + files_map = {"0": ["variables.myfile"]} |
| 515 | + files = {"0": "FILE-0-HERE"} |
| 516 | + |
| 517 | + assert place_files_in_operations(operations, files_map, files) == { |
| 518 | + 'variables': {'myfile': "FILE-0-HERE"}, |
| 519 | + "query": "QUERY", |
| 520 | + } |
| 521 | + |
| 522 | + |
| 523 | +def test_can_place_file_in_list_variable(): |
| 524 | + operations = { |
| 525 | + 'variables': {'myfile': [None]}, |
| 526 | + "query": "QUERY", |
| 527 | + } |
| 528 | + files_map = {"0": ["variables.myfile.0"]} |
| 529 | + files = {"0": "FILE-0-HERE"} |
| 530 | + |
| 531 | + assert place_files_in_operations(operations, files_map, files) == { |
| 532 | + 'variables': {'myfile': ["FILE-0-HERE"]}, |
| 533 | + "query": "QUERY", |
| 534 | + } |
| 535 | + |
| 536 | + |
| 537 | +def test_can_place_file_in_flat_variable_in_ops_list(): |
| 538 | + operations = [{ |
| 539 | + 'variables': {'myfile': None}, |
| 540 | + "query": "QUERY", |
| 541 | + }] |
| 542 | + files_map = {"0": ["0.variables.myfile"]} |
| 543 | + files = {"0": "FILE-0-HERE"} |
| 544 | + |
| 545 | + assert place_files_in_operations(operations, files_map, files) == [{ |
| 546 | + 'variables': {'myfile': "FILE-0-HERE"}, |
| 547 | + "query": "QUERY", |
| 548 | + }] |
| 549 | + |
| 550 | + |
| 551 | +def test_can_place_file_in_list_variable_in_ops_list(): |
| 552 | + operations = [{ |
| 553 | + 'variables': {'myfile': [None]}, |
| 554 | + "query": "QUERY", |
| 555 | + }] |
| 556 | + files_map = {"0": ["0.variables.myfile.0"]} |
| 557 | + files = {"0": "FILE-0-HERE"} |
| 558 | + |
| 559 | + assert place_files_in_operations(operations, files_map, files) == [{ |
| 560 | + 'variables': {'myfile': ["FILE-0-HERE"]}, |
| 561 | + "query": "QUERY", |
| 562 | + }] |
480 | 563 |
|
481 | 564 |
|
482 | 565 | @pytest.mark.parametrize('app', [create_app(batch=True)])
|
@@ -514,8 +597,8 @@ def test_batch_supports_post_json_query_with_json_variables(client):
|
514 | 597 | # 'id': 1,
|
515 | 598 | 'data': {'test': "Hello Dolly"}
|
516 | 599 | }]
|
517 |
| - |
518 |
| - |
| 600 | + |
| 601 | + |
519 | 602 | @pytest.mark.parametrize('app', [create_app(batch=True)])
|
520 | 603 | def test_batch_allows_post_with_operation_name(client):
|
521 | 604 | response = client.post(
|
|
0 commit comments