Skip to content

Commit

Permalink
Update factory & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Nov 26, 2024
1 parent e8c3cbe commit 1162339
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 21 deletions.
8 changes: 4 additions & 4 deletions open_prices/api/prices/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,21 +120,21 @@ def setUpTestData(cls):
owner=cls.user_session.user.user_id,
)
PriceFactory(
product_code=None,
type=price_constants.TYPE_CATEGORY,
**PRICE_APPLES,
labels_tags=[],
origins_tags=["en:spain"],
owner=cls.user_session.user.user_id,
)
PriceFactory(
product_code=None,
type=price_constants.TYPE_CATEGORY,
**PRICE_APPLES,
labels_tags=["en:organic"],
origins_tags=["en:unknown"],
owner=cls.user_session.user.user_id,
)
PriceFactory(
product_code=None,
type=price_constants.TYPE_CATEGORY,
**PRICE_APPLES,
labels_tags=["en:organic"],
origins_tags=["en:france"],
Expand Down Expand Up @@ -581,7 +581,7 @@ def setUpTestData(cls):
PriceFactory(product_code=cls.product.code, price=25)
PriceFactory(product_code=cls.product.code, price=30)
PriceFactory(
product_code=None,
type=price_constants.TYPE_CATEGORY,
category_tag="en:apples",
price=2,
price_per=price_constants.PRICE_PER_KILOGRAM,
Expand Down
2 changes: 1 addition & 1 deletion open_prices/locations/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def setUpTestData(cls):
owner=cls.user_2.user_id,
)
PriceFactory(
product_code=None,
type=price_constants.TYPE_CATEGORY,
category_tag="en:tomatoes",
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
Expand Down
16 changes: 15 additions & 1 deletion open_prices/prices/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,23 @@ class PriceFactory(DjangoModelFactory):
class Meta:
model = Price

class Params:
product_code_faker = factory.Faker("ean13")
category_tag_faker = "en:mandarines"

type = price_constants.TYPE_PRODUCT # random.choice(price_constants.TYPE_LIST)

product_code = factory.Faker("ean13")
product_code = factory.LazyAttribute(
lambda x: x.product_code_faker
if x.type == price_constants.TYPE_PRODUCT
else None
)
category_tag = factory.LazyAttribute(
lambda x: x.category_tag_faker
if x.type == price_constants.TYPE_CATEGORY
else None
)

price = factory.LazyAttribute(lambda x: random.randrange(0, 100))
# currency = factory.Faker("currency_symbol")
currency = factory.fuzzy.FuzzyChoice(constants.CURRENCY_LIST)
Expand Down
28 changes: 14 additions & 14 deletions open_prices/prices/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,14 @@ def test_price_without_product_validation(self):
)
# product_code not set
PriceFactory(
product_code=None,
type=price_constants.TYPE_CATEGORY,
category_tag="en:tomatoes",
price=3,
price_per=price_constants.PRICE_PER_KILOGRAM,
)
with self.assertRaises(ValidationError) as cm:
PriceFactory(
product_code=None,
type=price_constants.TYPE_CATEGORY,
category_tag="test",
price=3,
price_per=price_constants.PRICE_PER_KILOGRAM,
Expand All @@ -120,13 +120,13 @@ def test_price_without_product_validation(self):
"Invalid value: 'test', expected value to be in 'lang:tag' format",
)
PriceFactory(
product_code=None,
type=price_constants.TYPE_CATEGORY,
category_tag="fr: Grenoble", # valid (even if not in the taxonomy)
price=3,
price_per=price_constants.PRICE_PER_KILOGRAM,
)
PriceFactory(
product_code=None,
type=price_constants.TYPE_CATEGORY,
category_tag="en:tomatoes",
labels_tags=["en:organic"],
price=3,
Expand All @@ -135,7 +135,7 @@ def test_price_without_product_validation(self):
self.assertRaises(
ValidationError,
PriceFactory,
product_code=None,
type=price_constants.TYPE_CATEGORY,
category_tag="en:tomatoes",
labels_tags="en:organic", # should be a list
price=3,
Expand All @@ -144,7 +144,7 @@ def test_price_without_product_validation(self):
self.assertRaises(
ValidationError,
PriceFactory,
product_code=None,
type=price_constants.TYPE_CATEGORY,
category_tag="en:tomatoes",
labels_tags=[
"en:organic",
Expand All @@ -154,7 +154,7 @@ def test_price_without_product_validation(self):
price_per=price_constants.PRICE_PER_KILOGRAM,
)
PriceFactory(
product_code=None,
type=price_constants.TYPE_CATEGORY,
category_tag="en:tomatoes",
labels_tags=["en:organic"],
origins_tags=["en:france"],
Expand All @@ -164,7 +164,7 @@ def test_price_without_product_validation(self):
self.assertRaises(
ValidationError,
PriceFactory,
product_code=None,
type=price_constants.TYPE_CATEGORY,
category_tag="en:tomatoes",
labels_tags=["en:organic"],
origins_tags="en:france", # should be a list
Expand All @@ -174,7 +174,7 @@ def test_price_without_product_validation(self):
self.assertRaises(
ValidationError,
PriceFactory,
product_code=None,
type=price_constants.TYPE_CATEGORY,
category_tag="en:tomatoes",
labels_tags=["en:organic"],
origins_tags=["en:france", "test"], # not valid
Expand All @@ -193,7 +193,7 @@ def test_price_category_validation(self):
("fr: Soupe aux lentilles", "en:lentil-soups"),
]:
price = PriceFactory(
product_code=None,
type=price_constants.TYPE_CATEGORY,
category_tag=input_category,
price=3,
price_per=price_constants.PRICE_PER_KILOGRAM,
Expand All @@ -208,7 +208,7 @@ def test_price_origin_validation(self):
(["fr: Fairyland"], ["fr:fairyland"]),
]:
price = PriceFactory(
product_code=None,
type=price_constants.TYPE_CATEGORY,
category_tag="en:tomatoes",
origins_tags=input_origin_tags,
price=3,
Expand All @@ -223,23 +223,23 @@ def test_price_price_validation(self):
self.assertRaises(ValidationError, PriceFactory, price=PRICE_NOT_OK)
# price_per
PriceFactory(
product_code=None,
type=price_constants.TYPE_CATEGORY,
category_tag="en:tomatoes",
price=3,
price_per=price_constants.PRICE_PER_KILOGRAM,
)
self.assertRaises(
ValidationError,
PriceFactory,
product_code=None,
type=price_constants.TYPE_CATEGORY,
category_tag="en:tomatoes",
price=3,
price_per=None,
)
self.assertRaises(
ValidationError,
PriceFactory,
product_code=None,
type=price_constants.TYPE_CATEGORY,
category_tag="en:tomatoes",
price=3,
price_per="test",
Expand Down
2 changes: 1 addition & 1 deletion open_prices/stats/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def setUpTestData(cls):
owner=cls.user.user_id,
)
PriceFactory(
product_code=None,
type=price_constants.TYPE_CATEGORY,
category_tag="en:tomatoes",
location_osm_id=cls.location.osm_id,
location_osm_type=cls.location.osm_type,
Expand Down

0 comments on commit 1162339

Please sign in to comment.