Skip to content

Commit

Permalink
Refactored fixtures
Browse files Browse the repository at this point in the history
  • Loading branch information
henrypoydar committed Aug 28, 2009
1 parent 765f03e commit a7c6838
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 22 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
*.DS_Store
4 changes: 1 addition & 3 deletions README.md
Expand Up @@ -112,9 +112,7 @@ There are also some simple matchers you can can use to spec validations. See `s

You can store fixtures as Yaml files in the following path pattern:

test/fixtures/couch/<database_name>/
|-- first_fixtures.yml
|-- some_more_fixtures.yml
test/fixtures/couch/<database_name>.yml
You can customize this path in an initializer or environment file:

Expand Down
37 changes: 22 additions & 15 deletions lib/couch_rest_rails/fixtures.rb
Expand Up @@ -17,8 +17,8 @@ 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"
unless File.exist?(File.join(RAILS_ROOT, CouchRestRails.fixtures_path, "#{db}.yml"))
response << "Fixtures file (#{File.join(CouchRestRails.fixtures_path, "#{db}.yml")}) does not exist"
else
full_db_name = [COUCHDB_CONFIG[:db_prefix], db, COUCHDB_CONFIG[:db_suffix]].join
full_db_path = [COUCHDB_CONFIG[:host_path], '/', full_db_name].join
Expand All @@ -27,7 +27,7 @@ def load(database_name = '*')
else
db_conn = CouchRest.database(full_db_path)
fixture_files = []
Dir.glob(File.join(RAILS_ROOT, CouchRestRails.fixtures_path, db, "*.yml")).each do |file|
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
Expand All @@ -44,27 +44,34 @@ def load(database_name = '*')
end

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|
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
File.open(File.join(RAILS_ROOT, CouchRestRails.fixture_path, "#{database}.yml"), 'w' ) do |file|

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

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"
else
fixtures_file = File.join(RAILS_ROOT, CouchRestRails.fixtures_path, "#{db}.yml")
if File.exist?(fixtures_file)
response << "Overwriting fixtures in #{File.join(CouchRestRails.fixtures_path, "#{db}.yml")}"
end
File.open(fixtures_file, 'w' ) do |file|
yaml_hash = {}
db_con = CouchRest.database("#{COUCHDB_CONFIG[:host_path]}/#{db_name}")
docs = db_con.documents(:include_docs =>true )
docs["rows"].each { |data|
db_conn = CouchRest.database(full_db_path)
docs = db_conn.documents(:include_docs => true)
docs["rows"].each do |data|
doc = data["doc"]
unless (doc['_id'] =~ /^_design*/) == 0
doc.delete('_rev')
yaml_hash[doc['_id']] = doc
end
}
end
file.write yaml_hash.to_yaml
end
response << "Dumped fixtures into #{File.join(CouchRestRails.fixtures_path, "#{db}.yml")}"
end

end
end

Expand Down
35 changes: 35 additions & 0 deletions spec/lib/couch_rest_rails/fixtures_spec.rb
Expand Up @@ -70,6 +70,41 @@ class CouchRestRailsTestDocumentBar < CouchRestRails::Document

describe "#dump" do

before :each do
['foo', 'bar'].each do |db|
FileUtils.cp(File.join(RAILS_ROOT, CouchRestRails.fixtures_path, "#{db}.yml"),
File.join(RAILS_ROOT, CouchRestRails.fixtures_path, "#{db}x.yml"))
CouchRestRails::Database.create("#{db}x")
CouchRestRails::Fixtures.load("#{db}x")
FileUtils.rm_rf(File.join(RAILS_ROOT, CouchRestRails.fixtures_path, "#{db}x.yml"))
end
end

after :each do
['foo', 'bar'].each do |db|
CouchRestRails::Database.delete("#{db}x")
FileUtils.rm_rf(File.join(RAILS_ROOT, CouchRestRails.fixtures_path, "#{db}x.yml"))
end
cleanup_view_paths
end

it "should dump fixtures for the specified database" do
res = CouchRestRails::Fixtures.dump('foox')
File.exist?(File.join(RAILS_ROOT, CouchRestRails.fixtures_path, 'foox.yml')).should be_true
end

it "should dump fixtures for all databases as defined in CouchRestRails::Document models if no argument is specified" do
class CouchRestRailsTestDocumentFoo < CouchRestRails::Document
use_database :foox
end
class CouchRestRailsTestDocumentBar < CouchRestRails::Document
use_database :barx
end
CouchRestRails::Fixtures.dump
File.exist?(File.join(RAILS_ROOT, CouchRestRails.fixtures_path, 'foox.yml')).should be_true
File.exist?(File.join(RAILS_ROOT, CouchRestRails.fixtures_path, 'barx.yml')).should be_true
end

end

end
11 changes: 11 additions & 0 deletions spec/mock/fixtures/bar.yml
@@ -0,0 +1,11 @@
<% def blurb; CouchRestRails::Fixtures.random_blurb; end %>

<% 5.times do |i| %>
record_<%= i %>:
type: Bar
question: <%= blurb.split('.').first %>
answer: <%= blurb %>
rating: <%= rand(10) %>
created_at: <%= (Time.now - 3600*rand(10)).utc.strftime('%Y/%m/%d %H:%M:%S +0000') %>
updated_at: <%= Time.now.utc.strftime('%Y/%m/%d %H:%M:%S +0000') %>
<% end %>
11 changes: 11 additions & 0 deletions spec/mock/fixtures/foo.yml
@@ -0,0 +1,11 @@
<% def blurb; CouchRestRails::Fixtures.random_blurb; end %>

<% 10.times do |i| %>
record_<%= i %>:
type: Foo
title: <%= blurb.split('.').first %>
body: <%= blurb %>
tags: <%= blurb.split(' ').sort_by {rand}[0..3].map {|e| e.downcase.gsub(/[^a-z]/, '').strip}.inspect %>
created_at: <%= (Time.now - 3600*rand(10)).utc.strftime('%Y/%m/%d %H:%M:%S +0000') %>
updated_at: <%= Time.now.utc.strftime('%Y/%m/%d %H:%M:%S +0000') %>
<% end %>
8 changes: 4 additions & 4 deletions spec/spec_helper.rb
Expand Up @@ -6,8 +6,8 @@
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'))
['foo', 'bar', 'foox', 'barx'].each do |db|
FileUtils.rm_rf(File.join(RAILS_ROOT, CouchRestRails.views_path, db))
FileUtils.rm_rf(File.join(RAILS_ROOT, CouchRestRails.lucene_path, db))
end
end

0 comments on commit a7c6838

Please sign in to comment.