Skip to content

Commit

Permalink
Added add_files test
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Hui committed Mar 3, 2023
1 parent 57183a2 commit 76ce179
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
5 changes: 2 additions & 3 deletions e4e_data_management/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,12 @@ def add_files(args: List[str]):
parser.add_argument(
'paths',
nargs='+',
type=Path,
required=True
type=Path
)

args = parser.parse_args(args=args)
paths: List[Path] = args.paths
app.add(paths)
app.add(paths=paths)

def main():
"""Main function
Expand Down
20 changes: 20 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'''Common Test Fixtures
'''
import random
from collections import namedtuple
from pathlib import Path
from tempfile import TemporaryDirectory
Expand All @@ -11,6 +12,7 @@

AppFixture = namedtuple('AppFixture', ['app', 'root'])
MockAppFixture = namedtuple('MockAppFixture', ['mock', 'app', 'root'])
DataFixture = namedtuple('DataFixture', ['path', 'n_files', 'file_size'])

@pytest.fixture(name='test_app')
def create_test_app() -> AppFixture:
Expand Down Expand Up @@ -44,3 +46,21 @@ def create_mock_test_app() -> MockAppFixture:
mock.load.return_value = mock
with patch('e4e_data_management.cli.DataManager', mock):
yield MockAppFixture(mock, app, root_dir)

N_FILES = 128
FILE_SIZE = 1024
@pytest.fixture(name='test_data')
def create_test_data() -> DataFixture:
"""Creates test data
Returns:
DataFixture: Data Fixture
"""
random.seed(0)
with TemporaryDirectory() as path:
data_dir = Path(path)
for file_idx in range(N_FILES):
with open(data_dir.joinpath(f'{file_idx:04d}.bin'), 'wb') as handle:
data = random.randbytes(FILE_SIZE)
handle.write(data)
yield DataFixture(data_dir, N_FILES, FILE_SIZE)
36 changes: 34 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ def test_init_mission(test_mock_app: Tuple[Mock, DataManager, Path]):

args = split('e4edm init_mission --timestamp 2023-03-02T15:06-08:00 --device Device1 '
'--country USA --region California --site SD --name RUN001')
with patch('sys.argv', args),\
patch('e4e_data_management.cli.DataManager', mock):
with patch('sys.argv', args):
main()
mock.initialize_mission.assert_called_once_with(
metadata=Metadata(
Expand All @@ -56,3 +55,36 @@ def test_init_mission(test_mock_app: Tuple[Mock, DataManager, Path]):
notes=None
)
)

def test_add_files(test_mock_app: Tuple[Mock, DataManager, Path], test_data: Tuple[Path, int, int]):
"""Tests adding files
Args:
test_mock_app (Tuple[Mock, DataManager, Path]): Mock App
test_data (Tuple[Path, int, int]): Test Data
"""
mock, app, root_dir = test_mock_app
data_dir, _, _ = test_data

app.initialize_dataset(
date=dt.date(2023, 3, 2),
project='Test',
location='San Diego',
directory=root_dir
)
app.initialize_mission(
metadata=Metadata(
timestamp=dt.datetime.fromisoformat('2023-03-02T18:35-08:00'),
country='USA',
region='California',
device='Device1',
site='SD',
mission='TAF001'
)
)

bin_files = list(data_dir.rglob('*.bin'))[:2]
args = split(f'e4edm add {bin_files[0].as_posix()} {bin_files[1].as_posix()}')
with patch('sys.argv', args):
main()
mock.add.assert_called_once_with(paths=bin_files)

0 comments on commit 76ce179

Please sign in to comment.