Skip to content

Commit

Permalink
use tempfile in test_share.py and address minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mustaballer committed Jun 26, 2023
1 parent dafca05 commit 3434a00
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion openadapt/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def get_latest_recording():


def get_recording_by_id(recording_id):
return db.query(Recording).filter_by(id=recording_id).first().all()
return db.query(Recording).filter_by(id=recording_id).first()


def get_recording(timestamp):
Expand Down
2 changes: 1 addition & 1 deletion openadapt/share.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Usage:
python -m openadapt.share send --recording_id=1
python -m openadapt.share receive --wormhole_code=<wormhole_code>
python -m openadapt.share receive <wormhole_code>
"""

from zipfile import ZipFile, ZIP_DEFLATED
Expand Down
21 changes: 13 additions & 8 deletions tests/openadapt/test_share.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import subprocess
import tempfile
import unittest
import os
from unittest.mock import patch
from openadapt import share


class ShareTestCase(unittest.TestCase):
"""
A test case for the share module.
This test case verifies the functionality of the share module,
including exporting a recording to a folder, sending a file,
sending a recording, and receiving a recording.
"""

def test_export_recording_to_folder(self):
# Create a temporary recording database file
recording_id = 1
Expand All @@ -15,7 +24,7 @@ def test_export_recording_to_folder(self):

# Mock the crud.export_recording() function to return the temporary file path
with patch(
"openadapt.share.crud.export_recording", return_value=recording_db_path
"openadapt.share.db.export_recording", return_value=recording_db_path
):
zip_file_path = share.export_recording_to_folder(recording_id)

Expand All @@ -27,9 +36,9 @@ def test_export_recording_to_folder(self):

def test_send_file(self):
# Create a temporary file
file_path = "temp.txt"
with open(file_path, "w") as f:
f.write("File data")
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
file_path = temp_file.name
temp_file.write(b"File data")

# Mock the subprocess.run() function to avoid executing the command
with patch("openadapt.share.subprocess.run") as mock_run:
Expand Down Expand Up @@ -65,7 +74,3 @@ def test_receive_recording(self):
subprocess.run.assert_called_once_with(
["wormhole", "receive", "wormhole_code"], check=True
)


if __name__ == "__main__":
unittest.main()

0 comments on commit 3434a00

Please sign in to comment.