Module: Zena::Use::Fixtures

Included in:
Controller::TestCase, Integration::TestCase, TestController, Zena::Unit::TestCase, View::TestCase
Defined in:
lib/zena/use/fixtures.rb

Constant Summary collapse

FIXTURE_PATH =

load all fixtures and setup fixture_accessors:

File.join(RAILS_ROOT, 'test', 'fixtures')
FILE_FIXTURES_PATH =
File.join(RAILS_ROOT, 'test', 'fixtures', 'files')

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.dest_filepath(filename, id, format = 'full') ⇒ Object

TODO: Use Attachment filepath class methods.



34
35
36
# File 'lib/zena/use/fixtures.rb', line 34

def self.dest_filepath(filename, id, format='full')
  "#{SITES_ROOT}/test.host/data/#{format}/#{Versions::SharedAttachment.filepath(id, filename)}"
end

.included(base) ⇒ Object

We use transactional fixtures with a single load for ALL tests (this is not the default rails implementation). Tests are now 5x-10x faster.



9
10
11
12
# File 'lib/zena/use/fixtures.rb', line 9

def self.included(base)
  # We load once using our own loader so that accessor methods are defined
  self.load_fixtures unless defined?(@@loaded_fixtures)
end

.load_fixturesObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/zena/use/fixtures.rb', line 64

def self.load_fixtures
  # make sure versions is of type InnoDB if testing with mysql (transaction support)
  begin
    Node.connection.remove_index "versions", ["title", "text", "summary"]
  rescue ActiveRecord::StatementInvalid
  ensure
    Zena::Db.change_engine('versions', 'InnoDB')
  end

  # Our version of loaded fixtures to help define "users_id", "nodes_id" and such.
  @@loaded_fixtures = {}
  fixture_table_names = []

  # no fixtures ? execute rake task
  unless File.exist?(File.join(FIXTURE_PATH, 'nodes.yml'))
    puts "No fixtures in 'test/fixtures'. Building from 'test/sites'."
    `cd #{RAILS_ROOT} && rake zena:build_fixtures`
  end

  # make sure files and import directories are in sync
  if RAILS_ROOT != Zena::ROOT
    ['test/fixtures/files', 'test/fixtures/import'].each do |path|
      from_dir = "#{Zena::ROOT}/#{path}"
      FileUtils.mkpath(from_dir) unless File.exist?(from_dir)
      Dir.foreach(from_dir) do |f|
        FileUtils.cp_r("#{from_dir}/#{f}", "#{RAILS_ROOT}/#{path}/") unless File.exist?("#{RAILS_ROOT}/#{path}/#{f}")
      end
    end
  end

  Dir.foreach(FIXTURE_PATH) do |file|
    next unless file =~ /^(.+)\.yml$/
    table_name = $1
    fixture_table_names << table_name

    if ['users', 'sites'].include?(table_name)

      define_method(table_name) do |fixture|
        if @@loaded_fixtures[table_name][fixture.to_s]
          # allways reload
          @@loaded_fixtures[table_name][fixture.to_s].find
        else
          raise StandardError, "No fixture with name '#{fixture}' found for table '#{table_name}'"
        end
      end

      define_method(table_name + "_id") do |fixture|
        Zena::FoxyParser::multi_site_id(fixture)
      end
    else
      define_method(table_name) do |fixture|
        raise Exception.new("$_test_site is blank!") if $_test_site.blank?
        fixture_name = "#{$_test_site}_#{fixture}"
        if fix = @@loaded_fixtures[table_name][fixture_name]
          # allways reload
          fix.find
        else
          raise StandardError, "No fixture with name '#{fixture_name}' found for table '#{table_name}' in site '#{$_test_site}'."
        end
      end

      define_method(table_name + "_id") do |*fixtures|
        raise Exception.new("$_test_site is blank!") if $_test_site.blank?
        res = fixtures.map {|f| Zena::FoxyParser::id($_test_site, f) }
        fixtures.size > 1 ? res : res.first
      end
    end

    if table_name == 'nodes' || table_name == 'zips'
      define_method(table_name + "_zip") do |*fixtures|
        res = fixtures.map do |fixture|
          fixture_name = table_name == 'zips' ? fixture.to_s : "#{$_test_site}_#{fixture}"
          if fix = @@loaded_fixtures[table_name][fixture_name]
            fix.instance_eval { @fixture['zip'].to_i }
          else
            raise StandardError, "No fixture with name '#{fixture_name}' found for table '#{table_name}'"
          end
        end
        fixtures.size > 1 ? res : res.first
      end
    end

    if table_name == 'sites'
      define_method("#{table_name}_host") do |fixture|
        if fix = @@loaded_fixtures[table_name][fixture]
          fix.instance_eval { @fixture['host'] }
        else
          raise StandardError, "No fixture with name '#{fixture}' found for '#{table_name}'"
        end
      end
    end
  end


  alias virtual_classes roles
  alias virtual_classes_id roles_id

  fixtures = ::Fixtures.create_fixtures(FIXTURE_PATH, fixture_table_names)
  unless fixtures.nil?
    if fixtures.instance_of?(::Fixtures)
      @@loaded_fixtures[fixtures.table_name] = fixtures
    else
      fixtures.each { |f| @@loaded_fixtures[f.table_name] = f }
    end
  end

  reset_data_folder
  reset_public_folder

  FileUtils::mkpath("#{SITES_ROOT}/test.host/log") unless File.exist?("#{SITES_ROOT}/test.host/log")

  Zena::Db.insert_dummy_ids
end

.reset_data_folderObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/zena/use/fixtures.rb', line 38

def self.reset_data_folder
  # Cleaning the test folder which include the attachment files.
  FileUtils::rmtree("#{RAILS_ROOT}/sites/test.host/data")

  # Copy attachments file from /test/fixtures/files folder to the folder
  # provided by the dest_filepath method.
  attachments = YAML.load_file("#{RAILS_ROOT}/test/fixtures/attachments.yml")
  attachments.each do |attachment_name, attachment_attributes|
    filename  = attachment_attributes['filename']
    dest_path = dest_filepath(filename, attachment_attributes['id'],'full')
    if File.exist?("#{RAILS_ROOT}/test/fixtures/files/#{filename}")
      FileUtils::mkdir_p(File.dirname(dest_path))
      FileUtils::cp("#{RAILS_ROOT}/test/fixtures/files/#{filename}",dest_path)
    end
  end
end

.reset_public_folderObject



55
56
57
58
59
60
61
62
# File 'lib/zena/use/fixtures.rb', line 55

def self.reset_public_folder
  unless File.exist?("#{SITES_ROOT}/test.host/public")
    FileUtils::mkpath("#{SITES_ROOT}/test.host/public")
    ['images', 'calendar', 'stylesheets', 'javascripts'].each do |dir|
      FileUtils.symlink_or_copy("../../../public/#{dir}", "#{SITES_ROOT}/test.host/public/#{dir}")
    end
  end
end

Instance Method Details

#load_fixturesObject



19
20
21
22
23
24
# File 'lib/zena/use/fixtures.rb', line 19

def load_fixtures
  super
  # Cannot insert zero link from fixtures (zero id is changed to some incremented value). We
  # have to insert it by hand.
  Zena::Db.insert_dummy_ids
end

#test_site(site_name) ⇒ Object

Set the current site used for testing (mostly to generate ids)



15
16
17
# File 'lib/zena/use/fixtures.rb', line 15

def test_site(site_name)
  $_test_site = site_name
end