populator 0.2.5 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,23 +3,23 @@ module Populator
3
3
  # called directly on Populator.
4
4
  module Random
5
5
  WORDS = %w(alias consequatur aut perferendis sit voluptatem accusantium doloremque aperiam eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo aspernatur aut odit aut fugit sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt neque dolorem ipsum quia dolor sit amet consectetur adipisci velit sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem ut enim ad minima veniam quis nostrum exercitationem ullam corporis nemo enim ipsam voluptatem quia voluptas sit suscipit laboriosam nisi ut aliquid ex ea commodi consequatur quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae et iusto odio dignissimos ducimus qui blanditiis praesentium laudantium totam rem voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident sed ut perspiciatis unde omnis iste natus error similique sunt in culpa qui officia deserunt mollitia animi id est laborum et dolorum fuga et harum quidem rerum facilis est et expedita distinctio nam libero tempore cum soluta nobis est eligendi optio cumque nihil impedit quo porro quisquam est qui minus id quod maxime placeat facere possimus omnis voluptas assumenda est omnis dolor repellendus temporibus autem quibusdam et aut consequatur vel illum qui dolorem eum fugiat quo voluptas nulla pariatur at vero eos et accusamus officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae itaque earum rerum hic tenetur a sapiente delectus ut aut reiciendis voluptatibus maiores doloribus asperiores repellat)
6
-
6
+
7
7
  # Pick a random value out of a given range.
8
8
  def value_in_range(range)
9
9
  case range.first
10
10
  when Integer then number_in_range(range)
11
11
  when Time then time_in_range(range)
12
12
  when Date then date_in_range(range)
13
- else range.to_a.rand
13
+ else range.to_a[rand(range.to_a.size)]
14
14
  end
15
15
  end
16
-
16
+
17
17
  # Generate a given number of words. If a range is passed, it will generate
18
18
  # a random number of words within that range.
19
19
  def words(total)
20
- (1..interpret_value(total)).map { WORDS.rand }.join(' ')
20
+ (1..interpret_value(total)).map { WORDS[rand(WORDS.size)] }.join(' ')
21
21
  end
22
-
22
+
23
23
  # Generate a given number of sentences. If a range is passed, it will generate
24
24
  # a random number of sentences within that range.
25
25
  def sentences(total)
@@ -27,7 +27,7 @@ module Populator
27
27
  words(5..20).capitalize
28
28
  end.join('. ')
29
29
  end
30
-
30
+
31
31
  # Generate a given number of paragraphs. If a range is passed, it will generate
32
32
  # a random number of paragraphs within that range.
33
33
  def paragraphs(total)
@@ -35,27 +35,27 @@ module Populator
35
35
  sentences(3..8).capitalize
36
36
  end.join("\n\n")
37
37
  end
38
-
38
+
39
39
  # If an array or range is passed, a random value will be selected to match.
40
40
  # All other values are simply returned.
41
41
  def interpret_value(value)
42
42
  case value
43
- when Array then value.rand
43
+ when Array then value[rand(value.size)]
44
44
  when Range then value_in_range(value)
45
45
  else value
46
46
  end
47
47
  end
48
-
48
+
49
49
  private
50
-
50
+
51
51
  def time_in_range(range)
52
52
  Time.at number_in_range(Range.new(range.first.to_i, range.last.to_i, range.exclude_end?))
53
53
  end
54
-
54
+
55
55
  def date_in_range(range)
56
56
  Date.jd number_in_range(Range.new(range.first.jd, range.last.jd, range.exclude_end?))
57
57
  end
58
-
58
+
59
59
  def number_in_range(range)
60
60
  if range.exclude_end?
61
61
  rand(range.last - range.first) + range.first
@@ -64,6 +64,6 @@ module Populator
64
64
  end
65
65
  end
66
66
  end
67
-
67
+
68
68
  extend Random # load it into the populator module directly so we can call the methods
69
69
  end
@@ -2,7 +2,7 @@ module Populator
2
2
  # This is what is passed to the block when calling populate.
3
3
  class Record
4
4
  attr_accessor :attributes
5
-
5
+
6
6
  # Creates a new instance of Record. Some attributes are set by default:
7
7
  #
8
8
  # * <tt>id</tt> - defaults to id passed
@@ -25,33 +25,33 @@ module Populator
25
25
  end
26
26
  end
27
27
  end
28
-
28
+
29
29
  # override id since method_missing won't catch this column name
30
30
  def id
31
31
  @attributes[:id]
32
32
  end
33
-
33
+
34
34
  # override type since method_missing won't catch this column name
35
35
  def type
36
36
  @attributes[:type]
37
37
  end
38
-
38
+
39
39
  # Return values for all columns inside an array.
40
40
  def attribute_values
41
41
  @columns.map do |column|
42
42
  @attributes[column.to_sym]
43
43
  end
44
44
  end
45
-
45
+
46
46
  def attributes=(values_hash)
47
47
  values_hash.each_pair do |key, value|
48
48
  value = value.call if value.is_a?(Proc)
49
49
  self.send((key.to_s + "=").to_sym, value)
50
50
  end
51
51
  end
52
-
52
+
53
53
  private
54
-
54
+
55
55
  def method_missing(sym, *args, &block)
56
56
  name = sym.to_s
57
57
  if @columns.include?(name.sub('=', ''))
@@ -1,14 +1,11 @@
1
- Running Specs
2
- -------------
1
+ = Running Populator Specs
3
2
 
4
- To prepare the specs, run this command.
3
+ To prepare the specs, run the following commands.
5
4
 
6
- script/setup
7
-
8
- This will generate the spec/database.yml file. Configure this to work
9
- with your databases. You can add and remove entries here as well to
10
- test different databases. A rake task is available to run the specs for
11
- each database.
5
+ bundle
6
+ cp spec/example_database.yml spec/database.yml
7
+
8
+ Configure the database.yml file to work with your databases. You can add and remove entries here as well to test different databases. A rake task is available to run the specs for each database.
12
9
 
13
10
  rake spec # Run specs under all databases
14
11
  rake spec:mysql # Run specs under mysql
@@ -16,8 +13,7 @@ each database.
16
13
  rake spec:postgresql # Run specs under postgresql
17
14
  ...
18
15
 
19
- Don't forget to create the user and database as necessary. You can do
20
- so under MySQL with these commands.
16
+ Don't forget to create the user and database as necessary. You can do so under MySQL with these commands.
21
17
 
22
18
  CREATE DATABASE populator_test;
23
19
  GRANT ALL ON populator_test.* TO populator@localhost;
@@ -1,13 +1,18 @@
1
- # you can add and remove entries to support different databases in tests.
2
-
3
1
  sqlite3:
4
2
  adapter: sqlite3
5
- database: spec/test.sqlite3
3
+ database: ":memory:"
6
4
  timeout: 5000
7
5
 
8
6
  mysql:
9
- adapter: mysql
7
+ adapter: mysql2
8
+ database: populator_test
9
+ username: root
10
+ password:
11
+ host: localhost
12
+
13
+ postgresql:
14
+ adapter: postgresql
10
15
  database: populator_test
11
- username: populator
12
- password:
16
+ username:
17
+ password:
13
18
  host: localhost
@@ -9,7 +9,7 @@ mysql:
9
9
  adapter: mysql
10
10
  database: populator_test
11
11
  username: populator
12
- password:
12
+ password:
13
13
  host: localhost
14
14
 
15
15
  postgresql:
@@ -8,7 +8,7 @@ class CreateCategories < ActiveRecord::Migration
8
8
  t.string :name
9
9
  end
10
10
  end
11
-
11
+
12
12
  def self.down
13
13
  drop_table :categories
14
14
  end
@@ -15,7 +15,7 @@ class CreateProducts < ActiveRecord::Migration
15
15
  t.integer :category_id
16
16
  end
17
17
  end
18
-
18
+
19
19
  def self.down
20
20
  drop_table :products
21
21
  end
@@ -1,17 +1,17 @@
1
- require File.dirname(__FILE__) + '/../spec_helper.rb'
1
+ require "spec_helper"
2
2
 
3
3
  describe Populator::Factory do
4
4
  describe "for products" do
5
5
  before(:each) do
6
6
  @factory = Populator::Factory.for_model(Product)
7
7
  end
8
-
8
+
9
9
  it "should only use one query when inserting records" do
10
10
  $queries_executed = []
11
11
  @factory.populate(5)
12
12
  $queries_executed.grep(/^insert/i).should have(1).record
13
13
  end
14
-
14
+
15
15
  it "should start id at 1 and increment when table is empty" do
16
16
  Product.delete_all
