code-corps-api icon indicating copy to clipboard operation
code-corps-api copied to clipboard

Update project factory so a set of task lists is always created

Open joshsmith opened this issue 7 years ago • 6 comments

Problem

We should upgrade our project factory so a set of task lists is always created. In a real use scenario, projects cannot get created without associated task lists. Our factories should mirror this.

joshsmith avatar Sep 04 '17 16:09 joshsmith

@joshsmith I'd like to give this one a shot.

boddhisattva avatar Dec 23 '17 07:12 boddhisattva

@joshsmith I was looking at the code in factories.ex to see how task_list_factory and project_factory are currently being created. It looks like the requirement as per this issue is we need to create tasks_lists after creating the actual project and we need to add relevant code in project_factory to do both operations. Is my understanding correct ?

I'm sorry, I'm not quite sure how one is currently going about creating tasks lists while creating projects from the UI but the create_changeset of project.ex model gave me a high level overview of tasks lists get created as part of creating a project and I'm assuming you're expecting something similar as part of the modified project factory, right ?

boddhisattva avatar Dec 23 '17 14:12 boddhisattva

@begedin if you have a moment tomorrow would you mind giving thoughts on this? I'm a little too exhausted to think straight at the moment.

joshsmith avatar Dec 28 '17 23:12 joshsmith

@boddhisattva Sorry for the delay here.

Simply put, we want project_factory to also create the set of default task lists every time it's being used

If you look at the documentation for exmachina

https://github.com/thoughtbot/ex_machina#ecto-associations

They have an example of creating an article with one comment. What we want here is to create a project with all four of the default task lists.

We also want this to be up to date at all times with the defaults specified in model/task_list.ex, so I would do it along the lines of:

def project_factory do
  %CodeCorps.Project{
    approved: true,
    long_description_markdown: sequence(:long_description_markdown, &"Description #{&1}"), # once approved, this MUST be set
    slug: sequence(:slug, &"project-#{&1}"),
    title: sequence(:title, &"Project #{&1}"),
    website: sequence(:website, &"http://test-#{&1}.com"),

    organization: build(:organization),
    task_lists: CodeCorps.TaskList.default_task_lists() |> Enum.map(&build(:task_list, &1))
  }
end

Then, to make sure this doesn't get broken in the future, I would also add a test to model/project_test along the lines of

test "factory creates default task lists unless specified otherwise" do
  # implement test here
end
``` 

Lastly, this change might break some existing tests, so you would need to fix them accordingly.

begedin avatar Jan 02 '18 13:01 begedin

@begedin I'd like to apologize for the delay from my end as well. I haven't yet been able to get back at this. I definitely plan to take a look at this sometime in the coming weeks.

boddhisattva avatar Jan 13 '18 14:01 boddhisattva

So I checked this out from my testing it seems this is already covered, as mentioned in the ex_machina docs all ecto associations are inserted automatically and since we are using the actual default task list when creating a project model/project.ex

  |> put_assoc(:task_lists, TaskList.default_task_lists())

this creates the lists whenever a project is created. Even without any changes to the source my test that asserts on the names of items in the default TaskList being present in the Repo passes.

zacck-zz avatar Feb 14 '18 17:02 zacck-zz