fixture_background 0.9.0.2 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,6 +1,7 @@
1
1
  require 'fixture_background/version'
2
2
  require 'fixture_background/background'
3
3
  require 'fixture_background/generator'
4
+ require 'fixture_background/ivars'
4
5
  require 'fixture_background/shoulda'
5
6
  require 'fixture_background/active_support/test_case'
6
7
 
@@ -4,7 +4,7 @@ module FixtureBackground
4
4
  extend ::ActiveSupport::Concern
5
5
 
6
6
  included do
7
- class_inheritable_accessor :background_ivar_cache
7
+ class_inheritable_accessor :background_ivars
8
8
  class_inheritable_accessor :active_record_fixture_cache_resetted
9
9
 
10
10
  set_callback(:setup, :before, :reset_active_record_fixture_cache, {:prepend => true})
@@ -27,22 +27,12 @@ module FixtureBackground
27
27
 
28
28
  module InstanceMethods
29
29
  def setup_background_ivars
30
- return unless File.exist?("#{fixture_path}/ivars.dump")
30
+ self.background_ivars ||= IVars.deserialize((YAML.load_file("#{fixture_path}/ivars.dump") rescue {}))
31
31
 
32
- fill_background_ivar_cache unless background_ivar_cache
33
- background_ivar_cache.each do |ivar, record|
34
- # deep clone the object
35
- instance_variable_set(ivar, Marshal.load(Marshal.dump(record)))
36
- end
37
- end
38
-
39
- def fill_background_ivar_cache
40
- bm = Benchmark.realtime do
41
- self.background_ivar_cache = YAML.load_file("#{fixture_path}/ivars.dump")
42
- background_ivar_cache.each do |ivar, (class_name, id)|
43
- record = class_name.constantize.find(id)
44
- self.background_ivar_cache[ivar] = record
45
- end
32
+ deep_copy = Marshal.load(Marshal.dump(self.background_ivars))
33
+
34
+ deep_copy.each do |ivar, record|
35
+ instance_variable_set(ivar,record)
46
36
  end
47
37
  end
48
38
 
@@ -1,5 +1,31 @@
1
1
  module FixtureBackground
2
2
  class Background
3
+
4
+ class << self
5
+ def class_for_test(full_class_name, background_to_use, test_unit_class)
6
+ klass = class_by_name(full_class_name) || Object.const_set(full_class_name, Class.new(test_unit_class))
7
+
8
+ if helper_class = test_unit_class.instance_variable_get(:@helper_class)
9
+ klass.instance_variable_set(:@helper_class, helper_class)
10
+ end
11
+
12
+ if controller_class = (test_unit_class.respond_to?(:controller_class) && test_unit_class.controller_class)
13
+ klass.controller_class = controller_class
14
+ end
15
+
16
+ klass.fixture_path = background_to_use.fixture_path
17
+ klass.fixtures :all
18
+ klass
19
+ end
20
+
21
+ def class_by_name(class_name)
22
+ klass = Module.const_get(class_name)
23
+ klass.is_a?(Class) && klass
24
+ rescue NameError
25
+ return false
26
+ end
27
+ end
28
+
3
29
  attr_reader :background_block
4
30
 
5
31
  def initialize(full_class_name, test_unit_class, parent, blk)
@@ -34,10 +60,7 @@ module FixtureBackground
34
60
  end
35
61
 
36
62
  def class_for_test
37
- klass = Kernel.const_set(@full_class_name, Class.new(@test_unit_class))
38
- klass.fixture_path = fixture_path
39
- klass.fixtures :all
40
- klass
63
+ self.class.class_for_test(@full_class_name, self, @test_unit_class)
41
64
  end
42
65
  end
43
66
  end
@@ -54,12 +54,13 @@ module FixtureBackground
54
54
 
55
55
  yield klass
56
56
 
