Added tests for validators

This commit is contained in:
J Guthrie 2018-11-05 15:41:35 +00:00
parent 6cde8c9b0c
commit 1e57189366
21 changed files with 169 additions and 32 deletions

View file

@ -3,8 +3,8 @@
# Table name: changeset_comments # Table name: changeset_comments
# #
# id :integer not null, primary key # id :integer not null, primary key
# changeset_id :bigint(8) not null # changeset_id :integer not null
# author_id :bigint(8) not null # author_id :integer not null
# body :text not null # body :text not null
# created_at :datetime not null # created_at :datetime not null
# visible :boolean not null # visible :boolean not null

View file

@ -2,7 +2,7 @@
# #
# Table name: changeset_tags # Table name: changeset_tags
# #
# changeset_id :bigint(8) not null, primary key # changeset_id :integer not null, primary key
# k :string default(""), not null, primary key # k :string default(""), not null, primary key
# v :string default(""), not null # v :string default(""), not null
# #

View file

@ -2,9 +2,9 @@
# #
# Table name: diary_comments # Table name: diary_comments
# #
# id :bigint(8) not null, primary key # id :integer not null, primary key
# diary_entry_id :bigint(8) not null # diary_entry_id :integer not null
# user_id :bigint(8) not null # user_id :integer not null
# body :text not null # body :text not null
# created_at :datetime not null # created_at :datetime not null
# updated_at :datetime not null # updated_at :datetime not null

View file

@ -2,8 +2,8 @@
# #
# Table name: diary_entries # Table name: diary_entries
# #
# id :bigint(8) not null, primary key # id :integer not null, primary key
# user_id :bigint(8) not null # user_id :integer not null
# title :string not null # title :string not null
# body :text not null # body :text not null
# created_at :datetime not null # created_at :datetime not null

View file

@ -2,13 +2,13 @@
# #
# Table name: messages # Table name: messages
# #
# id :bigint(8) not null, primary key # id :integer not null, primary key
# from_user_id :bigint(8) not null # from_user_id :integer not null
# title :string not null # title :string not null
# body :text not null # body :text not null
# sent_on :datetime not null # sent_on :datetime not null
# message_read :boolean default(FALSE), not null # message_read :boolean default(FALSE), not null
# to_user_id :bigint(8) not null # to_user_id :integer not null
# to_user_visible :boolean default(TRUE), not null # to_user_visible :boolean default(TRUE), not null
# from_user_visible :boolean default(TRUE), not null # from_user_visible :boolean default(TRUE), not null
# body_format :enum default("markdown"), not null # body_format :enum default("markdown"), not null

View file

@ -2,7 +2,7 @@
# #
# Table name: current_node_tags # Table name: current_node_tags
# #
# node_id :bigint(8) not null, primary key # node_id :integer not null, primary key
# k :string default(""), not null, primary key # k :string default(""), not null, primary key
# v :string default(""), not null # v :string default(""), not null
# #

View file

@ -2,8 +2,8 @@
# #
# Table name: node_tags # Table name: node_tags
# #
# node_id :bigint(8) not null, primary key # node_id :integer not null, primary key
# version :bigint(8) not null, primary key # version :integer not null, primary key
# k :string default(""), not null, primary key # k :string default(""), not null, primary key
# v :string default(""), not null # v :string default(""), not null
# #

View file

@ -2,10 +2,10 @@
# #
# Table name: relation_tags # Table name: relation_tags
# #
# relation_id :bigint(8) default(0), not null, primary key # relation_id :integer default(0), not null, primary key
# k :string default(""), not null, primary key # k :string default(""), not null, primary key
# v :string default(""), not null # v :string default(""), not null
# version :bigint(8) not null, primary key # version :integer not null, primary key
# #
# Foreign Keys # Foreign Keys
# #

View file

@ -2,10 +2,10 @@
# #
# Table name: way_tags # Table name: way_tags
# #
# way_id :bigint(8) default(0), not null, primary key # way_id :integer default(0), not null, primary key
# k :string not null, primary key # k :string not null, primary key
# v :string not null # v :string not null
# version :bigint(8) not null, primary key # version :integer not null, primary key
# #
# Foreign Keys # Foreign Keys
# #

View file

@ -7,7 +7,7 @@
# description :text # description :text
# created_at :datetime # created_at :datetime
# updated_at :datetime # updated_at :datetime
# user_id :bigint(8) not null # user_id :integer not null
# description_format :enum default("markdown"), not null # description_format :enum default("markdown"), not null
# #
# Foreign Keys # Foreign Keys

View file

@ -2,7 +2,7 @@
# #
# Table name: current_relation_tags # Table name: current_relation_tags
# #
# relation_id :bigint(8) not null, primary key # relation_id :integer not null, primary key
# k :string default(""), not null, primary key # k :string default(""), not null, primary key
# v :string default(""), not null # v :string default(""), not null
# #

View file

@ -2,18 +2,17 @@
# #
# Table name: gpx_files # Table name: gpx_files
# #
# id :bigint(8) not null, primary key # id :integer not null, primary key
# user_id :bigint(8) not null # user_id :integer not null
# visible :boolean default(TRUE), not null # visible :boolean default(TRUE), not null
# name :string default(""), not null # name :string default(""), not null
# size :bigint(8) # size :integer
# latitude :float # latitude :float
# longitude :float # longitude :float
# timestamp :datetime not null # timestamp :datetime not null
# description :string default(""), not null # description :string default(""), not null
# inserted :boolean not null # inserted :boolean not null
# visibility :enum default("public"), not null # visibility :enum default("public"), not null
# length :bigint(8)
# #
# Indexes # Indexes
# #

View file

@ -2,9 +2,9 @@
# #
# Table name: gpx_file_tags # Table name: gpx_file_tags
# #
# gpx_id :bigint(8) default(0), not null # gpx_id :integer default(0), not null
# tag :string not null # tag :string not null
# id :bigint(8) not null, primary key # id :integer not null, primary key
# #
# Indexes # Indexes
# #

View file

@ -3,12 +3,12 @@
# Table name: user_blocks # Table name: user_blocks
# #
# id :integer not null, primary key # id :integer not null, primary key
# user_id :bigint(8) not null # user_id :integer not null
# creator_id :bigint(8) not null # creator_id :integer not null
# reason :text not null # reason :text not null
# ends_at :datetime not null # ends_at :datetime not null
# needs_view :boolean default(FALSE), not null # needs_view :boolean default(FALSE), not null
# revoker_id :bigint(8) # revoker_id :integer
# created_at :datetime # created_at :datetime
# updated_at :datetime # updated_at :datetime
# reason_format :enum default("markdown"), not null # reason_format :enum default("markdown"), not null

View file

@ -2,7 +2,7 @@
# #
# Table name: user_preferences # Table name: user_preferences
# #
# user_id :bigint(8) not null, primary key # user_id :integer not null, primary key
# k :string not null, primary key # k :string not null, primary key
# v :string not null # v :string not null
# #

View file

@ -2,7 +2,7 @@
# #
# Table name: current_way_tags # Table name: current_way_tags
# #
# way_id :bigint(8) not null, primary key # way_id :integer not null, primary key
# k :string default(""), not null, primary key # k :string default(""), not null, primary key
# v :string default(""), not null # v :string default(""), not null
# #

View file