17
17
  expected_id = 1
@@ -20,7 +20,7 @@ describe Populator::Factory do
20
20
  expected_id += 1
21
21
  end
22
22
  end
23
-
23
+
24
24
  it "should start id at last id and increment" do
25
25
  Product.delete_all
26
26
  product = Product.create
@@ -30,21 +30,21 @@ describe Populator::Factory do
30
30
  expected_id += 1
31
31
  end
32
32
  end
33
-
33
+
34
34
  it "should generate within range" do
35
35
  Product.delete_all
36
36
  @factory.populate(2..4)
37
37
  Product.count.should >= 2
38
38
  Product.count.should <= 4
39
39
  end
40
-
40
+
41
41
  it "should limit number of records per query" do
42
42
  $queries_executed = []
43
43
  @factory.populate(5, :per_query => 2)
44
44
  $queries_executed.grep(/^insert/i).should have(3).records
45
45
  end
46
46
  end
47
-
47
+
48
48
  it "should only use two queries when nesting factories (one for each class)" do
49
49
  $queries_executed = []
50
50
  Populator::Factory.for_model(Category).populate(3) do |category|
@@ -54,7 +54,7 @@ describe Populator::Factory do
54
54
  end
55
55
  $queries_executed.grep(/^insert/i).should have(2).records
56
56
  end
57
-
57
+
58
58
  it "should only use one query when nesting factories of the same type" do
59
59
  $queries_executed = []
60
60
  Populator::Factory.for_model(Product).populate(3) do |product|
@@ -62,7 +62,7 @@ describe Populator::Factory do
62
62
  end
63
63
  $queries_executed.grep(/^insert/i).should have(1).record
64
64
  end
65
-
65
+
66
66
  it "should default to 1000 records per query" do
67
67
  Populator::Factory::DEFAULT_RECORDS_PER_QUERY.should == 1000
68
68
  end
@@ -1,29 +1,29 @@
1
- require File.dirname(__FILE__) + '/../spec_helper.rb'
1
+ require "spec_helper"
2
2
 
3
3
  describe Populator::ModelAdditions do
4
4
  it "should add populate method to active record class" do
5
5
  Product.should respond_to(:populate)
6
6
  end
7
-
7
+
8
8
  it "should add 10 records to database" do
9
9
  Product.delete_all
10
10
  Product.populate(10)
11
11
  Product.count.should == 10
12
12
  end
13
-
13
+
14
14
  it "should set attribute columns" do
15
15
  Product.populate(1) do |product|
16
16
  product.name = "foo"
17
17
  end
18
18
  Product.last.name.should == "foo"
19
19
  end
20
-
20
+
21
21
  it "should not pass in an instance of Active Record for performance reasons" do
22
22
  Product.populate(1) do |product|
23
23
  product.should_not be_kind_of(ActiveRecord::Base)
24
24
  end
25
25
  end
26
-
26
+
27
27
  it "should not pass options hash" do
28
28
  $queries_executed = []
29
29
  Product.populate(5, :per_query => 2) do |product|
@@ -1,48 +1,48 @@
1
- require File.dirname(__FILE__) + '/../spec_helper.rb'
1
+ require "spec_helper"
2
2
 
3
3
  describe Populator::Random do
4
4
  it "should pick a random number in range excluding last value" do
5
- Populator.expects(:rand).with(5).returns(3)
5
+ Populator.stubs(:rand).with(5).returns(3)
6
6
  Populator.value_in_range(10...15).should == 13
7
7
  end
8
-
8
+
9
9
  it "should pick a random number in range including last value" do
10
- Populator.expects(:rand).with(5).returns(3)
10
+ Populator.stubs(:rand).with(5).returns(3)
11
11
  Populator.value_in_range(10..14).should == 13
12
12
  end
13
-
13
+
14
14
  it "should pick a random time in range" do
15
15
  start_time = 2.days.ago
16
16
  end_time = Time.now
17
- Populator.expects(:rand).with(end_time.to_i-start_time.to_i).returns(1)
17
+ Populator.stubs(:rand).with(end_time.to_i-start_time.to_i).returns(1)
18
18
  Populator.value_in_range(start_time...end_time).should == Time.at(start_time.to_i + 1)
19
19
  end
20
-
20
+
21
21
  it "should pick a random date in range" do
22
22
  start_date = 2.years.ago.to_date
23
23
  end_date = Date.today
