data_factory 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +56 -5
- data/lib/data_factory/base_api.rb +1 -1
- metadata +33 -46
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# DataFactory
|
2
2
|
|
3
|
-
DataFactory is a simple Ruby gem that generates
|
3
|
+
DataFactory is a simple Ruby gem that generates random test data and inserts it into database tables. It was created to help with unit testing in a project that had database tables having many (50+) columns, and manually crafing insert statements was tedious and error prone.
|
4
4
|
|
5
5
|
DataFactory reads the table definition from the database, and generates random values for all not null columns. It inserts this data into the table, while providing the option of specifying non-random defaults.
|
6
6
|
|
@@ -16,7 +16,7 @@ There are no strict dependencies for DataFactory to work, and it is a pure Ruby
|
|
16
16
|
|
17
17
|
However, DataFactory doesn't actually have a database access layer built in, as it is designed to use an external access layer that knows how to connect and query the database.
|
18
18
|
|
19
|
-
If you can use JRuby, consider using Simple JDBC Oracle to interact with your database. If you cannot use JRuby, implementing your own database interface is
|
19
|
+
If you can use JRuby, consider using Simple JDBC Oracle to interact with your database. If you cannot use JRuby, implementing your own database interface is not too difficult. Create a class that handles creating a database connection, and implement the following three methods to run SQL statements on the database, and issue commits:
|
20
20
|
|
21
21
|
# should return an object that implements the each_array method
|
22
22
|
# below
|
@@ -29,7 +29,7 @@ If you can use JRuby, consider using Simple JDBC Oracle to interact with your da
|
|
29
29
|
def each_array(&blk)
|
30
30
|
end
|
31
31
|
|
32
|
-
The first two methods are fairly obvious. The each_array method is expected to iterate over the result set returned by the executed sql statement,
|
32
|
+
The first two methods are fairly obvious. The each_array method is expected to iterate over the result set returned by the executed sql statement, ie execute_sql should return an object that responds to each_array. The each_array method accepts a block, and will pass an array to the block for each row in the result set. The array passed to the block should contain an element for each column selected by the original query. The array should contain the value of each column in Ruby types, ie not Java types if using JRuby.
|
33
33
|
|
34
34
|
The OCI8 gem is pretty good place to start if you are using MRI Ruby, but you will need the Oracle client installed. Raw JDBC can get the job done if you are using JRuby and you do not want to use Simple JDBC Oracle.
|
35
35
|
|
@@ -44,7 +44,7 @@ For these examples to run, create a table on the database as follows:
|
|
44
44
|
first_name varchar2(50),
|
45
45
|
last_name varchar2(50),
|
46
46
|
email varchar2(50),
|
47
|
-
ssn varchar2(10));
|
47
|
+
ssn varchar2(10) not null);
|
48
48
|
|
49
49
|
## Define a DataFactory Class
|
50
50
|
|
@@ -85,6 +85,28 @@ The create! call will take the column defaults defined in the Employee class, an
|
|
85
85
|
|
86
86
|
An Employee instance is returned, containing all the generated values.
|
87
87
|
|
88
|
+
There is also a create method that works just like create! but does not issue a commit.
|
89
|
+
|
90
|
+
Finally there is a build method that creates an instance of the class with default and generated values, but does not insert it into the database at all.
|
91
|
+
|
92
|
+
## Accessing The Column Values
|
93
|
+
|
94
|
+
When an instance of a DataFactory class is created, you can access the generated values for the columns with the column_values method, which returns a hash. The keys of the hash are the uppercase column names and the values contain the generated data:
|
95
|
+
|
96
|
+
f.column_values.keys.each do |k|
|
97
|
+
puts "#{k} :: #{f.column_values[k]}"
|
98
|
+
end
|
99
|
+
|
100
|
+
# EMP_ID :: 1001
|
101
|
+
# DEPT_ID ::
|
102
|
+
# FIRST_NAME ::
|
103
|
+
# LAST_NAME :: Smith
|
104
|
+
# EMAIL :: 4506@5941.com
|
105
|
+
# SSN :: Gb3
|
106
|
+
|
107
|
+
Notice how columns that are nullable, have not got a default value and were not passed a value are generated with null values.
|
108
|
+
|
109
|
+
|
88
110
|
## Putting It Together
|
89
111
|
|
90
112
|
Combining each of the steps above, gives the following script:
|
@@ -114,14 +136,43 @@ Combining each of the steps above, gives the following script:
|
|
114
136
|
f = Employee.create!("emp_id" => 1001)
|
115
137
|
|
116
138
|
f.column_values.keys.each do |k|
|
117
|
-
puts f.column_values[k]
|
139
|
+
puts "#{k} :: #{f.column_values[k]}"
|
118
140
|
end
|
119
141
|
|
120
142
|
## Other Methods
|
121
143
|
|
122
144
|
The sample above illustrates how the create! method returns an Employee object, giving access to the generated values. For an overview of other methods browse the documentation for the base_api, base_dsl and base_factory methods.
|
123
145
|
|
146
|
+
## Using multiple Database connections
|
147
|
+
|
148
|
+
If you look at the source for the Base class, you can see it doesn't define any methods on its own:
|
149
|
+
|
150
|
+
class Base
|
151
|
+
|
152
|
+
extend BaseDSL
|
153
|
+
extend BaseFactory
|
154
|
+
|
155
|
+
include BaseAPI
|
156
|
+
|
157
|
+
end
|
158
|
+
|
159
|
+
In the examples above, the database interface is set by calling the set_database_interface method on Base:
|
160
|
+
|
161
|
+
DataFactory::Base.set_database_interface(interface)
|
162
|
+
|
163
|
+
Behind the scenes, this sets a class variable which is inherited by all other classes that inherit from Base - in other words, once you set the database interface on Base, that connection is shared by all sub classes.
|
164
|
+
|
165
|
+
This creates a problem if you need to have connections to multiple databases, with some Data Factory classes pointing to one database and other to another. To work around this, you can easily create a new Base class to inherit from, by including the relevant Data Factory modules:
|
166
|
+
|
167
|
+
class NewBase
|
168
|
+
extend DataFactory::BaseDSL
|
169
|
+
extend DataFactory::BaseFactory
|
170
|
+
|
171
|
+
include DataFactory::BaseAPI
|
172
|
+
|
173
|
+
end
|
124
174
|
|
175
|
+
I may make improvements in the future to remove this limitation and improve the design.
|
125
176
|
|
126
177
|
|
127
178
|
|
metadata
CHANGED
@@ -1,77 +1,64 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_factory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
4
|
+
version: 0.1.2
|
5
|
+
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Stephen O'Donnell
|
9
|
-
autorequire:
|
9
|
+
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-02-
|
12
|
+
date: 2013-02-28 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
|
-
description: Generates data to insert into database tables, allowing columns to be
|
14
|
+
description: Generates data to insert into database tables, allowing columns to be
|
15
|
+
defaulted or overriden. Intended to be used when testing wide tables where many
|
16
|
+
not null columns may need to be populated but are not part of the test
|
15
17
|
email: stephen@betteratoracle.com
|
16
18
|
executables: []
|
17
19
|
extensions: []
|
18
20
|
extra_rdoc_files:
|
19
21
|
- README.md
|
20
22
|
files:
|
21
|
-
-
|
22
|
-
|
23
|
-
-
|
24
|
-
|
25
|
-
-
|
26
|
-
|
27
|
-
-
|
28
|
-
|
29
|
-
-
|
30
|
-
|
31
|
-
-
|
32
|
-
|
33
|
-
-
|
34
|
-
|
35
|
-
-
|
36
|
-
bGliL2RhdGFfZmFjdG9yeS9iYXNlX2FwaS5yYg==
|
37
|
-
- !binary |-
|
38
|
-
bGliL2RhdGFfZmFjdG9yeS9iYXNlX2RzbC5yYg==
|
39
|
-
- !binary |-
|
40
|
-
bGliL2RhdGFfZmFjdG9yeS9iYXNlX2ZhY3RvcnkucmI=
|
41
|
-
- !binary |-
|
42
|
-
bGliL2RhdGFfZmFjdG9yeS9jb2x1bW4ucmI=
|
43
|
-
- !binary |-
|
44
|
-
bGliL2RhdGFfZmFjdG9yeS9leGNlcHRpb25zLnJi
|
45
|
-
- !binary |-
|
46
|
-
bGliL2RhdGFfZmFjdG9yeS9yYW5kb20ucmI=
|
47
|
-
- !binary |-
|
48
|
-
UmFrZWZpbGUucmI=
|
49
|
-
- !binary |-
|
50
|
-
UkVBRE1FLm1k
|
23
|
+
- test/base_api_test.rb
|
24
|
+
- test/base_dsl_test.rb
|
25
|
+
- test/helper.rb
|
26
|
+
- test/sanity.rb
|
27
|
+
- test/test_runner.rb
|
28
|
+
- lib/data_factory/base.rb
|
29
|
+
- lib/data_factory/base_api.rb
|
30
|
+
- lib/data_factory/base_dsl.rb
|
31
|
+
- lib/data_factory/base_factory.rb
|
32
|
+
- lib/data_factory/column.rb
|
33
|
+
- lib/data_factory/exceptions.rb
|
34
|
+
- lib/data_factory/random.rb
|
35
|
+
- lib/data_factory.rb
|
36
|
+
- Rakefile.rb
|
37
|
+
- README.md
|
51
38
|
homepage: http://betteratoracle.com
|
52
39
|
licenses: []
|
53
|
-
post_install_message:
|
40
|
+
post_install_message:
|
54
41
|
rdoc_options: []
|
55
42
|
require_paths:
|
56
43
|
- lib
|
57
44
|
required_ruby_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
58
46
|
requirements:
|
59
47
|
- - ! '>='
|
60
48
|
- !ruby/object:Gem::Version
|
61
|
-
version:
|
62
|
-
MA==
|
63
|
-
none: false
|
49
|
+
version: '0'
|
64
50
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
65
52
|
requirements:
|
66
53
|
- - ! '>='
|
67
54
|
- !ruby/object:Gem::Version
|
68
|
-
version:
|
69
|
-
MA==
|
70
|
-
none: false
|
55
|
+
version: '0'
|
71
56
|
requirements: []
|
72
|
-
rubyforge_project:
|
73
|
-
rubygems_version: 1.8.
|
74
|
-
signing_key:
|
57
|
+
rubyforge_project:
|
58
|
+
rubygems_version: 1.8.24
|
59
|
+
signing_key:
|
75
60
|
specification_version: 3
|
76
|
-
summary: A gem to generate template insert statements for use when unit testing database
|
61
|
+
summary: A gem to generate template insert statements for use when unit testing database
|
62
|
+
code
|
77
63
|
test_files: []
|
64
|
+
has_rdoc: true
|