subject.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. """testing subjects utils"""
  2. import random
  3. from sqlalchemy.orm import Session
  4. from db.repository import subjects as db_rep_sub
  5. from schemas.subjects import SubjectCreate
  6. from utils.utils import fake
  7. def generate_subject_fake_data():
  8. return {
  9. "name":
  10. fake.name(),
  11. "id_card":
  12. None,
  13. "birthday":
  14. str(fake.date_between_dates(date_start="-100y", date_end="-5y")),
  15. "gender":
  16. fake.subject_gender(),
  17. "rehabilitation_parts":
  18. fake.rehabilitation_parts()
  19. }
  20. def create_test_subject2db(db: Session,
  21. name=fake.name(),
  22. id_card=None,
  23. gender=fake.subject_gender(),
  24. birthday=fake.date_between_dates(date_start="-100y",
  25. date_end="-5y"),
  26. rehabilitation_parts=fake.rehabilitation_parts(),
  27. create_time=None) -> SubjectCreate:
  28. if create_time is None:
  29. subject = SubjectCreate(name=name,
  30. id_card=id_card,
  31. gender=gender,
  32. birthday=birthday,
  33. rehabilitation_parts=rehabilitation_parts)
  34. else:
  35. subject = SubjectCreate(name=name,
  36. id_card=id_card,
  37. gender=gender,
  38. birthday=birthday,
  39. rehabilitation_parts=rehabilitation_parts,
  40. create_time=create_time)
  41. subject = db_rep_sub.create_subject(subject, db)
  42. return subject
  43. def get_all_subject(db: Session):
  44. return db_rep_sub.list_subjects(db=db)
  45. def get_random_existing_subject(db: Session):
  46. subjects = get_all_subject(db)
  47. return random.choice(subjects)