|
| 1 | +"""Shared factory helpers for `ctj_api` test files. |
| 2 | +
|
| 3 | +Each helper saves a model instance with sensible defaults that callers |
| 4 | +can override via keyword arguments. The helpers do not return shared |
| 5 | +state - each call creates a new row. Callers wire factories together |
| 6 | +explicitly when they need relationships (e.g. an `Opportunity` needs |
| 7 | +a `Project`, a `Role`, and a `created_by` user; the test's `setUp` |
| 8 | +creates the dependencies and passes them in). |
| 9 | +
|
| 10 | +Per-test-file `setUp` methods import only the helpers they need. |
| 11 | +""" |
| 12 | + |
| 13 | +from ctj_api.models import ( |
| 14 | + CommunityOfPractice, |
| 15 | + CustomUser, |
| 16 | + Opportunity, |
| 17 | + Project, |
| 18 | + Role, |
| 19 | + Skill, |
| 20 | +) |
| 21 | + |
| 22 | + |
| 23 | +def make_pm_user( |
| 24 | + *, |
| 25 | + username: str = "pm_user", |
| 26 | + email: str = "pm_user@example.com", |
| 27 | + password: str = "password123", |
| 28 | + people_depot_user_id: str = "pm_user_pd_id", |
| 29 | +) -> CustomUser: |
| 30 | + """Create a project-manager user.""" |
| 31 | + return CustomUser.objects.create_user( |
| 32 | + username=username, |
| 33 | + email=email, |
| 34 | + password=password, |
| 35 | + people_depot_user_id=people_depot_user_id, |
| 36 | + isProjectManager=True, |
| 37 | + ) |
| 38 | + |
| 39 | + |
| 40 | +def make_regular_user( |
| 41 | + *, |
| 42 | + username: str = "regular_user", |
| 43 | + email: str = "regular_user@example.com", |
| 44 | + password: str = "password123", |
| 45 | + people_depot_user_id: str = "regular_user_pd_id", |
| 46 | +) -> CustomUser: |
| 47 | + """Create a non-PM user.""" |
| 48 | + return CustomUser.objects.create_user( |
| 49 | + username=username, |
| 50 | + email=email, |
| 51 | + password=password, |
| 52 | + people_depot_user_id=people_depot_user_id, |
| 53 | + isProjectManager=False, |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +def make_cop( |
| 58 | + *, |
| 59 | + practice_area: str = "engineering", |
| 60 | + description: str = "Engineering CoP", |
| 61 | +) -> CommunityOfPractice: |
| 62 | + """Create a Community of Practice row.""" |
| 63 | + return CommunityOfPractice.objects.create( |
| 64 | + practice_area=practice_area, |
| 65 | + description=description, |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +def make_role(*, title: str = "Developer", cop: CommunityOfPractice) -> Role: |
| 70 | + """Create a Role row anchored to the given CoP.""" |
| 71 | + return Role.objects.create(title=title, community_of_practice=cop) |
| 72 | + |
| 73 | + |
| 74 | +def make_skill(*, name: str = "Python") -> Skill: |
| 75 | + """Create a Skill row.""" |
| 76 | + return Skill.objects.create(name=name) |
| 77 | + |
| 78 | + |
| 79 | +def make_project( |
| 80 | + *, |
| 81 | + name: str = "Civic Tech Jobs", |
| 82 | + people_depot_project_id: str = "1234-abcd", |
| 83 | +) -> Project: |
| 84 | + """Create a Project row.""" |
| 85 | + return Project.objects.create( |
| 86 | + name=name, |
| 87 | + people_depot_project_id=people_depot_project_id, |
| 88 | + ) |
| 89 | + |
| 90 | + |
| 91 | +def make_opportunity( |
| 92 | + *, |
| 93 | + project: Project, |
| 94 | + role: Role, |
| 95 | + created_by: CustomUser, |
| 96 | + body: str = "Test opportunity", |
| 97 | + min_experience_required: str = "junior", |
| 98 | + min_hours_required: int = 10, |
| 99 | + work_environment: str = "remote", |
| 100 | + status: str = "open", |
| 101 | +) -> Opportunity: |
| 102 | + """Create an Opportunity row. |
| 103 | +
|
| 104 | + Caller supplies the FK rows (project, role, created_by) explicitly |
| 105 | + because tests typically want to assert against a specific PM user |
| 106 | + or project; default-constructing them inside the helper would |
| 107 | + obscure those references. |
| 108 | + """ |
| 109 | + return Opportunity.objects.create( |
| 110 | + project=project, |
| 111 | + role=role, |
| 112 | + body=body, |
| 113 | + min_experience_required=min_experience_required, |
| 114 | + min_hours_required=min_hours_required, |
| 115 | + work_environment=work_environment, |
| 116 | + status=status, |
| 117 | + created_by=created_by, |
| 118 | + ) |
0 commit comments