-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathactive_record_test.rb
156 lines (128 loc) · 4.98 KB
/
active_record_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
require_relative "test_helper"
require "active_record"
ActiveRecord::Base.logger = SemanticLogger[ActiveRecord::Base]
ActiveRecord::Base.configurations = {
"test" => {
"adapter" => "sqlite3",
"database" => "test/test_db.sqlite3",
"pool" => 5,
"timeout" => 5000
}
}
ActiveRecord::Base.establish_connection(:test)
ActiveRecord::Schema.define version: 0 do
create_table :users, force: true do |t|
t.string :first_name
t.string :last_name
t.string :address1
t.string :address2
t.string :ssn
t.integer :zip_code
t.text :text
end
end
# Log data cleansing result
# Set to :warn or higher to disable
DataCleansing.logger.level = :debug
# Set the Global list of fields to be masked
DataCleansing.register_masked_attributes :ssn, :bank_account_number
class User < ActiveRecord::Base
include DataCleansing::Cleanse
# Also cleanse non-database backed fields
attr_accessor :instance_var
# Use a global cleaner
cleanse :first_name, :last_name, cleaner: :strip
# Define a once off cleaner
cleanse :address1, :address2, :instance_var, cleaner: proc { |string| "<< #{string.strip} >>" }
# Custom Zip Code cleaner
cleanse :zip_code, cleaner: :string_to_integer
# Automatically cleanse data before validation
before_validation :cleanse_attributes!
end
class User2 < ActiveRecord::Base
include DataCleansing::Cleanse
# Use the same table as User above
self.table_name = "users"
serialize :text
# Test :all cleaner. Only works with ActiveRecord Models
# Must explicitly excelude :text since it is serialized
cleanse :all, cleaner: [:strip, proc { |s| "@#{s}@" }], except: %i[address1 zip_code text]
# Clean :first_name multiple times
cleanse :first_name, cleaner: proc { |string| "<< #{string} >>" }
# Clean :first_name multiple times
cleanse :first_name, cleaner: proc { |string| "$#{string}$" }
# Custom Zip Code cleaner
cleanse :zip_code, cleaner: :string_to_integer
# Automatically cleanse data before validation
before_validation :cleanse_attributes!
end
class ActiveRecordTest < Minitest::Test
describe "ActiveRecord Models" do
it "have globally registered cleaner" do
assert DataCleansing.cleaner(:strip)
end
it "Model.cleanse_attribute" do
assert_equal "joe", User.cleanse_attribute(:first_name, " joe ")
assert_equal "black", User.cleanse_attribute(:last_name, "\n black\n")
assert_equal "<< 2632 Brown St >>", User.cleanse_attribute(:address1, "2632 Brown St \n")
assert_equal "<< instance >>", User.cleanse_attribute(:instance_var, "\n instance\n\t ")
assert_equal 12_345, User.cleanse_attribute(:zip_code, "\n\tblah 12345badtext\n")
end
describe "with user" do
before do
@user = User.new(
first_name: " joe ",
last_name: "\n black\n",
address1: "2632 Brown St \n",
zip_code: "\n\tblah 12345badtext\n",
instance_var: "\n instance\n\t "
)
end
it "only have 3 cleaners" do
assert_equal 3, User.send(:data_cleansing_cleaners).size, User.send(:data_cleansing_cleaners)
end
it "cleanse_attributes! using global cleaner" do
assert_equal true, @user.valid?
assert_equal "joe", @user.first_name
assert_equal "black", @user.last_name
end
it "cleanse_attributes! using attribute specific custom cleaner" do
assert_equal true, @user.valid?
assert_equal "<< 2632 Brown St >>", @user.address1
assert_equal "<< instance >>", @user.instance_var
end
it "cleanse_attributes! using global cleaner using rails extensions" do
@user.cleanse_attributes!
assert_equal 12_345, @user.zip_code
end
end
describe "with user2" do
before do
@user = User2.new(
first_name: " joe ",
last_name: "\n black\n",
ssn: "\n 123456789 \n ",
address1: "2632 Brown St \n",
zip_code: "\n\t blah\n",
text: ["\n 123456789 \n ", " second "]
)
end
it "have 4 cleaners defined" do
assert_equal 4, User2.send(:data_cleansing_cleaners).size, User2.send(:data_cleansing_cleaners)
end
it "have 3 attributes cleaners defined" do
# :all, :first_name, :zip_code
assert_equal 3, User2.send(:data_cleansing_attribute_cleaners).size, User2.send(:data_cleansing_attribute_cleaners)
end
it "cleanse_attributes! clean all attributes" do
assert_equal true, @user.valid?
assert_equal "$<< @joe@ >>$", @user.first_name, User2.send(:data_cleansing_cleaners)
assert_equal "@black@", @user.last_name
assert_equal "2632 Brown St \n", @user.address1
assert_equal "@123456789@", @user.ssn
assert_nil @user.zip_code, User2.send(:data_cleansing_cleaners)
assert_equal ["\n 123456789 \n ", " second "], @user.text
end
end
end
end