Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Idea: Enums #223

Closed
bnns opened this issue Jun 8, 2017 · 4 comments
Closed

Feature Idea: Enums #223

bnns opened this issue Jun 8, 2017 · 4 comments

Comments

@bnns
Copy link

bnns commented Jun 8, 2017

It would be great if we could do something like:

def role_factory do
  %Role{
    name: enum(["owner", "admin", "guest"])
  }
end

which would behave like:

build(:role)
# %Role{name: "owner"}
build(:role)
# %Role{name: "admin"}
@pdawczak
Copy link
Contributor

pdawczak commented Jun 8, 2017

Wouldn't Enum.random/1 work for that case?

Something like:

Enum.random(["owner", "admin", "guest"])

in

def role_factory do
  %Role{
    name: Enum.random(["owner", "admin", "guest"])
  }
end

@bnns
Copy link
Author

bnns commented Jun 8, 2017

Yeah, kind of - but I was thinking of cases where you need at least 1 struct for each value. Besides creating a user for each role, another use-case for me is to build a struct per day of the week.

def schedule_factory do
  %Schedule{
    day_of_week: enum(["Sun", "Mon", "Tue", ...]),
    start_time: Ecto.Time.cast!("12:00:00Z"),
    end_time: Ecto.Time.cast!("3:00:00Z")
  }
end

The ability to go in order would nice for that situation.

@suhrawardi
Copy link
Contributor

Please have a look at this PR #227,

def schedule_factory do
  %Schedule{
    day_of_week: sequence(:weekdays, ["Sun", "Mon", "Tue"]),
    start_time: Ecto.Time.cast!("12:00:00Z"),
    end_time: Ecto.Time.cast!("3:00:00Z")
  }
end

suhrawardi added a commit to suhrawardi/ex_machina that referenced this issue Jun 29, 2017
@bnns
Copy link
Author

bnns commented Jun 29, 2017

Looks great! Thanks @suhrawardi. Closing this issue.

@bnns bnns closed this as completed Jun 29, 2017
germsvel pushed a commit that referenced this issue Oct 20, 2017
Can use lists in sequences. Fixes #223 

```
  def user_factory do
    %MyApp.User{
      name: "Jane Smith",
      email: sequence(:email, &"email-#{&1}@example.com"),
      role: sequence(:role, ["admin", "user", "other"]),
    }
  end
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants