test/reform_test.rb in reform-0.1.0 vs test/reform_test.rb in reform-0.1.1
- old
+ new
@@ -1,7 +1,16 @@
require 'test_helper'
+require 'active_record'
+class Artist < ActiveRecord::Base
+end
+ActiveRecord::Base.establish_connection(
+ :adapter => "sqlite3",
+ :database => "#{Dir.pwd}/database.sqlite3"
+)
+
+
class RepresenterTest < MiniTest::Spec
class SongRepresenter < Reform::Representer
properties [:title, :year]
end
@@ -48,12 +57,12 @@
let (:rio) { OpenStruct.new(:title => "Rio") }
let (:form) { SongForm.new(SongAndArtistMap, comp) }
class SongAndArtistMap < Reform::Representer
- property :name, on: :artist
- property :title, on: :song
+ property :name, :on => :artist
+ property :title, :on => :song
end
class SongForm < Reform::Form
end
@@ -150,15 +159,71 @@
it "returns true when valid" do
form.validate("name" => "Duran Duran").must_equal true
end
# TODO: test errors. test valid.
- it "returns false when invalid" do
+ describe "invalid input" do
class ValidatingForm < Reform::Form
- validates :name, :presence => true
+ validates :name, :presence => true
+ validates :title, :presence => true
end
+ let (:form) { ValidatingForm.new(SongAndArtistMap, comp) }
- ValidatingForm.new(SongAndArtistMap, comp).validate({}).must_equal false
+ it "returns false when invalid" do
+ form.validate({}).must_equal false
+ end
+
+ it "populates errors" do
+ form.validate({})
+ form.errors.messages.must_equal({:name=>["can't be blank"], :title=>["can't be blank"]})
+ end
+ end
+
+ describe "method validations" do
+ it "allows accessing models" do
+ form = Class.new(Reform::Form) do
+ validate "name_correct?"
+
+ def name_correct?
+ errors.add :name, "Please give me a name" if model.artist.name.nil?
+ end
+ end.new(SongAndArtistMap, comp)
+
+ form.validate({}).must_equal false
+ form.errors.messages.must_equal({:name=>["Please give me a name"]})
+ end
+ end
+
+ describe "UniquenessValidator" do
+ # ActiveRecord::Schema.define do
+ # create_table :artists do |table|
+ # table.column :name, :string
+ # end
+ # end
+ #Artist.new(:name => "Racer X").save
+ let (:comp) { SongAndArtist.new(:artist => Artist.new, :song => OpenStruct.new) }
+
+ it "allows accessing the database" do
+ end
+
+ it "is valid when name is unique" do
+ ActiveRecordForm.new(SongAndArtistMap, comp).validate({"name" => "Paul Gilbert", "title" => "Godzilla"}).must_equal true
+ end
+
+ it "is invalid and shows error when taken" do
+ form = ActiveRecordForm.new(SongAndArtistMap, comp)
+ form.validate({"name" => "Racer X"}).must_equal false
+ form.errors.messages.must_equal({:name=>["has already been taken"], :title => ["can't be blank"]})
+ end
+
+ require 'reform/rails'
+ class ActiveRecordForm < Reform::Form
+ include Reform::Form::ActiveRecord
+ model :artist, :on => :artist # FIXME: i want form.artist, so move this out of ActiveModel into ModelDelegations.
+
+ validates_uniqueness_of :name
+ validates :title, :presence => true # have another property to test if we mix up.
+ end
end
end
describe "#save" do