Skip to content

Commit

Permalink
Refactored fixtures code
Browse files Browse the repository at this point in the history
  • Loading branch information
henrypoydar committed Aug 28, 2009
1 parent e1044d1 commit 765f03e
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 64 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ You can customize this path in an initializer or environment file:

#### Rails integration unit testing

Create fixture file by via rake (`rake couchdb:fixture:dump[<database_name>]`) or within your code or console (`CouchRestRails::Fixtures.dump[<database_name>]`).
Create fixture file by via rake (`rake couchdb:fixtures:dump[<database_name>]`) or within your code or console (`CouchRestRails::Fixtures.dump[<database_name>]`).

Add fixtures to rails test:

Expand Down
21 changes: 19 additions & 2 deletions lib/couch_rest_rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ module CouchRestRails
mattr_accessor :lucene_path
self.lucene_path = 'db/couch'

mattr_accessor :fixture_path
self.fixture_path = 'test/fixtures/couch'
mattr_accessor :fixtures_path
self.fixtures_path = 'test/fixtures/couch'

mattr_accessor :test_environment
self.test_environment = 'test'
Expand All @@ -15,4 +15,21 @@ module CouchRestRails
mattr_accessor :views_path
self.views_path = 'db/couch'

def process_database_method(database_name, &block)
# If wildcard passed, use model definitions for database names
if database_name == '*'
databases = CouchRestRails::Database.list
else
databases = [database_name]
end
response = ['']
databases.each do |database|
yield database, response
end
response << ''
response.join("\n")
end

module_function :process_database_method

end
28 changes: 4 additions & 24 deletions lib/couch_rest_rails/database.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
module CouchRestRails
module Database
include FileUtils

extend self

def create(database_name = '*')

# If wildcard passed, use model definitions for database names
if database_name == '*'
databases = CouchRestRails::Database.list
else
databases = [database_name]
end

response = ['']

databases.each do |db|
CouchRestRails.process_database_method(database_name) do |db, response|

# Setup up views directory
database_views_path = File.join(RAILS_ROOT, CouchRestRails.views_path, db, 'views')
Expand Down Expand Up @@ -54,22 +44,13 @@ def create(database_name = '*')
response << CouchRestRails::Lucene.push(File.basename(db), "*") if CouchRestRails.use_lucene

end
response << ['']
response.join("\n")

end

def delete(database_name = '*')

# If wildcard passed, use model definitions for database names
if database_name == '*'
databases = CouchRestRails::Database.list
else
databases = [database_name]
end

response = ['']
CouchRestRails.process_database_method(database_name) do |db, response|

databases.each do |db|
full_db_name = [COUCHDB_CONFIG[:db_prefix], File.basename(db), COUCHDB_CONFIG[:db_suffix]].join
if !COUCHDB_SERVER.databases.include?(full_db_name)
response << "Database #{db} (#{full_db_name}) does not exist"
Expand All @@ -91,8 +72,7 @@ def delete(database_name = '*')
end

end
response << ''
response.join("\n")

end

def list
Expand Down
52 changes: 31 additions & 21 deletions lib/couch_rest_rails/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,37 @@ def blurbs
res << "Donec placerat. Nullam nibh dolor, blandit sed, fermentum id, imperdiet sit amet, neque. Nam mollis ultrices justo. Sed tempor. Sed vitae tellus. Etiam sem arcu, eleifend sit amet, gravida eget, porta at, wisi. Nam non lacus vitae ipsum viverra pretium. Phasellus massa. Fusce magna sem, gravida in, feugiat ac, molestie eget, wisi. Fusce consectetuer luctus ipsum. Vestibulum nunc. Suspendisse dignissim adipiscing libero. Integer leo. Sed pharetra ligula a dui. Quisque ipsum nibh, ullamcorper eget, pulvinar sed, posuere vitae, nulla. Sed varius nibh ut lacus. Curabitur fringilla. Nunc est ipsum, pretium quis, dapibus sed, varius non, lectus. Proin a quam. Praesent lacinia, eros quis aliquam porttitor, urna lacus volutpat urna, ut fermentum neque mi egestas dolor."
end



