Browse files

Working within Rails 3, sketching in some self-contained tests

  • Loading branch information...
1 parent 722668d commit fc8ffae6c2a9201209667785e26543e17503fdca @tadman tadman committed Sep 1, 2010
Showing with 103 additions and 20 deletions.
  1. +10 −1 Rakefile
  2. +1 −1 VERSION
  3. +14 −15 lib/test_dummy.rb
  4. +50 −0 lib/test_dummy/helper.rb
  5. +5 −0 test/config/database.yml
  6. +1 −0 test/db/.gitignore
  7. +4 −0 test/helper.rb
  8. +5 −0 test/models/simple.rb
  9. +7 −2 test/test_test_dummy.rb
  10. +6 −1 test_dummy.gemspec
View
11 Rakefile
@@ -36,7 +36,7 @@ rescue LoadError
end
end
-task :test => :check_dependencies
+task :test => [ :check_dependencies, "test:db:prepare" ]
task :default => :test
@@ -49,3 +49,12 @@ Rake::RDocTask.new do |rdoc|
rdoc.rdoc_files.include('README*')
rdoc.rdoc_files.include('lib/**/*.rb')
end
+
+namespace :test do
+ namespace :db do
+ desc "Prepares the test Sqlite3 database"
+ task :prepare do
+ # ...
+ end
+ end
+end
View
2 VERSION
@@ -1 +1 @@
-0.1.2
+0.1.3
View
29 lib/test_dummy.rb
@@ -1,6 +1,7 @@
module TestDummy
require 'test_dummy/railtie'
+ autoload(:Helper, File.expand_path('test_dummy/helper', File.dirname(__FILE__)))
autoload(:TestHelper, File.expand_path('test_dummy/test_helper', File.dirname(__FILE__)))
def self.included(base)
@@ -31,14 +32,14 @@ def self.combine_attributes(*sets)
end
end
- # Adds a mixin to the core DummyMethods module
+ # Adds a mixin to the core Helper module
def self.add_module(new_module)
- DummyMethods.send(:extend, new_module)
+ Helper.send(:extend, new_module)
end
- # Used in an initializer to define things that can be dummyd by all
+ # Used in an initializer to define things that can be dummied by all
# models if these properties are available.
- def self.can_dummy(*names, &block)
+ def self.to_dummy(*names, &block)
case (names.last)
when Hash
options = names.pop
@@ -48,7 +49,9 @@ def self.can_dummy(*names, &block)
block = options[:with]
end
- DummyMethods.send(
+ # Create a temporary Module and use this to roll up the methods defined
+ # into the Helper module
+ Helper.send(
:extend,
names.inject(Module.new) do |m, name|
m.send(:define_method, name, &block)
@@ -62,10 +65,6 @@ def self.config(&block)
TestDummy.instance_eval(&block)
end
- module DummyMethods
- # Container for common data faking methods as they are defined.
- end
-
module ClassMethods
# Returns a Hash which describes the dummy configuration for this
# Model class.
@@ -77,7 +76,7 @@ def dummy_attributes
# that can receive up to two parameters, the first the instance of
# the model being created, the second the parameters supplied to create
# it. The first and second parameters may be nil.
- def can_dummy(*names, &block)
+ def to_dummy(*names, &block)
options = nil
case (names.last)
@@ -97,7 +96,7 @@ def can_dummy(*names, &block)
# For associations, delay creation of block until first call
# to allow for additional relationships to be defined after
- # the can_dummy call. Leave placeholder (true) instead.
+ # the to_dummy call. Leave placeholder (true) instead.
@test_dummy[name] = block || true
@test_dummy_order << name
@@ -119,7 +118,7 @@ def can_dummy?(*names)
# the dummy operation is completed. Returns a dummy model which has not
# been saved.
def build_dummy(with_attributes = nil)
- model = new(TestDummy::Support.combine_attributes(scope(:create), with_attributes))
+ model = new(TestDummy::Support.combine_attributes(scoped.scope_for_create, with_attributes))
yield(model) if (block_given?)
@@ -157,15 +156,15 @@ def create_dummy!(with_attributes = nil, &block)
# Produces dummy data for a single attribute.
def dummy(name, with_attributes = nil)
- with_attributes = TestDummy.combine_attributes(scope(:create), with_attributes)
+ with_attributes = TestDummy.combine_attributes(scoped.scope_for_create, with_attributes)
dummy_method_call(nil, with_attributes, dummy_method(name))
end
# Produces a complete set of dummy attributes. These can be used to
# create a model.
def dummy_attributes(with_attributes = nil)
- with_attributes = TestDummy.combine_attributes(scope(:create), with_attributes)
+ with_attributes = TestDummy.combine_attributes(scoped.scope_for_create, with_attributes)
@test_dummy_order.each do |field|
unless (with_attributes.key?(field))
@@ -223,7 +222,7 @@ def dummy_method(name)
when Module
block.method(name)
when Symbol
- DummyMethods.method(name)
+ Helper.method(name)
when true
# Configure association dummyr the first time it is called
if (reflection = reflect_on_association(name))
View
50 lib/test_dummy/helper.rb
@@ -0,0 +1,50 @@
+module TestDummy::Helper
+ ALPHANUMERIC_SET = [ 'a'..'z', 'A'..'Z', '0'..'9' ].collect(&:to_a).flatten.freeze
+
+ # There are certain substrings that shouldn't be emitted as part of the
+ # random strings in order to keep them safe and inoffensive. While this is
+ # not exhaustive, it should scrub the random strings enough that it would
+ # be a stretch to see something wrong with them, at least in English.
+ AVOID_SUBSTRINGS = Regexp.new(%w[
+ ass a55 as5 a5s bal bit btc b1t bul bll bls but btu chi ch1 cun cnt dic dik d1c d1k fag f4g fuc fuk fck fcu fkn fkc kik kyk k1k jer jrk j3r jew j3w mot m0t m07 mth m7h mtr m7r neg n3g ngr nig n1g pak p4k pof p0f poo po0 p00 que qu3 qee q3e qe3 shi sh1 shy stf sht sfu spi spk sp1 tar t4r trd wtf wth xxx
+ ].join('|'))
+
+ def random_string(length = 12)
+ string = nil
+
+ while (!string or !AVOID_WORDS.match(string))
+ string = ''
+
+ length.times do
+ string << CHARACTER_SET.rand
+ end
+
+ # As it's not especially hard to find a random word that passes this
+ # simple filter for relatively short strings, the chance of this loop
+ # spinning for an extended period of time is very slim.
+ end
+
+ string
+ end
+
+ CONSONANTS = %w[ b c d f g h j k l m n p qu r s t v w x z ch cr fr nd ng nk nt ph pr rd sh sl sp st th tr ]
+ VOWELS = %w[ a e i o u y ]
+
+ def random_phonetic_string(length = 12)
+ string = nil
+
+ while (!string or !AVOID_WORDS.match(string))
+ string = ''
+
+ length.times do |i|
+ word << (i % 2 != 0) ? CONSONANTS[rand(CONSONANTS.size)] : VOWELS[rand(VOWELS.size)]
+ end
+ end
+
+ # The generated string is probably longer than it needs to be, so trim
+ # to fit.
+ string.to_s[0, length]
+ end
+
+ extend(self)
+end
View
5 test/config/database.yml
@@ -0,0 +1,5 @@
+test:
+ adapter: sqlite3
+ database: db/test.sqlite3
+ pool: 5
+ timeout: 5000
View
1 test/db/.gitignore
@@ -0,0 +1 @@
+*.sqlite3
View
4 test/helper.rb
@@ -4,6 +4,10 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
+gem 'rails'
+require 'rails'
+gem 'activerecord'
+
require 'test_dummy'
class Test::Unit::TestCase
View
5 test/models/simple.rb
@@ -0,0 +1,5 @@
+class Simple
+ to_dummy :name do
+ TestDummy::Helper.random_string(8)
+ end
+end
View
9 test/test_test_dummy.rb
@@ -1,7 +1,12 @@
require 'helper'
class TestTestDummy < Test::Unit::TestCase
- should "probably rename this file and start testing for real" do
- flunk "hey buddy, you should probably rename this file and start testing for real"
+ def test_simple_model
+ simple = Simple.create_fake
+
+ assert_equal [ ], simple.errors.full_messages
+ assert !simple.new_record?
+
+ assert_equal 8, simple.name.length
end
end
View
7 test_dummy.gemspec
@@ -5,7 +5,7 @@
Gem::Specification.new do |s|
s.name = %q{test_dummy}
- s.version = "0.1.2"
+ s.version = "0.1.3"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["tadman"]
@@ -24,9 +24,13 @@ Gem::Specification.new do |s|
"Rakefile",
"VERSION",
"lib/test_dummy.rb",
+ "lib/test_dummy/helper.rb",
"lib/test_dummy/railtie.rb",
"lib/test_dummy/test_helper.rb",
+ "test/config/database.yml",
+ "test/db/.gitignore",
"test/helper.rb",
+ "test/models/simple.rb",
"test/test_test_dummy.rb",
"test_dummy.gemspec"
]
@@ -37,6 +41,7 @@ Gem::Specification.new do |s|
s.summary = %q{Quick test data generator and fake model maker}
s.test_files = [
"test/helper.rb",
+ "test/models/simple.rb",
"test/test_test_dummy.rb"
]

0 comments on commit fc8ffae

Please sign in to comment.