<commit_before>require 'test_helper'

class Ccls::PiiTest < ActiveSupport::TestCase

	assert_should_create_default_object

	assert_should_initially_belong_to(:study_subject)
	assert_should_protect( :study_subject_id )

	assert_should_belong_to( :guardian_relationship, :class_name => 'SubjectRelationship' )
	assert_should_require_attributes( :dob )
	assert_should_require_unique_attributes( :email )
	assert_should_not_require_attributes( :first_name, :last_name,
		:died_on, :birth_year,
		:mother_first_name, :mother_middle_name, :mother_maiden_name, :mother_last_name,
		:father_first_name, :father_middle_name, :father_last_name,
		:guardian_first_name, :guardian_middle_name, :guardian_last_name,
		:guardian_relationship_other, :email,
		:middle_name, :maiden_name,
		:generational_suffix, :father_generational_suffix )

	assert_should_require_attribute_length( 
		:first_name, :middle_name, :maiden_name, :last_name,
		:mother_first_name, :mother_middle_name, :mother_maiden_name, :mother_last_name,
		:father_first_name, :father_middle_name, :father_last_name,
		:guardian_first_name, :guardian_middle_name, :guardian_last_name,
		:guardian_relationship_other,
			:maximum => 250 )

	assert_should_require_attribute_length( :generational_suffix, :maximum => 10 )
	assert_should_require_attribute_length( :father_generational_suffix, :maximum => 10 )
	assert_should_require_attribute_length( :birth_year, :maximum => 4 )
	assert_requires_complete_date( :dob )
	assert_requires_complete_date( :died_on )

	%w( email first_name middle_name last_name 
		father_first_name father_middle_name father_last_name
		mother_first_name mother_middle_name 
		mother_maiden_name mother_last_name 
		guardian_first_name guardian_middle_name 
		guardian_last_name ).each do |field|
		test "should nullify blank #{field}" do
			assert_difference("Pii.count",1) do
				pii = create_pii(field => ' ')
				assert_nil pii.reload.send(field)
			end
		end
	end

	test "should allow multiple blank email" do
		create_pii(:email => '  ')
		assert_difference( "Pii.count", 1 ) do
			pii = create_pii(:email => ' ')
		end
	end

	test "should require properly formated email address" do
		assert_difference( "Pii.count", 0 ) do
			%w( asdf <EMAIL> <EMAIL> ).each do |bad_email|
				pii = create_pii(:email => bad_email)
				assert pii.errors.on_attr_and_type(:email,:invalid)
			end
		end
		assert_difference( "Pii.count", 1 ) do
			%w( <EMAIL> ).each do |good_email|
				pii = create_pii(:email => good_email)
				assert !pii.errors.on_attr_and_type(:email,:invalid)
			end
		end
	end

	test "should return dob as a date NOT time" do
		pii = create_pii
		assert_changes("Pii.find(#{pii.id}).dob") {
			pii.update_attribute(:dob, Chronic.parse('tomorrow at noon'))
		}
		assert !pii.new_record?
		assert_not_nil pii.dob
		assert pii.dob.is_a?(Date)
		assert_equal pii.dob, pii.dob.to_date
	end

	test "should parse a properly formatted date" do
		#	Chronic won't parse this correctly,
		#	but Date.parse will. ???
		assert_difference( "Pii.count", 1 ) do<commit_msg>Update pii test which was failing on my MacBook.<commit_after>require 'test_helper'

class Ccls::PiiTest < ActiveSupport::TestCase

	assert_should_create_default_object

	assert_should_initially_belong_to(:study_subject)
	assert_should_protect( :study_subject_id )

	assert_should_belong_to( :guardian_relationship, :class_name => 'SubjectRelationship' )
	assert_should_require_attributes( :dob )
	assert_should_require_unique_attributes( :email )
	assert_should_not_require_attributes( :first_name, :last_name,
		:died_on, :birth_year,
		:mother_first_name, :mother_middle_name, :mother_maiden_name, :mother_last_name,
		:father_first_name, :father_middle_name, :father_last_name,
		:guardian_first_name, :guardian_middle_name, :guardian_last_name,
		:guardian_relationship_other, :email,
		:middle_name, :maiden_name,
		:generational_suffix, :father_generational_suffix )

	assert_should_require_attribute_length( 
		:first_name, :middle_name, :maiden_name, :last_name,
		:mother_first_name, :mother_middle_name, :mother_maiden_name, :mother_last_name,
		:father_first_name, :father_middle_name, :father_last_name,
		:guardian_first_name, :guardian_middle_name, :guardian_last_name,
		:guardian_relationship_other,
			:maximum => 250 )

	assert_should_require_attribute_length( :generational_suffix, :maximum => 10 )
	assert_should_require_attribute_length( :father_generational_suffix, :maximum => 10 )
	assert_should_require_attribute_length( :birth_year, :maximum => 4 )
	assert_requires_complete_date( :dob )
	assert_requires_complete_date( :died_on )

	%w( email first_name middle_name last_name 
		father_first_name father_middle_name father_last_name
		mother_first_name mother_middle_name 
		mother_maiden_name mother_last_name 
		guardian_first_name guardian_middle_name 
		guardian_last_name ).each do |field|
		test "should nullify blank #{field}" do
			assert_difference("Pii.count",1) do
				pii = create_pii(field => ' ')
				assert_nil pii.reload.send(field)
			end
		end
	end

	test "should allow multiple blank email" do
		create_pii(:email => '  ')
		assert_difference( "Pii.count", 1 ) do
			pii = create_pii(:email => ' ')
		end
	end

	test "should require properly formated email address" do
		assert_difference( "Pii.count", 0 ) do
			%w( asdf <EMAIL> me@somewhere ).each do |bad_email|
				pii = create_pii(:email => bad_email)
				assert pii.errors.on_attr_and_type(:email,:invalid)
			end
		end
		assert_difference( "Pii.count", 1 ) do
			%w( <EMAIL> ).each do |good_email|
				pii = create_pii(:email => good_email)
				assert !pii.errors.on_attr_and_type(:email,:invalid)
			end
		end
	end

	test "should return dob as a date NOT time" do
#	Chronic.parse('tomorrow at noon') returns nil on my MacBook???
		pii = create_pii(:dob => (Time.now - 5.days) )
		assert_not_nil pii.dob
		assert_changes("Pii.find(#{pii.id}).dob") {
			pii.update_attributes(:dob => (Time.now - 4.days) )
		}
		assert !pii.new_record?
		assert_not_nil pii.dob
		assert pii.dob.is_a?(Date)
		assert_equal pii.dob, pii.dob.to_date
	end

	test "should parse a properly formatted date" do
		#	Chronic won't parse this correctly,
		#	but Date.parse will. ???
		assert_difference( "Pii.count", 1 ) do
{'fraction_non_alphanumeric': 0.09977578475336323, 'fraction_numerical': 0.004484304932735426, 'mean_word_length': 5.780673181324647, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 30, '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': '91345', 'n_tokens_mistral': 2467, 'n_tokens_neox': 2209, 'n_words': 440}