1
0
Fork 0
mirror of https://github.com/mealie-recipes/mealie.git synced 2025-08-03 04:25:24 +02:00

chore: file generation cleanup (#1736)

This PR does too many things :( 

1. Major refactoring of the dev/scripts and dev/code-generation folders. 

Primarily this was removing duplicate code and cleaning up some poorly written code snippets as well as making them more idempotent so then can be re-run over and over again but still maintain the same results. This is working on my machine, but I've been having problems in CI and comparing diffs so running generators in CI will have to wait. 

2. Re-Implement using the generated api routes for testing

This was a _huge_ refactor that touched damn near every test file but now we have auto-generated typed routes with inline hints and it's used for nearly every test excluding a few that use classes for better parameterization. This should greatly reduce errors when writing new tests. 

3. Minor Perf improvements for the All Recipes endpoint

  A. Removed redundant loops
  B. Uses orjson to do the encoding directly and returns a byte response instead of relying on the default 
       jsonable_encoder.

4. Fix some TS type errors that cropped up for seemingly no reason half way through the PR.

See this issue https://github.com/phillipdupuis/pydantic-to-typescript/issues/28

Basically, the generated TS type is not-correct since Pydantic will automatically fill in null fields. The resulting TS type is generated with a ? to indicate it can be null even though we _know_ that i can't be.
This commit is contained in:
Hayden 2022-10-18 14:49:41 -08:00 committed by GitHub
parent a8f0fb14a7
commit 9ecef4c25f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
107 changed files with 2520 additions and 1948 deletions

View file

@ -2,6 +2,5 @@ from .fixture_admin import *
from .fixture_database import *
from .fixture_multitenant import *
from .fixture_recipe import *
from .fixture_routes import *
from .fixture_shopping_lists import *
from .fixture_users import *

View file

@ -3,23 +3,24 @@ from starlette.testclient import TestClient
from mealie.core.config import get_app_settings
from tests import utils
from tests.utils import api_routes
@fixture(scope="session")
def admin_token(api_client: TestClient, api_routes: utils.AppRoutes):
def admin_token(api_client: TestClient):
settings = get_app_settings()
form_data = {"username": settings.DEFAULT_EMAIL, "password": settings.DEFAULT_PASSWORD}
return utils.login(form_data, api_client, api_routes)
return utils.login(form_data, api_client)
@fixture(scope="session")
def admin_user(api_client: TestClient, api_routes: utils.AppRoutes):
def admin_user(api_client: TestClient):
settings = get_app_settings()
form_data = {"username": settings.DEFAULT_EMAIL, "password": settings.DEFAULT_PASSWORD}
token = utils.login(form_data, api_client, api_routes)
token = utils.login(form_data, api_client)
user_data = api_client.get(api_routes.users_self, headers=token).json()
assert token is not None

View file

@ -1,8 +0,0 @@
from pytest import fixture
from tests import utils
@fixture(scope="session")
def api_routes():
return utils.AppRoutes()

View file

@ -1,14 +1,15 @@
import json
from typing import Generator
from pytest import fixture
from starlette.testclient import TestClient
from tests import utils
from tests.utils import api_routes
from tests.utils.factories import random_string
def build_unique_user(group: str, api_client: TestClient) -> utils.TestUser:
api_routes = utils.AppRoutes()
group = group or random_string(12)
registration = utils.user_registration_factory()
@ -17,7 +18,7 @@ def build_unique_user(group: str, api_client: TestClient) -> utils.TestUser:
form_data = {"username": registration.username, "password": registration.password}
token = utils.login(form_data, api_client, api_routes)
token = utils.login(form_data, api_client)
user_data = api_client.get(api_routes.users_self, headers=token).json()
assert token is not None
@ -26,14 +27,14 @@ def build_unique_user(group: str, api_client: TestClient) -> utils.TestUser:
_group_id=user_data.get("groupId"),
user_id=user_data.get("id"),
email=user_data.get("email"),
password=registration.password,
username=user_data.get("username"),
password=registration.password,
token=token,
)
@fixture(scope="module")
def g2_user(admin_token, api_client: TestClient, api_routes: utils.AppRoutes):
def g2_user(admin_token, api_client: TestClient):
group = random_string(12)
# Create the user
create_data = {
@ -46,7 +47,7 @@ def g2_user(admin_token, api_client: TestClient, api_routes: utils.AppRoutes):
"tokens": [],
}
response = api_client.post(api_routes.groups, json={"name": group}, headers=admin_token)
response = api_client.post(api_routes.admin_groups, json={"name": group}, headers=admin_token)
response = api_client.post(api_routes.users, json=create_data, headers=admin_token)
assert response.status_code == 201
@ -54,7 +55,7 @@ def g2_user(admin_token, api_client: TestClient, api_routes: utils.AppRoutes):
# Log in as this user
form_data = {"username": create_data["email"], "password": "useruser"}
token = utils.login(form_data, api_client, api_routes)
token = utils.login(form_data, api_client)
self_response = api_client.get(api_routes.users_self, headers=token)
@ -68,9 +69,9 @@ def g2_user(admin_token, api_client: TestClient, api_routes: utils.AppRoutes):
user_id=user_id,
_group_id=group_id,
token=token,
password="useruser",
email=create_data["email"],
username=create_data.get("username"),
email=create_data["email"], # type: ignore
username=create_data.get("username"), # type: ignore
password=create_data.get("password"), # type: ignore
)
finally:
# TODO: Delete User after test
@ -78,14 +79,14 @@ def g2_user(admin_token, api_client: TestClient, api_routes: utils.AppRoutes):
@fixture(scope="module")
def unique_user(api_client: TestClient, api_routes: utils.AppRoutes):
def unique_user(api_client: TestClient):
registration = utils.user_registration_factory()
response = api_client.post("/api/users/register", json=registration.dict(by_alias=True))
assert response.status_code == 201
form_data = {"username": registration.username, "password": registration.password}
token = utils.login(form_data, api_client, api_routes)
token = utils.login(form_data, api_client)
user_data = api_client.get(api_routes.users_self, headers=token).json()
assert token is not None
@ -94,9 +95,9 @@ def unique_user(api_client: TestClient, api_routes: utils.AppRoutes):
yield utils.TestUser(
_group_id=user_data.get("groupId"),
user_id=user_data.get("id"),
password=registration.password,
email=user_data.get("email"),
username=user_data.get("username"),
password=registration.password,
token=token,
)
finally:
@ -105,7 +106,7 @@ def unique_user(api_client: TestClient, api_routes: utils.AppRoutes):
@fixture(scope="module")
def user_tuple(admin_token, api_client: TestClient, api_routes: utils.AppRoutes) -> tuple[utils.TestUser]:
def user_tuple(admin_token, api_client: TestClient) -> Generator[list[utils.TestUser], None, None]:
group_name = utils.random_string()
# Create the user
create_data_1 = {
@ -128,16 +129,17 @@ def user_tuple(admin_token, api_client: TestClient, api_routes: utils.AppRoutes)
"tokens": [],
}
api_client.post(api_routes.admin_groups, json={"name": group_name}, headers=admin_token)
users_out = []
for usr in [create_data_1, create_data_2]:
response = api_client.post(api_routes.groups, json={"name": "New Group"}, headers=admin_token)
response = api_client.post(api_routes.users, json=usr, headers=admin_token)
assert response.status_code == 201
# Log in as this user
form_data = {"username": usr["email"], "password": "useruser"}
token = utils.login(form_data, api_client, api_routes)
token = utils.login(form_data, api_client)
response = api_client.get(api_routes.users_self, headers=token)
assert response.status_code == 200
user_data = json.loads(response.text)
@ -147,8 +149,8 @@ def user_tuple(admin_token, api_client: TestClient, api_routes: utils.AppRoutes)
_group_id=user_data.get("groupId"),
user_id=user_data.get("id"),
username=user_data.get("username"),
password="useruser",
email=user_data.get("email"),
password="useruser",
token=token,
)
)
@ -160,7 +162,7 @@ def user_tuple(admin_token, api_client: TestClient, api_routes: utils.AppRoutes)
@fixture(scope="session")
def user_token(admin_token, api_client: TestClient, api_routes: utils.AppRoutes):
def user_token(admin_token, api_client: TestClient):
# Create the user
create_data = {
"fullName": utils.random_string(),
@ -178,4 +180,4 @@ def user_token(admin_token, api_client: TestClient, api_routes: utils.AppRoutes)
# Log in as this user
form_data = {"username": create_data["email"], "password": "useruser"}
return utils.login(form_data, api_client, api_routes)
return utils.login(form_data, api_client)