Skip to content

Commit

Permalink
Run tests against fondant package with tox (#283)
Browse files Browse the repository at this point in the history
This PR contains a couple of changes to make sure we test against the
built fondant package instead of against the raw source code. This is
best practice, but I also need this for the next PR I'll open as there
the tests will fail if they are not run against the built package.

- Introduce tox, which makes it easy to build the fondant package before
running our tests
- Move the fondant source code into a `src` folder, so we don't
accidentally test against the source code. See these articles for more
info (https://hynek.me/articles/testing-packaging/,
https://blog.ganssle.io/articles/2019/08/test-as-installed.html)
- Ruff now recognizes `fondant` as a third party package in our
components and tests, and has reorganized the imports accordingly

Each of these changes is contained in a separate PR.

To run tests locally, you can just run one of the following commands:
- `tox` this will automatically skip test environments that require
other python versions
- `tox -e py310` to select a specific environment to run, skipping other
environments like pre-commit
  • Loading branch information
RobbeSneyders authored Jul 10, 2023
1 parent 1a99c36 commit b27bbc8
Show file tree
Hide file tree
Showing 48 changed files with 62 additions and 61 deletions.
23 changes: 4 additions & 19 deletions .github/workflows/pipeline.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,11 @@ jobs:
run: |
pip install --upgrade pip
pip install poetry==1.4.0
poetry install --all-extras
- name: Test with pre-commit
run: poetry run pre-commit run --all-files --show-diff-on-failure
- name: Check licenses
run: |
poetry export -f requirements.txt --output /tmp/requirements.txt
poetry run liccheck -s license_strategy.ini -r /tmp/requirements.txt -l PARANOID
- name: Shadow-run pylint
# Check if pylint catches errors not caught by our pre-commit checks to validate switch
# to ruff
run: |
pip install pylint==2.16.4
pylint fondant --exit-zero
- name: Test with pytest
run: |
poetry run pytest tests --cov fondant --cov-report term-missing
poetry install --all-extras --with test
- name: Test with tox
run: poetry run tox
- name: Coveralls
run: |
pip install "coveralls<4"
coveralls --service github
run: poetry run coveralls --service github
env:
COVERALLS_PARALLEL: true
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand Down
3 changes: 1 addition & 2 deletions components/caption_images/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import numpy as np
import pandas as pd
import torch
from fondant.component import PandasTransformComponent
from PIL import Image
from transformers import BatchEncoding, BlipForConditionalGeneration, BlipProcessor

from fondant.component import PandasTransformComponent

logger = logging.getLogger(__name__)


Expand Down
3 changes: 1 addition & 2 deletions components/download_images/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
import urllib

import dask.dataframe as dd
from resizer import Resizer

from fondant.component import DaskTransformComponent
from resizer import Resizer

logger = logging.getLogger(__name__)

Expand Down
1 change: 0 additions & 1 deletion components/embedding_based_laion_retrieval/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import pandas as pd
from clip_client import ClipClient, Modality

from fondant.component import PandasTransformComponent

logger = logging.getLogger(__name__)
Expand Down
3 changes: 1 addition & 2 deletions components/filter_comments/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import logging

import dask.dataframe as dd
from utils.text_extraction import get_comments_to_code_ratio

from fondant.component import DaskTransformComponent
from utils.text_extraction import get_comments_to_code_ratio

logger = logging.getLogger(__name__)

Expand Down
1 change: 0 additions & 1 deletion components/filter_image_resolution/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import numpy as np
import pandas as pd

from fondant.component import PandasTransformComponent

logger = logging.getLogger(__name__)
Expand Down
1 change: 0 additions & 1 deletion components/filter_line_length/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import logging

import dask.dataframe as dd

from fondant.component import DaskTransformComponent

logger = logging.getLogger(__name__)
Expand Down
3 changes: 1 addition & 2 deletions components/image_cropping/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@

import dask.dataframe as dd
import numpy as np
from fondant.component import DaskTransformComponent
from image_crop import remove_borders
from PIL import Image

from fondant.component import DaskTransformComponent

logger = logging.getLogger(__name__)


Expand Down
3 changes: 1 addition & 2 deletions components/image_embedding/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
import numpy as np
import pandas as pd
import torch
from fondant.component import PandasTransformComponent
from PIL import Image
from transformers import CLIPProcessor, CLIPVisionModelWithProjection

from fondant.component import PandasTransformComponent

logger = logging.getLogger(__name__)


Expand Down
1 change: 0 additions & 1 deletion components/image_resolution_extraction/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import imagesize
import numpy as np
import pandas as pd

from fondant.component import PandasTransformComponent

logger = logging.getLogger(__name__)
Expand Down
1 change: 0 additions & 1 deletion components/language_filter/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import fasttext
import pandas as pd

from fondant.component import PandasTransformComponent

logger = logging.getLogger(__name__)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Unit test for language filter component."""
import pandas as pd
from fondant.component_spec import ComponentSpec

from components.language_filter.src.main import LanguageFilterComponent
from fondant.component_spec import ComponentSpec


def test_run_component_test():
Expand Down
1 change: 0 additions & 1 deletion components/load_from_hf_hub/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import typing as t

import dask.dataframe as dd

from fondant.component import LoadComponent

logger = logging.getLogger(__name__)
Expand Down
3 changes: 1 addition & 2 deletions components/pii_redaction/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
import logging

import dask.dataframe as dd
from fondant.component import DaskTransformComponent
from pii_detection import scan_pii
from pii_redaction import redact_pii

from fondant.component import DaskTransformComponent

logger = logging.getLogger(__name__)


Expand Down
1 change: 0 additions & 1 deletion components/prompt_based_laion_retrieval/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import pandas as pd
from clip_client import ClipClient, Modality

from fondant.component import PandasTransformComponent

logger = logging.getLogger(__name__)
Expand Down
3 changes: 1 addition & 2 deletions components/segment_images/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
import numpy as np
import pandas as pd
import torch
from fondant.component import PandasTransformComponent
from palette import palette
from PIL import Image
from transformers import AutoModelForSemanticSegmentation, BatchFeature, SegformerImageProcessor

from fondant.component import PandasTransformComponent

logger = logging.getLogger(__name__)


Expand Down
1 change: 0 additions & 1 deletion components/text_normalization/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from typing import List

import pandas as pd

from fondant.component import PandasTransformComponent

logger = logging.getLogger(__name__)
Expand Down
3 changes: 1 addition & 2 deletions components/write_to_hf_hub/src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
# Define the schema for the struct using PyArrow
import huggingface_hub
from datasets.features.features import generate_from_arrow_type
from PIL import Image

from fondant.component import WriteComponent
from PIL import Image

logger = logging.getLogger(__name__)

Expand Down
1 change: 0 additions & 1 deletion fondant/components

This file was deleted.

6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ gcp = ["gcsfs"]
pipelines = ["kfp", "kubernetes"]

[tool.poetry.group.test.dependencies]
liccheck = "^0.7.3"
pre-commit = "^3.1.1"
pytest = "^7.2.2"
pytest-cov = "^4.1.0"
tox = "^4.6.4"
tox-gh-actions = "^3.1.3"
coveralls = "^3.3.1"

[tool.poetry.group.docs.dependencies]
mkdocs-material = "^9.1.8"
Expand Down
6 changes: 3 additions & 3 deletions scripts/pre-build.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/bin/bash
# This script copies the components/ directory to fondant/components, replacing the symlink
# This script copies the components/ directory to src/fondant/components, replacing the symlink
# It should be run before building the fondant package'
# This script makes changes to the local files, which should not be committed to git
set -e
Expand All @@ -8,6 +8,6 @@ scripts_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
root_path=$(dirname "$scripts_path")

pushd "$root_path"
rm fondant/components
cp -r components/ fondant/
rm -rf src/fondant/components
cp -r components/ src/fondant/
popd
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 0 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from unittest.mock import patch

import pytest

from fondant.cli import ImportFromStringError, compile, pipeline_from_string, run
from fondant.pipeline import Pipeline

Expand Down
1 change: 0 additions & 1 deletion tests/test_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import pytest
import yaml

from fondant.compiler import DockerCompiler
from fondant.pipeline import ComponentOp, Pipeline

Expand Down
1 change: 0 additions & 1 deletion tests/test_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import pandas as pd
import pytest
import yaml

from fondant.component import (
Component,
DaskTransformComponent,
Expand Down
1 change: 0 additions & 1 deletion tests/test_component_specs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import pytest
import yaml

from fondant.component_spec import ComponentSpec, ComponentSubset, KubeflowComponentSpec
from fondant.exceptions import InvalidComponentSpec
from fondant.schema import Type
Expand Down
1 change: 0 additions & 1 deletion tests/test_data_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import dask.dataframe as dd
import pytest

from fondant.component_spec import ComponentSpec
from fondant.data_io import DaskDataLoader, DaskDataWriter
from fondant.manifest import Manifest
Expand Down
1 change: 0 additions & 1 deletion tests/test_import_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from unittest import mock

import pytest

from fondant.import_utils import (
is_datasets_available,
is_kfp_available,
Expand Down
1 change: 0 additions & 1 deletion tests/test_manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from pathlib import Path

import pytest

from fondant.exceptions import InvalidManifest
from fondant.manifest import Field, Index, Manifest, Subset, Type

Expand Down
1 change: 0 additions & 1 deletion tests/test_manifest_evolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import pytest
import yaml

from fondant.component_spec import ComponentSpec
from fondant.manifest import Manifest

Expand Down
1 change: 0 additions & 1 deletion tests/test_pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import pytest
import yaml

from fondant.exceptions import InvalidPipelineDefinition
from fondant.pipeline import ComponentOp, ComponentSpec, Pipeline

Expand Down
1 change: 0 additions & 1 deletion tests/test_schema.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import pyarrow as pa
import pytest

from fondant.exceptions import InvalidTypeSchema
from fondant.schema import Type

Expand Down
43 changes: 43 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[tox]
isolated_build = True
envlist =
pre-commit
check-licenses
py{38,39,310,311}

[gh-actions]
python =
3.8: py38
3.9: py39
3.10: pre-commit,check-licenses, py310
3.11: py311

[testenv:pre-commit]
allowlist_externals=
pre-commit
commands=pre-commit run --all-files --show-diff-on-failure

[testenv:check-licenses]
deps=
liccheck>=0.7.3,<0.8
commands=
poetry lock
poetry install --all-extras
poetry show
poetry export -f requirements.txt --output /tmp/requirements.txt
poetry run liccheck -s license_strategy.ini -r /tmp/requirements.txt -l PARANOID

[testenv]
setenv=PYTHONPATH = {toxinidir}:{toxinidir}
deps=
pytest>=7.4.0,<8
pytest-cov>=4.1.0,<5
allowlist_externals=
poetry
/usr/bin/bash
commands=
bash ./scripts/pre-build.sh
poetry lock
poetry install --all-extras
poetry show
poetry run python -m pytest tests --cov fondant --cov-report term-missing

0 comments on commit b27bbc8

Please sign in to comment.