57
- ivar_hash = {}
58
- (klass.instance_variables - existing_ivars).each do |ivar|
59
- record = klass.instance_variable_get(ivar)
60
- ivar_hash[ivar.to_s] = [record.class.name, record.id] if record.class.respond_to? :find
57
+ ivars_with_value = (klass.instance_variables - existing_ivars).inject({}) do |memo, ivar|
58
+ memo[ivar] = klass.instance_variable_get(ivar)
59
+ memo
61
60
  end
62
61
 
62
+ ivar_hash = IVars.serialize(ivars_with_value)
63
+
63
64
  File.open("#{@background_dir}/ivars.dump", 'w+') do |f|
64
65
  YAML.dump(ivar_hash, f)
65
66
  end
@@ -73,7 +74,7 @@ module FixtureBackground
73
74
 
74
75
  fixtures = {}
75
76
  records.each do |record|
76
- fixtures[table_name + record.id.to_s] = record.attributes
77
+ fixtures[table_name + record.id.to_s] = record.instance_variable_get(:@attributes)
77
78
  end
78
79
 
79
80
  File.open("#{@background_dir}/#{table_name}.yml", 'w+') do |f|
@@ -0,0 +1,122 @@
1
+ module FixtureBackground
2
+ class IVars
3
+ class << self
4
+ def serialize(value)
5
+ case value
6
+ when Hash
7
+ value.inject({}) do |memo, (key, v)|
8
+ memo[key] = serialize(v)
9
+ memo
10
+ end
11
+ when Array
12
+ value.map { |v| serialize(v) }
13
+ else
14
+ if value.class.respond_to?(:find) && value.respond_to?(:id)
15
+ "#{value.class.name}##{value.id}"
16
+ else
17
+ raise ArgumentError
18
+ end
19
+ end
20
+ end
21
+
22
+ def deserialize(value)
23
+ case value
24
+ when Hash
25
+ value.inject({}) do |memo, (key, v)|
26
+ memo[key] = deserialize(v)
27
+ memo
28
+ end
29
+ when Array
30
+ value.map { |v| deserialize(v) }
31
+ when String
32
+ klass, id = value.split("#")
33
+ klass.constantize.find(id)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ if __FILE__ == $0
41
+ require 'test/unit'
42
+ gem "activesupport"
43
+ require 'active_support/all'
44
+
45
+ include FixtureBackground
46
+
47
+ class Record
48
+ attr_reader :id
49
+
50
+ class << self
51
+ def find(id)
52
+ new(id)
53
+ end
54
+ end
55
+
56
+ def initialize(id)
57
+ @id = id.to_i
58
+ end
59
+
60
+ def ==(other)
61
+ other.class == Record && other.id == id
62
+ end
63
+ end
64
+
65
+ class IVarsTest < Test::Unit::TestCase
66
+ def test_serialize_simple_values
67
+ data = {
68
+ :hase => Record.find(1),
69
+ :igel => Record.find(2)
70
+ }
71
+
72
+ expected = {
73
+ :hase => 'Record#1',
74
+ :igel => 'Record#2'
75
+ }
76
+
77
+ assert_serialize_deserialize expected, data
78
+ end
79
+
80
+ def test_serialize_arrays
81
+ data = {
82
+ :hase => [Record.find(1), Record.find(2), [Record.find(3), Record.find(4)]]
83
+ }
84
+
85
+ expected = {
86
+ :hase => ['Record#1', 'Record#2', ['Record#3', 'Record#4']]
87
+ }
88
+
89
+ assert_serialize_deserialize expected, data
90
+ end
91
+
92
+ def test_serialize_hash
93
+ data = {
94
+ :hase => { 'thies' => [Record.find(1), Record.find(2)], :sebastian => [Record.find(3), Record.find(4)]}
95
+ }
96
+
97
+ expected = {
98
+ :hase => {'thies' => ['Record#1', 'Record#2'], :sebastian => ['Record#3', 'Record#4']}
99
+ }
100
+
101
+ assert_serialize_deserialize expected, data
102
+ end
103
+
104
+ def test_raises_exception_if_some_var_does_not_respond_to_find
105
+ data = [
106
+ :some => 1
107
+ ]
108
+
109
+ assert_raises(ArgumentError) { IVars.serialize(data) }
110
+ end
111
+ end
112
+
113
+ def assert_serialize_deserialize(expected, data)
114
+ serialized = IVars.serialize(data)
115
+ deserialized = IVars.deserialize(serialized)
116
+
117
+ assert_equal expected, serialized
118
+ assert_equal deserialized, data
119
+ end
120
+ end
121
+
122
+
@@ -1,11 +1,17 @@
1
1
  module Shoulda
