<commit_before>module SimplyTestable::Associations

	def self.included(base)
		base.extend ClassMethods
#		base.send(:include,InstanceMethods)
	end

	module ClassMethods

		def assert_should_initially_belong_to(*associations)
			user_options = associations.extract_options!
			model = user_options[:model] || st_model_name
			
			associations.each do |assoc|
				class_name = ( assoc = assoc.to_s ).camelize

				title = "#{brand}should initially belong to #{assoc}"
				if !user_options[:class_name].blank?
					title << " ( #{user_options[:class_name]} )"
					class_name = user_options[:class_name].to_s
				end
				test title do
					object = create_object
					assert_not_nil object.send(assoc)
					if object.send(assoc).respond_to?(
						"#{model.underscore.pluralize}_count")
						assert_equal 1, object.reload.send(assoc).send(
							"#{model.underscore.pluralize}_count")
					end
					if !user_options[:class_name].blank?
						assert object.send(assoc).is_a?(class_name.constantize)
					end
				end

			end

		end

		def assert_should_belong_to(*associations)
			user_options = associations.extract_options!
			model = user_options[:model] || st_model_name
			
			associations.each do |assoc|
				class_name = ( assoc = assoc.to_s ).camelize
				title = "#{brand}should belong to #{assoc}" 
#				if !user_options[:as].blank?
#					title << " as #{user_options[:as]}"
#					as = user_options[:as]
#				end
				if !user_options[:class_name].blank?
					title << " ( #{user_options[:class_name]} )"
					class_name = user_options[:class_name].to_s
				end
				test title do
					object = create_object
					assert_nil object.send(assoc)
					object.send("#{assoc}=",Factory(class_name.underscore))
					assert_not_nil object.send(assoc)
					assert object.send(assoc).is_a?(class_name.constantize)
				end

			end

		end

		def assert_should_have_one(*associations)
			user_options = associations.extract_options!
			model = user_options[:model] || st_model_name
			
			foreign_key = if !user_options[:foreign_key].blank?
				user_options[:foreign_key].to_sym
			else
				"#{model.underscore}_id".to_sym
			end

			associations.each do |assoc|
				assoc = assoc.to_s

				test "#{brand}should have one #{assoc}" do
					object = create_object
					assert_nil object.send(assoc)
					Factory(assoc, foreign_key => object.id)
					assert_not_nil object.reload.send(assoc)
					object.send(assoc).destroy
					assert_nil object.reload.send(assoc)
				end

			end

		end

		def assert_should_have_many_(*associations)
			user_options = associations.extract_options!
			model = user_options[:model] || st_model_name

			foreign_key = if !user_options[:foreign_key].blank?
				user_options[:foreign_key].to_sym
			else
				"#{model.underscore}_id".to_sym
			end

			associations.each do |assoc|
				class_name = ( assoc = assoc.to_s ).camelize

				title = "#{brand}should have many #{assoc}"
				if !user_options[:class_name].blank?
					title << " ( #{user_options[:class_name]} )"
					class_name = user_options[:class_name].to_s
				end
				test title do
					object = create_object
					assert_equal 0, object.send(assoc).length
					Factory(class_name.singularize.underscore, 
						foreign_key => object.id)
#	doesn't work for all
#object.send(assoc) << Factory(assoc.singularize)
					assert_equal 1, object.reload.send(assoc).length
					if object.respond_to?("#{assoc}_count")
						assert_equal 1, object.reload.send("#{assoc}_count")
					end
					Factory(class_name.singularize.underscore, 
						foreign_key => object.id)
					assert_equal 2, object.reload.send(assoc).length
					if object.respond_to?("#{assoc}_count")
						assert_equal 2, object.reload.send("#{assoc}_count")
					end
				end

			end

		end
		alias_method :assert_should_have_many, 
			:assert_should_have_many_
		alias_method :assert_should_have_many_associations, 
			:assert_should_have_many_

		def assert_should_habtm(*associations)
			user_options = associations.extract_options!
			model = user_options[:model] || st_model_name
			
			associations.each do |assoc|
				assoc = assoc.to_s

				test "#{brand}should habtm #{assoc}" do
					object = create_object
					assert_equal 0, object.send(assoc).length
					object.send(assoc) << Factory(assoc.singularize)
					assert_equal 1, object.reload.send(assoc).length
					if object.respond_to?("#{assoc}_count")
						assert_equal 1, object.reload.send("#{assoc}_count")
					end
					object.send(assoc) << Factory(assoc.singularize)
					assert_equal 2, object.reload.send(assoc).length
					if object.respond_to?("#{assoc}_count")
						assert_equal 2, object.reload.send("#{assoc}_count")
					end
				end

			end

		end

		def assert_requires_valid_associations(*associations)
			user_options = associations.extract_options!
			model = user_options[:model] || st_model_name

			associations.each do |assoc|
				as = assoc = assoc.to_s
				as = user_options[:as] if !user_options[:as].blank?

				test "#{brand}should require foreign key #{as}_id" do
					assert_difference("#{model}.count",0) do
						object = create_object("#{as}_id".to_sym => nil)
						assert object.errors.on(as.to_sym)<commit_msg>Trying to get a few more options working.<commit_after>module SimplyTestable::Associations

	def self.included(base)
		base.extend ClassMethods
