temping 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,46 @@
1
+ # Temping
2
+
3
+ Temping is used to create temporary table-backed ActiveRecord models for use in tests.
4
+
5
+ include Temping
6
+
7
+ before(:all) do
8
+ create_model :posts do
9
+ with_columns do |table|
10
+ table.string :headline
11
+ table.text :body
12
+ table.integer :view_count
13
+ end
14
+
15
+ validates_presence_of :headline
16
+
17
+ def popular?
18
+ view_count > 100
19
+ end
20
+ end
21
+
22
+ describe "#popular" do
23
+ context "when a post is view_count over 100 times" do
24
+ it "returns true" do
25
+ post = Post.create! do |p|
26
+ p.headline = "Headline"
27
+ p.view_count = 200
28
+ end
29
+
30
+ post.should be_popular
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ This is especially useful if testing an ActiveRecord plugin or a Module used in a Rails application:
37
+
38
+ include Temping
39
+
40
+ before(:all) do
41
+ create_model :posts do
42
+ include HasArticles
43
+ end
44
+ end
45
+
46
+ describe "HasArticles tests"...
@@ -0,0 +1,23 @@
1
+ require 'spec/rake/spectask'
2
+
3
+ task :default => [:spec]
4
+
5
+ Spec::Rake::SpecTask.new(:spec) do |spec|
6
+ spec.libs << 'lib' << 'spec'
7
+ spec.spec_files = FileList['spec/**/*_spec.rb']
8
+ end
9
+
10
+ begin
11
+ require 'jeweler'
12
+ Jeweler::Tasks.new do |gem|
13
+ gem.name = "temping"
14
+ gem.summary = "Create temporary table-backed ActiveRecord models for use in tests"
15
+ gem.homepage = "http://github.com/jpignata/temping"
16
+ gem.email = "john.pignata@gmail.com"
17
+ gem.authors = ["John Pignata"]
18
+ gem.add_dependency "rails", ">= 2.3.3"
19
+ gem.add_development_dependency "rspec", "1.2.9"
20
+ end
21
+ rescue LoadError
22
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
23
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
@@ -0,0 +1,39 @@
1
+ require 'active_record'
2
+ require 'active_support'
3
+
4
+ module Temping
5
+ def create_model(model_name, &block)
6
+ unless eval("defined?(#{model_name.to_s.classify})")
7
+ ModelFactory.new(model_name, &block).klass
8
+ end
9
+ end
10
+
11
+ class ModelFactory
12
+ attr_accessor :klass
13
+
14
+ def initialize(model_name, &block)
15
+ @klass = Class.new(ActiveRecord::Base)
16
+ Object.const_set(model_name.to_s.classify, @klass)
17
+ create_table
18
+ add_methods
19
+ @klass.class_eval(&block) if block_given?
20
+ end
21
+
22
+ private
23
+
24
+ def create_table
25
+ @klass.connection.create_table(@klass.table_name, :temporary => true) do |table|
26
+ table.integer :id
27
+ end
28
+ end
29
+
30
+ def add_methods
31
+ class << @klass
32
+ def with_columns
33
+ connection.change_table(table_name) { |table| yield(table) }
34
+ end
35
+ end
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,20 @@
1
+ $: << File.join(File.dirname(__FILE__), "/../lib")
2
+
3
+ require 'spec'
4
+ require 'temping'
5
+
6
+ ActiveRecord::Base.configurations = {
7
+ 'mysql' => { :adapter => 'mysql',
8
+ :username => 'rails',
9
+ :database => 'activerecord_unittest',
10
+ :host => '127.0.0.1' },
11
+
12
+ 'postgres' => { :adapter => 'postgresql',
13
+ :database => 'activerecord_unittest',
14
+ :min_messages => 'warning' },
15
+
16
+ 'sqlite3' => { :adapter => 'sqlite3',
17
+ :database => ':memory:' }
18
+ }
19
+
20
+ ActiveRecord::Base.establish_connection 'mysql'
@@ -0,0 +1,59 @@
1
+ require File.join(File.dirname(__FILE__), "/spec_helper")
2
+
3
+ describe Temping do
4
+ include Temping
5
+
6
+ describe ".create_model" do
7
+ it "creates and returns an AR::Base-derived model" do
8
+ posts = create_model :posts
9
+ posts.ancestors.should include(ActiveRecord::Base)
10
+ end
11
+
12
+ it "evals all statements passed in through a block" do
13
+ votes = create_model :votes do
14
+ with_columns do |table|
15
+ table.string :voter
16
+ end
17
+
18
+ validates_presence_of :voter
19
+ end
20
+
21
+ vote = Vote.new
22
+ vote.should_not be_valid
23
+
24
+ vote.voter = "John Pignata"
25
+ vote.should be_valid
26
+ end
27
+
28
+ describe ".with_columns" do
29
+ it "creates columns passed in through a block" do
30
+ create_model :comments do
31
+ with_columns do |table|
32
+ table.integer :count
33
+ table.string :headline
34
+ table.text :body
35
+ end
36
+ end
37
+
38
+ Comment.columns.map(&:name).should include("count", "headline", "body")
39
+ end
40
+ end
41
+ end
42
+
43
+ describe "database agnostism" do
44
+ it "supports MySQL" do
45
+ ActiveRecord::Base.establish_connection 'mysql'
46
+ create_model(:apples).should == Apple
47
+ end
48
+
49
+ it "supports PostgreSQL" do
50
+ ActiveRecord::Base.establish_connection 'postgres'
51
+ create_model(:cherries).should == Cherry
52
+ end
53
+
54
+ it "supports Sqlite3" do
55
+ ActiveRecord::Base.establish_connection 'sqlite3'
56
+ create_model(:oranges).should == Orange
57
+ end
58
+ end
59
+ end
metadata ADDED
@@ -0,0 +1,80 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: temping
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - John Pignata
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-12-15 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.3.3
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.2.9
34
+ version:
35
+ description:
36
+ email: john.pignata@gmail.com
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README.md
43
+ files:
44
+ - README.md
45
+ - Rakefile
46
+ - VERSION
47
+ - lib/temping.rb
48
+ - spec/spec_helper.rb
49
+ - spec/temping_spec.rb
50
+ has_rdoc: true
51
+ homepage: http://github.com/jpignata/temping
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options:
56
+ - --charset=UTF-8
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: "0"
64
+ version:
65
+ required_rubygems_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: "0"
70
+ version:
71
+ requirements: []
72
+
73
+ rubyforge_project:
74
+ rubygems_version: 1.3.5
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: Create temporary table-backed ActiveRecord models for use in tests
78
+ test_files:
79
+ - spec/spec_helper.rb
80
+ - spec/temping_spec.rb