def load(database)
fixture_files = []
return "Database '#{database}' doesn't exists" unless (database == "*" ||
File.exist?(File.join(RAILS_ROOT, CouchRestRails.setup_path, database)))
Dir[File.join(RAILS_ROOT, CouchRestRails.setup_path, database)].each do |db|
db_name =COUCHDB_CONFIG[:db_prefix] + File.basename( db) +
COUCHDB_CONFIG[:db_suffix]
res = CouchRest.get("#{COUCHDB_CONFIG[:host_path]}/#{db_name}") rescue nil
if res
db_con = CouchRest.database("#{COUCHDB_CONFIG[:host_path]}/#{db_name}")
Dir.glob(File.join(RAILS_ROOT, CouchRestRails.fixture_path, "#{database}.yml")).each do |file|
db_con.bulk_save(YAML::load(ERB.new(IO.read(file)).result).map {|f| f[1]})
fixture_files << File.basename(file)
end
end
if fixture_files.empty?
return "No fixtures found in #{CouchRestRails.fixture_path}"
def load(database_name = '*')

CouchRestRails.process_database_method(database_name) do |db, response|

unless File.exist?(File.join(RAILS_ROOT, CouchRestRails.fixtures_path, db))
response << "Fixtures directory (#{File.join(CouchRestRails.fixtures_path, db)}) does not exist"
else
return "Loaded the following fixture files into '#{db}': #{fixture_files.join(', ')}"
full_db_name = [COUCHDB_CONFIG[:db_prefix], db, COUCHDB_CONFIG[:db_suffix]].join
full_db_path = [COUCHDB_CONFIG[:host_path], '/', full_db_name].join
unless COUCHDB_SERVER.databases.include?(full_db_name)
response << "The CouchDB database #{db} (#{full_db_name}) does not exist - create it first"
else
db_conn = CouchRest.database(full_db_path)
fixture_files = []
Dir.glob(File.join(RAILS_ROOT, CouchRestRails.fixtures_path, db, "*.yml")).each do |file|
db_conn.bulk_save(YAML::load(ERB.new(IO.read(file)).result).map {|f| f[1]})
fixture_files << File.basename(file)
end
if fixture_files.empty?
response << "No fixtures found in #{CouchRestRails.fixtures_path}"
else
response << "Loaded the following fixture files into #{db} (#{full_db_name}): #{fixture_files.join(', ')}"
end
end
end

end

end

def dump(database)
def dump(database_name = '*')
return "Database '#{database}' doesn't exists" unless (database == "*" ||
File.exist?(File.join(RAILS_ROOT, CouchRestRails.setup_path, database)))
Dir[File.join(RAILS_ROOT, CouchRestRails.setup_path, database)].each do |db|
Expand All @@ -62,5 +67,10 @@ def dump(database)
end
end
end

def random_blurb
blurbs.sort_by {rand}.first
end

end
end
5 changes: 1 addition & 4 deletions spec/lib/couch_rest_rails/database_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@
after :all do
CouchRest.delete(@foo_db_url) rescue nil
CouchRest.delete(@bar_db_url) rescue nil
FileUtils.rm_rf(File.join(RAILS_ROOT, CouchRestRails.views_path, 'foo'))
FileUtils.rm_rf(File.join(RAILS_ROOT, CouchRestRails.views_path, 'bar'))
FileUtils.rm_rf(File.join(RAILS_ROOT, CouchRestRails.lucene_path, 'foo'))
FileUtils.rm_rf(File.join(RAILS_ROOT, CouchRestRails.lucene_path, 'bar'))
cleanup_view_paths
end

describe '#create' do
Expand Down
56 changes: 45 additions & 11 deletions spec/lib/couch_rest_rails/fixtures_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

describe CouchRestRails::Fixtures do

