Skip to content

Commit

Permalink
fix: fix unit and integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raphael0202 committed Dec 29, 2022
1 parent e8949da commit ab17fd2
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 37 deletions.
2 changes: 1 addition & 1 deletion robotoff/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def categorize(barcode: str, deepest_only: bool = False) -> None:

if predictions:
for prediction in predictions:
print(f"{prediction.value_tag}: {prediction.data['confidence']}")
print(f"{prediction.value_tag}: {prediction.confidence}")
else:
print(f"Nothing predicted for product {barcode}")

Expand Down
11 changes: 3 additions & 8 deletions robotoff/insights/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -933,17 +933,12 @@ def create_prediction_model(
server_domain: str,
timestamp: datetime.datetime,
):
prediction_dict = prediction.to_dict()
prediction_dict.pop("id")
return {
"barcode": prediction.barcode,
"type": prediction.type.name,
"data": prediction.data,
**prediction_dict,
"timestamp": timestamp,
"value_tag": prediction.value_tag,
"value": prediction.value,
"source_image": prediction.source_image,
"automatic_processing": prediction.automatic_processing,
"server_domain": server_domain,
"predictor": prediction.predictor,
}


Expand Down
6 changes: 1 addition & 5 deletions robotoff/logos.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,11 +339,7 @@ def import_logo_insights(
)
& (~(PredictionModel.data["is_annotation"].cast("bool") == True)) # noqa: E712
# Add a filter on barcode to speed-up filtering
& (
PredictionModel.barcode.in_(
[logo.image_prediction.image.barcode for logo in logos]
)
)
& (PredictionModel.barcode.in_([logo.barcode for logo in logos]))
).execute()
predictions = predict_logo_predictions(selected_logos, logo_probs)
import_result = import_insights(predictions, server_domain)
Expand Down
4 changes: 1 addition & 3 deletions robotoff/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,9 +277,7 @@ class Meta:
constraints = [peewee.SQL("UNIQUE(image_prediction_id, index)")]

def get_crop_image_url(self) -> str:
return crop_image_url(
self.image_prediction.image.source_image, self.bounding_box
)
return crop_image_url(self.source_image, self.bounding_box)