2
2
  raise "fixture_background is only compatible with shoulda 2.11.3 installed #{VERSION}" if VERSION != "2.11.3"
3
3
 
4
+ module ClassMethods
5
+ def described_type
6
+ self.name.gsub(/Test.*/, '').constantize
7
+ end
8
+ end
9
+
4
10
  class Context
5
11
  attr_reader :fixture_background
6
12
 
7
13
  def full_class_name
8
- (test_unit_class.name + full_name.gsub(/\s+/, '_')).camelcase
14
+ (test_unit_class.name + full_name.gsub(/[^a-zA-Z0-9]/, '_')).camelcase
9
15
  end
10
16
 
11
17
  def parent_fixture_background
@@ -19,9 +25,10 @@ module Shoulda
19
25
  end
20
26
 
21
27
  def class_for_test
22
- @fixture_background ? @fixture_background.class_for_test : test_unit_class
28
+ (@fixture_background && @fixture_background.class_for_test) ||
29
+ (parent_fixture_background && FixtureBackground::Background.class_for_test(full_class_name, parent_fixture_background, test_unit_class)) ||
30
+ test_unit_class
23
31
  end
24
-
25
32
 
26
33
  #
27
34
  # the following functions are copied from shoulda/context.rb
@@ -29,11 +36,11 @@ module Shoulda
29
36
 
30
37
  def create_test_from_should_hash(klass, should)
31
38
  test_name = ["test:", full_name, "should", "#{should[:name]}. "].flatten.join(' ').to_sym
32
-
39
+
33
40
  if klass.instance_methods.include?(test_name.to_s)
34
41
  warn " * WARNING: '#{test_name}' is already defined"
35
42
  end
36
-
43
+
37
44
  context = self
38
45
  klass.send(:define_method, test_name) do
39
46
  @shoulda_context = context
@@ -54,9 +61,9 @@ module Shoulda
54
61
  shoulds.each do |should|
55
62
  create_test_from_should_hash(klass, should)
56
63
  end
57
-
64
+
58
65
  subcontexts.each { |context| context.build }
59
-
66
+
60
67
  print_should_eventuallys
61
68
  end
62
69
  end
@@ -1,4 +1,4 @@
1
1
  module FixtureBackground
2
- VERSION = "0.9.0.2"
2
+ VERSION = "0.9.1"
3
3
  end
4
4
 
metadata CHANGED
@@ -5,9 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 9
8
- - 0
9
- - 2
10
- version: 0.9.0.2
8
+ - 1
9
+ version: 0.9.1
11
10
  platform: ruby
12
11
  authors:
13
12
  - Thies C. Arntzen
@@ -16,7 +15,7 @@ autorequire:
16
15
  bindir: bin
17
16
  cert_chain: []
18
17
 
19
- date: 2011-01-07 00:00:00 +01:00
18
+ date: 2011-01-12 00:00:00 +01:00
20
19
  default_executable:
21
20
  dependencies: []
22
21
 
@@ -41,6 +40,7 @@ files:
41
40
  - lib/fixture_background/active_support/test_case.rb
42
41
  - lib/fixture_background/background.rb
43
42
  - lib/fixture_background/generator.rb
43
+ - lib/fixture_background/ivars.rb
44
44
  - lib/fixture_background/shoulda.rb
45
45
  - lib/fixture_background/version.rb
46
46
  has_rdoc: true