@ -79,7 +79,7 @@ class UserTest < ActiveSupport::TestCase
# expact are allowed # expact are allowed
# However, would they affect the xml planet dumps? # However, would they affect the xml planet dumps?
ok = ["Name", "'me", "he\"", "<hr>", "*ho", "\"help\"@", ok = ["Name", "'me", "he\"", "<hr>", "*ho", "\"help\"@",
"vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲"] "vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲", "space space"]
# These need to be 3 chars in length, otherwise the length test above # These need to be 3 chars in length, otherwise the length test above
# should be used. # should be used.
bad = ["<hr/>", "test@example.com", "s/f", "aa/", "aa;", "aa.", bad = ["<hr/>", "test@example.com", "s/f", "aa/", "aa;", "aa.",

View file

@ -0,0 +1,36 @@
require "test_helper"
class InvalidCharsValidatable
include ActiveModel::Validations
validates :chars, :invalid_chars => true
attr_accessor :chars
end
class InvalidCharsValidatorTest < ActiveSupport::TestCase
include Rails::Dom::Testing::Assertions::SelectorAssertions
def test_with_valid_chars
c = InvalidCharsValidatable.new
valid = ["Name.", "'me", "he\"", "<hr>", "*ho", "\"help\"@",
"vergrößern", "ルシステムにも対応します", "輕觸搖晃的遊戲", "/;.,?%#"]
valid.each do |v|
c.chars = v
assert c.valid?, "'#{v}' should be valid"
end
end
def test_with_invalid_chars
c = InvalidCharsValidatable.new
invalid = ["\x7f<hr/>", "test@example.com\x0e-", "s/\x1ff", "aa/\ufffe",
"aa\x0b-,", "aa?\x08", "/;\uffff.,?", "\x00-も対応します/", "\x0c#ping",
"foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar"]
invalid.each do |v|
c.chars = v
assert_not c.valid?, "'#{v}' should not be valid"
end
end
end

View file

@ -0,0 +1,36 @@
require "test_helper"
class InvalidUrlCharsValidatable
include ActiveModel::Validations
validates :chars, :invalid_url_chars => true
attr_accessor :chars
end
class InvalidUrlCharsValidatorTest < ActiveSupport::TestCase
include Rails::Dom::Testing::Assertions::SelectorAssertions
def test_with_valid_url_chars
c = InvalidUrlCharsValidatable.new
valid = ["\x7f<hr>", "test@examplecom\x0e-", "s\x1ff", "aa\ufffe",
"aa\x0b-", "aa\x08", "\uffff::", "\x00-も対応します", "\x0c*ping",
"foo\x1fbar", "foo\x7fbar", "foo\ufffebar", "foo\uffffbar"]
valid.each do |v|
c.chars = v
assert c.valid?, "'#{v}' should be valid"
end
end
def test_with_invalid_url_chars
c = InvalidUrlCharsValidatable.new
invalid = ["Name.", "you;me", "he\"#", "<hr/>", "50%", "good?",
"vergrößern,deutsche", "ルシステムに;.も対応します", "輕觸搖/晃的遊戲", "/;.,?%#"]
invalid.each do |v|
c.chars = v
assert_not c.valid?, "'#{v}' should not be valid"
end
end
end

View file

@ -0,0 +1,33 @@
require "test_helper"
class LeadingWhitespaceValidatable
include ActiveModel::Validations
validates :string, :leading_whitespace => true
attr_accessor :string
end
class LeadingWhitespaceValidatorTest < ActiveSupport::TestCase
include Rails::Dom::Testing::Assertions::SelectorAssertions
def test_with_leading_whitespace
validator = LeadingWhitespaceValidatable.new
strings = [" ", " test", " ", "\ttest"]
strings.each do |v|
validator.string = v
assert_not validator.valid?, "'#{v}' should not be valid"
end
end
def test_without_leading_whitespace
validator = LeadingWhitespaceValidatable.new
strings = ["test", "test ", "t est", "test\t", ".test", "_test"]
strings.each do |v|
validator.string = v
assert validator.valid?, "'#{v}' should be valid"
end
end
end

View file

@ -0,0 +1,33 @@
require "test_helper"
class TrailingWhitespaceValidatable
include ActiveModel::Validations
validates :string, :trailing_whitespace => true
attr_accessor :string
end
class TrailingWhitespaceValidatorTest < ActiveSupport::TestCase
include Rails::Dom::Testing::Assertions::SelectorAssertions
def test_with_trailing_whitespace
validator = TrailingWhitespaceValidatable.new
strings = [" ", "test ", " ", "test\t", "_test_ "]
strings.each do |v|
validator.string = v
assert_not validator.valid?, "'#{v}' should not be valid"
end
end
def test_without_trailing_whitespace
validator = TrailingWhitespaceValidatable.new
strings = ["test", " test", "tes t", "\ttest", "test.", "test_"]
strings.each do |v|
validator.string = v
assert validator.valid?, "'#{v}' should be valid"
end
end
end