-
Notifications
You must be signed in to change notification settings - Fork 0
Add route for importing data from UI #150
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
c0b4593
add view
OlivierCoen c638bdb
add tests
OlivierCoen 0e07293
fix imports
OlivierCoen 1d0991b
fix issue with woring filename
OlivierCoen d276449
add charset detection and automatic encoding in utf8
OlivierCoen 1d0a384
Enhance tests and add git worklfow
arnaudfnr 0a8e403
Fix python versions
arnaudfnr ef72ad2
Use correct python versions
arnaudfnr 48d7c86
Change command to actually run test
arnaudfnr 3dc8453
Fix syntax
arnaudfnr 35c14a2
Remove python 3.11
arnaudfnr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| name: Backend CI | ||
|
|
||
| on: | ||
| push: | ||
| branches: [ main ] | ||
| pull_request: | ||
| branches: [ main ] | ||
|
|
||
| jobs: | ||
| test: | ||
|
|
||
| runs-on: ubuntu-latest | ||
| strategy: | ||
| max-parallel: 4 | ||
| matrix: | ||
| python-version: [3.12, 3.13, 3.14 ] | ||
|
|
||
| steps: | ||
| - uses: actions/checkout@v2 | ||
| - name: Set up Python ${{ matrix.python-version }} | ||
| uses: actions/setup-python@v6 | ||
| with: | ||
| python-version: ${{ matrix.python-version }} | ||
| - name: Install Dependencies | ||
| run: | | ||
| python -m pip install --upgrade pip | ||
| pip install -r ./backend/requirements.txt | ||
| - name: Run Tests' | ||
| run: | | ||
| cd backend | ||
| python manage.py migrate | ||
| python manage.py test |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,127 @@ | ||
| from django.contrib.auth.models import Permission | ||
| from django.contrib.auth import get_user_model | ||
| from django.test import TestCase, Client | ||
| from django.core.files.uploadedfile import SimpleUploadedFile | ||
| from django.urls import reverse | ||
| import shutil | ||
|
|
||
| # Create your tests here. | ||
| TEST_DIR = 'catalog/test' | ||
| class FileUploadTest(TestCase): | ||
| def setUp(self): | ||
| self.client = Client() | ||
| self.url = reverse('maps-add-data') | ||
|
|
||
| def tearDown(self): | ||
| try: | ||
| shutil.rmtree(TEST_DIR) | ||
| except OSError: | ||
| pass | ||
|
|
||
| def get_user_with_permission(self, username, password, codename): | ||
| user = get_user_model().objects.create_user(username=username, password=password) | ||
| permission = Permission.objects.get(codename=codename) | ||
| user.user_permissions.add(permission) | ||
| return user | ||
|
|
||
|
|
||
| def test_file_upload_add_ascii_content(self): | ||
| user = self.get_user_with_permission("testuser", "pass", "add_data") | ||
| self.client.force_login(user) | ||
|
|
||
| file_content = b'col_1,col_2\nvalue1,value2' | ||
|
|
||
| uploaded_file = SimpleUploadedFile( | ||
| name="ascii.csv", | ||
| content=file_content, | ||
| content_type='multipart/form-data' | ||
| ) | ||
|
|
||
| response = self.client.post( | ||
| self.url, | ||
| data={ | ||
| 'file': uploaded_file, | ||
| 'package': TEST_DIR, | ||
| 'action': 'add' | ||
| } | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 200, f"response is {response}") | ||
| self.assertEqual(uploaded_file.name, response.json()['filename']) | ||
| self.assertIn('File uploaded successfully', response.json()['message']) | ||
|
|
||
|
|
||
| def test_file_upload_add_ut8_content(self): | ||
| user = self.get_user_with_permission("testuser", "pass", "add_data") | ||
| self.client.force_login(user) | ||
|
|
||
| file_content = 'col_1,col_2\néàë,-°$' | ||
|
|
||
| uploaded_file = SimpleUploadedFile( | ||
| name="utf8.csv", | ||
| content=file_content.encode('utf-8'), | ||
| content_type='multipart/form-data' | ||
| ) | ||
|
|
||
| response = self.client.post( | ||
| self.url, | ||
| data={ | ||
| 'file': uploaded_file, | ||
| 'package': TEST_DIR, | ||
| 'action': 'add' | ||
| } | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 200, f"response is {response}") | ||
| self.assertEqual(uploaded_file.name, response.json()['filename']) | ||
| self.assertIn('File uploaded successfully', response.json()['message']) | ||
|
|
||
| def test_file_upload_weird_byte(self): | ||
| user = self.get_user_with_permission("testuser", "pass", "add_data") | ||
| self.client.force_login(user) | ||
| data = b"id,name\n1,John\n2,Ana\n3,Bob\x96\n" # 0x96 is typical Windows-1252 dash | ||
|
|
||
| uploaded_file = SimpleUploadedFile( | ||
| name="ascii_with_weird_byte.csv", | ||
| content=data, | ||
| content_type='multipart/form-data' | ||
| ) | ||
|
|
||
| response = self.client.post( | ||
| self.url, | ||
| data={ | ||
| 'file': uploaded_file, | ||
| 'package': TEST_DIR, | ||
| 'action': 'add' | ||
| } | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 200, f"response is {response}") | ||
| self.assertEqual(uploaded_file.name, response.json()['filename']) | ||
| self.assertIn('File uploaded successfully', response.json()['message']) | ||
|
|
||
| def test_file_mixed_encodings(self): | ||
| user = self.get_user_with_permission("testuser", "pass", "add_data") | ||
| self.client.force_login(user) | ||
| part_utf8 = "id,name,city\n1,Élodie,Paris\n".encode("utf-8") | ||
| part_latin1 = "2,José,Lisboa\n3,François,Lyon\n".encode("latin-1") | ||
| file_content = part_utf8 + part_latin1 | ||
|
|
||
| uploaded_file = SimpleUploadedFile( | ||
| name="mixed_encoding_file.csv", | ||
| content=file_content, | ||
| content_type='multipart/form-data' | ||
| ) | ||
|
|
||
| response = self.client.post( | ||
| self.url, | ||
| data={ | ||
| 'file': uploaded_file, | ||
| 'package': TEST_DIR, | ||
| 'action': 'add' | ||
| } | ||
| ) | ||
|
|
||
| self.assertEqual(response.status_code, 200, f"response is {response}") | ||
| self.assertEqual(uploaded_file.name, response.json()['filename']) | ||
| self.assertIn('File uploaded successfully', response.json()['message']) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also assert the filename