sample_models 1.2.6 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,42 @@
1
+ RAILS_ENV = 'test'
2
+ require 'rubygems'
3
+ require 'bundler'
4
+ begin
5
+ Bundler.setup(:default, :test)
6
+ rescue Bundler::BundlerError => e
7
+ $stderr.puts e.message
8
+ exit e.status_code
9
+ end
10
+
11
+ require 'test/setup/schema'
12
+ require 'validates_email_format_of'
13
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
14
+ require 'sample_models'
15
+ require 'test/setup/models'
16
+ require 'test/unit'
17
+
18
+ class SampleModelsTestCase < Test::Unit::TestCase
19
+ def assert_difference(expression, difference = 1, message = nil, &block)
20
+ expressions = Array.wrap expression
21
+
22
+ exps = expressions.map { |e|
23
+ e.respond_to?(:call) ? e : lambda { eval(e, block.binding) }
24
+ }
25
+ before = exps.map { |e| e.call }
26
+
27
+ yield
28
+
29
+ expressions.zip(exps).each_with_index do |(code, e), i|
30
+ error = "#{code.inspect} didn't change by #{difference}"
31
+ error = "#{message}.\n#{error}" if message
32
+ assert_equal(before[i] + difference, e.call, error)
33
+ end
34
+ end
35
+
36
+ def assert_no_difference(expression, message = nil, &block)
37
+ assert_difference expression, 0, message, &block
38
+ end
39
+
40
+ def default_test
41
+ end
42
+ end
@@ -0,0 +1,143 @@
1
+ require File.dirname(__FILE__) + "/../test_helper"
2
+
3
+ class BelongsToTest < SampleModelsTestCase
4
+ def test_associated_with_belongs_to_recipient_by_default
5
+ assert_difference('BlogPost.count') do
6
+ blog_post = BlogPost.sample
7
+ assert blog_post.user
8
+ assert blog_post.user.is_a?(User)
9
+ end
10
+ end
11
+
12
+ def test_sets_a_custom_value_by_association_name
13
+ user = nil
14
+ assert_difference('User.count') do
15
+ user = User.sample
16
+ end
17
+ assert_difference('BlogPost.count') do
18
+ assert_no_difference('User.count') do
19
+ blog_post = BlogPost.sample :user => user
20
+ assert_equal user, blog_post.user
21
+ end
22
+ end
23
+ end
24
+
25
+ def test_sets_a_custom_value_by_column_name
26
+ user = nil
27
+ assert_difference('User.count') do
28
+ user = User.sample
29
+ end
30
+ assert_difference('BlogPost.count') do
31
+ assert_no_difference('User.count') do
32
+ blog_post = BlogPost.sample :user_id => user.id
33
+ assert_equal user, blog_post.user
34
+ end
35
+ end
36
+ end
37
+
38
+ def test_sets_a_custom_nil_value_by_association_name
39
+ assert_difference('Show.count') do
40
+ show = Show.sample :network => nil
41
+ assert_nil show.network
42
+ assert_nil show.network_id
43
+ end
44
+ end
45
+
46
+ def test_sets_a_custom_nil_value_by_column_name
47
+ assert_difference('Show.count') do
48
+ show = Show.sample :network_id => nil
49
+ assert_nil show.network
50
+ assert_nil show.network_id
51
+ end
52
+ end
53
+
54
+ def test_has_no_problem_with_circular_associations
55
+ assert_difference('User.count') do
56
+ assert User.sample.favorite_blog_post.is_a?(BlogPost)
57
+ end
58
+ assert_difference('BlogPost.count') do
59
+ assert BlogPost.sample.user.is_a?(User)
60
+ end
61
+ end
62
+
63
+ def test_allows_creation_of_a_custom_associated_instance_with_a_hash
64
+ assert_difference('Show.count') do
65
+ assert_difference('Network.count') do
66
+ show = Show.sample(
67
+ :name => 'The Daily Show', :network => {:name => 'Comedy Central'}
68
+ )
69
+ assert_equal "The Daily Show", show.name
70
+ assert_equal 'Comedy Central', show.network.name
71
+ end
72
+ end
73
+ end
74
+
75
+ def test_assigns_association_if_a_value_is_passed_in_as_the_first_argument
76
+ user = nil
77
+ assert_difference('User.count') do
78
+ user = User.sample
79
+ end
80
+ assert_difference('BlogPost.count') do
81
+ assert_no_difference('User.count') do
82
+ blog_post = BlogPost.sample(user, :title => 'some title')
83
+ assert_equal user, blog_post.user
84
+ assert_equal 'some title', blog_post.title
85
+ end
86
+ end
87
+ end
88
+
89
+ def test_creates_associated_records_with_shortcuts
90
+ network = nil
91
+ assert_difference('Network.count') do
92
+ network = Network.sample
93
+ end
94
+ assert_difference('Video.count') do
95
+ assert_difference('Show.count') do
96
+ assert_no_difference('Network.count') do
97
+ video = Video.sample(:show => [network, {:name => "Jersey Shore"}])
98
+ assert_equal network, video.show.network
99
+ assert_equal "Jersey Shore", video.show.name
100
+ end
101
+ end
102
+ end
103
+ end
104
+
105
+ def test_unique_belongs_to_should_be_unique_every_time
106
+ video_ids = {}
107
+ 10.times do
108
+ created = VideoTakedownEvent.sample
109
+ assert_nil video_ids[created.video_id]
110
+ video_ids[created.video_id] = true
111
+ end
112
+ end
113
+
114
+ def test_unique_scoped_belongs_to
115
+ video_fav1 = VideoFavorite.sample
116
+ video_fav2 = VideoFavorite.sample :user => video_fav1.user
117
+ assert_equal video_fav1.user, video_fav2.user
118
+ # VideoFavorites validate uniqueness of video_id in scope of user_id. Since
119
+ # these two VideoFavorite instances have the same User, SampleModels should
120
+ # automatically create a new Video for the same User, and then attach the
121
+ # 2nd VideoFavorite to the new Video.
122
+ assert_not_equal video_fav1.video, video_fav2.video
123
+ end
124
+
125
+ def test_belongs_to_assoc_of_same_class
126
+ blog_post = BlogPost.sample
127
+ assert_nil blog_post.merged_into
128
+ end
129
+
130
+ def test_assoc_doesnt_require_creation_just_because_its_presence_is_required
131
+ calendar = Calendar.sample
132
+ assert_no_difference('User.count') do
133
+ blog_post = BlogPost.sample
134
+ end
135
+ end
136
+
137
+ def test_assoc_if_the_record_was_destroyed
138
+ bp1 = BlogPost.sample
139
+ bp1.user.destroy
140
+ bp2 = BlogPost.sample
141
+ assert_equal(User, bp2.user.class)
142
+ end
143
+ end
@@ -0,0 +1,85 @@
1
+ require File.dirname(__FILE__) + "/../test_helper"
2
+
3
+ class ConfigurationTest < SampleModelsTestCase
4
+ def test_model_with_configured_default
5
+ assert_equal 0, Video.sample.view_count
6
+ end
7
+
8
+ def test_model_with_configured_default_assoc
9
+ cat = Category.sample
10
+ assert_nil cat.parent
11
+ end
12
+
13
+ def test_configured_default_assoc_can_be_overridden_by_name
14
+ sports = Category.sample :name => 'Sports'
15
+ soccer = Category.sample :name => 'Soccer', :parent => sports
16
+ assert_equal sports, soccer.parent
17
+ end
18
+
19
+ def test_configured_default_assoc_can_be_overridden_by_id
20
+ sports = Category.sample :name => 'Sports'
21
+ soccer = Category.sample :name => 'Soccer', :parent_id => sports.id
22
+ assert_equal sports, soccer.parent
23
+ end
24
+
25
+ def test_configuration_with_a_bad_field_name_should_raise_NoMethodError
26
+ assert_raises(NoMethodError) do
27
+ SampleModels.configure Category do |category|
28
+ category.foobar.default ''
29
+ end
30
+ end
31
+ end
32
+
33
+ def test_force_unique
34
+ bp1 = BlogPost.sample
35
+ bp2 = BlogPost.sample
36
+ assert_not_equal bp1, bp2
37
+ assert_not_equal bp1.published_at, bp2.published_at
38
+ end
39
+
40
+ def test_force_unique_allows_nil_uniqued_attr_if_the_underlying_model_allows
41
+ bp = BlogPost.sample :published_at => nil
42
+ assert_nil bp.published_at
43
+ end
44
+
45
+ def test_before_save
46
+ assert_difference('Video.count') do
47
+ assert_difference('Episode.count') do
48
+ video1 = Video.sample :episode => {:name => 'The one about the parents'}
49
+ assert video1.show
50
+ assert video1.episode
51
+ assert_equal video1.show, video1.episode.show
52
+ end
53
+ end
54
+ assert_difference('Video.count') do
55
+ assert_difference('Show.count') do
56
+ video2 = Video.sample :show => {:name => 'South Park'}
57
+ assert_equal video2.show, video2.episode.show
58
+ assert_equal 'South Park', video2.show.name
59
+ end
60
+ end
61
+ end
62
+
63
+ def test_before_save_with_only_the_first_argument
64
+ assert_difference('Appointment.count') do
65
+ Appointment.sample
66
+ end
67
+ end
68
+
69
+ def test_validate_uniqueness_with_an_allow_nil_allows_nil_configuration
70
+ User.sample
71
+ user = User.sample
72
+ assert_nil user.external_user
73
+ assert_nil user.external_user_id
74
+ end
75
+
76
+ def test_attr_accessor_can_have_configured_default
77
+ blog_post = BlogPost.sample
78
+ assert_equal('I am an instance attribute', blog_post.instance_attribute)
79
+ end
80
+
81
+ def test_belongs_to_configured_to_set_in_a_before_save
82
+ topic = Topic.sample(:name => 'Comedy')
83
+ assert(topic.parent.root?)
84
+ end
85
+ end
@@ -0,0 +1,46 @@
1
+ require File.dirname(__FILE__) + "/../test_helper"
2
+
3
+ class HasManyThroughTest < SampleModelsTestCase
4
+ def test_standard_instance_assignment
5
+ Tag.destroy_all
6
+ funny = nil
7
+ assert_difference('Tag.count') do
8
+ funny = Tag.sample :tag => 'funny'
9
+ end
10
+ assert_difference('BlogPost.count') do
11
+ assert_no_difference('Tag.count') do
12
+ bp = BlogPost.sample :tags => [funny]
13
+ assert_equal 1, bp.tags.size
14
+ assert_equal 'funny', bp.tags.first.tag
15
+ end
16
+ end
17
+ end
18
+
19
+ def test_hash_assignment
20
+ Tag.destroy_all
21
+ assert_difference('Tag.count') do
22
+ assert_difference('BlogPost.count') do
23
+ bp1 = BlogPost.sample :tags => [{:tag => 'funny'}]
24
+ assert_equal 1, bp1.tags.size
25
+ assert_equal 'funny', bp1.tags.first.tag
26
+ end
27
+ end
28
+ end
29
+
30
+ def test_hash_and_record_assignment
31
+ Tag.destroy_all
32
+ funny = nil
33
+ assert_difference('Tag.count') do
34
+ funny = Tag.sample :tag => 'funny'
35
+ end
36
+ assert_difference('BlogPost.count') do
37
+ assert_difference('Tag.count') do
38
+ bp = BlogPost.sample :tags => [{:tag => 'sad'}, funny]
39
+ assert_equal 2, bp.tags.size
40
+ %w(sad funny).each do |t|
41
+ assert bp.tags.map(&:tag).include?(t)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + "/../test_helper"
2
+
3
+ class NamedSampleTest < SampleModelsTestCase
4
+ def test_named_sample_fills_default_fields
5
+ bp = BlogPost.sample :funny
6
+ assert_equal 'Funny haha', bp.title
7
+ assert_equal 3.0, bp.average_rating
8
+ end
9
+
10
+ def test_lets_you_override_fields
11
+ bp = BlogPost.sample :funny, :average_rating => 4.0
12
+ assert_equal 'Funny haha', bp.title
13
+ assert_equal 4.0, bp.average_rating
14
+ end
15
+ end
@@ -0,0 +1,29 @@
1
+ require File.dirname(__FILE__) + "/../test_helper"
2
+
3
+ class PolymorphicBelongsToTest < SampleModelsTestCase
4
+ def test_fills_association_with_anything_from_another_class
5
+ bookmark = Bookmark.sample
6
+ assert_not_nil bookmark.bookmarkable
7
+ assert_equal ActiveRecord::Base, bookmark.bookmarkable.class.superclass
8
+ assert_not_equal Bookmark, bookmark.bookmarkable.class
9
+ end
10
+
11
+ def test_can_specify_association
12
+ blog_post = BlogPost.sample
13
+ bookmark = Bookmark.sample :bookmarkable => blog_post
14
+ assert_equal blog_post, bookmark.bookmarkable
15
+ end
16
+
17
+ def test_can_specify_association_with_other_leading_associations
18
+ user = User.sample
19
+ blog_post = BlogPost.sample
20
+ sub = Subscription.sample user, :subscribable => blog_post
21
+ assert_equal user, sub.user
22
+ assert_equal blog_post, sub.subscribable
23
+ end
24
+
25
+ def test_can_set_default_assoc_value_type
26
+ sub = Subscription.sample
27
+ assert_equal 'BlogPost', sub.subscribable_type
28
+ end
29
+ end
@@ -0,0 +1,134 @@
1
+ require File.dirname(__FILE__) + "/../test_helper"
2
+
3
+ class SampleTest < SampleModelsTestCase
4
+ def test_fills_non_validated_non_configured_fields_with_a_non_blank_value
5
+ assert_difference('Appointment.count') do
6
+ appt = Appointment.sample
7
+ assert appt.end_time.is_a?(Time)
8
+ end
9
+ bp_count_before = BlogPost.count
10
+ blog_post = BlogPost.sample
11
+ assert blog_post.average_rating.is_a?(Float)
12
+ assert(BlogPost.count > bp_count_before)
13
+ assert_not_nil(blog_post.body)
14
+ assert_difference('Show.count') do
15
+ show = Show.sample
16
+ assert show.subscription_price.is_a?(Integer)
17
+ end
18
+ assert_difference('Network.count') do
19
+ network = Network.sample
20
+ assert network.name.is_a?(String)
21
+ end
22
+ end
23
+
24
+ def test_allows_overrides_of_all_fields
25
+ assert_difference('User.count') do
26
+ user = User.sample(
27
+ :homepage => 'http://mysite.com/', :password => 'myownpassword'
28
+ )
29
+ assert_equal 'http://mysite.com/', user.homepage
30
+ assert_equal 'myownpassword', user.password
31
+ end
32
+ end
33
+
34
+ def test_picks_a_value_given_in_a_validates_inclusion_of
35
+ assert_difference('User.count') do
36
+ user = User.sample
37
+ assert(%(m f).include?(user.gender))
38
+ end
39
+ end
40
+
41
+ def test_cant_override_validations
42
+ assert_no_difference('User.count') do
43
+ assert_raise(ActiveRecord::RecordInvalid) do
44
+ User.sample(:gender => 'x')
45
+ end
46
+ end
47
+ assert_no_difference('User.count') do
48
+ assert_raise(ActiveRecord::RecordInvalid) do
49
+ User.sample(:email => 'call.me')
50
+ end
51
+ end
52
+ end
53
+
54
+ def test_set_emails
55
+ assert_difference('User.count') do
56
+ user = User.sample
57
+ assert_match /^.*@.*\..*/, user.email
58
+ end
59
+ end
60
+
61
+ def test_doesnt_override_a_db_default
62
+ assert_difference('Comment.count') do
63
+ assert !Comment.sample.flagged_as_spam
64
+ end
65
+ end
66
+
67
+ def test_returns_a_new_instance_with_every_sample_call
68
+ assert_difference('User.count', 2) do
69
+ assert(User.sample != User.sample)
70
+ end
71
+ end
72
+
73
+ def test_unique_string_attribute
74
+ logins = []
75
+ 10.times do
76
+ custom = User.sample
77
+ assert !logins.include?(custom.login)
78
+ logins << custom.login
79
+ end
80
+ end
81
+
82
+ def test_unique_time_attribute
83
+ times = {}
84
+ 10.times do
85
+ custom = Appointment.sample
86
+ assert times[custom.start_time].nil?
87
+ times[custom.start_time] = true
88
+ end
89
+ end
90
+
91
+ def test_unique_email_attribute
92
+ emails = {}
93
+ 10.times do
94
+ custom = User.sample
95
+ assert emails[custom.email].nil?
96
+ emails[custom.email] = true
97
+ end
98
+ end
99
+
100
+ def test_required_date_field
101
+ episode = Episode.sample
102
+ assert episode.original_air_date.is_a?(Date)
103
+ end
104
+
105
+ def test_required_accessor
106
+ user_with_password = UserWithPassword.sample
107
+ assert user_with_password.password.present?
108
+ end
109
+
110
+ def test_string_which_is_required_to_be_present_and_unique
111
+ # Ensuring that it doesn't get tripped up by a pre-existing record
112
+ User2.create!(:login => 'login 1')
113
+ User2.sample
114
+ end
115
+
116
+ def test_create_sample_works_for_now
117
+ assert_difference('Appointment.count') do
118
+ appt = Appointment.create_sample
119
+ end
120
+ end
121
+
122
+ def test_doesnt_mess_with_created_at_or_updated_at
123
+ blog_post = BlogPost.sample
124
+ assert_in_delta(Time.now.utc, blog_post.created_at, 5)
125
+ assert_in_delta(Time.now.utc, blog_post.updated_at, 5)
126
+ end
127
+
128
+ def test_dates_and_times_start_from_now_and_sequence_down
129
+ blog_post = BlogPost.sample
130
+ assert(Time.now.utc.advance(:years => -1) < blog_post.published_at)
131
+ episode = Episode.sample
132
+ assert(Date.today - 365 < episode.original_air_date)
133
+ end
134
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sample_models
3
3
  version: !ruby/object:Gem::Version
