Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rdmolony committed Jan 12, 2024
1 parent 935457b commit 02573ee
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 15 deletions.
2 changes: 1 addition & 1 deletion sensor/api/viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ class FileViewSet(viewsets.ModelViewSet):

def perform_create(self, serializer):
instance = serializer.save()
instance.parse_and_import_to_db()
instance.import_to_db()
10 changes: 7 additions & 3 deletions sensor/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

import json
import textwrap
from pathlib import Path

from datetime import datetime
from datetime import timezone
Expand Down Expand Up @@ -124,7 +125,7 @@ def clean(self):
)
self.file.seek(0)

def import_directly_to_db(self):
def import_json(self):

# NOTE: assume uploaded file is JSON
with self.file.open(mode="rb") as f:
Expand All @@ -148,7 +149,7 @@ def import_directly_to_db(self):
break
Reading.objects.bulk_create(batch, batch_size)

def parse_and_import_to_db(self):
def parse_and_import(self):

with self.file.open(mode="rb") as f:

Expand Down Expand Up @@ -191,7 +192,10 @@ def parse_and_import_to_db(self):

def import_to_db(self) -> None:

self.import_directly()
if Path(self.file.path).suffix == ".json":
self.import_json()
else:
self.parse_and_import()


class Reading(models.Model):
Expand Down
4 changes: 2 additions & 2 deletions sensor/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@


@shared_task
def parse_and_import_to_db(file_id):
def import_to_db(file_id):
file_obj = File.objects.get(id=file_id)
file_obj.parse_and_import_to_db()
file_obj.import_to_db()
2 changes: 1 addition & 1 deletion sensor/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def upload_file(request):
form = FileForm(request.POST, request.FILES)
if form.is_valid():
form.save()
form.instance.parse_and_import_to_db()
form.instance.import_to_db()
return HttpResponse("File upload was successful")
else:
return HttpResponse(f"File type creation failed: {form.errors}")
Expand Down
2 changes: 1 addition & 1 deletion tests/sensor/api/test_viewsets.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,6 @@ def test_parse_and_import_to_db_is_called(
)
url = reverse("sensor:upload-file")

with patch("sensor.models.File.parse_and_import_to_db") as importer:
with patch("sensor.models.File.import_to_db") as importer:
client.post(url, {"file": file, "type": file_type.id})
assert importer.called
4 changes: 2 additions & 2 deletions tests/sensor/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_import_directly_to_db(
file_obj = File(file=file, type=file_type_obj)
file_obj.save()

file_obj.import_directly_to_db()
file_obj.import_json()

output = Reading.objects.all()
assert output == snapshot
Expand Down Expand Up @@ -81,7 +81,7 @@ def test_parse_and_import_to_db(
file_obj = File(file=file, type=file_type_obj)
file_obj.save()

file_obj.parse_and_import_to_db()
file_obj.parse_and_import()

output = Reading.objects.all()
assert output == snapshot
8 changes: 4 additions & 4 deletions tests/sensor/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@

from sensor.models import File
from sensor.models import FileType
from sensor.tasks import parse_and_import_to_db
from sensor.tasks import import_to_db


@pytest.mark.django_db
def test_parse_and_import_to_db_is_called():
def test_import_to_db_is_called():

with patch("sensor.models.File.parse_and_import_to_db") as importer:
with patch("sensor.models.File.import_to_db") as importer:
file_obj = File.objects.create(
file=ContentFile(b"", name="sensor-readings.txt"),
type=FileType.objects.create(name="type")
)
parse_and_import_to_db(file_id=file_obj.id)
import_to_db(file_id=file_obj.id)

assert importer.called, "parse_and_import_to_db was not called!"
2 changes: 1 addition & 1 deletion tests/sensor/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,6 @@ def test_parse_and_import_to_db_is_called(
)
url = reverse("sensor:upload-file")

with patch("sensor.models.File.parse_and_import_to_db") as importer:
with patch("sensor.models.File.import_to_db") as importer:
client.post(url,{"file": file, "type": file_type.id})
assert importer.called

0 comments on commit 02573ee

Please sign in to comment.