Class: Test::Unit::TestCase

Inherits:
Object
  • Object
show all
Defined in:
lib/marjoree/fixtures.rb

Constant Summary collapse

@@fixture_path =
'fixtures'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#fixturesObject (readonly)

Returns the value of attribute fixtures.



7
8
9
# File 'lib/marjoree/fixtures.rb', line 7

def fixtures
  @fixtures
end

Class Method Details

.fixture_pathObject



14
15
16
# File 'lib/marjoree/fixtures.rb', line 14

def self.fixture_path
  @@fixture_path
end

.fixture_path=(path) ⇒ Object



10
11
12
# File 'lib/marjoree/fixtures.rb', line 10

def self.fixture_path=(path)
  @@fixture_path = path
end

.fixtures(*tables) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
# File 'lib/marjoree/fixtures.rb', line 24

def self.fixtures(*tables)      
  self.module_eval <<-EOS

    def find_fixture_file( fixture_name )
        return Dir["#{Test::Unit::TestCase.fixture_path}/**/\#{fixture_name}.yml"].first
    end

    def connect_to_db_for_fixture( path )
        path_elements = path.split( '/' )
        odbc_name = path_elements[ path_elements.size - 2]

        connect_me_to( odbc_name.eql?( 'fixtures' ) ? 'default' : odbc_name )
    end

    def connect_to_default
      connect_me_to 'default'
    end

    def setup_with_fixtures
      @fixtures = [#{tables.collect{ |table| ":#{table}"}.join(',')}]

      empty_fixture_tables( @fixtures )

      @fixtures.each do |fixture|
        path = find_fixture_file( fixture )
        yaml = IO.read(path)
        rows = YAML.load(ERB.new(yaml).result(binding)).sort

        connect_to_db_for_fixture( path )

        rows.each do |name, map|
          Marjoree.execute map.to_sql(fixture)
        end
      end

      connect_to_default
    end

    def teardown_with_fixtures
      empty_fixture_tables( fixtures )
    end

    def empty_fixture_tables( fixtures )
      fixtures.reverse.each do |fixture|
        path = find_fixture_file( fixture )
        connect_to_db_for_fixture( path )

        begin
          Marjoree.execute "DELETE FROM \#{fixture}"
        rescue=>e
          puts "Failed to truncate \#{fixture}: \#{e}"
        end

        connect_to_default
      end
    end
  EOS

  tables.each do |table|
    module_eval <<-EOS
      def #{table}(key)
        path = find_fixture_file( "#{table}" )          
        yaml = IO.read(path)
        rows = YAML.load(ERB.new(yaml).result)
        map = rows[key.to_s]

        def map.id;
          return fetch('id') if member?('id')
          return fetch("id_#{table.to_s.downcase}")
        end

        def map.method_missing(name, *args)
          return self[name.to_s] || defaults[name]
        end

        def map.with_defaults(defaults)
          @defaults = defaults
          self
        end

        def map.defaults
           @defaults || {}
        end
        return map
      end
    EOS
    unless table.to_s.downcase == table.to_s
      module_eval <<-EOS
        def #{table.to_s.downcase}(key)
          #{table}(key)
        end
      EOS
    end
  end
end

.method_added(method) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/marjoree/fixtures.rb', line 128

def self.method_added(method)
  case method.to_s
  when 'setup'
    unless method_defined?(:setup_without_fixtures)
      alias_method :setup_without_fixtures, :setup
      define_method(:setup) do
        setup_with_fixtures
        setup_without_fixtures
      end
    end
  when 'teardown'
    unless method_defined?(:teardown_without_fixtures)
      alias_method :teardown_without_fixtures, :teardown
      define_method(:teardown) do
        teardown_without_fixtures
        teardown_with_fixtures
      end
    end
  end
end

Instance Method Details

#setupObject



120
121
122
# File 'lib/marjoree/fixtures.rb', line 120

def setup
  setup_with_fixtures
end

#setup_with_fixturesObject



18
19
# File 'lib/marjoree/fixtures.rb', line 18

def setup_with_fixtures
end

#teardownObject



124
125
126
# File 'lib/marjoree/fixtures.rb', line 124

def teardown
  teardown_with_fixtures
end

#teardown_with_fixturesObject



21
22
# File 'lib/marjoree/fixtures.rb', line 21

def teardown_with_fixtures
end