seed entire profile

This commit is contained in:
weeman 2021-06-26 10:51:39 +02:00
parent 09669cf369
commit ace1b1ed85
Signed by: weeman
GPG Key ID: 34F0524D4DA694A1
7 changed files with 112 additions and 64 deletions

View File

@ -23,7 +23,7 @@ cp env.dev .env
pipenv install --dev
pipenv shell
flask db upgrade
flask seed
flask seed --dev
flask run
```

1
ki/actions/__init__.py Normal file
View File

@ -0,0 +1 @@
from ki.actions.seed import seed # noqa

83
ki/actions/seed.py Normal file
View File

@ -0,0 +1,83 @@
import csv
import logging
from app import app, db
from ki.models import Address, Contact, ContactType, Language, Skill, Profile, ProfileLanguage, ProfileSkill, User
def seed(dev: bool):
skill_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/skills.csv"
logging.info("importing skills")
with open(skill_seed_file_path) as skills_file:
skills_csv_reader = csv.DictReader(skills_file)
for skill in skills_csv_reader:
id = int(skill["id"])
db_skill = Skill.query.get(id)
if db_skill is None:
db.session.add(Skill(id=int(skill["id"]), name=skill["name"]))
logging.info("importing languages")
iso_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/iso_639_1.csv"
with open(iso_seed_file_path) as iso_file:
iso_csv_reader = csv.DictReader(iso_file)
for iso in iso_csv_reader:
id = iso["639-1"]
db_language = Language.query.get(id)
if db_language is None:
db.session.add(Language(id=iso["639-1"], name=iso["Sprache"]))
if dev:
logging.info("seeding peter :)")
peter = User(auth_id="peter")
db.session.add(peter)
peters_profile = Profile(nickname="peternichtlustig",
pronouns="Herr Dr. Dr.",
volunteerwork="Gartenverein",
freetext="Ich mag Kaffee",
user=peter)
db.session.add(peters_profile)
matrix_type = ContactType(name="Matrix")
db.session.add(matrix_type)
matrix_contact = Contact(profile=peters_profile,
contacttype=matrix_type,
content="@peter:wtf-eg.de")
db.session.add(matrix_contact)
peters_address = Address(name="Peter Nichtlustig",
street="Waldweg",
house_number="23i",
additional="Hinterhaus",
postcode="13337",
city="Bielefeld",
country="Deutschland",
profile=peters_profile)
db.session.add(peters_address)
peters_python_skill = ProfileSkill(profile=peters_profile,
skill_id=3,
level=5)
db.session.add(peters_python_skill)
peter_de = ProfileLanguage(profile=peters_profile,
language_id="de",
level=5)
db.session.add(peter_de)
peter_fr = ProfileLanguage(profile=peters_profile,
language_id="fr",
level=3)
db.session.add(peter_fr)
db.session.commit()

View File

@ -1,39 +1,10 @@
import csv
import click
from ki.models import Language, Skill
from app import app, db
def seed():
skill_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/skills.csv"
print("importing skills")
with open(skill_seed_file_path) as skills_file:
skills_csv_reader = csv.DictReader(skills_file)
for skill in skills_csv_reader:
id = int(skill["id"])
db_skill = Skill.query.get(id)
if db_skill is None:
db.session.add(Skill(id=int(skill["id"]), name=skill["name"]))
iso_seed_file_path = app.config["KI_DATA_DIR"] + "/seed_data/iso_639_1.csv"
with open(iso_seed_file_path) as iso_file:
iso_csv_reader = csv.DictReader(iso_file)
for iso in iso_csv_reader:
id = iso["639-1"]
db_language = Language.query.get(id)
if db_language is None:
db.session.add(Language(id=iso["639-1"], name=iso["Sprache"]))
db.session.commit()
from app import app
from ki.actions import seed
@app.cli.command("seed")
def seed_command():
seed()
@click.option("--dev", is_flag=True)
def seed_command(dev):
seed(dev)

View File

@ -2,8 +2,8 @@ from alembic import command
import json
import unittest
from app import app, migrate
from ki.commands import seed
from app import app, db, migrate
from ki.actions import seed
class TestLoginEndpoint(unittest.TestCase):
@ -16,7 +16,11 @@ class TestLoginEndpoint(unittest.TestCase):
config = migrate.get_config()
command.upgrade(config, "head")
seed()
seed(True)
def tearDown(self):
db.drop_all()
db.engine.dispose()
def test_login(self):
response1_data = self.login()

View File

@ -3,8 +3,8 @@ import unittest
import json
from app import app, db, migrate
from ki.commands import seed
from ki.models import Profile, User
from ki.actions import seed
from ki.models import User
class TestProfileEndpoint(unittest.TestCase):
@ -15,22 +15,16 @@ class TestProfileEndpoint(unittest.TestCase):
self.client = app.test_client()
with app.app_context():
db.drop_all()
db.engine.dispose()
config = migrate.get_config()
command.upgrade(config, "head")
seed()
seed(True)
def tearDown(self):
db.drop_all()
db.engine.dispose()
def test_create_profile(self):
user = User(auth_id="peter")
db.session.add(user)
db.session.commit()
login_data = {"username": "peter", "password": "geheim"}
login_response = self.client.post("/users/login",
data=json.dumps(login_data),
@ -62,15 +56,6 @@ class TestProfileEndpoint(unittest.TestCase):
self.assertEqual("Hallo", profile.freetext)
def test_get_profile(self):
user = User(auth_id="peter")
db.session.add(user)
profile = Profile(user=user)
profile.nickname = "Popeter"
db.session.add(profile)
db.session.commit()
login_data = {"username": "peter", "password": "geheim"}
login_response = self.client.post("/users/login",
data=json.dumps(login_data),
@ -89,10 +74,10 @@ class TestProfileEndpoint(unittest.TestCase):
self.assertEqual(
response.json, {
"profile": {
"freetext": "",
"nickname": "Popeter",
"pronouns": "",
"volunteerwork": ""
"freetext": "Ich mag Kaffee",
"nickname": "peternichtlustig",
"pronouns": "Herr Dr. Dr.",
"volunteerwork": "Gartenverein"
}
})

View File

@ -1,8 +1,8 @@
from alembic import command
import unittest
from app import app, migrate
from ki.commands import seed
from app import app, db, migrate
from ki.actions import seed
class TestSkillsEndpoint(unittest.TestCase):
@ -15,7 +15,11 @@ class TestSkillsEndpoint(unittest.TestCase):
config = migrate.get_config()
command.upgrade(config, "head")
seed()
seed(True)
def tearDown(self):
db.drop_all()
db.engine.dispose()
def test_skills_options(self):
response = self.client.options("/skills")