#		base.send(:include,InstanceMethods)
	end

	module ClassMethods

		def assert_should_initially_belong_to(*associations)
			user_options = associations.extract_options!
			model = user_options[:model] || st_model_name
			
			associations.each do |assoc|
				class_name = ( assoc = assoc.to_s ).camelize

				title = "#{brand}should initially belong to #{assoc}"
				if !user_options[:class_name].blank?
					title << " ( #{user_options[:class_name]} )"
					class_name = user_options[:class_name].to_s
				end
				test title do
					object = create_object
					assert_not_nil object.send(assoc)
					if object.send(assoc).respond_to?(
						"#{model.underscore.pluralize}_count")
						assert_equal 1, object.reload.send(assoc).send(
							"#{model.underscore.pluralize}_count")
					end
					if !user_options[:class_name].blank?
						assert object.send(assoc).is_a?(class_name.constantize)
					end
				end

			end

		end

		def assert_should_belong_to(*associations)
			user_options = associations.extract_options!
			model = user_options[:model] || st_model_name
			
			associations.each do |assoc|
				class_name = ( assoc = assoc.to_s ).camelize
				title = "#{brand}should belong to #{assoc}" 
#				if !user_options[:as].blank?
#					title << " as #{user_options[:as]}"
#					as = user_options[:as]
#				end
				if !user_options[:class_name].blank?
					title << " ( #{user_options[:class_name]} )"
					class_name = user_options[:class_name].to_s
				end
				test title do
					object = create_object
					assert_nil object.send(assoc)
#					object.send("#{assoc}=",Factory(class_name.underscore))
					object.send("#{assoc}=",send("create_#{class_name.underscore}"))
					assert_not_nil object.send(assoc)
					assert object.send(assoc).is_a?(class_name.constantize)
				end

			end

		end

		def assert_should_have_one(*associations)
			user_options = associations.extract_options!
			model = user_options[:model] || st_model_name
			
			foreign_key = if !user_options[:foreign_key].blank?
				user_options[:foreign_key].to_sym
			else
				"#{model.underscore}_id".to_sym
			end

			associations.each do |assoc|
				assoc = assoc.to_s

				test "#{brand}should have one #{assoc}" do
					object = create_object
					assert_nil object.send(assoc)
#					Factory(assoc, foreign_key => object.id)
					send("create_#{assoc}", foreign_key => object.id)
					assert_not_nil object.reload.send(assoc)
					object.send(assoc).destroy
					assert_nil object.reload.send(assoc)
				end

			end

		end

		def assert_should_have_many_(*associations)
			user_options = associations.extract_options!
			model = user_options[:model] || st_model_name

			foreign_key = if !user_options[:foreign_key].blank?
				user_options[:foreign_key].to_sym
			else
				"#{model.underscore}_id".to_sym
			end

			associations.each do |assoc|
				class_name = ( assoc = assoc.to_s ).camelize

				title = "#{brand}should have many #{assoc}"
				if !user_options[:class_name].blank?
					title << " ( #{user_options[:class_name]} )"
					class_name = user_options[:class_name].to_s
				end
				test title do
					object = create_object
					assert_equal 0, object.send(assoc).length
#					Factory(class_name.singularize.underscore, foreign_key => object.id)
					send("create_#{class_name.singularize.underscore}", foreign_key => object.id)
#	doesn't work for all
#object.send(assoc) << Factory(assoc.singularize)
					assert_equal 1, object.reload.send(assoc).length
					if object.respond_to?("#{assoc}_count")
						assert_equal 1, object.reload.send("#{assoc}_count")
					end
#					Factory(class_name.singularize.underscore, foreign_key => object.id)
					send("create_#{class_name.singularize.underscore}", foreign_key => object.id)
					assert_equal 2, object.reload.send(assoc).length
					if object.respond_to?("#{assoc}_count")
						assert_equal 2, object.reload.send("#{assoc}_count")
					end
				end

			end

		end
		alias_method :assert_should_have_many, 
			:assert_should_have_many_
		alias_method :assert_should_have_many_associations, 
			:assert_should_have_many_

		def assert_should_habtm(*associations)
			user_options = associations.extract_options!
			model = user_options[:model] || st_model_name
			
			associations.each do |assoc|
				assoc = assoc.to_s

				test "#{brand}should habtm #{assoc}" do
					object = create_object
					assert_equal 0, object.send(assoc).length
#					object.send(assoc) << Factory(assoc.singularize)
					object.send(assoc) << send("create_#{assoc.singularize}")
					assert_equal 1, object.reload.send(assoc).length
					if object.respond_to?("#{assoc}_count")
						assert_equal 1, object.reload.send("#{assoc}_count")
					end
#					object.send(assoc) << Factory(assoc.singularize)
					object.send(assoc) << send("create_#{assoc.singularize}")
					assert_equal 2, object.reload.send(assoc).length
					if object.respond_to?("#{assoc}_count")
						assert_equal 2, object.reload.send("#{assoc}_count")
					end
				end

			end

		end

		def assert_requires_valid_associations(*associations)
			user_options = associations.extract_options!
			model = user_options[:model] || st_model_name

			associations.each do |assoc|
				as = assoc = assoc.to_s
				as = user_options[:as] if !user_options[:as].blank?

				test "#{brand}should require foreign key #{as}_id" do
					assert_difference("#{model}.count",0) do
						object = create_object("#{as}_id".to_sym => nil)
						assert object.errors.on(as.to_sym)
{'fraction_non_alphanumeric': 0.12042810609585854, 'fraction_numerical': 0.0022335970218706376, 'mean_word_length': 3.7696404793608522, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4920368', 'n_tokens_mistral': 4548, 'n_tokens_neox': 3570, 'n_words': 634}