Skip to content

Commit 98dbee5

Browse files
authored
Merge pull request #277 from visual-layer/dnth/tests
Add tests folder structure and GitHub actions
2 parents 1a7fee6 + 2c76d63 commit 98dbee5

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-0
lines changed
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Daily Run Tests on PyPI Wheels
2+
3+
on:
4+
workflow_dispatch:
5+
schedule:
6+
- cron: '0 16 * * *' # Trigger at 4PM every day
7+
8+
jobs:
9+
run_unit_tests:
10+
runs-on: ${{ matrix.os }}
11+
env:
12+
SENTRY_OPT_OUT: True
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
os: [ubuntu-22.04] # windows-2022 macos-13
17+
python: ['3.7', '3.8', '3.9','3.10']
18+
steps:
19+
- name: Checkout code
20+
uses: actions/checkout@v3
21+
with:
22+
fetch-depth: 0
23+
24+
- name: Set up Python
25+
uses: actions/setup-python@v3
26+
with:
27+
python-version: ${{ matrix.python }}
28+
29+
- name: Install dependencies (Linux)
30+
if: runner.os == 'Linux'
31+
run: |
32+
python -m pip install --upgrade pip
33+
pip install fastdup pytest pytest-md pytest-emoji datasets
34+
35+
- name: Install dependencies (macOS)
36+
if: runner.os == 'macOS'
37+
run: |
38+
python -m pip install --upgrade pip
39+
pip install pytest pytest-cov datasets fastdup
40+
pip install opencv-python-headless
41+
pip install fastdup --no-deps -U
42+
43+
- name: Run pytest
44+
uses: pavelzw/pytest-action@v2
45+
with:
46+
verbose: true
47+
emoji: true
48+
job-summary: true
49+
custom-arguments: '-q'
50+
click-to-expand: true
51+
report-title: 'Test Report'

tests/README.md

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Running Tests Locally
2+
3+
To run the tests first install `pytest`:
4+
5+
```
6+
pip install pytest
7+
```
8+
9+
Then `cd` into the fastdup site-packages root directory.
10+
11+
For example
12+
13+
```
14+
cd /home/dnth/anaconda3/envs/tests/lib/python3.10/site-packages/fastdup
15+
```
16+
17+
and run
18+
19+
```
20+
pytest
21+
```
22+
23+
If all test passes, the output should look like the following:
24+
25+
```shell
26+
========================================================================================== test session starts ===========================================================================================
27+
platform linux -- Python 3.10.13, pytest-7.4.2, pluggy-1.3.0
28+
rootdir: /media/dnth/Active-Projects/fastdup
29+
plugins: anyio-3.7.1, cov-4.1.0, mock-3.11.1
30+
collected 5 items
31+
32+
tests/examples/test_datasets_example.py . [ 20%]
33+
tests/unit/test_datasets.py .... [100%]
34+
35+
============================================================================================ warnings summary ============================================================================================
36+
tests/examples/test_datasets_example.py::test_datasets_example
37+
/home/dnth/anaconda3/envs/tests/lib/python3.10/site-packages/fastdup/fastdup_controller.py:385: UserWarning: No connected components found, try using a lower threshold
38+
warnings.warn(f'No connected components found, try using a lower threshold')
39+
40+
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
41+
====================================================================================== 5 passed, 1 warning in 9.12s ======================================================================================
42+
```
43+
44+
# Adding More Tests
45+
Include new tests in the approriate folders.
46+
47+
+ `unit/` - Put all unit tests here.
48+
+ `examples/` - Put all examples tests here.

tests/__init__.py

Whitespace-only changes.
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import fastdup
2+
from fastdup.datasets import FastdupHFDataset
3+
4+
def test_datasets_example():
5+
dataset = FastdupHFDataset("zh-plus/tiny-imagenet", split="train[:5%]") # Download 5% of the train set
6+
fd = fastdup.create(input_dir=dataset.img_dir)
7+
fd.run(annotations=dataset.annotations, num_images=100)

tests/unit/test_datasets.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import pytest
2+
from fastdup.datasets import FastdupHFDataset
3+
from datasets.config import HF_DATASETS_CACHE
4+
import os
5+
import pandas as pd
6+
7+
8+
@pytest.fixture(scope="module")
9+
def dataset():
10+
return FastdupHFDataset("zh-plus/tiny-imagenet", split="train[:5%]")
11+
12+
13+
def test_instantiating_dataset_with_valid_dataset_name_and_split(dataset):
14+
assert isinstance(dataset, FastdupHFDataset)
15+
assert dataset.img_key == "image"
16+
assert dataset.label_key == "label"
17+
assert dataset.jpg_save_dir == "jpg_images"
18+
assert dataset.cache_dir == HF_DATASETS_CACHE
19+
assert dataset.info.dataset_name == "tiny-imagenet"
20+
assert len(dataset) == 5000
21+
22+
23+
def test_img_dir_exists(dataset):
24+
assert os.path.isdir(dataset.img_dir)
25+
assert dataset.img_dir == os.path.join(
26+
HF_DATASETS_CACHE, "tiny-imagenet", "jpg_images"
27+
)
28+
29+
30+
# Accessing img_dir property after successful instantiation
31+
def test_accessing_img_dir_property(dataset):
32+
assert isinstance(dataset.img_dir, str)
33+
assert os.path.exists(dataset.img_dir)
34+
35+
36+
# Accessing annotations property after successful instantiation
37+
def test_accessing_annotations_property_after_successful_instantiation(dataset):
38+
annotations = dataset.annotations
39+
assert isinstance(annotations, pd.DataFrame)
40+
assert "filename" in annotations.columns
41+
assert "label" in annotations.columns
42+
43+
sample_row = dataset.annotations.iloc[0]
44+
assert os.path.exists(sample_row["filename"])
45+
assert isinstance(sample_row["label"], str)

0 commit comments

Comments
 (0)