-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add python-version of the dynamodb model. Implement test-case to veri…
…fy that everything is parsed properly without validation issues.
- Loading branch information
Showing
9 changed files
with
298 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[pytest] | ||
pythonpath = "src" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import datetime | ||
from enum import Enum | ||
from typing import List, Optional, Set | ||
|
||
from pydantic import BaseModel, Field, field_validator | ||
|
||
|
||
class NutritionFlag(str, Enum): | ||
"""An enumeration of the possible nutrition flags.""" | ||
|
||
VEGETARIAN = "vegetarian" | ||
VEGAN = "vegan" | ||
PORK = "pork" | ||
BEEF = "beef" | ||
FISH = "fish" | ||
CHICKEN = "chicken" | ||
|
||
|
||
class MensaMenuExtra(BaseModel): | ||
"""A model for the extra menu items.""" | ||
|
||
name: str = Field(alias='Name') | ||
description: str = Field(alias='Description') | ||
|
||
|
||
class MensaMenu(BaseModel): | ||
"""A model for the menu items.""" | ||
|
||
name: str = Field(alias='Name') | ||
contents: List[str] = Field(alias='Contents') | ||
price: Optional[str] = Field(alias='Price', default=None) | ||
nutrition_flags: Set[NutritionFlag] = Field(alias='NutritionFlags', default_factory=set) | ||
|
||
@field_validator('nutrition_flags', mode='before') | ||
def parse_nutrition_flags(cls, value): | ||
"""Parse the nutrition flags from the expected input format to a set of NutritionFlag enums. | ||
Sample input value: { | ||
"NutritionFlags": { | ||
"vegetarian": {} | ||
}, | ||
} | ||
""" | ||
if isinstance(value, dict): | ||
return {NutritionFlag(flag) for flag in value.keys()} | ||
return value | ||
|
||
|
||
class MensaDayMenus(BaseModel): | ||
"""A model for the dynamodb mensa menu item.""" | ||
|
||
date: datetime.date = Field(alias='Date') | ||
menus: List[MensaMenu] = Field(alias='Menus') | ||
extras: List[MensaMenuExtra] = Field(alias='Extras') | ||
mensa_id: str = Field(alias='MensaId') | ||
LanguageKey: str = Field(alias='LanguageKey') |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
{ | ||
"MensaId": "mensa-academica", | ||
"Date": "2024-05-15", | ||
"Menus": [ | ||
{ | ||
"NutritionFlags": { | ||
"fish": {} | ||
}, | ||
"Contents": [ | ||
"Potato creme soup", | ||
"Smoked salmon", | ||
"Bread roll" | ||
], | ||
"Price": "2,00 €", | ||
"Name": "Stew" | ||
}, | ||
{ | ||
"NutritionFlags": { | ||
"vegetarian": {} | ||
}, | ||
"Contents": [ | ||
"Potato creme soup", | ||
"Bread roll" | ||
], | ||
"Price": "1,60 €", | ||
"Name": "Vegetarian table dish" | ||
}, | ||
{ | ||
"NutritionFlags": { | ||
"vegetarian": {}, | ||
"vegan": {} | ||
}, | ||
"Contents": [ | ||
"Crispy sesame-carrot-stick", | ||
"Sweet sour sauce" | ||
], | ||
"Price": "2,20 €", | ||
"Name": "Vegetarian" | ||
}, | ||
{ | ||
"NutritionFlags": { | ||
"pork": {} | ||
}, | ||
"Contents": [ | ||
"Asparagus with ham", | ||
"White wine hollandaise", | ||
"Potatoes" | ||
], | ||
"Price": "6,50 €", | ||
"Name": "Suggestion of the day" | ||
}, | ||
{ | ||
"NutritionFlags": { | ||
"beef": {} | ||
}, | ||
"Contents": [ | ||
"Beef strips", | ||
"Mushrooms" | ||
], | ||
"Price": "2,80 €", | ||
"Name": "Classics" | ||
}, | ||
{ | ||
"NutritionFlags": { | ||
"chicken": {} | ||
}, | ||
"Contents": [ | ||
"Sweet n spicy chicken", | ||
"Banana peanut sauce", | ||
"Basmati rice" | ||
], | ||
"Price": "3,80 €", | ||
"Name": "Wok" | ||
}, | ||
{ | ||
"NutritionFlags": { | ||
"vegetarian": {}, | ||
"vegan": {} | ||
}, | ||
"Contents": [ | ||
"Sweet-spicy vegetables", | ||
"Banana peanut sauce", | ||
"Basmati rice" | ||
], | ||
"Price": "3,80 €", | ||
"Name": "Wok" | ||
}, | ||
{ | ||
"NutritionFlags": { | ||
"beef": {} | ||
}, | ||
"Contents": [ | ||
"Cheeseburger", | ||
"French fries", | ||
"Soft drink 0,33 L" | ||
], | ||
"Price": "4,90 €", | ||
"Name": "Burger Classics" | ||
}, | ||
{ | ||
"NutritionFlags": { | ||
"vegetarian": {} | ||
}, | ||
"Contents": [ | ||
"Veggie burger", | ||
"French fries", | ||
"Soft drink 0,33 L" | ||
], | ||
"Price": "4,90 €", | ||
"Name": "Burger Classics" | ||
}, | ||
{ | ||
"NutritionFlags": { | ||
"beef": {} | ||
}, | ||
"Contents": [ | ||
"Italian burger", | ||
"Rocket, tomatoes", | ||
"cheese sauce", | ||
"French fries", | ||
"Soft drink 0,33 L" | ||
], | ||
"Price": "4,90 €", | ||
"Name": "Burger of the week" | ||
} | ||
], | ||
"Extras": [ | ||
{ | ||
"Name": "Main side-dish", | ||
"Description": "potatoes or rice" | ||
}, | ||
{ | ||
"Name": "secondary", | ||
"Description": "Romanesco or Mixed salad" | ||
} | ||
], | ||
"LanguageKey": "en", | ||
"MensaIdLanguageKeyDate": "mensa-academica;en;2024-05-15" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import json | ||
from pathlib import Path | ||
|
||
from data.model import MensaDayMenus | ||
|
||
|
||
def test_model_parsing(): | ||
"""Test the parsing of the model from a json file.""" | ||
model_path = Path(__file__).parent / 'example_model.json' | ||
assert model_path.exists() | ||
assert model_path.is_file() | ||
|
||
with open(model_path, 'r') as f: | ||
model = json.load(f) | ||
|
||
MensaDayMenus(**model) |