data_sanity 0.0.5 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/data_sanity.gemspec CHANGED
@@ -16,6 +16,7 @@ Gem::Specification.new do |gem|
16
16
  gem.version = DataSanity::VERSION
17
17
 
18
18
  gem.add_dependency "rails", "=3.0.6"
19
+ gem.add_dependency "rake", "=0.8.7"
19
20
 
20
21
  gem.add_development_dependency "rspec"
21
22
  gem.add_development_dependency "sqlite3"
@@ -2,20 +2,25 @@ module DataSanity
2
2
  class Inspector
3
3
 
4
4
  CONSIDERED_RECORDS = 1
5
- ALLOWED_VALIDATION = [:all, :random]
6
- attr_accessor :all, :random, :models, :records_per_model
5
+ ALLOWED_VALIDATION = [:all, :random, :criteria]
6
+ attr_accessor :all, :random, :criteria, :models, :records_per_model
7
7
 
8
8
  def initialize options = {}
9
9
  options[:validate] == :random ? @random = true : @all = true
10
10
  @records_per_model = options[:records_per_model] || CONSIDERED_RECORDS
11
11
  @models = load_models
12
+ file_path = "#{Rails.root}/config/data_sanity_criteria.yml"
13
+ @criteria = File.exists?(file_path) ? (YAML.load File.open(file_path).read) : false
12
14
  end
13
15
 
14
16
  def investigate
15
17
  @models.each do |model_string|
16
18
  model = model_string.constantize
17
19
  validate_all(model) if @all
18
- validate_random(model) if @random
20
+ if @random
21
+ validate_criteria(model, @criteria[model_string]) and return if @criteria && @criteria[model_string].present?
22
+ validate_random(model)
23
+ end
19
24
  end
20
25
  end
21
26
 
@@ -29,12 +34,25 @@ module DataSanity
29
34
 
30
35
  def validate_random(model)
31
36
  no_of_records = model.count
37
+ return if no_of_records == 0
32
38
  @records_per_model.times do
33
- instance = model.first(:limit => 1, :offset => rand(no_of_records))
39
+ instance = model.offset(rand(no_of_records)).first
34
40
  populate_if_invalid_record(instance, model)
35
41
  end
36
42
  end
37
43
 
44
+ def validate_criteria(model, criteria)
45
+ criteria.each do |attribute, values|
46
+ values.each do |value|
47
+ results = model.where(attribute.to_sym => value)
48
+ count = results.count
49
+ next if count == 0
50
+ instance = results.offset(rand(count)).first
51
+ populate_if_invalid_record(instance, model)
52
+ end
53
+ end
54
+ end
55
+
38
56
  def populate_if_invalid_record(instance, model)
