Skip to content

Commit

Permalink
Merging test_app and test_mock_app
Browse files Browse the repository at this point in the history
  • Loading branch information
Nathan Hui committed Mar 3, 2023
1 parent a68ba29 commit a1d8a13
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 119 deletions.
104 changes: 90 additions & 14 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
'''Common Test Fixtures
'''
import datetime as dt
import random
from collections import namedtuple
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Tuple
from unittest.mock import Mock, patch

import pytest

from e4e_data_management.core import DataManager
from e4e_data_management.metadata import Metadata

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:
"""Creates a test app
Yields:
TestFixture: App and root directory
"""
with TemporaryDirectory() as temp_dir:
root_dir = Path(temp_dir)
app = DataManager(
app_config_dir=root_dir
)
yield AppFixture(app, root_dir)

@pytest.fixture(name='test_mock_app')
def create_mock_test_app() -> MockAppFixture:
"""Creates a mock test app
Expand Down Expand Up @@ -81,3 +70,90 @@ def create_test_readme() -> Path:
with open(readme_path, 'w', encoding='ascii') as handle:
handle.write('readme\n')
yield readme_path

@pytest.fixture(name='single_mission')
def create_single_mission(test_app: Tuple[Mock, DataManager, Path]
) -> Tuple[Tuple[DataManager, Path], Tuple[Path, int, int]]:
"""Creates a single mission
Args:
test_app (Tuple[DataManager, Path]): Test App
Returns:
Tuple[Tuple[DataManager, Path], Tuple[Path, int, int]]: test app, test data
"""
_, app, root_dir = test_app

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-02T19:38-08:00'),
device='Device1',
country='USA',
region='California',
site='SD',
mission='TPF001'
)
)

return app, root_dir

@pytest.fixture(name='single_mission_data')
def create_single_mission_data(single_mission: Tuple[DataManager, Path],
test_data: Tuple[Path, int, int]
) -> Tuple[Tuple[DataManager, Path], Tuple[Path, int, int]]:
"""Creates a single mission
Args:
test_app (Tuple[DataManager, Path]): Test App
test_data (Tuple[Path, int, int]): Test Data
Returns:
Tuple[Tuple[DataManager, Path], Tuple[Path, int, int]]: test app, test data
"""
app, _ = single_mission
data_dir, _, _ = test_data

app.add(data_dir.rglob('*.bin'))
app.commit()

return single_mission, test_data

@pytest.fixture(name='mock_single_mission')
def create_mock_single_mission(test_app: Tuple[Mock, DataManager, Path]
) -> Tuple[DataManager, Path]:
"""Creates a single mission
Args:
test_app (Tuple[DataManager, Path]): Test App
Returns:
Tuple[Tuple[DataManager, Path], Tuple[Path, int, int]]: test app, test data
"""
_, app, root_dir = test_app

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-02T19:38-08:00'),
device='Device1',
country='USA',
region='California',
site='SD',
mission='TPF001'
)
)

return test_app
42 changes: 13 additions & 29 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ def test_init_dataset():
directory=Path('.')
)

def test_init_mission(test_mock_app: Tuple[Mock, DataManager, Path]):
def test_init_mission(test_app: Tuple[Mock, DataManager, Path]):
"""Tests init_mission
"""
mock, app, root_dir = test_mock_app
mock, app, root_dir = test_app

app.initialize_dataset(
date=dt.date(2023,3,2),
Expand All @@ -57,14 +57,14 @@ def test_init_mission(test_mock_app: Tuple[Mock, DataManager, Path]):
)
)

def test_add_files(test_mock_app: Tuple[Mock, DataManager, Path], test_data: Tuple[Path, int, int]):
def test_add_files(test_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_app (Tuple[Mock, DataManager, Path]): Mock App
test_data (Tuple[Path, int, int]): Test Data
"""
mock, app, root_dir = test_mock_app
mock, app, root_dir = test_app
data_dir, _, _ = test_data