4
- hash: 19
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
- - 1
8
7
  - 2
9
- - 6
10
- version: 1.2.6
8
+ - 0
9
+ - 0
10
+ version: 2.0.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Francis Hwang
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-11-18 00:00:00 -05:00
18
+ date: 2011-11-22 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -104,28 +104,24 @@ files:
104
104
  - init.rb
105
105
  - install.rb
106
106
  - lib/sample_models.rb
107
+ - lib/sample_models/attribute_sequence.rb
107
108
  - lib/sample_models/creation.rb
108
- - lib/sample_models/finder.rb
109
+ - lib/sample_models/initializer.rb
109
110
  - lib/sample_models/model.rb
110
111
  - lib/sample_models/sampler.rb
111
112
  - sample_models.gemspec
112
- - spec/sample_models_spec.rb
113
- - spec_or_test/database.yml
114
- - spec_or_test/setup.rb
115
- - spec_or_test/specs_or_test_cases.rb
116
113
  - tasks/sample_models_tasks.rake
117
114
  - test/db/placeholder
118
- - test/test_sample_models.rb
115
+ - test/setup/models.rb
116
+ - test/setup/schema.rb
117
+ - test/test_helper.rb
118
+ - test/unit/belongs_to_test.rb
119
+ - test/unit/configuration_test.rb
120
+ - test/unit/has_many_through_test.rb
121
+ - test/unit/named_sample_test.rb
122
+ - test/unit/polymorphic_belongs_to_test.rb
123
+ - test/unit/sample_test.rb
119
124
  - uninstall.rb
