data_sanity 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -12,3 +12,15 @@
12
12
  +----+------------+-------------+-------------------+-----------------------+
13
13
  | 1 | Person | person_id | 128 | “Fmno can’t be blank” |
14
14
  +----+------------+-------------+-------------------+-----------------------+
15
+
16
+ # HOW TO USE
17
+ * adding migrations : rake data_sanity:db:migrate
18
+ * removing migrations : rake data_sanity:db:rollback
19
+ * Use normal rake db:migrate to migrate the migration added by rake data_sanity:db:migrate
20
+ * You can add a sample criteria file using rake data_sanity:criteria
21
+ * To Investigate your data use rake data_sanity:investigate
22
+ - By default it runs for all data
23
+ - rake data_sanity:investigate[random,2]
24
+ : parameter 1: random: show you want random selection
25
+ : parameter 2: lets you add as many random records you want to verify [default is 1]
26
+ : Note: if you have a criteria file it works only for models in criteria file
@@ -7,7 +7,7 @@ module DataSanity
7
7
 
8
8
  def initialize options = {}
9
9
  options[:validate] == "random" ? @random = true : @all = true
10
- @records_per_model = options[:records_per_model] || CONSIDERED_RECORDS
10
+ @records_per_model = options[:records_per_model].to_i == 0 ? CONSIDERED_RECORDS : options[:records_per_model].to_i
11
11
  @models = load_models
12
12
  file_path = "#{Rails.root}/config/data_sanity_criteria.yml"
13
13
  @criteria = (File.exists?(file_path) ? (YAML.load File.open(file_path).read) : false) rescue false
@@ -35,7 +35,7 @@ module DataSanity
35
35
  def validate_random(model)
36
36
  no_of_records = model.count
37
37
  return if no_of_records == 0
38
- @records_per_model.to_i.times do
38
+ [@records_per_model, no_of_records].min.times do
39
39
  instance = model.offset(rand(no_of_records)).first
40
40
  populate_if_invalid_record(instance, model)
41
41
  end
@@ -48,8 +48,10 @@ module DataSanity
48
48
  results = model.where(attribute.to_sym => value)
49
49
  count = results.count
50
50
  next if count == 0
51
- instance = results.offset(rand(count)).first
52
- populate_if_invalid_record(instance, model)
51
+ [@records_per_model, count].min.times do
52
+ instance = results.offset(rand(count)).first
53
+ populate_if_invalid_record(instance, model)
54
+ end
53
55
  end
54
56
  end
55
57
  end
@@ -3,5 +3,5 @@
3
3
  # Start with Model Name and
4
4
  #<Model Name>: # Criteria-Rule to be applied
5
5
  # <field name>: ['Raju', 'kaju', 'Taju'] # Field is considered for validations against distinct values in array
6
- # <age> # Field is considered for validations against all possible distinct values
6
+ # <age>: # Field is considered for validations against all possible distinct values
7
7
  ############################################
@@ -1,3 +1,3 @@
1
1
  module DataSanity
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -75,6 +75,15 @@ describe "DataSanity::Inspector" do
75
75
  DataInspector.count.should == 2
76
76
  end
77
77
 
78
+ it "should select minimum of records_per_model and records matching a criteria" do
79
+ inspector = DataSanity::Inspector.new(:validate => "random", :records_per_model => 20)
80
+
81
+ Person.new(:name => "InValid-Record").save(:validate => false)
82
+
83
+ inspector.investigate
84
+ DataInspector.count.should == 1
85
+ end
86
+
78
87
  describe "criteria" do
79
88
  before :each do
80
89
  setup_data_sanity_criteria
@@ -102,7 +111,7 @@ describe "DataSanity::Inspector" do
102
111
  end
103
112
 
104
113
  it "should check all distinct values of field for which a criteria exists" do
105
- update_data_sanity_criteria("Car:\n make")
114
+ update_data_sanity_criteria(criteria_car_make)
106
115
  inspector = DataSanity::Inspector.new(:validate => "random")
107
116
 
108
117
  5.times { |i| Car.new(:name => "Car Name#{i}", :make => "Brand1").save(:validate => false) }
@@ -116,6 +125,32 @@ describe "DataSanity::Inspector" do
116
125
  Car.find(DataInspector.last.primary_key_value).make.should == "Brand2"
117
126
  end
118
127
 
128
+ it "should check all distinct values of field for which a criteria exists" do
129
+ update_data_sanity_criteria(criteria_car_make)
130
+ inspector = DataSanity::Inspector.new(:validate => "random", :records_per_model => "2")
131
+
132
+ 5.times { |i| Car.new(:name => "Car Name#{i}", :make => "Brand1").save(:validate => false) }
133
+ 5.times { |i| Car.new(:name => "Car Name#{i}", :make => "Brand2").save(:validate => false) }
134
+
135
+ inspector.investigate
136
+
137
+ DataInspector.count.should == 4
138
+ DataInspector.all.collect(&:table_name).should == ["Car", "Car", "Car", "Car"]
139
+ Car.find(DataInspector.all.collect(&:primary_key_value)).collect(&:make).should == ["Brand1", "Brand1", "Brand2", "Brand2"]
140
+ end
141
+
142
+ it "should select minimum of records_per_model and records matching a criteria" do
143
+ update_data_sanity_criteria(criteria_car_make)
144
+ inspector = DataSanity::Inspector.new(:validate => "random", :records_per_model => "15")
145
+
146
+ 2.times { |i| Car.new(:name => "Car Name#{i}", :make => "Brand1").save(:validate => false) }
147
+ 2.times { |i| Car.new(:name => "Car Name#{i}", :make => "Brand2").save(:validate => false) }
148
+
149
+ inspector.investigate
150
+
151
+ DataInspector.count.should == 4
152
+ end
153
+
119
154
  after :each do
120
155
  cleanup_data_sanity_criteria
121
156
  end
@@ -127,4 +162,9 @@ describe "DataSanity::Inspector" do
127
162
  end
128
163
  end
129
164
 
165
+ def criteria_car_make
166
+ "Car:
167
+ make"
168
+ end
169
+
130
170
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_sanity
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 31
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 0
10
- version: 0.1.0
9
+ - 2
10
+ version: 0.1.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Habibullah, Rahul, Jigyasa, Jyotsna, Hephzibah, Garima