Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added api tests for importing JSON feature #2977

Merged
merged 6 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
48 changes: 48 additions & 0 deletions mathesar/tests/api/test_data_file_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os

import pytest
import tempfile
from mathesar.errors import URLNotReachable
from unittest.mock import patch
from django.core.files import File
Expand Down Expand Up @@ -212,6 +213,53 @@ def test_data_file_create_invalid_file(client):
assert response_dict[0]['message'] == 'Unable to tabulate data'


def test_data_file_create_unsupported_json_file(client):
invalid_json_structures = [
b'"Hello"',
b'["John", "Mary"]',
b'1'
]

for value in invalid_json_structures:
response = _get_response_for_unsupported_json_files(client, value)
response_dict = response.json()
assert response.status_code == 400
assert response_dict[0]['message'] == 'This JSON format is not supported.'


def _get_response_for_unsupported_json_files(client, json_content):
with tempfile.NamedTemporaryFile(suffix='tempfile.json') as f:
f.write(json_content)
f.seek(0)
response = client.post('/api/db/v0/data_files/', data={'file': f}, format='multipart')

return response


def test_data_file_create_invalid_json_file(client):
invalid_json_structures = [
b'[{"Name": "John"]',
b"'Hello'",
b'[{"Name": "John"},]',
b'[{"Name": "John",}]'
]

for value in invalid_json_structures:
response = _get_response_for_invalid_json_files(client, value)
response_dict = response.json()
assert response.status_code == 400
assert response_dict[0]['message'] == 'Invalid JSON file.'


def _get_response_for_invalid_json_files(client, invalid_json_content):
with tempfile.NamedTemporaryFile(suffix='invalid_structure.json') as f:
f.write(invalid_json_content)
f.seek(0)
response = client.post('/api/db/v0/data_files/', data={'file': f}, format='multipart')

return response


def test_data_file_create_non_unicode_file(client, non_unicode_csv_filepath):
with open(non_unicode_csv_filepath, 'rb') as non_unicode_file:
response = client.post('/api/db/v0/data_files/', data={'file': non_unicode_file}, format='multipart')
Expand Down
27 changes: 27 additions & 0 deletions mathesar/tests/api/test_table_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ def long_column_data_file():
return data_file


@pytest.fixture
def missing_keys_json_data_file():
data_filepath = 'mathesar/tests/data/json_parsing/missing_keys.json'
with open(data_filepath, "rb") as json_file:
data_file = DataFile.objects.create(
file=File(json_file),
created_from='file',
base_name='missing_keys',
type='json'
)
return data_file


@pytest.fixture
def schema_name():
return 'table_tests'
Expand Down Expand Up @@ -1902,3 +1915,17 @@ def test_create_table_long_name_data_file(client, long_column_data_file, schema)
)
# This just makes sure we can get records. This was a bug with long column names.
client.get(f'/api/db/v0/tables/{table.id}/records/')


def test_create_table_and_normalize_json_data_file(client, missing_keys_json_data_file, schema):
table_name = 'Missing keys'
expt_name = _get_expected_name(table_name, data_file=missing_keys_json_data_file)
first_row = (1, 'Matt', 'Murdock', 'Male', '["Stick", "Foggy"]', '{"street": "210", "city": "NY"}', None)
column_names = [
"first_name", "last_name", "gender", "friends", "address", "email"
]

check_create_table_response(
client, table_name, expt_name, missing_keys_json_data_file, schema, first_row,
column_names, import_target_table=None
)