table_warnings 0.0.7 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/CHANGELOG +14 -0
- data/Gemfile +1 -2
- data/README.rdoc +3 -3
- data/Rakefile +5 -10
- data/lib/table_warnings/arbitrary.rb +17 -0
- data/lib/table_warnings/blank.rb +13 -0
- data/lib/table_warnings/column.rb +70 -0
- data/lib/table_warnings/debug.rb +2 -0
- data/lib/table_warnings/exclusive.rb +31 -0
- data/lib/table_warnings/nonexistent_owner.rb +31 -0
- data/lib/table_warnings/null.rb +13 -0
- data/lib/table_warnings/range.rb +32 -0
- data/lib/table_warnings/registry.rb +38 -0
- data/lib/table_warnings/scout.rb +85 -0
- data/lib/table_warnings/size.rb +55 -0
- data/lib/table_warnings/version.rb +1 -1
- data/lib/table_warnings.rb +134 -24
- data/table_warnings.gemspec +6 -5
- data/test/helper.rb +94 -16
- data/test/test_combinations.rb +85 -0
- data/test/test_warn_if_nonexistent_owner.rb +60 -0
- data/test/test_warn_if_nonexistent_owner_except.rb +67 -0
- data/test/test_warn_if_nulls_except.rb +120 -0
- data/test/test_warn_if_nulls_in.rb +114 -0
- data/test/test_warn_unless_range.rb +18 -0
- metadata +105 -25
- data/lib/table_warnings/config.rb +0 -11
- data/lib/table_warnings/warning/arbitrary.rb +0 -13
- data/lib/table_warnings/warning/blank.rb +0 -25
- data/lib/table_warnings/warning/size.rb +0 -34
- data/lib/table_warnings/warning.rb +0 -30
- data/test/test_table_warnings.rb +0 -64
@@ -1,25 +0,0 @@
|
|
1
|
-
module TableWarnings
|
2
|
-
class Warning
|
3
|
-
class Blank < Warning
|
4
|
-
def column_name
|
5
|
-
@column_name.to_s
|
6
|
-
end
|
7
|
-
|
8
|
-
def columns_hash
|
9
|
-
if column_name.present?
|
10
|
-
table.columns_hash.slice column_name
|
11
|
-
else
|
12
|
-
table.columns_hash
|
13
|
-
end
|
14
|
-
end
|
15
|
-
|
16
|
-
def messages
|
17
|
-
columns_hash.map do |_, c|
|
18
|
-
if table.count(:conditions => { c.name => nil }) > 0 or (c.text? and table.count(:conditions => [ "TRIM(#{table.quoted_table_name}.#{c.name}) = ''"]) > 0)
|
19
|
-
"There are blanks in the `#{c.name}` column."
|
20
|
-
end
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
module TableWarnings
|
2
|
-
class Warning
|
3
|
-
class Size < Warning
|
4
|
-
attr_reader :approximate_size
|
5
|
-
|
6
|
-
def allowed_range
|
7
|
-
case approximate_size
|
8
|
-
when :few
|
9
|
-
1..10
|
10
|
-
when :dozens, :tens
|
11
|
-
10..100
|
12
|
-
when :hundreds
|
13
|
-
100..1_000
|
14
|
-
when :thousands
|
15
|
-
1_000..99_000
|
16
|
-
when :hundreds_of_thousands
|
17
|
-
100_000..1_000_000
|
18
|
-
when :millions
|
19
|
-
1_000_000..1_000_000_000
|
20
|
-
when Range
|
21
|
-
approximate_size
|
22
|
-
when Numeric
|
23
|
-
approximate_size..approximate_size
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def messages
|
28
|
-
unless allowed_range.include? table.count
|
29
|
-
"Table is not of expected size (expected: #{allowed_range.to_s}, actual: #{table.count})"
|
30
|
-
end
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
@@ -1,30 +0,0 @@
|
|
1
|
-
module TableWarnings
|
2
|
-
class Warning
|
3
|
-
autoload :Blank, 'table_warnings/warning/blank'
|
4
|
-
autoload :Size, 'table_warnings/warning/size'
|
5
|
-
autoload :Arbitrary, 'table_warnings/warning/arbitrary'
|
6
|
-
|
7
|
-
attr_reader :table
|
8
|
-
|
9
|
-
def initialize(options = {})
|
10
|
-
options.each do |k, v|
|
11
|
-
instance_variable_set "@#{k}", v
|
12
|
-
end
|
13
|
-
end
|
14
|
-
|
15
|
-
def to_hash
|
16
|
-
instance_variables.sort.inject({}) do |memo, ivar_name|
|
17
|
-
memo[ivar_name.to_s.sub('@', '')] = instance_variable_get ivar_name
|
18
|
-
memo
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def hash
|
23
|
-
to_hash.hash
|
24
|
-
end
|
25
|
-
|
26
|
-
def eql?(other)
|
27
|
-
other.is_a?(Warning) and self.to_hash == other.to_hash
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
data/test/test_table_warnings.rb
DELETED
@@ -1,64 +0,0 @@
|
|
1
|
-
require 'helper'
|
2
|
-
|
3
|
-
class AutomobileMake < ActiveRecord::Base
|
4
|
-
col :name
|
5
|
-
col :fuel_efficiency, :type => :float
|
6
|
-
col :fuel_efficiency_units
|
7
|
-
|
8
|
-
warn_if_blanks_in :name
|
9
|
-
warn_if_blanks_in :fuel_efficiency
|
10
|
-
warn_unless_size_is :hundreds
|
11
|
-
warn do
|
12
|
-
if exists? ['fuel_efficiency < ?', 0]
|
13
|
-
"That's a strange looking fuel efficiency"
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
class AutomobileFuelType < ActiveRecord::Base
|
19
|
-
col :name
|
20
|
-
col :code
|
21
|
-
col :fuel_efficiency, :type => :float
|
22
|
-
col :fuel_efficiency_units
|
23
|
-
|
24
|
-
warn_if_any_blanks
|
25
|
-
warn_unless_size_is 1..6
|
26
|
-
end
|
27
|
-
|
28
|
-
AutomobileMake.auto_upgrade!
|
29
|
-
AutomobileFuelType.auto_upgrade!
|
30
|
-
|
31
|
-
class TestTableWarnings < Test::Unit::TestCase
|
32
|
-
def setup
|
33
|
-
AutomobileMake.delete_all
|
34
|
-
AutomobileFuelType.delete_all
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_001_warn_for_blanks_in_specific_columns
|
38
|
-
AutomobileMake.create! :name => ' ', :fuel_efficiency => nil, :fuel_efficiency_units => 'kilometres_per_litre'
|
39
|
-
AutomobileMake.create! :name => 'Alfa Romeo', :fuel_efficiency => 10.4075, :fuel_efficiency_units => 'kilometres_per_litre'
|
40
|
-
assert AutomobileMake.table_warnings.one? { |w| w =~ /blanks.*name.*column/ }
|
41
|
-
assert AutomobileMake.table_warnings.one? { |w| w =~ /blanks.*fuel_efficiency.*column/ }
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_002_warn_of_size
|
45
|
-
assert AutomobileMake.table_warnings.one? { |w| w =~ /expected.*100\.\.1000/ }
|
46
|
-
assert AutomobileFuelType.table_warnings.one? { |w| w =~ /expected.*1\.\.6/ }
|
47
|
-
end
|
48
|
-
|
49
|
-
def test_003_warn_for_blanks_in_any_column
|
50
|
-
AutomobileFuelType.create! :name => 'gas'
|
51
|
-
assert AutomobileFuelType.table_warnings.one? { |w| w =~ /blanks.*code.*column/ }
|
52
|
-
assert AutomobileFuelType.table_warnings.many? { |w| w =~ /blanks.*fuel_efficiency.*column/ }
|
53
|
-
end
|
54
|
-
|
55
|
-
def test_004_dont_treat_0_as_blank
|
56
|
-
AutomobileMake.create! :name => 'Acme', :fuel_efficiency => 0
|
57
|
-
assert !AutomobileMake.table_warnings.any? { |w| w =~ /blanks.*fuel_efficiency.*column/ }
|
58
|
-
end
|
59
|
-
|
60
|
-
def test_005_warn_if_arbitrary_block
|
61
|
-
AutomobileMake.create! :name => 'Acme', :fuel_efficiency => -5
|
62
|
-
assert AutomobileMake.table_warnings.one? { |w| w =~ /strange looking fuel efficiency/ }
|
63
|
-
end
|
64
|
-
end
|