Permalink
Browse files

fixture file will validate fixture format

  • Loading branch information...
1 parent 5278af3 commit 081b36c6ce799f2e4c755bdaf2fa978c9e494567 @tenderlove tenderlove committed May 10, 2011
Showing with 26 additions and 1 deletion.
  1. +8 −1 activerecord/lib/active_record/fixtures/file.rb
  2. +18 −0 activerecord/test/cases/fixtures/file_test.rb
@@ -34,12 +34,19 @@ def rows
return @rows if @rows
data = YAML.load(render(IO.read(@file)))
- @rows = data ? data.to_a : []
+ @rows = data ? validate(data).to_a : []
end
def render(content)
ERB.new(content).result
end
+
+ # Validate our unmarshalled data.
+ def validate(data)
+ raise Fixture::FormatError, 'fixture is not a hash' unless Hash === data
+ raise Fixture::FormatError unless data.all? { |name, row| Hash === row }
+ data
+ end
end
end
end
@@ -48,6 +48,24 @@ def test_empty_file
end
end
+ # A valid YAML file is not necessarily a value Fixture file. Make sure
+ # an exception is raised if the format is not valid Fixture format.
+ def test_wrong_fixture_format_string
+ tmp_yaml ['empty', 'yml'], 'qwerty' do |t|
+ assert_raises(ActiveRecord::Fixture::FormatError) do
+ File.open(t.path) { |fh| fh.to_a }
+ end
+ end
+ end
+
+ def test_wrong_fixture_format_nested
+ tmp_yaml ['empty', 'yml'], 'one: two' do |t|
+ assert_raises(ActiveRecord::Fixture::FormatError) do
+ File.open(t.path) { |fh| fh.to_a }
+ end
+ end
+ end
+
private
def tmp_yaml(name, contents)
t = Tempfile.new name

0 comments on commit 081b36c

Please sign in to comment.