ki-backend/ki/test/ApiTest.py

65 lines
2.2 KiB
Python
Raw Permalink Normal View History

2024-03-19 20:14:48 +01:00
# SPDX-FileCopyrightText: WTF Kooperative eG <https://wtf-eg.de/>
# SPDX-License-Identifier: AGPL-3.0-or-later
2021-07-05 19:37:05 +02:00
2021-06-27 14:25:44 +02:00
from alembic import command
import json
import unittest
from app import app, db, migrate
from ki.actions import seed
2021-07-26 19:56:36 +02:00
from ki.models import Skill
2024-03-19 20:51:22 +01:00
from sqlalchemy import select
2024-01-23 20:11:35 +01:00
2021-06-27 14:25:44 +02:00
class ApiTest(unittest.TestCase):
maxDiff = None
def setUp(self):
2024-01-23 20:11:35 +01:00
print("Running setup")
2021-06-27 14:25:44 +02:00
app.debug = True
2021-07-12 18:37:54 +02:00
app.config["KI_AUTH"] = "file"
2021-06-27 14:25:44 +02:00
app.config["TESTING"] = True
app.config["SQLALCHEMY_DATABASE_URI"] = "sqlite:///:memory:"
2021-07-11 12:16:41 +02:00
2021-06-27 14:25:44 +02:00
self.client = app.test_client()
2024-01-23 20:11:35 +01:00
config = migrate.get_config()
2024-03-19 20:14:48 +01:00
with app.app_context():
2021-06-27 14:25:44 +02:00
command.upgrade(config, "head")
2024-01-23 20:11:35 +01:00
seed(True)
2021-06-27 14:25:44 +02:00
2024-03-19 20:14:48 +01:00
# statement = select(Skill).order_by(Skill.id.desc())
# print(statement)
# skill_obj = db.session.scalars(statement).all()
# print(skill_obj)
# statement = select(Skill.id)
# print(statement)
# max_skill = db.session.Skill().order_by(Skill.id.desc()).first()
2024-03-19 20:46:22 +01:00
# max_skill = Skill.query.order_by(Skill.id.desc()).first() # TODO: problematic
2024-03-19 20:47:09 +01:00
with db.session.no_autoflush: # only works on first test run
2024-03-19 20:46:22 +01:00
max_skill = db.session.query(Skill).order_by(Skill.id.desc()).first() # TODO: also problematic,
2024-03-19 20:14:48 +01:00
# skills = db.session.execute(db.select(Skill)).scalars()
# print(max_skill)
# max_skill = db.session.execute(db.select(Skill)
# .order_by(Skill.id.desc())
# ).scalar_one()
2024-01-23 20:11:35 +01:00
print(max_skill)
print("max_skill done")
2021-07-26 19:56:36 +02:00
self.max_skill_id = max_skill.id
2021-06-27 14:25:44 +02:00
def tearDown(self):
2024-01-23 20:11:35 +01:00
print("Running teardown")
with app.app_context():
db.drop_all()
db.engine.dispose()
2021-06-27 14:25:44 +02:00
def login(self, username, password):
2024-03-19 20:14:48 +01:00
# with app.app_context():
2021-06-27 14:25:44 +02:00
login_data = {"username": username, "password": password}
login_response = self.client.post("/users/login", data=json.dumps(login_data), content_type="application/json")
self.assertEqual(login_response.status_code, 200)
self.assertIn("token", login_response.json)
return login_response.json