Skip to content

Commit

Permalink
fix: fix tests + some i18n utils (#799)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgarel authored Jun 14, 2022
1 parent 0389b91 commit 17619ec
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 40 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8]
python-version:
- "3.7"
- "3.8"

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
Expand Down Expand Up @@ -93,7 +95,9 @@ jobs:
# Launch tests
#----------------------------------------------
- name: Configure docker
run: echo "OFF_UID=$UID" >> .env
run: |
echo "OFF_UID=$UID" >> .env
echo "PYTHON_VERSION=${{ matrix.python-version }}" >> .env
- name: Launch tests
run: make tests

Expand Down
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
ARG PYTHON_VERSION=3.7

# base python setup
# -----------------
FROM python:3.7-slim as python-base
FROM python:$PYTHON_VERSION-slim as python-base
RUN apt-get update && \
apt-get install --no-install-suggests --no-install-recommends -y gettext curl build-essential && \
apt-get autoremove --purge && \
Expand Down
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,19 @@ checks: toml-check flake8 black-check mypy isort-check docs

lint: toml-lint isort black

tests: create_external_networks unit-tests integration-tests
tests: create_external_networks i18n-compile unit-tests integration-tests

quality: lint checks tests

health:
@echo "🥫 Running health tests …"
@curl --fail --fail-early 127.0.0.1:5500/api/v1/health

i18n-compile:
@echo "🥫 Compiling translations …"
# Note it's important to have --no-deps, to avoid launching a concurrent postgres instance
${DOCKER_COMPOSE} run --rm --entrypoint bash --no-deps workers -c "cd i18n && . compile.sh"

unit-tests:
@echo "🥫 Running tests …"
# run tests in worker to have more memory
Expand Down
3 changes: 3 additions & 0 deletions docker/dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ x-robotoff-dev: &robotoff-dev
args:
OFF_UID: ${OFF_UID:-1000}
OFF_GID: ${OFF_GID:-1000}
PYTHON_VERSION: ${PYTHON_VERSION:-3.7}
volumes:
# make updates to code available
- ./robotoff:/opt/robotoff/robotoff
Expand All @@ -18,6 +19,8 @@ x-robotoff-dev: &robotoff-dev
# make tests available
- ./tests:/opt/robotoff/tests
- ./.cov:/opt/robotoff/.cov
# make i18N available
- ./i18n:/opt/robotoff/i18n
# make data available
- ./data:/opt/robotoff/data
# make doc generation available
Expand Down
1 change: 0 additions & 1 deletion i18n/compile.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/bin/bash