39
57
  DataInspector.create(:table_name => model.to_s,
40
58
  :table_primary_key => model.primary_key,
@@ -1,5 +1,5 @@
1
- namespace :db do
2
- namespace :data_sanity do
1
+ namespace :data_sanity do
2
+ 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
@@ -16,4 +16,11 @@ namespace :db do
16
16
  end
17
17
  end
18
18
  end
19
+
20
+ desc 'Creating a sample criteria file'
21
+ task :citeria do
22
+ Dir.chdir("#{Rails.root}") do
23
+ system "cp #{File.open('./templates/data_sanity_criteria.yml').read} config/."
24
+ end
25
+ end
19
26
  end
@@ -0,0 +1,10 @@
1
+ ############################################
2
+ # This is a sample criteria.
3
+ # Start with Model Name and
4
+ #<Model Name>: # Criteria-Rule to be applied
5
+ # <field name>: # Anthing mentioned is considered for validations against mentioned below distinct values
6
+ # - 'Raju' # Various values field can take
7
+ # - 'Saju'
8
+ # - 'Taju'
9
+ # <age> # Anthing mentioned is considered for validations against all possible distinct values
10
+ ############################################
@@ -1,3 +1,3 @@
1
1
  module DataSanity
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -7,21 +7,21 @@ describe "DataSanity::Inspector" do
7
7
  it "should set all switch as default options and populate all models of the applications" do
8
8
  inspector = DataSanity::Inspector.new
9
9
  inspector.all.should be_true
10
- inspector.models.should == ["Person"]
10
+ inspector.models.should == ["Car", "Person"]
11
11
  end
12
12
 
13
13
  it "should set random switch, populate default records_per_model and populate all models of the applications" do
14
14
  inspector = DataSanity::Inspector.new :validate => :random
15
15
  inspector.random.should be_true
16
16
  inspector.records_per_model.should == 1
17
- inspector.models.should == ["Person"]
17
+ inspector.models.should == ["Car", "Person"]
18
18
  end
19
19
 
20
20
  it "should set random switch, populate records_per_model and populate all models of the applications" do
21
21
  inspector = DataSanity::Inspector.new :validate => :random, :records_per_model => 5
22
22
  inspector.random.should be_true
23
23
  inspector.records_per_model.should == 5
24
- inspector.models.should == ["Person"]
24
+ inspector.models.should == ["Car", "Person"]
25
25
  end
26
26
  end
27
27
 
@@ -30,14 +30,14 @@ describe "DataSanity::Inspector" do
30
30
  setup_data_inspector
31
31
  end
32
32
 
33
- it "should check for errors on the model and populate fields in DataInspector" do
34
- inspector = DataSanity::Inspector.new
35
- inspector.investigate
36
- DataInspector.count.should == 0
37
- end
38
-
39
33
  describe "all" do
40
34
 
35
+ it "should check for models with no data" do
36
+ inspector = DataSanity::Inspector.new
37
+ inspector.investigate
38
+ DataInspector.count.should == 0
39
+ end
40
+
41
41
  it "should check for errors on the model and populate fields in DataInspector" do
42
42
  inspector = DataSanity::Inspector.new
43
43
 
@@ -57,10 +57,16 @@ describe "DataSanity::Inspector" do
57
57
  end
58
58
 
59
59
  describe "random" do
60
+ it "should check for models with no data" do
61
+ inspector = DataSanity::Inspector.new :validate => :random
62
+ inspector.investigate
63
+ DataInspector.count.should == 0
64
+ end
65
+
60
66
  it "should check for errors on the model and populate fields in DataInspector" do
61
67
  inspector = DataSanity::Inspector.new(:validate => :random, :records_per_model => 2)
62
68
 
63
- Person.new(:name => "Valid-Record").save(:validate => false)
69
+ Person.new(:name => "InValid-Record").save(:validate => false)
64
70
  Person.new(:name => "UnderAge", :age => 1).save(:validate => false)
65
71
  Person.new(:age => 20).save(:validate => false)
66
72
  Person.new(:age => 1).save(:validate => false)
@@ -68,6 +74,36 @@ describe "DataSanity::Inspector" do
68
74
  inspector.investigate
69
75
  DataInspector.count.should == 2
70
76
  end
77
+
78
+ describe "criteria" do
79
+ before :each do
80
+ setup_data_sanity_criteria
81
+ end
82
+
83
+ it "should check for models with no data" do
84
+ inspector = DataSanity::Inspector.new :validate => :random
85
+ inspector.investigate
86
+ DataInspector.count.should == 0
87
+ end
88
+
89
+ it "should check for errors on model based on criteria picked from data_sanity_criteria.yml" do
90
+ inspector = DataSanity::Inspector.new(:validate => :random)
91
+
92
+ 2.times { Person.new(:name => "Raju").save(:validate => false) }
93
+ 2.times { Person.new(:name => "Saju").save(:validate => false) }
94
+ 2.times { Car.new(:name => "Santro").save(:validate => false) }
95
+ Person.new(:age => 20).save(:validate => false)
96
+ car = Car.new(:name => "800", :make => "Maruti", :color => "Black", :person => Person.first).save(:validate => false)
97
+
98
+ inspector.investigate
99
+ DataInspector.count.should == 3
100
+ DataInspector.all.collect(&:table_name).should == ["Car", "Person", "Person"]
101
+ end
102
+
103
+ after :each do
104
+ cleanup_data_sanity_criteria
105
+ end
106
+ end
71
107
  end
72
108
 
73
109
  after :each do
@@ -1,7 +1,7 @@
1
1
  def clean_data_inspector_migration
2
2
  Dir.chdir("#{Rails.root}") do
3
3
  system "rake db:rollback"
4
- system "rake db:data_sanity:rollback"
4
+ system "rake data_sanity:db:rollback"
5
5
  end
6
6
  end
7
7
 
@@ -14,7 +14,27 @@ end
14
14
 
15
15
  def setup_data_inspector
16
16
  Dir.chdir("#{Rails.root}") do
17
- system "rake db:data_sanity:migrate"
17
+ system "rake data_sanity:db:migrate"
18
18
  system "rake db:migrate"
19
19
  end
20
20
  end
21
+
22
+ def setup_data_sanity_criteria
23
+ Dir.chdir("#{Rails.root}") do
24
+ system "rake data_sanity:criteria"
25
+ file = File.open("config/data_sanity_criteria.yml", "w")
26
+ file << template_yml
27
+ file.close
28
+ end
29
+ end
30
+
31
+ def cleanup_data_sanity_criteria
32
+ Dir.chdir("#{Rails.root}") do
33
+ system "rm config/data_sanity_criteria.yml"
34
+ end
35
+ end
36
+
37
+ def template_yml
38
+ "Person:
39
+ name: ['Raju', 'Saju']"
40
+ end
@@ -0,0 +1,7 @@
1
+ class Car < ActiveRecord::Base
2
+
3
+ validates_presence_of :name, :make, :color
4
+ validates_inclusion_of :color, :in => [:red, :blue, :black, :green, :white, :silver, :golden]
5
+
6
+ belongs_to :person
7
+ end
@@ -1,4 +1,6 @@
1
1
  class Person < ActiveRecord::Base
2
2
  validates_presence_of :name, :age
3
3
  validates_numericality_of :age, :greater_than => 18
4
+
5
+ has_many :cars
4
6
  end
@@ -0,0 +1,15 @@
1
+ class CreateCars < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :cars do |t|
4
+ t.string :name
5
+ t.string :make
6
+ t.string :color
7
+ t.integer :person_id
8
+ t.timestamps
9
+ end
10
+ end
11
+
12
+ def self.down
13
+ drop_table :cars
14
+ end
15
+ end
@@ -10,7 +10,16 @@
10
10
  #
11
11
  # It's strongly recommended to check this file into your version control system.
12
12
 
13
- ActiveRecord::Schema.define(:version => 20111201095603) do
13
+ ActiveRecord::Schema.define(:version => 20111205042922) do
14
+
15
+ create_table "cars", :force => true do |t|
16
+ t.string "name"
17
+ t.string "make"
18
+ t.string "color"
19
+ t.integer "person_id"
20
+ t.datetime "created_at"
21
+ t.datetime "updated_at"
22
+ end
14
23
 
15
24
  create_table "data_inspectors", :force => true do |t|
16
25
  t.string "table_name"
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: 21
4
+ hash: 17
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 7
10
+ version: 0.0.7
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-02 00:00:00 Z
18
+ date: 2011-12-05 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  type: :runtime
@@ -33,11 +33,27 @@ dependencies:
33
33
  - 6
34
34
  version: 3.0.6
35
35
  requirement: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ type: :runtime
38
+ prerelease: false
39
+ name: rake
40
+ version_requirements: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - "="
44
+ - !ruby/object:Gem::Version
45
+ hash: 49
46
+ segments:
47
+ - 0
48
+ - 8
49
+ - 7
50
+ version: 0.8.7
51
+ requirement: *id002
36
52
  - !ruby/object:Gem::Dependency
37
53
  type: :development
38
54
  prerelease: false
39
55
  name: rspec
40
- version_requirements: &id002 !ruby/object:Gem::Requirement
56
+ version_requirements: &id003 !ruby/object:Gem::Requirement
41
57
  none: false
42
58
  requirements:
43
59
  - - ">="
@@ -46,12 +62,12 @@ dependencies:
46
62
  segments:
47
63
  - 0
48
64
  version: "0"
49
- requirement: *id002
65
+ requirement: *id003
50
66
  - !ruby/object:Gem::Dependency
51
67
  type: :development
52
68
  prerelease: false
53
69
  name: sqlite3
54
- version_requirements: &id003 !ruby/object:Gem::Requirement
70
+ version_requirements: &id004 !ruby/object:Gem::Requirement
55
71
  none: false
56
72
  requirements:
57
73
  - - ">="
@@ -60,7 +76,7 @@ dependencies:
60
76
  segments:
61
77
  - 0
62
78
  version: "0"
63
- requirement: *id003
79
+ requirement: *id004
64
80
  description: Gem for checking data sanity
65
81
  email:
66
82
  - ""
@@ -82,6 +98,7 @@ files:
82
98
  - lib/data_sanity/models/data_inspector.rb
83
99
  - lib/data_sanity/railtie.rb
84
100
  - lib/data_sanity/tasks.rb
101
+ - lib/data_sanity/templates/data_sanity_criteria.yml
85
102
  - lib/data_sanity/version.rb
86
103
  - spec/data_sanity/inspector_spec.rb
87
104
  - spec/data_sanity/models/data_inspector_spec.rb
@@ -92,6 +109,7 @@ files:
92
109
  - spec/support/sample_app/README
93
110
  - spec/support/sample_app/Rakefile
94
111
  - spec/support/sample_app/app/helpers/application_helper.rb
112
+ - spec/support/sample_app/app/models/car.rb
95
113
  - spec/support/sample_app/app/models/not_an_active_record_model.rb
96
114
  - spec/support/sample_app/app/models/person.rb
97
115
  - spec/support/sample_app/app/views/layouts/application.html.erb
@@ -111,6 +129,7 @@ files:
111
129
  - spec/support/sample_app/config/locales/en.yml
112
130
  - spec/support/sample_app/config/routes.rb
113
131
  - spec/support/sample_app/db/migrate/20111130045224_create_people.rb
132
+ - spec/support/sample_app/db/migrate/20111205042538_create_cars.rb
114
133
  - spec/support/sample_app/db/schema.rb
115
134
  - spec/support/sample_app/db/seeds.rb
116
135
  - spec/support/sample_app/lib/tasks/.gitkeep
@@ -129,10 +148,8 @@ files:
129
148
  - spec/support/sample_app/public/robots.txt
130
149
  - spec/support/sample_app/public/stylesheets/.gitkeep
131
150
  - spec/support/sample_app/script/rails
132
- - spec/support/sample_app/test/fixtures/people.yml
133
151
  - spec/support/sample_app/test/performance/browsing_test.rb
134
152
  - spec/support/sample_app/test/test_helper.rb
135
- - spec/support/sample_app/test/unit/person_test.rb
136
153
  - spec/support/sample_app/vendor/plugins/.gitkeep
137
154
  homepage: ""
138
155
  licenses: []
@@ -177,6 +194,7 @@ test_files:
177
194
  - spec/support/sample_app/README
178
195
  - spec/support/sample_app/Rakefile
179
196
  - spec/support/sample_app/app/helpers/application_helper.rb
197
+ - spec/support/sample_app/app/models/car.rb
180
198
  - spec/support/sample_app/app/models/not_an_active_record_model.rb
181
199
  - spec/support/sample_app/app/models/person.rb
182
200
  - spec/support/sample_app/app/views/layouts/application.html.erb
@@ -196,6 +214,7 @@ test_files:
196
214
  - spec/support/sample_app/config/locales/en.yml
197
215
  - spec/support/sample_app/config/routes.rb
198
216
  - spec/support/sample_app/db/migrate/20111130045224_create_people.rb
217
+ - spec/support/sample_app/db/migrate/20111205042538_create_cars.rb
199
218
  - spec/support/sample_app/db/schema.rb
200
219
  - spec/support/sample_app/db/seeds.rb
201
220
  - spec/support/sample_app/lib/tasks/.gitkeep
@@ -214,8 +233,6 @@ test_files:
214
233
  - spec/support/sample_app/public/robots.txt
215
234
  - spec/support/sample_app/public/stylesheets/.gitkeep
216
235
  - spec/support/sample_app/script/rails
217
- - spec/support/sample_app/test/fixtures/people.yml
218
236
  - spec/support/sample_app/test/performance/browsing_test.rb
219
237
  - spec/support/sample_app/test/test_helper.rb
220
- - spec/support/sample_app/test/unit/person_test.rb
221
238
  - spec/support/sample_app/vendor/plugins/.gitkeep
@@ -1,9 +0,0 @@
1
- # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
2
-
3
- one:
4
- name: MyString
5
- age: 1
6
-
7
- two:
8
- name: MyString
9
- age: 1
@@ -1,8 +0,0 @@
1
- require 'test_helper'
2
-
3
- class PersonTest < ActiveSupport::TestCase
4
- # Replace this with your real tests.
5
- test "the truth" do
6
- assert true
7
- end
8
- end