24
- Populator.expects(:rand).with(end_date.jd-start_date.jd).returns(1)
24
+ Populator.stubs(:rand).with(end_date.jd-start_date.jd).returns(1)
25
25
  Populator.value_in_range(start_date...end_date).should == Date.jd(start_date.jd + 1)
26
26
  end
27
-
27
+
28
28
  it "should pick a random string by converting to array" do
29
- Kernel.expects(:rand).with(5).returns(2)
29
+ Populator.stubs(:rand).with(5).returns(2)
30
30
  Populator.value_in_range('a'..'e').should == 'c'
31
31
  end
32
-
32
+
33
33
  it "should pick 3 random words" do
34
34
  Populator.words(3).split.should have(3).records
35
35
  end
36
-
36
+
37
37
  it "should pick a random number of random words" do
38
- Populator.expects(:rand).with(5).returns(3)
38
+ Populator.stubs(:rand).returns(3)
39
39
  Populator.words(10...15).split.should have(13).records
40
40
  end
41
-
41
+
42
42
  it "should generate 3 random sentences" do
43
43
  Populator.sentences(3).split(/\. [A-Z]/).should have(3).records
44
44
  end
45
-
45
+
46
46
  it "should generate 3 random paragraphs" do
47
47
  Populator.paragraphs(3).split(/\n\n/).should have(3).records
48
48
  end
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../spec_helper.rb'
1
+ require "spec_helper"
2
2
 
3
3
  describe Populator::Record do
4
4
  it "should have a writer and reader methods for each column" do
@@ -8,31 +8,31 @@ describe Populator::Record do
8
8
  record.send(column).should == "foo"
9
9
  end
10
10
  end
11
-
11
+
12
12
  it "should return attribute values in same order as columns" do
13
13
  record = Populator::Record.new(Product, nil)
14
14
  record.name = "foo"
15
15
  expected = Product.column_names.map { |c| "foo" if c == 'name' }
16
16
  record.attribute_values.should == expected
17
17
  end
18
-
18
+
19
19
  it "should assign second parameter to id" do
20
20
  Populator::Record.new(Product, 2).id.should == 2
21
21
  end
22
-
22
+
23
23
  it "should pick random number from range" do
24
24
  record = Populator::Record.new(Product, 1)
25
25
  record.stock = 2..5
26
26
  record.stock.should >= 2
27
27
  record.stock.should <= 5
28
28
  end
29
-
29
+
30
30
  it "should pick random value from array" do
31
31
  record = Populator::Record.new(Product, 1)
32
32
  record.name = %w[foo bar]
33
33
  %w[foo bar].should include(record.name)
34
34
  end
35
-
35
+
36
36
  it "should automatically set created/updated columns" do
37
37
  Product.stubs(:column_names).returns(%w[id created_at updated_at created_on updated_on])
38
38
  record = Populator::Record.new(Product, 1)
@@ -41,31 +41,31 @@ describe Populator::Record do
41
41
  record.created_on.should == Date.today
42
42
  record.updated_on.should == Date.today
43
43
  end
44
-
44
+
45
45
  it "should use custom primary_key for auto-increment if specified" do
46
46
  Product.stubs(:primary_key).returns('foo')
47
47
  Product.stubs(:column_names).returns(['foo', 'name'])
48
48
  Populator::Record.new(Product, 123).foo.should == 123
49
49
  end
50
-
50
+
51
51
  it "should default type to class name" do
52
52
  Product.stubs(:column_names).returns(['id', 'type'])
53
53
  Populator::Record.new(Product, 1).type.should == 'Product'
54
54
  end
55
-
55
+
56
56
  it "should default specified inheritance_column to class name" do
57
57
  Product.stubs(:inheritance_column).returns('foo')
58
58
  Product.stubs(:column_names).returns(['id', 'foo'])
59
59
  Populator::Record.new(Product, 1).foo.should == 'Product'
60
60
  end
61
-
61
+
62
62
  it "should allow set via attributes hash" do
63
63
  record = Populator::Record.new(Product, 1)
64
64
  record.attributes = {:stock => 2..5}
65
65
  record.stock.should >= 2
66
66
  record.stock.should <= 5
67
67
  end
68
-
68
+
69
69
  it "should take a proc object via attributes hash" do
70
70
  record = Populator::Record.new(Product, 1)
71
71
  record.attributes = {:stock => lambda {15}}