Skip to content

Commit

Permalink
Throw friendly error message when fixture is not a hash
Browse files Browse the repository at this point in the history
Right now, when fixture is not a Hash we throw an error message
saying "fixture is not a hash". This is not very user friendly because
it's not saying which fixture is invalid.
  • Loading branch information
kirs committed Dec 15, 2016
1 parent 753da21 commit 3e018ec
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
7 changes: 5 additions & 2 deletions activerecord/lib/active_record/fixture_set/file.rb
Expand Up @@ -66,10 +66,13 @@ def render(content)
# Validate our unmarshalled data.
def validate(data)
unless Hash === data || YAML::Omap === data
raise Fixture::FormatError, "fixture is not a hash"
raise Fixture::FormatError, "fixture is not a hash: #{@file}"
end

raise Fixture::FormatError unless data.all? { |name, row| Hash === row }
invalid = data.reject { |_, row| Hash === row }
if invalid.any?
raise Fixture::FormatError, "fixture key is not a hash: #{@file}, keys: #{invalid.keys.inspect}"
end
data
end
end
Expand Down
14 changes: 12 additions & 2 deletions activerecord/test/cases/fixtures_test.rb
Expand Up @@ -211,9 +211,19 @@ def test_nonexistent_fixture_file
end

def test_dirty_dirty_yaml_file
assert_raise(ActiveRecord::Fixture::FormatError) do
ActiveRecord::FixtureSet.new(Account.connection, "courses", Course, FIXTURES_ROOT + "/naked/yml/courses")
fixture_path = FIXTURES_ROOT + "/naked/yml/courses"
error = assert_raise(ActiveRecord::Fixture::FormatError) do
ActiveRecord::FixtureSet.new(Account.connection, "courses", Course, fixture_path)
end
assert_equal "fixture is not a hash: #{fixture_path}.yml", error.to_s
end

def test_yaml_file_with_one_invalid_fixture
fixture_path = FIXTURES_ROOT + "/naked/yml/courses_with_invalid_key"
error = assert_raise(ActiveRecord::Fixture::FormatError) do
ActiveRecord::FixtureSet.new(Account.connection, "courses", Course, fixture_path)
end
assert_equal "fixture key is not a hash: #{fixture_path}.yml, keys: [\"two\"]", error.to_s
end

def test_yaml_file_with_invalid_column
Expand Down
@@ -0,0 +1,3 @@
one:
id: 1
two: ['not a hash']

0 comments on commit 3e018ec

Please sign in to comment.