sexy_pg_constraints 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/Manifest +4 -0
- data/README.rdoc +6 -0
- data/Rakefile +1 -1
- data/lib/constrainer.rb +1 -1
- data/lib/constraints.rb +80 -55
- data/sexy_pg_constraints.gemspec +3 -3
- data/test/sexy_pg_constraints_test.rb +597 -270
- data/test/support/assert_prohibits_allows.rb +28 -0
- data/test/support/database.yml +6 -0
- data/test/support/database.yml.example +6 -0
- data/test/support/models.rb +36 -0
- data/test/test_helper.rb +6 -1
- metadata +6 -2
@@ -0,0 +1,28 @@
|
|
1
|
+
module AssertProhibitsAllows
|
2
|
+
def assert_prohibits(model, column, constraint, constraint_type = 'check')
|
3
|
+
column = column.join('_') if column.respond_to?(:join)
|
4
|
+
|
5
|
+
book = model.new
|
6
|
+
yield(book)
|
7
|
+
assert book.valid?
|
8
|
+
error = assert_raise ActiveRecord::StatementInvalid do
|
9
|
+
book.save
|
10
|
+
end
|
11
|
+
assert_match /PGError/, error.message
|
12
|
+
assert_match /violates #{constraint_type} constraint/, error.message
|
13
|
+
assert_match /"#{model.table_name}_#{column}_(#{Array(constraint).map {|c| c.to_s }.join('|')})"/, error.message if constraint_type == 'check'
|
14
|
+
end
|
15
|
+
|
16
|
+
def assert_allows(model)
|
17
|
+
first_count = model.count
|
18
|
+
book = model.new
|
19
|
+
yield(book)
|
20
|
+
assert book.valid?
|
21
|
+
assert_nothing_raised do
|
22
|
+
book.save
|
23
|
+
end
|
24
|
+
assert_equal first_count + 1, model.count
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
Test::Unit::TestCase.send(:include, AssertProhibitsAllows)
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# Setting up sample migrations
|
2
|
+
class CreateBooks < ActiveRecord::Migration
|
3
|
+
def self.up
|
4
|
+
create_table :books do |t|
|
5
|
+
t.string :title
|
6
|
+
t.string :author
|
7
|
+
t.integer :author_id
|
8
|
+
t.integer :quantity
|
9
|
+
t.integer :from
|
10
|
+
t.string :isbn
|
11
|
+
t.string :as
|
12
|
+
t.integer :xor_col_1
|
13
|
+
t.integer :xor_col_2
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.down
|
18
|
+
drop_table :books
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class CreateAuthors < ActiveRecord::Migration
|
23
|
+
def self.up
|
24
|
+
create_table :authors do |t|
|
25
|
+
t.string :name
|
26
|
+
t.string :bio
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.down
|
31
|
+
drop_table :authors
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class Book < ActiveRecord::Base; end
|
36
|
+
class Author < ActiveRecord::Base; end
|
data/test/test_helper.rb
CHANGED
@@ -2,4 +2,9 @@ require 'rubygems'
|
|
2
2
|
require 'test/unit'
|
3
3
|
require 'active_record'
|
4
4
|
require 'postgresql_adapter'
|
5
|
-
require "sexy_pg_constraints"
|
5
|
+
require "sexy_pg_constraints"
|
6
|
+
require File.dirname(__FILE__) + '/support/models'
|
7
|
+
require File.dirname(__FILE__) + '/support/assert_prohibits_allows'
|
8
|
+
|
9
|
+
db_config_path = File.join(File.dirname(__FILE__), 'support', 'database.yml')
|
10
|
+
ActiveRecord::Base.establish_connection(YAML::load(open(db_config_path)))
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sexy_pg_constraints
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maxim Chernyak
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2010-02-01 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
@@ -41,6 +41,10 @@ files:
|
|
41
41
|
- sexy_pg_constraints.gemspec
|
42
42
|
- test/postgresql_adapter.rb
|
43
43
|
- test/sexy_pg_constraints_test.rb
|
44
|
+
- test/support/assert_prohibits_allows.rb
|
45
|
+
- test/support/database.yml
|
46
|
+
- test/support/database.yml.example
|
47
|
+
- test/support/models.rb
|
44
48
|
- test/test_helper.rb
|
45
49
|
has_rdoc: true
|
46
50
|
homepage: http://github.com/maxim/sexy_pg_constraints
|