data_factory 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -159,7 +159,7 @@ module DataFactory
159
159
 
160
160
  def generate_value(col)
161
161
  if col.nullable?
162
- return nil
162
+ return nil unless self.class.populate_nullable_columns
163
163
  end
164
164
 
165
165
  case col.data_type
@@ -235,7 +235,7 @@ module DataFactory
235
235
  def validate_columns(params)
236
236
  params.keys.each do |k|
237
237
  unless column_details.has_key? k.upcase
238
- raise DataFactory::ColumnNotInTable
238
+ raise DataFactory::ColumnNotInTable, "Column #{k.upcase} is not in #{table_name}"
239
239
  end
240
240
  end
241
241
  end
@@ -32,7 +32,7 @@ module DataFactory
32
32
 
33
33
  module BaseDSL
34
34
 
35
- attr_reader :table_name, :column_details, :column_defaults, :meta_data_loaded
35
+ attr_reader :table_name, :column_details, :column_defaults, :meta_data_loaded, :populate_nullable_columns
36
36
 
37
37
  # Pass a database interface object to be used by all DataFactory sub-classes
38
38
  # The interface must implement the following two methods:
@@ -67,6 +67,19 @@ module DataFactory
67
67
  # all instances of the class, but is not inherited with subclasses.
68
68
  def set_table_name(tab)
69
69
  @table_name = tab.to_s.upcase
70
+ @populate_nullable_columns = false
71
+ end
72
+
73
+ # By default, no values will be generated for columns that can be null. By calling this
74
+ # method in the class defintion, it will set @populate_nullable_columns to true, which will
75
+ # cause values to be generated for nullable columns in the same way as for not null columns
76
+ # @example
77
+ # class MyTab < DataFactory::Base
78
+ # set_table_name 'my_table'
79
+ # populate_nullable_columns
80
+ # end
81
+ def set_populate_nullable_columns
82
+ @populate_nullable_columns = true
70
83
  end
71
84
 
72
85
  # Sets the default value to be used for a column if nothing else is
@@ -105,7 +118,7 @@ module DataFactory
105
118
  load_meta_data unless meta_data_loaded
106
119
 
107
120
  unless @column_details.has_key?(column_name.to_s.upcase)
108
- raise DataFactory::ColumnNotInTable
121
+ raise DataFactory::ColumnNotInTable, "Column #{column_name.to_s.upcase} is not in #{table_name}"
109
122
  end
110
123
 
111
124
  @column_details[column_name.to_s.upcase]
@@ -167,7 +180,7 @@ module DataFactory
167
180
 
168
181
  def validate_column_default(column_name, column_value)
169
182
  unless @column_details.has_key? column_name
170
- raise DataFactory::ColumnNotInTable, column_name
183
+ raise DataFactory::ColumnNotInTable, "Column #{column_name.to_s.upcase} is not in #{table_name}"
171
184
  end
172
185
  end
173
186
 
@@ -105,6 +105,14 @@ class BaseAPITest < Test::Unit::TestCase
105
105
  assert_nil(instance.column_value('COL1'))
106
106
  end
107
107
 
108
+ def test_generate_data_generates_nil_if_column_nullable_unless_populate_nullable_columns_is_true
109
+ @klass.set_populate_nullable_columns
110
+ instance = @klass.new
111
+ instance.generate_column_data
112
+ assert_not_nil(instance.column_value('COL1'))
113
+ end
114
+
115
+
108
116
  def test_generate_data_generates_column_data_if_column_not_nullable
109
117
  instance = @klass.new
110
118
  instance.generate_column_data
@@ -130,6 +138,12 @@ class BaseAPITest < Test::Unit::TestCase
130
138
  assert_raises DataFactory::ColumnNotInTable do
131
139
  instance.generate_column_data(:notexist => 'OVERRIDE')
132
140
  end
141
+ # Also ensure the error message contains the column name
142
+ begin
143
+ instance.generate_column_data(:notexist => 'OVERRIDE')
144
+ rescue DataFactory::ColumnNotInTable => e
145
+ assert_match(/NOTEXIST is not in/, e.to_s)
146
+ end
133
147
  end
134
148
 
135
149
 
@@ -99,11 +99,20 @@ class BaseDSLTest < Test::Unit::TestCase
99
99
  def test_column_detail_raises_exception_if_column_does_not_exist
100
100
  @klass.set_database_interface(DBInterfaceMock.new)
101
101
  @klass.set_table_name(:foobar)
102
- assert_raises DataFactory::ColumnNotInTable do
102
+ assert_raises DataFactory::ColumnNotInTable do |e|
103
103
  assert(@klass.column_detail('COLNOTTHERE').is_a?(DataFactory::Column))
104
104
  end
105
105
  end
106
106
 
107
+ def test_error_message_contains_column_name_when_column_does_not_exist
108
+ @klass.set_database_interface(DBInterfaceMock.new)
109
+ @klass.set_table_name(:foobar)
110
+ begin
111
+ @klass.column_detail('COLNOTTHERE').is_a?(DataFactory::Column)
112
+ rescue DataFactory::ColumnNotInTable => e
113
+ assert_match(/COLNOTTHERE is not/, e.to_s)
114
+ end
115
+ end
107
116
 
108
117
  end
109
118
 
data/test/helper.rb CHANGED
@@ -10,7 +10,7 @@ require 'mocha/setup'
10
10
  module TestHelper
11
11
 
12
12
  DB_USER = 'sodonnel'
13
- DB_PASSWORD = 'sodonnel'
13
+ DB_PASSWORD = 'sodonnell'
14
14
  DB_SERVICE = 'local11gr2.world'
15
15
  DB_HOST = 'localhost'
16
16
  DB_PORT = '1521'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: data_factory
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-28 00:00:00.000000000 Z
12
+ date: 2013-03-17 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Generates data to insert into database tables, allowing columns to be
15
15
  defaulted or overriden. Intended to be used when testing wide tables where many