Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ dependencies = [
"cryptease",
"lxml",
"python-dateutil",
"requests",
# Pinning to the last 1.x version for now. The root cause of this issue is currently unknown. Further investigation is required.
# Higher versions (2.0.0+) cause file download inconsistencies in tests, leading to `BadZipFile` exceptions.
"urllib3==1.26.20"
"requests"
]
classifiers = [
"Development Status :: 4 - Beta",
Expand All @@ -46,6 +43,7 @@ Source = "https://github.com/onnela-lab/mano"
[project.optional-dependencies]
dev = [
"pytest",
"responses",
"twine",
"vcrpy"
]
Expand Down
1,735 changes: 0 additions & 1,735 deletions tests/cassettes/download.v1.yaml

This file was deleted.

92 changes: 92 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"""
Pytest configuration and shared fixtures for mano tests.
"""
import os

import pytest
import responses


@pytest.fixture
def keyring():
"""Fixture providing test credentials for Beiwe API authentication."""
return {
'URL': 'https://studies.beiwe.org',
'USERNAME': 'foobar',
'PASSWORD': 'bizbat',
'ACCESS_KEY': 'ACCESS_KEY',
'SECRET_KEY': 'SECRET_KEY'
}


@pytest.fixture
def mock_zip_data():
"""Create a mock zip file response using the original ZIP file.

This fixture uses the extracted download.v1.zip to provide test data with
the exact same content and CRC values as the original download,
eliminating the need for VCR.py dependencies.
"""
# Load the original ZIP file extracted from the cassette
original_zip_file = os.path.join(
os.path.dirname(__file__), 'data', 'download.v1.zip'
)

with open(original_zip_file, 'rb') as f:
return f.read()


@pytest.fixture
def mock_download_api(mock_zip_data):
"""Fixture that sets up the mock API endpoint for download testing."""
with responses.RequestsMock() as rsps:
rsps.add(
responses.POST,
'https://studies.beiwe.org/get-data/v1',
body=mock_zip_data,
status=200,
content_type='application/zip'
)
yield rsps


@pytest.fixture
def expected_download_files():
"""Expected files and CRC values from the original download.

These values represent what we expect the download function to return
based on the original API response captured in the cassette.
"""
return {
('6y6s1w4g/identifiers/2018-06-15 16_00_00.csv', 4113954587),
('6y6s1w4g/gps/2018-06-15 22_00_00.csv', 3073924694),
('6y6s1w4g/gps/2018-06-15 17_00_00.csv', 4177290012),
('6y6s1w4g/gps/2018-06-15 20_00_00.csv', 3999192708),
('6y6s1w4g/gps/2018-06-15 18_00_00.csv', 4038997044),
('6y6s1w4g/gps/2018-06-15 21_00_00.csv', 2945081574),
('6y6s1w4g/gps/2018-06-15 19_00_00.csv', 4035326526),
('6y6s1w4g/gps/2018-06-15 16_00_00.csv', 427497446),
('6y6s1w4g/gps/2018-06-16 04_00_00.csv', 2574140219),
('6y6s1w4g/gps/2018-06-16 01_00_00.csv', 503869630),
('6y6s1w4g/gps/2018-06-16 03_00_00.csv', 1817118505),
('6y6s1w4g/gps/2018-06-16 00_00_00.csv', 963879171),
('6y6s1w4g/gps/2018-06-15 23_00_00.csv', 2924619047),
('6y6s1w4g/gps/2018-06-16 02_00_00.csv', 2868941135),
('6y6s1w4g/gps/2018-06-16 07_00_00.csv', 2619751600),
('6y6s1w4g/gps/2018-06-16 12_00_00.csv', 3738822868),
('6y6s1w4g/gps/2018-06-16 10_00_00.csv', 2382158563),
('6y6s1w4g/gps/2018-06-16 08_00_00.csv', 3580985466),
('6y6s1w4g/gps/2018-06-16 09_00_00.csv', 771315427),
('6y6s1w4g/gps/2018-06-16 11_00_00.csv', 1386915032),
('6y6s1w4g/gps/2018-06-16 14_00_00.csv', 641388715),
('6y6s1w4g/gps/2018-06-16 15_00_00.csv', 3728053865),
('6y6s1w4g/gps/2018-06-16 16_00_00.csv', 1258485774),
('6y6s1w4g/gps/2018-06-16 18_00_00.csv', 1597315532),
('6y6s1w4g/gps/2018-06-16 13_00_00.csv', 2905130265),
('6y6s1w4g/gps/2018-06-16 19_00_00.csv', 565983226),
('6y6s1w4g/gps/2018-06-16 17_00_00.csv', 2142570097),
('6y6s1w4g/gps/2018-06-16 06_00_00.csv', 3417900549),
('6y6s1w4g/gps/2018-06-16 20_00_00.csv', 1834664030),
('6y6s1w4g/gps/2018-06-16 05_00_00.csv', 3970320035),
('registry', 942145567)
}
2 changes: 2 additions & 0 deletions tests/data/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# This file ensures the data directory is tracked in git
# Test data files are stored in this directory
18 changes: 18 additions & 0 deletions tests/data/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Test Data Directory

This directory contains static test data files used by the test suite.

## Files:

- `download.v1.zip`: Original API response from the download cassette (443KB)
- Contains 31 files: 29 GPS data files, 1 identifiers file, 1 registry file
- Used by `mock_zip_data` fixture in `conftest.py`
- Extracted from `tests/cassettes/download.v1.yaml`

## Usage:

Test data files should be:
- Small and focused (avoid large files when possible)
- Versioned (e.g., v1, v2) when format changes
- Documented with purpose and source
- Committed to git for reproducible tests
Binary file added tests/data/download.v1.zip
Binary file not shown.
64 changes: 0 additions & 64 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,67 +250,3 @@ def test_device_settings():
"""


def test_download():
"""
The data used in this test (cassette) are real, but they were captured on a
test phone from a test deployment of beiwe-backend.
"""
cassette = os.path.join(CASSETTES, 'download.v1.yaml')
filter_params = [
('access_key', Keyring['ACCESS_KEY']),
('secret_key', Keyring['SECRET_KEY']),
('study_id', 'STUDY_ID'),
('user_ids', 'USER_ID')
]

# urllib3 2.0.0+ causes the downloaded file to be 97,409 bytes instead of 443,610 bytes, and
# ZipFile throws a BadZipFile exception. (Unable to find a valid end of central directory
# structure). I don't even know how to begin debugging this, so I have to pin urllib3 to an
# older version.

with vcr.use_cassette(cassette, filter_post_data_parameters=filter_params):
zf = mano.sync.download(Keyring,
study_id='STUDY_ID',
user_ids=['USER_ID'],
data_streams=['identifiers', 'gps'],
time_start='2018-06-15T00:00:00',
time_end='2018-06-17T00:00:00')
ans = set()
for zinfo in zf.infolist():
ans.add((zinfo.filename, zinfo.CRC))
assert ans == test_download.index


test_download.index = set([
('6y6s1w4g/identifiers/2018-06-15 16_00_00.csv', 4113954587),
('6y6s1w4g/gps/2018-06-15 22_00_00.csv', 3073924694),
('6y6s1w4g/gps/2018-06-15 17_00_00.csv', 4177290012),
('6y6s1w4g/gps/2018-06-15 20_00_00.csv', 3999192708),
('6y6s1w4g/gps/2018-06-15 18_00_00.csv', 4038997044),
('6y6s1w4g/gps/2018-06-15 21_00_00.csv', 2945081574),
('6y6s1w4g/gps/2018-06-15 19_00_00.csv', 4035326526),
('6y6s1w4g/gps/2018-06-15 16_00_00.csv', 427497446),
('6y6s1w4g/gps/2018-06-16 04_00_00.csv', 2574140219),
('6y6s1w4g/gps/2018-06-16 01_00_00.csv', 503869630),
('6y6s1w4g/gps/2018-06-16 03_00_00.csv', 1817118505),
('6y6s1w4g/gps/2018-06-16 00_00_00.csv', 963879171),
('6y6s1w4g/gps/2018-06-15 23_00_00.csv', 2924619047),
('6y6s1w4g/gps/2018-06-16 02_00_00.csv', 2868941135),
('6y6s1w4g/gps/2018-06-16 07_00_00.csv', 2619751600),
('6y6s1w4g/gps/2018-06-16 12_00_00.csv', 3738822868),
('6y6s1w4g/gps/2018-06-16 10_00_00.csv', 2382158563),
('6y6s1w4g/gps/2018-06-16 08_00_00.csv', 3580985466),
('6y6s1w4g/gps/2018-06-16 09_00_00.csv', 771315427),
('6y6s1w4g/gps/2018-06-16 11_00_00.csv', 1386915032),
('6y6s1w4g/gps/2018-06-16 14_00_00.csv', 641388715),
('6y6s1w4g/gps/2018-06-16 15_00_00.csv', 3728053865),
('6y6s1w4g/gps/2018-06-16 16_00_00.csv', 1258485774),
('6y6s1w4g/gps/2018-06-16 18_00_00.csv', 1597315532),
('6y6s1w4g/gps/2018-06-16 13_00_00.csv', 2905130265),
('6y6s1w4g/gps/2018-06-16 19_00_00.csv', 565983226),
('6y6s1w4g/gps/2018-06-16 17_00_00.csv', 2142570097),
('6y6s1w4g/gps/2018-06-16 06_00_00.csv', 3417900549),
('6y6s1w4g/gps/2018-06-16 20_00_00.csv', 1834664030),
('6y6s1w4g/gps/2018-06-16 05_00_00.csv', 3970320035),
('registry', 942145567)
])
Loading