class LogoEmbedding(BaseModel):
Expand Down
2 changes: 1 addition & 1 deletion robotoff/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ def send_logo_notification(
)
)
)
barcode = logo.image_prediction.image.barcode
barcode = logo.barcode
base_off_url = settings.BaseURLProvider().get()
text = (
f"Prediction for <{crop_url}|image> "
Expand Down
6 changes: 3 additions & 3 deletions robotoff/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ def __repr__(self) -> str:
return (
f"<InsightImportResult insights: created={self.created_insights_count()}, "
f"updated={self.updated_insights_count()}, "
f"deleted={self.deleted_insights_count()}\n"
f"types: {list(set(result.type.value for result in self.product_insight_import_results))}"
f" predictions: created: {self.created_predictions_count()}>"
f"deleted={self.deleted_insights_count()}, "
f"types: {list(set(result.type.value for result in self.product_insight_import_results))}, "
f"predictions: created={self.created_predictions_count()}>"
)
6 changes: 4 additions & 2 deletions tests/integration/models_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"""
import uuid
from datetime import datetime
from typing import Any
from typing import Any, Optional

import factory
import numpy as np
Expand Down Expand Up @@ -67,7 +67,7 @@ class Meta:
server_domain = factory.LazyFunction(lambda: settings.OFF_SERVER_DOMAIN)
automatic_processing = None
predictor = None
confidence = None
confidence: Optional[float] = None


class AnnotationVoteFactory(UuidSequencer, PeeweeModelFactory):
Expand Down Expand Up @@ -125,6 +125,8 @@ class Meta:
taxonomy_value = "fr:ab-agriculture-biologique"
annotation_type = "label"
nearest_neighbors = {"logo_ids": [111111, 222222], "distances": [11.1, 12.4]}
barcode = factory.Sequence(lambda n: f"{n:013}")
source_image = factory.Sequence(lambda n: f"/images/{n:02}.jpg")


class LogoConfidenceThresholdFactory(PeeweeModelFactory):
Expand Down
17 changes: 11 additions & 6 deletions tests/integration/test_annotate_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,14 @@ def test_logo_annotation_incorrect_value_label_type(client, peewee_db):


def test_logo_annotation_brand(client, peewee_db, monkeypatch, mocker, fake_taxonomy):
barcode = "0000000000001"
source_image = "/000/000/000/0001/2.jpg"
with peewee_db:
ann = LogoAnnotationFactory(
image_prediction__image__source_image="/images/2.jpg",
barcode=barcode,
source_image=source_image,
annotation_type=None,
)
barcode = ann.image_prediction.image.barcode
_fake_store(monkeypatch, barcode)
mocker.patch(
"robotoff.brands.get_brand_prefix", return_value={("Etorki", "0000000xxxxxx")}
Expand Down Expand Up @@ -190,7 +192,7 @@ def test_logo_annotation_brand(client, peewee_db, monkeypatch, mocker, fake_taxo
# we generate a prediction

with peewee_db:
predictions = list(Prediction.select().filter(barcode=barcode).execute())
predictions = list(Prediction.select().where(Prediction.barcode == barcode))
assert len(predictions) == 1
(prediction,) = predictions
assert prediction.type == "brand"
Expand All @@ -209,7 +211,9 @@ def test_logo_annotation_brand(client, peewee_db, monkeypatch, mocker, fake_taxo
# We check that this prediction in turn generates an insight

with peewee_db:
insights = list(ProductInsight.select().filter(barcode=barcode).execute())
insights = list(
ProductInsight.select().where(ProductInsight.barcode == barcode)
)
assert len(insights) == 1
(insight,) = insights
assert insight.type == "brand"
Expand All @@ -233,11 +237,12 @@ def test_logo_annotation_label(client, peewee_db, monkeypatch, fake_taxonomy, mo
"""This test will check that, given an image with a logo above the confidence threshold,
that is then fed into the ANN logos and labels model, we annotate properly a product.
"""
barcode = "0000000000001"
source_image = "/000/000/000/0001/2.jpg"
with peewee_db:
ann = LogoAnnotationFactory(
image_prediction__image__source_image="/images/2.jpg", annotation_type=None
barcode=barcode, source_image=source_image, annotation_type=None
)
barcode = ann.image_prediction.image.barcode
_fake_store(monkeypatch, barcode)
mocker.patch("robotoff.logos.annotate", return_value=UPDATED_ANNOTATION_RESULT)
start = datetime.utcnow()
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,11 +821,13 @@ def test_image_prediction_collection_empty(client):
def test_image_prediction_collection(client, peewee_db):
with peewee_db:
logo_annotation_category_123 = LogoAnnotationFactory(
barcode="123",
image_prediction__image__barcode="123",
image_prediction__type="category",
)
prediction_category_123 = logo_annotation_category_123.image_prediction
logo_annotation_label_789 = LogoAnnotationFactory(
barcode="789",
image_prediction__image__barcode="789",
image_prediction__type="label",
)
Expand Down Expand Up @@ -904,31 +906,37 @@ def test_logo_annotation_collection_api(client, peewee_db):
with peewee_db:
LogoAnnotation.delete().execute() # remove default sample
annotation_123_1 = LogoAnnotationFactory(
barcode="123",
image_prediction__image__barcode="123",
annotation_value_tag="etorki",
annotation_type="brand",
)
annotation_123_2 = LogoAnnotationFactory(
barcode="123",
image_prediction__image__barcode="123",
annotation_value_tag="etorki",
annotation_type="brand",
)
annotation_295 = LogoAnnotationFactory(
barcode="295",
image_prediction__image__barcode="295",
annotation_value_tag="cheese",
annotation_type="dairies",
)
annotation_789 = LogoAnnotationFactory(
barcode="789",
image_prediction__image__barcode="789",
annotation_value_tag="creme",
annotation_type="dairies",
)
annotation_306 = LogoAnnotationFactory(
barcode="306",
image_prediction__image__barcode="306",
annotation_value_tag="yoghurt",
annotation_type="dairies",
)
annotation_604 = LogoAnnotationFactory(
barcode="604",
image_prediction__image__barcode="604",
annotation_value_tag="meat",
annotation_type="category",
Expand Down
5 changes: 5 additions & 0 deletions tests/integration/test_core_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,29 +203,34 @@ def test_get_unanswered_questions_list():

def test_get_logo_annotation():
annotation_123 = LogoAnnotationFactory(
barcode="123",
image_prediction__image__barcode="123",
annotation_value_tag="etorki",
annotation_type="brand",
)

annotation_789 = LogoAnnotationFactory(
barcode="789",
image_prediction__image__barcode="789",
annotation_value_tag="creme",
annotation_type="dairies",
)

annotation_295 = LogoAnnotationFactory(
barcode="295",
image_prediction__image__barcode="295",
annotation_value_tag="cheese",
annotation_type="dairies",
)

annotation_396 = LogoAnnotationFactory(
barcode="396",
image_prediction__image__barcode="396",
annotation_type="label",
)

LogoAnnotationFactory(
barcode="306",
image_prediction__image__barcode="306",
annotation_value_tag="yoghurt",
annotation_type="dairies",
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_logos.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def test_compute_iou(box_1, box_2, expected_iou):
@pytest.mark.parametrize(
"logo_type,logo_value,data,automatic_processing,confidence,prediction",
[
("category", "en:breads", {}, False, None),
("label", None, {}, False, None),
("category", "en:breads", {}, 0.1, False, None),
("label", None, {}, 0.1, False, None),
(
"label",
"en:eu-organic",
Expand All @@ -36,7 +36,7 @@ def test_compute_iou(box_1, box_2, expected_iou):
value=None,
automatic_processing=False,
predictor="universal-logo-detector",
confidence=0.5,
confidence=0.8,
),
),
(
Expand Down
8 changes: 5 additions & 3 deletions tests/unit/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ def test_crop_image_url(monkeypatch):
model_version="1.0",
image=ImageModel(
barcode="123",
image_id="image_id",
source_image="/image",
image_id="1",
source_image="/123/1.jpg",
width=20,
height=20,
),
),
bounding_box=(1, 1, 2, 2),
barcode="123",
source_image="/123/1.jpg",
)

assert logo_annotation.get_crop_image_url() == (
f"https://robotoff.{settings._robotoff_domain}/api/v1/images/crop"
+ f"?image_url={settings.OFF_IMAGE_BASE_URL}/image&y_min=1&x_min=1&y_max=2&x_max=2"
+ f"?image_url={settings.OFF_IMAGE_BASE_URL}/123/1.jpg&y_min=1&x_min=1&y_max=2&x_max=2"
)
5 changes: 3 additions & 2 deletions tests/unit/test_slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,12 +211,13 @@ def test_noop_slack_notifier_logging(caplog):
notifier.send_logo_notification(
LogoAnnotation(
image_prediction=ImagePrediction(
barcode="123",
image=ImageModel(
source_image="/path/to/image.jpg", width=10, height=10
barcode="123", source_image="/123/1.jpg", width=10, height=10
),
),
bounding_box=(1, 1, 2, 2),
barcode="123",
source_image="/123/1.jpg",
),
{},
)
Expand Down

0 comments on commit ab17fd2

Please sign in to comment.