before :each do
CouchRestRails.use_lucene = false
CouchRestRails.views_path = 'vendor/plugins/couchrest-rails/spec/mock/couch'
CouchRestRails.fixtures_path = 'vendor/plugins/couchrest-rails/spec/mock/fixtures'
@foo_db_name = [
COUCHDB_CONFIG[:db_prefix], 'foo',
COUCHDB_CONFIG[:db_suffix]
].join
@foo_db_url = [
COUCHDB_CONFIG[:host_path], "/",
@foo_db_name
].join
@bar_db_name = @foo_db_name.gsub(/foo/, 'bar')
@bar_db_url = @foo_db_url.gsub(/foo/, 'bar')
end


describe '#blurbs' do

it 'should produce an array of text blurbs for testing purposes' do
Expand All @@ -16,26 +33,43 @@

describe '#load' do

before :each do
CouchRest.delete(COUCHDB_CONFIG[:full_path]) rescue nil
after :each do
CouchRestRails::Database.delete('foo')
CouchRestRails::Database.delete('bar')
cleanup_view_paths
end

after :all do
CouchRest.delete(COUCHDB_CONFIG[:full_path]) rescue nil
end

it "should exit if the database doesn't exist" do
it "should notify if the specified database doesn't exist" do
res = CouchRestRails::Fixtures.load('foo')
res.should =~ /does not exist/i
end

it "should load up the yaml files in CouchRestRails.fixtures_path as documents" do
db = CouchRest.database!(COUCHDB_CONFIG[:full_path])
CouchRestRails.fixtures_path = 'vendor/plugins/couchrest-rails/spec/mock/fixtures'
CouchRestRails::Fixtures.load
it "should load up the Yaml files in CouchRestRails.fixtures_path as documents for the specified database" do
CouchRestRails::Database.create('foo')
res = CouchRestRails::Fixtures.load('foo')
db = CouchRest.database(@foo_db_url)
db.documents['rows'].size.should == 10
end

it "should load up the Yaml files in CouchRestRails.fixtures_path as documents for all databases if no argument is passed" do
class CouchRestRailsTestDocumentFoo < CouchRestRails::Document
use_database :foo
end
class CouchRestRailsTestDocumentBar < CouchRestRails::Document
use_database :bar
end
CouchRestRails::Database.create('foo')
CouchRestRails::Database.create('bar')
CouchRestRails::Fixtures.load
db_foo = CouchRest.database(@foo_db_url)
db_bar = CouchRest.database(@bar_db_url)
(db_foo.documents['rows'].size + db_bar.documents['rows'].size).should == 15
end

end

describe "#dump" do

end

end
27 changes: 27 additions & 0 deletions spec/lib/couch_rest_rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,31 @@

end

describe ".process_database_method" do

before :each do
class CouchRestRailsTestDocumentFoo < CouchRestRails::Document
use_database :foo
end
class CouchRestRailsTestDocumentBar < CouchRestRails::Document
use_database :bar
end
end

it "should act on all databases if * passed as an argument" do
res = CouchRestRails.process_database_method('*') do |db, r|
r << db
end
res.should == "\nbar\nfoo\n"
end

it "should act on a single database if passed" do
res = CouchRestRails.process_database_method('foo') do |db, r|
r << db
end
res.should == "\nfoo\n"
end

end

end
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<% def blurb; CouchRestRails::Fixtures.random_blurb; end %>

<% 5.times do |i| %>
<% 10.times do |i| %>
record_<%= i %>:
type: Foo
title: <%= blurb.split('.').first %>
Expand Down
7 changes: 7 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@
rescue LoadError
puts "You need to install rspec in your base application"
exit
end

def cleanup_view_paths
FileUtils.rm_rf(File.join(RAILS_ROOT, CouchRestRails.views_path, 'foo'))
FileUtils.rm_rf(File.join(RAILS_ROOT, CouchRestRails.views_path, 'bar'))
FileUtils.rm_rf(File.join(RAILS_ROOT, CouchRestRails.lucene_path, 'foo'))
FileUtils.rm_rf(File.join(RAILS_ROOT, CouchRestRails.lucene_path, 'bar'))
end

0 comments on commit 765f03e

Please sign in to comment.