120
- - vendor/ar_query/MIT-LICENSE
121
- - vendor/ar_query/README
122
- - vendor/ar_query/ar_query.gemspec
123
- - vendor/ar_query/init.rb
124
- - vendor/ar_query/install.rb
125
- - vendor/ar_query/lib/ar_query.rb
126
- - vendor/ar_query/spec/ar_query_spec.rb
127
- - vendor/ar_query/tasks/ar_query_tasks.rake
128
- - vendor/ar_query/uninstall.rb
129
125
  has_rdoc: true
130
126
  homepage: http://github.com/fhwang/sample_models
131
127
  licenses:
@@ -1,86 +0,0 @@
1
- module SampleModels
2
- class Finder
3
- def initialize(model, attrs)
4
- @model = model
5
- attrs = Sampler.reify_association_hashes @model, attrs.clone
6
- @attrs = SampleModels.hash_with_indifferent_access_class.new attrs
7
- @ar_query = ARQuery.new
8
- end
9
-
10
- def add_empty_has_many_subselect(assoc)
11
- value = @attrs[assoc.name]
12
- not_matching_subselect = @model.construct_finder_sql(
13
- :select => "#{@model.table_name}.id", :joins => assoc.name,
14
- :group => "#{@model.table_name}.id"
15
- )
16
- @ar_query.condition_sqls << "id not in (#{not_matching_subselect})"
17
- end
18
-
19
- def add_non_empty_has_many_subselect(assoc)
20
- @ar_query.condition_sqls << has_many_matching_subselect(assoc)
21
- value = @attrs[assoc.name]
22
- not_matching_subselect = @model.construct_finder_sql(
23
- :select => "#{@model.table_name}.id", :joins => assoc.name,
24
- :conditions => [
25
- "#{assoc.klass.table_name}.id not in (?)", value.map(&:id)
26
- ],
27
- :group => "#{@model.table_name}.id"
28
- )
29
- @ar_query.condition_sqls << "id not in (#{not_matching_subselect})"
30
- end
31
-
32
- def attach_belongs_to_associations_to_query
33
- @model.belongs_to_associations.each do |assoc|
34
- foreign_key = if assoc.respond_to?(:foreign_key)
35
- assoc.foreign_key
36
- else
37
- assoc.primary_key_name
38
- end
39
- if @attrs.keys.include?(assoc.name.to_s)
40
- @ar_query.conditions[foreign_key] = if @attrs[assoc.name]
41
- @attrs[assoc.name].id
42
- else
43
- @attrs[assoc.name]
44
- end
45
- end
46
- end
47
- end
48
-
49
- def attach_non_associated_attrs_to_query
50
- @attrs.each do |k,v|
51
- if @model.column_names.include?(k.to_s)
52
- @ar_query.conditions[k] = v
53
- end
54
- end
55
- end
56
-
57
- def has_many_matching_subselect(assoc)
58
- value = @attrs[assoc.name]
59
- matching_inner_subselect = @model.construct_finder_sql(
60
- :select =>
61
- "#{@model.table_name}.id, count(#{assoc.klass.table_name}.id) as count",
62
- :joins => assoc.name,
63
- :conditions => [
64
- "#{assoc.klass.table_name}.id in (?)", value.map(&:id)
65
- ],
66
- :group => "#{@model.table_name}.id"
67
- )
68
- "id in (select matching.id from (#{matching_inner_subselect}) as matching where matching.count = #{value.size})"
69
- end
70
-
71
- def instance
72
- attach_non_associated_attrs_to_query
73
- attach_belongs_to_associations_to_query
74
- @model.has_many_associations.each do |assoc|
75
- if @attrs.keys.include?(assoc.name.to_s)
76
- if @attrs[assoc.name].empty?
77
- add_empty_has_many_subselect assoc
78
- else
79
- add_non_empty_has_many_subselect assoc
80
- end
81
- end
82
- end
83
- @model.first @ar_query.to_hash
84
- end
85
- end
86
- end
@@ -1,11 +0,0 @@
1
- require File.dirname(__FILE__) + "/../spec_or_test/setup"
2
- require 'test/unit/assertions'
3
-
4
- class RSpec::Core::ExampleGroup
5
- include Test::Unit::Assertions
6
- end
7
-
8
- initialize_db
9
-
10
- require File.dirname(__FILE__) + "/../spec_or_test/specs_or_test_cases"
11
-
@@ -1,6 +0,0 @@
1
- mysql:
2
- adapter: sqlite3
3
- database: test/db/test.sqlite3
4
- pool: 5
5
- timeout: 5000
6
-