soveran-spawner 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,4 @@
1
- require 'ostruct'
1
+ require "ostruct"
2
2
 
3
3
  module Spawner
4
4
  def spawner &default
@@ -8,6 +8,7 @@ module Spawner
8
8
 
9
9
  def spawn attrs = {}
10
10
  @@spawn[self.name].call(model = OpenStruct.new)
11
- create!(model.send(:table).merge(attrs))
11
+ factory_method = respond_to?(:create!) ? :create! : :create
12
+ send(factory_method, model.send(:table).merge(attrs))
12
13
  end
13
14
  end
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "spawner"
3
- s.version = "0.0.7"
3
+ s.version = "0.0.8"
4
4
  s.date = "2008-12-18"
5
5
  s.summary = "Simple fixtures replacement for Sequel, ActiveRecord and probably many other ORMs"
6
6
  s.email = "michel@soveran.com"
@@ -15,5 +15,7 @@ Gem::Specification.new do |s|
15
15
  "lib/spawner.rb",
16
16
  "rails/init.rb",
17
17
  "extras/samples.rake",
18
- "test/all_test.rb"]
18
+ "test/all_test.rb",
19
+ "test/sequel_test.rb",
20
+ "test/active_record_test.rb"]
19
21
  end
@@ -0,0 +1,44 @@
1
+ require "rubygems"
2
+ require "active_record"
3
+ require "contest"
4
+ require File.dirname(__FILE__) + "/../lib/spawner"
5
+ require "faker"
6
+
7
+ ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
8
+ ActiveRecord::Schema.define do
9
+ create_table :active_record_users do |table|
10
+ table.column :name, :string
11
+ table.column :email, :string
12
+ end
13
+ end
14
+
15
+ class ActiveRecordUser < ActiveRecord::Base
16
+ extend Spawner
17
+
18
+ validates_presence_of :name
19
+
20
+ spawner do |user|
21
+ user.name = Faker::Name.name
22
+ user.email = Faker::Internet.email
23
+ end
24
+ end
25
+
26
+ class TestSpawnerWithActiveRecord < Test::Unit::TestCase
27
+ setup do
28
+ @user = ActiveRecordUser.spawn :name => "John"
29
+ end
30
+
31
+ context "spawned user" do
32
+ should "have John as name" do
33
+ assert_equal "John", @user.name
34
+ end
35
+
36
+ context "with invalid attributes" do
37
+ should "raise an error" do
38
+ assert_raise ActiveRecord::RecordInvalid do
39
+ ActiveRecordUser.spawn :name => nil
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -2,14 +2,14 @@ require 'rubygems'
2
2
  require 'contest'
3
3
  require File.dirname(__FILE__) + "/../lib/spawner"
4
4
 
5
- class Foo
5
+ class Base
6
6
  attr_accessor :attributes
7
7
 
8
8
  def initialize(attrs = {})
9
9
  @attributes = attrs
10
10
  end
11
11
 
12
- def self.create!(attrs = {})
12
+ def self.create(attrs = {})
13
13
  new(attrs)
14
14
  end
15
15
 
@@ -19,9 +19,15 @@ class Foo
19
19
 
20
20
  extend Spawner
21
21
 
22
- spawner do |foo|
23
- foo.bar = 7
24
- foo.baz = 8
22
+ spawner do |object|
23
+ object.bar = 7
24
+ object.baz = 8
25
+ end
26
+ end
27
+
28
+ class Foo < Base
29
+ class << self
30
+ alias_method :create!, :create
25
31
  end
26
32
  end
27
33
 
@@ -34,6 +40,9 @@ class Bar < Foo
34
40
  end
35
41
  end
36
42
 
43
+ class Baz < Base
44
+ end
45
+
37
46
  class TestFoo < Test::Unit::TestCase
38
47
  should "have a name class method" do
39
48
  assert Foo.respond_to?(:name)
@@ -97,5 +106,12 @@ class TestFoo < Test::Unit::TestCase
97
106
  end
98
107
  end
99
108
  end
109
+
110
+ context "and it doesn't understand #create! (only #create)" do
111
+ should "work anyway" do
112
+ baz = Baz.spawn :bar => 1
113
+ assert_equal 1, baz.bar
114
+ end
115
+ end
100
116
  end
101
117
  end
@@ -0,0 +1,41 @@
1
+ require "rubygems"
2
+ require "sequel"
3
+ require "contest"
4
+ require File.dirname(__FILE__) + "/../lib/spawner"
5
+ require "faker"
6
+
7
+ DB = Sequel.sqlite
8
+ DB << "CREATE TABLE sequel_users (name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL)"
9
+
10
+ class SequelUser < Sequel::Model
11
+ extend Spawner
12
+
13
+ validates do
14
+ presence_of :name
15
+ end
16
+
17
+ spawner do |user|
18
+ user.name = Faker::Name.name
19
+ user.email = Faker::Internet.email
20
+ end
21
+ end
22
+
23
+ class TestSpawnerWithSequel < Test::Unit::TestCase
24
+ setup do
25
+ @user = SequelUser.spawn :name => "John"
26
+ end
27
+
28
+ context "spawned user" do
29
+ should "have John as name" do
30
+ assert_equal "John", @user.name
31
+ end
32
+
33
+ context "with invalid attributes" do
34
+ should "raise an error" do
35
+ assert_raise Sequel::Error::InvalidValue do
36
+ SequelUser.spawn :name => nil
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: soveran-spawner
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michel Martens
@@ -29,6 +29,8 @@ files:
29
29
  - rails/init.rb
30
30
  - extras/samples.rake
31
31
  - test/all_test.rb
32
+ - test/sequel_test.rb
33
+ - test/active_record_test.rb
32
34
  has_rdoc: false
33
35
  homepage: http://github.com/soveran/spawner
34
36
  post_install_message: