activerecord_constraints 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,93 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright 2007-2011 Ease Software, Inc. and Perry Smith
4
+ # All Rights Reserved
5
+ #
6
+
7
+ # Copyright (c) 2009 Perry Smith
8
+
9
+ # This file is part of activerecord_constraints.
10
+
11
+ # activerecord_constraints is free software: you can redistribute it
12
+ # and/or modify it under the terms of the GNU General Public License as
13
+ # published by the Free Software Foundation, either version 3 of the
14
+ # License, or (at your option) any later version.
15
+
16
+ # activerecord_constraints is distributed in the hope that it will be
17
+ # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
+ # General Public License for more details.
20
+
21
+ # You should have received a copy of the GNU General Public License
22
+ # along with activerecord_constraints. If not, see
23
+ # <http://www.gnu.org/licenses/>.
24
+
25
+ require 'test_helper'
26
+
27
+ # table with unique clauses
28
+ class CreateBars < ActiveRecord::Migration
29
+ def self.up
30
+ create_table :bars do |t|
31
+ t.string :name, :unique => true, :null => false
32
+ end
33
+ end
34
+
35
+ def self.down
36
+ drop_table :bars
37
+ end
38
+ end
39
+
40
+ # The matching model
41
+ class Bar < ActiveRecord::Base
42
+ end
43
+
44
+ class UniqueConstraintsNullTest < ActiveSupport::TestCase
45
+ def setup
46
+ create_db(ActiveRecord::Base, db_config1)
47
+ CreateBars.up
48
+ end
49
+
50
+ def teardown
51
+ CreateBars.down
52
+ end
53
+
54
+ def test_cannot_save_valid_null
55
+ f = Bar.new()
56
+ assert_equal(false, f.save, "Can save model with null name")
57
+ end
58
+
59
+ def test_can_save_valid
60
+ f = Bar.new(:name => "dog")
61
+ assert(f.save, "Can not save valid model with name")
62
+ end
63
+
64
+ def test_cannot_save_duplicate
65
+ f = Bar.new(:name => "myname")
66
+ g = Bar.new(:name => "myname")
67
+ f.save
68
+ assert_equal(false, g.save, "Should not be able to save duplicate")
69
+ end
70
+
71
+ def test_save_bang_throws
72
+ f = Bar.new(:name => "myname")
73
+ g = Bar.new(:name => "myname")
74
+ f.save
75
+ assert_raise(ActiveRecord::RecordNotSaved) do
76
+ g.save!
77
+ end
78
+ end
79
+
80
+ def test_error_message
81
+ f = Bar.new(:name => "myname")
82
+ g = Bar.new(:name => "myname")
83
+ f.save
84
+ g.save
85
+ assert_equal("has already been taken", g.errors.on(:name))
86
+ end
87
+
88
+ def test_null_name_message
89
+ f = Bar.new
90
+ f.save
91
+ assert("can't be blank", f.errors.on(:name))
92
+ end
93
+ end
@@ -0,0 +1,87 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright 2007-2011 Ease Software, Inc. and Perry Smith
4
+ # All Rights Reserved
5
+ #
6
+
7
+ # Copyright (c) 2009 Perry Smith
8
+
9
+ # This file is part of activerecord_constraints.
10
+
11
+ # activerecord_constraints is free software: you can redistribute it
12
+ # and/or modify it under the terms of the GNU General Public License as
13
+ # published by the Free Software Foundation, either version 3 of the
14
+ # License, or (at your option) any later version.
15
+
16
+ # activerecord_constraints is distributed in the hope that it will be
17
+ # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
+ # General Public License for more details.
20
+
21
+ # You should have received a copy of the GNU General Public License
22
+ # along with activerecord_constraints. If not, see
23
+ # <http://www.gnu.org/licenses/>.
24
+
25
+ require 'test_helper'
26
+
27
+ # table with unique clauses
28
+ class CreateFoos < ActiveRecord::Migration
29
+ def self.up
30
+ create_table :foos do |t|
31
+ t.string :name, :unique => true
32
+ end
33
+ end
34
+
35
+ def self.down
36
+ drop_table :foos
37
+ end
38
+ end
39
+
40
+ # The matching models
41
+ class Foo < ActiveRecord::Base
42
+ end
43
+
44
+ class UniqueConstraintsTest < ActiveSupport::TestCase
45
+ def setup
46
+ create_db(ActiveRecord::Base, db_config1)
47
+ CreateFoos.up
48
+ end
49
+
50
+ def teardown
51
+ CreateFoos.down
52
+ end
53
+
54
+ def test_can_save_valid_null
55
+ f = Foo.new()
56
+ assert(f.save, "Can not save valid model with null name")
57
+ end
58
+
59
+ def test_can_save_valid
60
+ f = Foo.new(:name => "dog")
61
+ assert(f.save, "Can not save valid model with name")
62
+ end
63
+
64
+ def test_cannot_save_duplicate
65
+ f = Foo.new(:name => "myname")
66
+ g = Foo.new(:name => "myname")
67
+ f.save
68
+ assert_equal(false, g.save, "Should not be able to save duplicate")
69
+ end
70
+
71
+ def test_save_bang_throws
72
+ f = Foo.new(:name => "myname")
73
+ g = Foo.new(:name => "myname")
74
+ f.save
75
+ assert_raise(ActiveRecord::RecordNotSaved) do
76
+ g.save!
77
+ end
78
+ end
79
+
80
+ def test_error_message
81
+ f = Foo.new(:name => "myname")
82
+ g = Foo.new(:name => "myname")
83
+ f.save
84
+ g.save
85
+ assert_equal("has already been taken", g.errors.on(:name))
86
+ end
87
+ end
@@ -0,0 +1,76 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright 2007-2011 Ease Software, Inc. and Perry Smith
4
+ # All Rights Reserved
5
+ #
6
+
7
+ # Copyright (c) 2009 Perry Smith
8
+
9
+ # This file is part of activerecord_constraints.
10
+
11
+ # activerecord_constraints is free software: you can redistribute it
12
+ # and/or modify it under the terms of the GNU General Public License as
13
+ # published by the Free Software Foundation, either version 3 of the
14
+ # License, or (at your option) any later version.
15
+
16
+ # activerecord_constraints is distributed in the hope that it will be
17
+ # useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
18
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
+ # General Public License for more details.
20
+
21
+ # You should have received a copy of the GNU General Public License
22
+ # along with activerecord_constraints. If not, see
23
+ # <http://www.gnu.org/licenses/>.
24
+ #
25
+ #
26
+ # To get the tests to work, the plug in needs to be in the typical
27
+ # vendor/plugins directory of a working rails project
28
+ #
29
+ ENV["RAILS_ENV"] = "test"
30
+ ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
31
+ require File.expand_path(File.join(ENV["RAILS_ROOT"], "/config/environment"))
32
+ require 'test_help'
33
+
34
+ def db_config1
35
+ configurations = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
36
+ db_adapter = ENV['DB']
37
+ db_adapter ||= 'postgres'
38
+ configurations[db_adapter]
39
+ end
40
+
41
+ def db_config2
42
+ configurations = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
43
+ db_adapter = ENV['DB2']
44
+ if db_adapter.nil? && ENV['DB']
45
+ db_adapter = ENV['DB'] + "2"
46
+ end
47
+ db_adapter ||= 'postgres2'
48
+ configurations[db_adapter]
49
+ end
50
+
51
+ #
52
+ # create_db is called at the start of a test case to create a fresh
53
+ # and empty database.
54
+ #
55
+ def create_db(klass, config)
56
+ klass.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
57
+ case config['adapter']
58
+ when 'postgresql'
59
+ # Connect to postgres database
60
+ klass.establish_connection(config.merge('database' => 'postgres'))
61
+ # Drop old database (error ignored if not already present)
62
+ klass.connection.drop_database(config['database'])
63
+ # Create new fresh database
64
+ klass.connection.create_database(config['database'])
65
+ # Attach to new database
66
+ klass.establish_connection(config)
67
+ end
68
+ end
69
+
70
+ def one_time_setup
71
+ # Stop the printing of the migration messages
72
+ ActiveRecord::Migration.verbose = false
73
+ require File.dirname(__FILE__) + "/../init.rb"
74
+ end
75
+
76
+ one_time_setup
data/uninstall.rb ADDED
@@ -0,0 +1,6 @@
1
+ # -*- coding: utf-8 -*-
2
+ #
3
+ # Copyright 2007-2011 Ease Software, Inc. and Perry Smith
4
+ # All Rights Reserved
5
+ #
6
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_constraints
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Perry Smith
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-19 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: ! 'This plugin currently implements constraints for PostgreSQL only. It
15
+
16
+ should provide a structure for a more abstract implementation.
17
+
18
+
19
+ Currently it implements foreign key constraints, unique
20
+
21
+ constraints and check constraints. null and not null constraints
22
+
23
+ would be easy to add but they may collide with preexisting Active
24
+
25
+ Record code.'
26
+ email: pedzsan@gmail.con
27
+ executables: []
28
+ extensions: []
29
+ extra_rdoc_files: []
30
+ files:
31
+ - GNU-LICENSE
32
+ - README
33
+ - Rakefile
34
+ - init.rb
35
+ - install.rb
36
+ - lib/activerecord_constraint_handlers.rb
37
+ - lib/activerecord_constraints.rb
38
+ - lib/tasks/activerecord_constraints_tasks.rake
39
+ - test/database.yml
40
+ - test/migration/double_connection_test.rb
41
+ - test/migration/unique_constraints_multi_test.rb
42
+ - test/migration/unique_constraints_null_test.rb
43
+ - test/migration/unique_constraints_test.rb
44
+ - test/test_helper.rb
45
+ - uninstall.rb
46
+ homepage: https://github.com/pedz/activerecord_constraints
47
+ licenses: []
48
+ post_install_message:
49
+ rdoc_options: []
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ! '>='
56
+ - !ruby/object:Gem::Version
57
+ version: '0'
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ requirements: []
65
+ rubyforge_project:
66
+ rubygems_version: 1.8.24
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Nice constraint helpers for Rails and PostgreSQL
70
+ test_files: []