Skip to content
Browse files

Jyotsna/garima - fixing bugs for random selection; making no of recor…

…ds work with random and criteria; Updating README
  • Loading branch information...
1 parent 3d607a2 commit 683ebcf22f330e38f77f589ff1e0c41f6302a07c Jigyasa Makkar committed
View
12 README.md
@@ -12,3 +12,15 @@
+----+------------+-------------+-------------------+-----------------------+
| 1 | Person | person_id | 128 | “Fmno can’t be blank” |
+----+------------+-------------+-------------------+-----------------------+
+
+# HOW TO USE
+ * adding migrations : rake data_sanity:db:migrate
+ * removing migrations : rake data_sanity:db:rollback
+ * Use normal rake db:migrate to migrate the migration added by rake data_sanity:db:migrate
+ * You can add a sample criteria file using rake data_sanity:criteria
+ * To Investigate your data use rake data_sanity:investigate
+ - By default it runs for all data
+ - rake data_sanity:investigate[random,2]
+ : parameter 1: random: show you want random selection
+ : parameter 2: lets you add as many random records you want to verify [default is 1]
+ : Note: if you have a criteria file it works only for models in criteria file
View
10 lib/data_sanity/inspector.rb
@@ -7,7 +7,7 @@ class Inspector
def initialize options = {}
options[:validate] == "random" ? @random = true : @all = true
- @records_per_model = options[:records_per_model] || CONSIDERED_RECORDS
+ @records_per_model = options[:records_per_model].to_i == 0 ? CONSIDERED_RECORDS : options[:records_per_model].to_i
@models = load_models
file_path = "#{Rails.root}/config/data_sanity_criteria.yml"
@criteria = (File.exists?(file_path) ? (YAML.load File.open(file_path).read) : false) rescue false
@@ -35,7 +35,7 @@ def validate_all(model)
def validate_random(model)
no_of_records = model.count
return if no_of_records == 0
- @records_per_model.to_i.times do
+ [@records_per_model, no_of_records].min.times do
instance = model.offset(rand(no_of_records)).first
populate_if_invalid_record(instance, model)
end
@@ -48,8 +48,10 @@ def validate_criteria(model, criteria)
results = model.where(attribute.to_sym => value)
count = results.count
next if count == 0
- instance = results.offset(rand(count)).first
- populate_if_invalid_record(instance, model)
+ [@records_per_model, count].min.times do
+ instance = results.offset(rand(count)).first
+ populate_if_invalid_record(instance, model)
+ end
end
end
end
View
2 lib/data_sanity/templates/data_sanity_criteria.yml
@@ -3,5 +3,5 @@
# Start with Model Name and
#<Model Name>: # Criteria-Rule to be applied
# <field name>: ['Raju', 'kaju', 'Taju'] # Field is considered for validations against distinct values in array
-# <age> # Field is considered for validations against all possible distinct values
+# <age>: # Field is considered for validations against all possible distinct values
############################################
View
2 lib/data_sanity/version.rb
@@ -1,3 +1,3 @@
module DataSanity
- VERSION = "0.1.0"
+ VERSION = "0.1.2"
end
View
42 spec/data_sanity/inspector_spec.rb
@@ -75,6 +75,15 @@
DataInspector.count.should == 2
end
+ it "should select minimum of records_per_model and records matching a criteria" do
+ inspector = DataSanity::Inspector.new(:validate => "random", :records_per_model => 20)
+
+ Person.new(:name => "InValid-Record").save(:validate => false)
+
+ inspector.investigate
+ DataInspector.count.should == 1
+ end
+
describe "criteria" do
before :each do
setup_data_sanity_criteria
@@ -102,7 +111,7 @@
end
it "should check all distinct values of field for which a criteria exists" do
- update_data_sanity_criteria("Car:\n make")
+ update_data_sanity_criteria(criteria_car_make)
inspector = DataSanity::Inspector.new(:validate => "random")
5.times { |i| Car.new(:name => "Car Name#{i}", :make => "Brand1").save(:validate => false) }
@@ -116,6 +125,32 @@
Car.find(DataInspector.last.primary_key_value).make.should == "Brand2"
end
+ it "should check all distinct values of field for which a criteria exists" do
+ update_data_sanity_criteria(criteria_car_make)
+ inspector = DataSanity::Inspector.new(:validate => "random", :records_per_model => "2")
+
+ 5.times { |i| Car.new(:name => "Car Name#{i}", :make => "Brand1").save(:validate => false) }
+ 5.times { |i| Car.new(:name => "Car Name#{i}", :make => "Brand2").save(:validate => false) }
+
+ inspector.investigate
+
+ DataInspector.count.should == 4
+ DataInspector.all.collect(&:table_name).should == ["Car", "Car", "Car", "Car"]
+ Car.find(DataInspector.all.collect(&:primary_key_value)).collect(&:make).should == ["Brand1", "Brand1", "Brand2", "Brand2"]
+ end
+
+ it "should select minimum of records_per_model and records matching a criteria" do
+ update_data_sanity_criteria(criteria_car_make)
+ inspector = DataSanity::Inspector.new(:validate => "random", :records_per_model => "15")
+
+ 2.times { |i| Car.new(:name => "Car Name#{i}", :make => "Brand1").save(:validate => false) }
+ 2.times { |i| Car.new(:name => "Car Name#{i}", :make => "Brand2").save(:validate => false) }
+
+ inspector.investigate
+
+ DataInspector.count.should == 4
+ end
+
after :each do
cleanup_data_sanity_criteria
end
@@ -127,4 +162,9 @@
end
end
+ def criteria_car_make
+ "Car:
+ make"
+ end
+
end

0 comments on commit 683ebcf

Please sign in to comment.
Something went wrong with that request. Please try again.