for i in */LC_MESSAGES/*.po; do
msgfmt -o "${i%\.*}.mo" $i
done
8 changes: 6 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@

@pytest.fixture(scope="session")
def peewee_db_create():
with models.db:
models.db.create_tables(models.MODELS, safe=True)
models.db.close() # insure creating a new connection
with models.db as db:
db.create_tables(models.MODELS, safe=True)
print("DEBUG: models created ", db.get_tables())
yield models.db


@pytest.fixture()
Expand All @@ -18,3 +21,4 @@ def peewee_db(peewee_db_create):
# to avoid reusing same transaction next time
if not models.db.is_closed():
models.db.rollback()
models.db.close()
8 changes: 3 additions & 5 deletions tests/integration/insights/test_annotate.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@
from robotoff.insights.annotate import CategoryAnnotator
from robotoff.models import ProductInsight

from ..models_utils import ProductInsightFactory
from ..models_utils import ProductInsightFactory, clean_db


@pytest.fixture(autouse=True)
def _set_up_and_tear_down(peewee_db):
# clean db
ProductInsight.delete().execute()
clean_db()
# Run the test case.
yield
# Tear down.
ProductInsight.delete().execute()
clean_db()


def test_annotation_fails_is_rolledback(mocker):
Expand Down
9 changes: 3 additions & 6 deletions tests/integration/insights/test_category_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@

from robotoff import settings
from robotoff.insights.importer import import_insights
from robotoff.models import Prediction as PredictionModel
from robotoff.models import ProductInsight
from robotoff.prediction.types import Prediction, PredictionType
from robotoff.products import Product

from ..models_utils import PredictionFactory, ProductInsightFactory
from ..models_utils import PredictionFactory, ProductInsightFactory, clean_db

insight_id1 = "94371643-c2bc-4291-a585-af2cb1a5270a"
barcode1 = "00001"
Expand All @@ -16,8 +15,7 @@
@pytest.fixture(autouse=True)
def _set_up_and_tear_down(peewee_db):
# clean db
ProductInsight.delete().execute()
PredictionModel.delete().execute()
clean_db()
# a category already exists
PredictionFactory(
barcode=barcode1,
Expand All @@ -33,8 +31,7 @@ def _set_up_and_tear_down(peewee_db):
# Run the test case.
yield
# Tear down.
ProductInsight.delete().execute()
PredictionModel.delete().execute()
clean_db()


def matcher_prediction(category):
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/insights/test_process_insights.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@
from robotoff.models import ProductInsight
from robotoff.scheduler import process_insights

from ..models_utils import ProductInsightFactory
from ..models_utils import ProductInsightFactory, clean_db


@pytest.fixture(autouse=True)
def _set_up_and_tear_down(peewee_db):
# clean db
ProductInsight.delete().execute()
clean_db()
# Run the test case.
yield
# Tear down.
ProductInsight.delete().execute()
clean_db()


# global for generating items
Expand Down
10 changes: 6 additions & 4 deletions tests/integration/models_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import factory
from factory_peewee import PeeweeModelFactory

from robotoff import settings
from robotoff import models, settings
from robotoff.models import (
AnnotationVote,
ImageModel,
Expand Down Expand Up @@ -128,14 +128,16 @@ class Meta:


def clean_db():
print("DEBUG: Before cleaning: ", models.db.get_tables())
# remove all models
for model in (
AnnotationVote,
ImageModel,
ImagePrediction,
LogoAnnotation,
ImagePrediction,
ImageModel,
LogoConfidenceThreshold,
Prediction,
ProductInsight,
):
model.delete()
model.delete().execute()
print("DEBUG: After cleaning: ", models.db.get_tables())
4 changes: 3 additions & 1 deletion tests/integration/test_annotate_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ def fetch_taxonomy_mock(url: str, *args, **kwargs):


def test_image_brand_annotation(client, monkeypatch, fake_taxonomy):
ann = LogoAnnotationFactory(image_prediction__image__source_image="/images/2.jpg")
ann = LogoAnnotationFactory(
image_prediction__image__source_image="/images/2.jpg", annotation_type="brand"
)
barcode = ann.image_prediction.image.barcode
_fake_store(monkeypatch, barcode)
monkeypatch.setattr(
Expand Down
13 changes: 4 additions & 9 deletions tests/integration/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,20 @@
from robotoff.app.api import api
from robotoff.models import AnnotationVote, ProductInsight

from .models_utils import AnnotationVoteFactory, ProductInsightFactory
from .models_utils import AnnotationVoteFactory, ProductInsightFactory, clean_db

insight_id = "94371643-c2bc-4291-a585-af2cb1a5270a"


@pytest.fixture(autouse=True)
def _set_up_and_tear_down(peewee_db):
# clean db
AnnotationVote.delete().execute()
ProductInsight.delete().execute()
clean_db()
# Set up.
ProductInsightFactory(id=insight_id, barcode=1)

# Run the test case.
yield

# Tear down.
AnnotationVote.delete().execute()
ProductInsight.delete().execute()
clean_db()


@pytest.fixture()
Expand Down Expand Up @@ -95,7 +90,7 @@ def test_popular_question(client, mocker):

def test_popular_question_pagination(client, mocker):
mocker.patch("robotoff.insights.question.get_product", return_value={})
ProductInsight.delete().execute()
ProductInsight.delete().execute() # remove default sample
for i in range(0, 12):
ProductInsightFactory(barcode=i, unique_scans_n=100 - i)

Expand Down
8 changes: 3 additions & 5 deletions tests/integration/test_models_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

from robotoff.models import AnnotationVote, ProductInsight

from .models_utils import AnnotationVoteFactory, ProductInsightFactory
from .models_utils import AnnotationVoteFactory, ProductInsightFactory, clean_db


@pytest.fixture(autouse=True)
def _set_up_and_tear_down(peewee_db):
# clean db
AnnotationVote.delete().execute()
ProductInsight.delete().execute()
clean_db()
# Run the test case.
yield
# Tear down.
AnnotationVote.delete().execute()
ProductInsight.delete().execute()
clean_db()


def test_vote_cascade_on_insight_deletion(peewee_db):
Expand Down

0 comments on commit 17619ec

Please sign in to comment.