data_sanity 0.0.3 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/data_sanity/inspector.rb +31 -11
- data/lib/data_sanity/models/data_inspector.rb +4 -0
- data/lib/data_sanity/tasks.rb +4 -2
- data/lib/data_sanity/version.rb +1 -1
- data/lib/data_sanity.rb +1 -0
- data/spec/data_sanity/inspector_spec.rb +44 -13
- data/spec/data_sanity/models/data_inspector_spec.rb +7 -0
- data/spec/support/helper.rb +0 -1
- metadata +7 -4
@@ -1,34 +1,54 @@
|
|
1
1
|
module DataSanity
|
2
2
|
class Inspector
|
3
3
|
|
4
|
+
CONSIDERED_RECORDS = 1
|
4
5
|
ALLOWED_VALIDATION = [:all, :random]
|
5
|
-
|
6
|
-
attr_accessor :all, :random, :models
|
6
|
+
attr_accessor :all, :random, :models, :records_per_model
|
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
11
|
@models = load_models
|
11
12
|
end
|
12
13
|
|
13
14
|
def investigate
|
14
15
|
@models.each do |model_string|
|
15
16
|
model = model_string.constantize
|
16
|
-
model
|
17
|
-
|
18
|
-
DataInspector.create(:table_name => model_string,
|
19
|
-
:table_primary_key => model.primary_key,
|
20
|
-
:primary_key_value => instance.send(model.primary_key),
|
21
|
-
:validation_errors => instance.errors.full_messages.to_yaml)
|
22
|
-
end
|
23
|
-
end
|
17
|
+
validate_all(model) if @all
|
18
|
+
validate_random(model) if @random
|
24
19
|
end
|
25
20
|
end
|
26
21
|
|
27
22
|
private
|
28
23
|
|
24
|
+
def validate_all(model)
|
25
|
+
model.all.each do |instance|
|
26
|
+
populate_if_invalid_record(instance, model)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def validate_random(model)
|
31
|
+
no_of_records = model.count
|
32
|
+
@records_per_model.times do
|
33
|
+
instance = model.first(:limit => 1, :offset => rand(no_of_records))
|
34
|
+
populate_if_invalid_record(instance, model)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def populate_if_invalid_record(instance, model)
|
39
|
+
DataInspector.create(:table_name => model.to_s,
|
40
|
+
:table_primary_key => model.primary_key,
|
41
|
+
:primary_key_value => instance.send(model.primary_key),
|
42
|
+
:validation_errors => instance.errors.full_messages.to_yaml) unless instance.valid?
|
43
|
+
|
44
|
+
end
|
45
|
+
|
29
46
|
def load_models
|
30
47
|
Dir["#{Rails.root}/app/models/**/*.rb"].each { |file_path| require file_path rescue nil }
|
31
|
-
ActiveRecord::Base.descendants.select(&:descends_from_active_record?)
|
48
|
+
all_models = ActiveRecord::Base.descendants.select(&:descends_from_active_record?)
|
49
|
+
all_models.delete(DataInspector)
|
50
|
+
all_models.collect(&:name)
|
32
51
|
end
|
52
|
+
|
33
53
|
end
|
34
54
|
end
|
data/lib/data_sanity/tasks.rb
CHANGED
@@ -3,14 +3,16 @@ namespace :db do
|
|
3
3
|
desc 'Create data inspector model for data sanity results'
|
4
4
|
task :migrate => :environment do
|
5
5
|
Dir.chdir("#{Rails.root}") do
|
6
|
-
system "rails g
|
6
|
+
system "rails g migration create_data_inspector"
|
7
|
+
system "rails g migration add_columns_to_data_inspector table_name:string table_primary_key:string primary_key_value:string validation_errors:text"
|
7
8
|
end
|
8
9
|
end
|
9
10
|
|
10
11
|
desc 'Destroy data inspector model for data sanity results'
|
11
12
|
task :rollback do
|
12
13
|
Dir.chdir("#{Rails.root}") do
|
13
|
-
system "rails destroy
|
14
|
+
system "rails destroy migration add_columns_to_data_inspector"
|
15
|
+
system "rails destroy migration create_data_inspector"
|
14
16
|
end
|
15
17
|
end
|
16
18
|
end
|
data/lib/data_sanity/version.rb
CHANGED
data/lib/data_sanity.rb
CHANGED
@@ -9,6 +9,20 @@ describe "DataSanity::Inspector" do
|
|
9
9
|
inspector.all.should be_true
|
10
10
|
inspector.models.should == ["Person"]
|
11
11
|
end
|
12
|
+
|
13
|
+
it "should set random switch, populate default records_per_model and populate all models of the applications" do
|
14
|
+
inspector = DataSanity::Inspector.new :validate => :random
|
15
|
+
inspector.random.should be_true
|
16
|
+
inspector.records_per_model.should == 1
|
17
|
+
inspector.models.should == ["Person"]
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should set random switch, populate records_per_model and populate all models of the applications" do
|
21
|
+
inspector = DataSanity::Inspector.new :validate => :random, :records_per_model => 5
|
22
|
+
inspector.random.should be_true
|
23
|
+
inspector.records_per_model.should == 5
|
24
|
+
inspector.models.should == ["Person"]
|
25
|
+
end
|
12
26
|
end
|
13
27
|
|
14
28
|
describe "investigate" do
|
@@ -22,21 +36,38 @@ describe "DataSanity::Inspector" do
|
|
22
36
|
DataInspector.count.should == 0
|
23
37
|
end
|
24
38
|
|
25
|
-
|
26
|
-
inspector = DataSanity::Inspector.new
|
39
|
+
describe "all" do
|
27
40
|
|
28
|
-
|
29
|
-
|
41
|
+
it "should check for errors on the model and populate fields in DataInspector" do
|
42
|
+
inspector = DataSanity::Inspector.new
|
30
43
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
44
|
+
Person.new(:name => "Valid-Record", :age => 20).save(:validate => false)
|
45
|
+
Person.new(:age => 1).save(:validate => false)
|
46
|
+
|
47
|
+
inspector.investigate
|
48
|
+
DataInspector.count.should == 1
|
49
|
+
|
50
|
+
first_person = Person.find_by_age(1)
|
51
|
+
first_person.valid?
|
52
|
+
DataInspector.first.table_name.should == "Person"
|
53
|
+
DataInspector.first.table_primary_key.should == "id"
|
54
|
+
DataInspector.first.primary_key_value.should == first_person.id.to_s
|
55
|
+
YAML.load(DataInspector.first.validation_errors).should == first_person.errors.full_messages
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "random" do
|
60
|
+
it "should check for errors on the model and populate fields in DataInspector" do
|
61
|
+
inspector = DataSanity::Inspector.new(:validate => :random, :records_per_model => 2)
|
62
|
+
|
63
|
+
Person.new(:name => "Valid-Record").save(:validate => false)
|
64
|
+
Person.new(:name => "UnderAge", :age => 1).save(:validate => false)
|
65
|
+
Person.new(:age => 20).save(:validate => false)
|
66
|
+
Person.new(:age => 1).save(:validate => false)
|
67
|
+
|
68
|
+
inspector.investigate
|
69
|
+
DataInspector.count.should == 2
|
70
|
+
end
|
40
71
|
end
|
41
72
|
|
42
73
|
after :each do
|
data/spec/support/helper.rb
CHANGED
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:
|
4
|
+
hash: 21
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 5
|
10
|
+
version: 0.0.5
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Habibullah, Rahul, Jigyasa, Jyotsna, Hephzibah, Garima
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-12-
|
18
|
+
date: 2011-12-02 00:00:00 Z
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
21
21
|
type: :runtime
|
@@ -79,10 +79,12 @@ files:
|
|
79
79
|
- data_sanity.gemspec
|
80
80
|
- lib/data_sanity.rb
|
81
81
|
- lib/data_sanity/inspector.rb
|
82
|
+
- lib/data_sanity/models/data_inspector.rb
|
82
83
|
- lib/data_sanity/railtie.rb
|
83
84
|
- lib/data_sanity/tasks.rb
|
84
85
|
- lib/data_sanity/version.rb
|
85
86
|
- spec/data_sanity/inspector_spec.rb
|
87
|
+
- spec/data_sanity/models/data_inspector_spec.rb
|
86
88
|
- spec/spec_helper.rb
|
87
89
|
- spec/support/helper.rb
|
88
90
|
- spec/support/sample_app/.gitignore
|
@@ -167,6 +169,7 @@ specification_version: 3
|
|
167
169
|
summary: Gem for checking data sanity
|
168
170
|
test_files:
|
169
171
|
- spec/data_sanity/inspector_spec.rb
|
172
|
+
- spec/data_sanity/models/data_inspector_spec.rb
|
170
173
|
- spec/spec_helper.rb
|
171
174
|
- spec/support/helper.rb
|
172
175
|
- spec/support/sample_app/.gitignore
|