app.initialize_dataset(
Expand All @@ -90,15 +90,15 @@ def test_add_files(test_mock_app: Tuple[Mock, DataManager, Path], test_data: Tup
main()
mock.add.assert_called_once_with(paths=bin_files)

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

app.initialize_dataset(
Expand Down Expand Up @@ -127,15 +127,15 @@ def test_commit_files(test_mock_app: Tuple[Mock, DataManager, Path],
main()
mock.commit.assert_called_once()

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

app.initialize_dataset(
Expand Down Expand Up @@ -168,32 +168,16 @@ def test_push_files(test_mock_app: Tuple[Mock, DataManager, Path],
main()
mock.push.assert_called_once_with(path=push_path)

def test_duplicate(test_mock_app: Tuple[Mock, DataManager, Path], test_data: Tuple[Path, int, int]):
def test_duplicate(mock_single_mission: Tuple[Mock, DataManager, Path], test_data: Tuple[Path, int, int]):
"""Tests duplication
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
mock, app, root_dir = mock_single_mission
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='TD001'
)
)

app.add(data_dir.rglob('*.bin'))
app.commit()
Expand Down
9 changes: 5 additions & 4 deletions tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
import datetime as dt
from pathlib import Path
from typing import Tuple
from unittest.mock import Mock

import pytest

from e4e_data_management.core import DataManager


def test_init_dataset(test_app: Tuple[DataManager, Path]):
def test_init_dataset(test_app: Tuple[Mock, DataManager, Path]):
"""Tests creating the dataset
"""
app, root_dir = test_app
_, app, root_dir = test_app
date = dt.date(2023, 2, 28)
project = 'TEST'
location = 'San Diego'
Expand All @@ -37,10 +38,10 @@ def test_init_dataset(test_app: Tuple[DataManager, Path]):
assert app.active_dataset.name == '2023.02.TEST.San Diego'
assert app.active_mission is None

def test_init_existing(test_app: Tuple[DataManager, Path]):
def test_init_existing(test_app: Tuple[Mock, DataManager, Path]):
"""Tests that running init on an existing dataset will do nothing
"""
app, root_dir = test_app
_, app, root_dir = test_app
date = dt.date(2023, 2, 28)
project = 'TEST'
location = 'San Diego'
Expand Down
28 changes: 3 additions & 25 deletions tests/test_duplicate.py
Original file line number Diff line number Diff line change
@@ -1,44 +1,22 @@
'''Testing duplication
'''
import datetime as dt
from pathlib import Path
from tempfile import TemporaryDirectory
from typing import Tuple

from e4e_data_management.core import DataManager
from e4e_data_management.metadata import Metadata


def test_push(test_app: Tuple[DataManager, Path],
test_data: Tuple[Path, int, int]):
"""Tests pushing data
def test_duplicate(single_mission_data: Tuple[Tuple[DataManager, Path], Tuple[Path, int, int]]):
"""Tests duplicating data
Args:
test_app (Tuple[DataManager, Path]): Test app
test_data (Tuple[Path, int, int]): Test data
test_readme (Path): Test Readme
"""
test_app, _ = single_mission_data
app, root_dir = test_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-02T19:38-08:00'),
device='Device1',
country='USA',
region='California',
site='SD',
mission='TPF001'
)
)
app.add(data_dir.rglob('*.bin'))
app.commit()

with TemporaryDirectory() as duplication_dir:
target = Path(duplication_dir)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_mission.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

from e4e_data_management.core import DataManager
from e4e_data_management.metadata import Metadata
from unittest.mock import Mock


def test_create_multiple_missions(test_app: Tuple[DataManager, Path]):
def test_create_multiple_missions(test_app: Tuple[Mock, DataManager, Path]):
"""Tests creating multiple missions
"""
app, root = test_app
_, app, root = test_app
app.initialize_dataset(
date=dt.date(2023, 3, 1),
project='TEST',
Expand Down Expand Up @@ -78,10 +78,10 @@ def test_create_multiple_missions(test_app: Tuple[DataManager, Path]):
assert app.active_mission.path == dataset_dir.joinpath('ED-01', 'TCMM003')


def test_create_mission(test_app):
def test_create_mission(test_app: Tuple[Mock, DataManager, Path]):
"""Tests creating a mission
"""
app, root = test_app
_, app, root = test_app
app.initialize_dataset(
date=dt.date(2023, 3, 1),
project='TEST',
Expand Down
Loading

0 comments on commit a1d8a13

Please sign in to comment.