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

should define_enum_for should validate column is an integer #827

Closed
geoffharcourt opened this issue Oct 29, 2015 · 5 comments
Closed

should define_enum_for should validate column is an integer #827

geoffharcourt opened this issue Oct 29, 2015 · 5 comments

Comments

@geoffharcourt
Copy link
Contributor

While converting a column from being a string to an enum, I found that the enum matcher spec for my model passed, but every time I assigned a value to the field it came back as nil.

@mcmire
Copy link
Collaborator

mcmire commented Oct 29, 2015

Okay -- I'm not sure I understand exactly what's happening here, can you provide an example?

@geoffharcourt
Copy link
Contributor Author

cc: @mcmire

@geoffharcourt
Copy link
Contributor Author

Here's what happened:

class Example < ActiveRecord::Base
   enum status: { active: 0, inactive: 1 }
end

create_table :examples do |t|
  t.string :status
end

RSpec.describe Example do
  it { should define_enum_for(:status).with(active: 0, inactive: 1) }
end

The matcher passes, but this enum is actually broken in a subtle but very bad way:

example = Example.new(status: :active)

# => #<Example:0x007f9f811169f8 status: "0">

example.status
# => nil

When the column is a string, AR will allow you to save values, but after save, the strings don't get recast as integers, and any lookup against enum values fails.

What I'm struggling with is that right now the specs for the most part don't rely on a database table being created, so adding a criteria for the matcher that checks for the column breaks almost all the specs for define_enum_for.

geoffharcourt added a commit that referenced this issue Oct 30, 2015
If your ActiveRecord model stores its `enum` data in a non-integer
column, ActiveRecord will save the data without error. However, when you
access the attribute on the record after saving, AR will look for the
string to what it expects to be a list of numbers and return `nil`
rather than the mapped value.

This change adds a third criterion to the `define_enum_for` matcher,
verifying that the underlying database column has a `sql_type` of
`"integer"`.

Fix #827.
geoffharcourt added a commit that referenced this issue Oct 30, 2015
If your ActiveRecord model stores its `enum` data in a non-integer
column, ActiveRecord will save the data without error. However, when you
access the attribute on the record after saving, AR will look for the
string to what it expects to be a list of numbers and return `nil`
rather than the mapped value.

This change adds a third criterion to the `define_enum_for` matcher,
verifying that the underlying database column has a `sql_type` of
`"integer"`.

Fix #827.
geoffharcourt added a commit that referenced this issue Oct 30, 2015
If your ActiveRecord model stores its `enum` data in a non-integer
column, ActiveRecord will save the data without error. However, when you
access the attribute on the record after saving, AR will look for the
string to what it expects to be a list of numbers and return `nil`
rather than the mapped value.

This change adds a third criterion to the `define_enum_for` matcher,
verifying that the underlying database column has a `sql_type` of
`"integer"`.

Fix #827.
@mcmire
Copy link
Collaborator

mcmire commented Oct 30, 2015

Okay, I'll keep an eye on that issue.

geoffharcourt added a commit that referenced this issue Oct 30, 2015
If your ActiveRecord model stores its `enum` data in a non-integer
column, ActiveRecord will save the data without error. However, when you
access the attribute on the record after saving, AR will look for the
string to what it expects to be a list of numbers and return `nil`
rather than the mapped value.

This change adds a third criterion to the `define_enum_for` matcher,
verifying that the underlying database column has a `sql_type` of
`"integer"`.

Fix #827.
geoffharcourt added a commit that referenced this issue Oct 30, 2015
If your ActiveRecord model stores its `enum` data in a non-integer
column, ActiveRecord will save the data without error. However, when you
access the attribute on the record after saving, AR will look for the
string to what it expects to be a list of numbers and return `nil`
rather than the mapped value.

This change adds a third criterion to the `define_enum_for` matcher,
verifying that the underlying database column has a `sql_type` of
`"integer"`.

Fix #827.
geoffharcourt added a commit that referenced this issue Oct 30, 2015
If your ActiveRecord model stores its `enum` data in a non-integer
column, ActiveRecord will save the data without error. However, when you
access the attribute on the record after saving, AR will look for the
string to what it expects to be a list of numbers and return `nil`
rather than the mapped value.

This change adds a third criterion to the `define_enum_for` matcher,
verifying that the underlying database column has a `sql_type` of
`"integer"`.

Fix #827.
geoffharcourt added a commit that referenced this issue Nov 2, 2015
If your ActiveRecord model stores its `enum` data in a non-integer
column, ActiveRecord will save the data without error. However, when you
access the attribute on the record after saving, AR will look for the
string to what it expects to be a list of numbers and return `nil`
rather than the mapped value.

This change adds a third criterion to the `define_enum_for` matcher,
verifying that the underlying database column has a `sql_type` of
`"integer"`.

Fix #827.
@mcmire
Copy link
Collaborator

mcmire commented Nov 20, 2015

I'm going to close this issue in favor of the PR (#829).

@mcmire mcmire closed this as completed Nov 20, 2015
mcmire pushed a commit that referenced this issue Jan 11, 2016
If your ActiveRecord model stores its `enum` data in a non-integer
column, ActiveRecord will save the data without error. However, when you
access the attribute on the record after saving, AR will look for the
string to what it expects to be a list of numbers and return `nil`
rather than the mapped value.

This change adds a third criterion to the `define_enum_for` matcher,
verifying that the underlying database column has a `sql_type` of
`"integer"`.

Fix #827.
mcmire pushed a commit that referenced this issue Jan 11, 2016
If your ActiveRecord model stores its `enum` data in a non-integer
column, ActiveRecord will save the data without error. However, when you
access the attribute on the record after saving, AR will look for the
string to what it expects to be a list of numbers and return `nil`
rather than the mapped value.

This change adds a third criterion to the `define_enum_for` matcher,
verifying that the underlying database column has a `sql_type` of
`"integer"`.

Fix #827.
mcmire pushed a commit that referenced this issue Jan 11, 2016
If your ActiveRecord model stores its `enum` data in a non-integer
column, ActiveRecord will save the data without error. However, when you
access the attribute on the record after saving, AR will look for the
string to what it expects to be a list of numbers and return `nil`
rather than the mapped value.

This change adds a third criterion to the `define_enum_for` matcher,
verifying that the underlying database column has a `sql_type` of
`"integer"`.

Fix #827.
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

2 participants