xli-hickey 0.0.1

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,19 @@
1
+ class Topic < ActiveRecord::Base
2
+ belongs_to :owner, :class_name => 'User', :foreign_key => :user_id
3
+ belongs_to :writer, :polymorphic => true
4
+ belongs_to :disscution
5
+ should_bypass_all_callbacks_and_validations
6
+ end
7
+
8
+ class Disscution < ActiveRecord::Base
9
+ has_one :topic
10
+ has_one :speaker, :through => :topic, :source => :writer, :source_type => 'Author'
11
+ should_bypass_all_callbacks_and_validations
12
+ end
13
+
14
+ class DisscutionBelongsToTopic < ActiveRecord::Base
15
+ belongs_to :topic
16
+ has_one :owner, :through => :topic
17
+ has_one :speaker, :through => :topic, :source => :writer, :source_type => 'Author'
18
+ should_bypass_all_callbacks_and_validations
19
+ end
@@ -0,0 +1,9 @@
1
+ class User < ActiveRecord::Base
2
+ has_and_belongs_to_many :countries
3
+ has_many :my_topics, :class_name => 'Topic'
4
+ has_many :projects_members
5
+ has_many :projects, :through => :projects_members
6
+ has_one :address
7
+
8
+ should_bypass_all_callbacks_and_validations
9
+ end
@@ -0,0 +1,9 @@
1
+ class Writer < ActiveRecord::Base
2
+ set_table_name "users"
3
+
4
+ has_many :addresses, :foreign_key => 'user_id'
5
+ has_many :topics, :as => :writer
6
+ has_many :disscutions, :through => :topics
7
+
8
+ should_bypass_all_callbacks_and_validations
9
+ end
data/test/schema.rb ADDED
@@ -0,0 +1,88 @@
1
+ ActiveRecord::Schema.define do
2
+ create_table :simples do |t|
3
+ end
4
+
5
+ create_table "projects", :force => true do |t|
6
+ t.column "identifier", :string
7
+ t.column "description", :text
8
+ t.column "created_at", :datetime
9
+ t.column "updated_at", :datetime
10
+ t.column "created_on", :datetime
11
+ t.column "updated_on", :datetime
12
+ t.column "hidden", :boolean
13
+ end
14
+
15
+ create_table "users", :force => true do |t|
16
+ t.column "login", :string
17
+ t.column "admin", :boolean
18
+ end
19
+
20
+ create_table "topics", :force => true do |t|
21
+ t.column "user_id", :integer
22
+ t.column "writer_id", :integer
23
+ t.column "writer_type", :string
24
+ t.column "title", :string
25
+ t.column "disscution_id", :integer
26
+ end
27
+
28
+ create_table "projects_members", :force => true do |t|
29
+ t.column "user_id", :integer
30
+ t.column "project_id", :integer
31
+ t.column "admin", :boolean
32
+ end
33
+
34
+ create_table "countries_users", :id => false, :force => true do |t|
35
+ t.column "user_id", :integer, :null => false
36
+ t.column "country_id", :integer, :null => false
37
+ end
38
+
39
+ create_table "countries", :force => true do |t|
40
+ t.column "id", :integer
41
+ t.column "name", :string
42
+ end
43
+
44
+ create_table "addresses", :force => true do |t|
45
+ t.column "id", :integer
46
+ t.column "user_id", :integer
47
+ t.column "country_id", :integer
48
+ t.column "location", :string
49
+ end
50
+
51
+ create_table "disscutions", :force => true do |t|
52
+ t.column "id", :integer
53
+ end
54
+
55
+ create_table "disscution_belongs_to_topics", :force => true do |t|
56
+ t.column "id", :integer
57
+ t.column 'topic_id', :integer
58
+ end
59
+
60
+ create_table "property_definitions", :force => true do |t|
61
+ t.column "type", :string, :null => false
62
+ t.column "project_id", :integer
63
+ t.column "name", :string, :default => "", :null => false
64
+ end
65
+
66
+ create_table "enum_values", :force => true do |t|
67
+ t.column "value", :string, :default => "", :null => false
68
+ t.column "property_definition_id", :integer
69
+ t.column "position", :integer
70
+ end
71
+
72
+ create_table "tags", :force => true do |t|
73
+ t.column "name", :string, :default => "", :null => false
74
+ t.column "project_id", :integer, :null => false
75
+ end
76
+
77
+ create_table "taggings", :force => true do |t|
78
+ t.column "tag_id", :integer
79
+ t.column "taggable_id", :integer
80
+ t.column "taggable_type", :string
81
+ end
82
+
83
+ create_table "cards", :force => true do |t|
84
+ t.column "name", :string, :default => "", :null => false
85
+ t.column "project_id", :integer, :null => false
86
+ end
87
+
88
+ end
@@ -0,0 +1,117 @@
1
+ require File.expand_path(File.dirname(__FILE__)) + '/test_helper'
2
+
3
+ class SingleModelTest < Test::Unit::TestCase
4
+ def test_create_simple_model
5
+ Hickey.kiss(:simple => {})
6
+ assert_equal 1, Simple.find(:all).size
7
+ end
8
+
9
+ def test_should_return_created_domain
10
+ result = Hickey.kiss(:simple => {})
11
+ assert_equal Simple.find(:first), result
12
+ end
13
+
14
+ def test_should_return_created_domain
15
+ result = Hickey.kiss(:simple => [{}])
16
+ assert_equal Simple.find(:all), result
17
+ end
18
+
19
+ def test_should_return_created_domain_as_hash_when_kiss_multi_models
20
+ result = Hickey.kiss(:simple => {}, :user => {:login => 'xli'})
21
+ assert_equal({:simple => Simple.find(:first), :user => User.find(:first)}, result)
22
+ end
23
+
24
+ def test_should_bypass_validation_as_default
25
+ Simple.class_eval do
26
+ def validate
27
+ raise 'should bypass validation'
28
+ end
29
+ end
30
+
31
+ Hickey.kiss(:simple => {})
32
+ ensure
33
+ Simple.class_eval do
34
+ def validate
35
+ end
36
+ end
37
+ end
38
+
39
+ def test_create_simple_model_list
40
+ Hickey.kiss(:simple => [{}, {}])
41
+ assert_equal 2, Simple.find(:all).size
42
+ end
43
+
44
+ def test_should_bypass_callbacks
45
+ Simple.class_eval do
46
+ before_save :should_be_bypass
47
+ def should_be_bypass
48
+ raise 'should be bypass'
49
+ end
50
+ end
51
+
52
+ Hickey.kiss(:simple => {})
53
+ ensure
54
+ Simple.class_eval do
55
+ def should_be_bypass
56
+ end
57
+ end
58
+ end
59
+
60
+ def test_should_bypass_notifications
61
+ SimpleObserver.instance
62
+ Hickey.kiss(:simple => {})
63
+ assert_equal 1, Simple.count(:all)
64
+ end
65
+
66
+ def test_create_model_with_attributes
67
+ Hickey.kiss(:user => {:login => 'xli', :admin => true})
68
+ user = User.find_by_login_and_admin('xli', true)
69
+ assert_not_nil user
70
+ end
71
+
72
+ def test_should_auto_set_created_at_and_updated_at_attributes
73
+ Hickey.kiss(:project => {:identifier => 'hickey'})
74
+ assert_not_nil Project.find(:first).created_at
75
+ assert_not_nil Project.find(:first).updated_at
76
+ end
77
+
78
+ def test_should_auto_set_created_on_and_updated_on_attributes
79
+ Hickey.kiss(:project => {:identifier => 'hickey'})
80
+ assert_not_nil Project.find(:first).created_on
81
+ assert_not_nil Project.find(:first).updated_on
82
+ end
83
+
84
+ def test_should_ignore_created_timestamp_attributes_when_they_have_value
85
+ t = Time.parse("1/1/2000")
86
+ Hickey.kiss(:project => {:identifier => 'hickey', :created_on => t, :created_at => t})
87
+
88
+ assert_equal t, project.created_on
89
+ assert_equal t, project.created_at
90
+ end
91
+
92
+ def test_should_update_object_specified
93
+ project = Hickey.kiss(:project => {:identifier => 'hickey'})
94
+ Hickey.kiss project => {:identifier => 'new project identifier'}
95
+ project.reload
96
+
97
+ assert_equal 'new project identifier', project.identifier
98
+ end
99
+
100
+ def test_create_object_associating_with_exist_object
101
+ user = Hickey.kiss(:user => {:login => 'xli', :admin => true})
102
+ Hickey.kiss(:project => {:identifier => 'hickey', :users => [user]})
103
+
104
+ assert_equal 'xli', project.users.first.login
105
+ end
106
+
107
+ def test_should_raise_error_when_create_model_failed_by_sql_error
108
+ assert_raise ActiveRecord::StatementInvalid do
109
+ Hickey.kiss(:property_definition => {}) #type should not be nil
110
+ end
111
+ end
112
+
113
+ def test_should_work_after_redefined_accessor_method_for_column
114
+ Hickey.kiss(:prisoner => {:login => 'xli'})
115
+ assert_equal 'xli', prisoner.login
116
+ end
117
+ end
@@ -0,0 +1,83 @@
1
+ require 'test/unit'
2
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__))
3
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
4
+ require 'hickey'
5
+ TEST_ROOT = File.expand_path(File.dirname(__FILE__))
6
+ ASSETS_ROOT = TEST_ROOT
7
+ FIXTURES_ROOT = TEST_ROOT
8
+ MIGRATIONS_ROOT = TEST_ROOT
9
+ SCHEMA_ROOT = TEST_ROOT
10
+
11
+ require 'active_record'
12
+ require 'active_record/fixtures'
13
+ require 'active_record/test_case'
14
+ require 'database_config'
15
+ require 'growling_test'
16
+
17
+ module Hickey
18
+ module ShouldBypassCallbacksAndValidations
19
+ def self.included(base)
20
+ base.extend(ClassMethods)
21
+ end
22
+
23
+ module ClassMethods
24
+ def should_bypass_all_callbacks_and_validations
25
+ before_save :should_be_bypass
26
+ after_save :should_be_bypass
27
+ end
28
+ end
29
+
30
+ def validate
31
+ should_be_bypass
32
+ end
33
+
34
+ def should_be_bypass
35
+ return if $testing_active_record
36
+ raise 'should be bypass'
37
+ end
38
+ end
39
+ end
40
+
41
+ ActiveRecord::Base.send(:include, Hickey::ShouldBypassCallbacksAndValidations)
42
+
43
+ require 'models/projects_member'
44
+ require 'models/user'
45
+ require 'models/project'
46
+ require 'models/country'
47
+ require 'models/topic'
48
+ require 'models/simple'
49
+ require 'models/address'
50
+ require 'models/author'
51
+ require 'models/writer'
52
+ require 'models/property_definition'
53
+ require 'models/tag'
54
+
55
+ class Test::Unit::TestCase
56
+ self.use_transactional_fixtures = true
57
+
58
+ def method_missing(method, *args)
59
+ if models.include?(method.to_s)
60
+ method.to_s.classify.constantize.find(:first)
61
+ else
62
+ super
63
+ end
64
+ end
65
+
66
+ def with_testing_active_record
67
+ $testing_active_record = true
68
+ yield
69
+ ensure
70
+ $testing_active_record = false
71
+ end
72
+
73
+ def models
74
+ return @models if defined?(@models)
75
+ @models = []
76
+ ObjectSpace.each_object(Class) do |klass|
77
+ if(ActiveRecord::Base > klass)
78
+ @models << klass.name.underscore
79
+ end
80
+ end
81
+ @models
82
+ end
83
+ end
@@ -0,0 +1,56 @@
1
+ require File.expand_path(File.dirname(__FILE__)) + '/test_helper'
2
+
3
+ class WithScopeTest < Test::Unit::TestCase
4
+ def test_should_auto_associate_models_even_they_are_not_related_directly
5
+ Hickey.kiss :project => {
6
+ :identifier => 'hickey', :cards => [
7
+ {
8
+ :name => 'first card',
9
+ :taggings => [{:tag => {:name => 'first_tag'}}],
10
+ }
11
+ ]
12
+ }
13
+ assert_equal ['first_tag'], project.tags.collect(&:name)
14
+ assert_equal ['first card'], project.cards.collect(&:name)
15
+ end
16
+
17
+ def test_should_work_with_find_or_create_actions
18
+ Hickey.kiss :project => {
19
+ :identifier => 'hickey', :cards => [
20
+ {
21
+ :name => 'first card',
22
+ :taggings => [{:tag => {:find_or_create => {:name => 'first_tag'}}}],
23
+ },
24
+ {
25
+ :name => 'dont make me think',
26
+ :taggings => [{:tag => {:find_or_create => {:name => 'first_tag'}}}],
27
+ }
28
+ ]
29
+ }
30
+ assert_equal ['first_tag'], project.tags.collect(&:name)
31
+ assert_equal ['first card', 'dont make me think'].sort, project.cards.collect(&:name).sort
32
+ end
33
+
34
+ def test_should_not_effect_object_out_of_model_scope
35
+ Hickey.kiss :writer => {:login => 'writer', :disscutions => [{:speaker => {:login => 'xli'}}, {:speaker => {:login => 'oo'}}]}
36
+ assert_equal 2, writer.disscutions.collect(&:id).uniq.size
37
+ end
38
+
39
+ def test_should_ignore_keys_that_dont_belong_to_model_in_the_scopes
40
+ project = Hickey.kiss :project => {
41
+ :identifier => 'hickey', :cards => [
42
+ {
43
+ :name => 'first card',
44
+ :taggings => [{:tag => {:find_or_create => {:name => 'first_tag'}}}],
45
+ },
46
+ {
47
+ :name => 'second card',
48
+ :taggings => [{:tag => {:find_or_create => {:name => 'first_tag'}}}],
49
+ }
50
+ ]
51
+ }
52
+ tag = project.cards.first.taggings.first.tag
53
+ assert_equal 'first_tag', tag.read_attribute('name')
54
+ assert_nil tag.read_attribute('card_id')
55
+ end
56
+ end
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: xli-hickey
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Li Xiao
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-08-13 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activerecord
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.1.0
23
+ version:
24
+ description:
25
+ email: iam@li-xiao.com
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files: []
31
+
32
+ files:
33
+ - lib/hickey/acceptor.rb
34
+ - lib/hickey/domain_detector/actions.rb
35
+ - lib/hickey/domain_detector/associations.rb
36
+ - lib/hickey/domain_detector/base.rb
37
+ - lib/hickey/domain_detector/configurable.rb
38
+ - lib/hickey/domain_detector/scopes.rb
39
+ - lib/hickey/domain_detector.rb
40
+ - lib/hickey.rb
41
+ - test/belongs_to_association_test.rb
42
+ - test/database_config.rb
43
+ - test/enable_callbacks_test.rb
44
+ - test/find_or_create_actions_test.rb
45
+ - test/has_and_belongs_to_many_association_test.rb
46
+ - test/has_many_association_test.rb
47
+ - test/has_one_association_test.rb
48
+ - test/model_association_test.rb
49
+ - test/models/address.rb
50
+ - test/models/author.rb
51
+ - test/models/country.rb
52
+ - test/models/project.rb
53
+ - test/models/projects_member.rb
54
+ - test/models/property_definition.rb
55
+ - test/models/simple.rb
56
+ - test/models/tag.rb
57
+ - test/models/topic.rb
58
+ - test/models/user.rb
59
+ - test/models/writer.rb
60
+ - test/schema.rb
61
+ - test/single_model_test.rb
62
+ - test/test_helper.rb
63
+ - test/with_scope_test.rb
64
+ - CHANGES
65
+ - hickey.gemspec
66
+ - LICENSE.TXT
67
+ - Rakefile
68
+ - README
69
+ - TODO
70
+ has_rdoc: false
71
+ homepage: https://github.com/xli/hickey/tree
72
+ post_install_message:
73
+ rdoc_options: []
74
+
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ version:
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: "0"
88
+ version:
89
+ requirements: []
90
+
91
+ rubyforge_project:
92
+ rubygems_version: 1.2.0
93
+ signing_key:
94
+ specification_version: 2
95
+ summary: Hickey provides a simple way of preparing test data inside test for Rails project.
96
+ test_files: []
97
+