replicate 1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,71 @@
1
+ require 'test/unit'
2
+ require 'stringio'
3
+ require 'replicate'
4
+
5
+ class DumperTest < Test::Unit::TestCase
6
+ def setup
7
+ @dumper = Replicate::Dumper.new
8
+ end
9
+
10
+ def thing(attrs={})
11
+ attrs = {'number' => 123, 'string' => 'hello', 'time' => Time.new}.merge(attrs)
12
+ Replicate::Object.new attrs
13
+ end
14
+
15
+ def test_basic_filter
16
+ called = false
17
+ object = thing('test' => 'value')
18
+ @dumper.listen do |type, id, attrs, obj|
19
+ assert !called
20
+ assert_equal 'Replicate::Object', type
21
+ assert_equal object.id, id
22
+ assert_equal 'value', attrs['test']
23
+ assert_equal object.attributes, attrs
24
+ called = true
25
+ end
26
+ @dumper.dump object
27
+ assert called
28
+ end
29
+
30
+ def test_failure_when_object_not_respond_to_dump_replicant
31
+ assert_raise(NoMethodError) { @dumper.dump Object.new }
32
+ end
33
+
34
+ def test_never_dumps_objects_more_than_once
35
+ called = false
36
+ object = thing('test' => 'value')
37
+ @dumper.listen do |type, id, attrs, obj|
38
+ assert !called
39
+ called = true
40
+ end
41
+ @dumper.dump object
42
+ @dumper.dump object
43
+ @dumper.dump object
44
+ assert called
45
+ end
46
+
47
+ def test_writing_to_io
48
+ io = StringIO.new
49
+ @dumper.marshal_to io
50
+ @dumper.dump object = thing
51
+ data = Marshal.dump(['Replicate::Object', object.id, object.attributes])
52
+ assert_equal data, io.string
53
+ end
54
+
55
+ def test_stats
56
+ 10.times { @dumper.dump thing }
57
+ assert_equal({'Replicate::Object' => 10}, @dumper.stats)
58
+ end
59
+
60
+ def test_block_form_runs_complete
61
+ called = false
62
+ Replicate::Dumper.new do |dumper|
63
+ filter = lambda { |*args| }
64
+ (class <<filter;self;end).send(:define_method, :complete) { called = true }
65
+ dumper.listen filter
66
+ dumper.dump thing
67
+ assert !called
68
+ end
69
+ assert called
70
+ end
71
+ end
@@ -0,0 +1,93 @@
1
+ require 'test/unit'
2
+ require 'stringio'
3
+ require 'replicate'
4
+
5
+ class LoaderTest < Test::Unit::TestCase
6
+ def setup
7
+ @loader = Replicate::Loader.new
8
+ end
9
+
10
+ def thing(attrs={})
11
+ attrs = {'number' => 123, 'string' => 'hello', 'time' => Time.new}.merge(attrs)
12
+ Replicate::Object.new attrs
13
+ end
14
+
15
+ def test_basic_filter
16
+ called = false
17
+ object = thing('test' => 'value')
18
+ @loader.listen do |type, id, attrs, obj|
19
+ assert !called
20
+ assert_equal 'Replicate::Object', type
21
+ assert_equal object.id, id
22
+ assert_equal 'value', attrs['test']
23
+ assert_equal object.attributes, attrs
24
+ called = true
25
+ end
26
+ @loader.feed object.class, object.id, object.attributes
27
+ assert called
28
+ end
29
+
30
+ def test_reading_from_io
31
+ called = false
32
+ data = Marshal.dump(['Replicate::Object', 10, {'test' => 'value'}])
33
+ @loader.listen do |type, id, attrs, obj|
34
+ assert !called
35
+ assert_equal 'Replicate::Object', type
36
+ assert_equal 'value', attrs['test']
37
+ called = true
38
+ end
39
+ @loader.read(StringIO.new(data))
40
+ assert called
41
+ end
42
+
43
+ def test_stats
44
+ 10.times do
45
+ obj = thing
46
+ @loader.feed obj.class, obj.id, obj.attributes
47
+ end
48
+ assert_equal({'Replicate::Object' => 10}, @loader.stats)
49
+ end
50
+
51
+ def test_block_form_runs_complete
52
+ called = false
53
+ Replicate::Loader.new do |loader|
54
+ filter = lambda { |*args| }
55
+ (class <<filter;self;end).send(:define_method, :complete) { called = true }
56
+ loader.listen filter
57
+ obj = thing
58
+ loader.feed obj.class, obj.id, obj.attributes
59
+ assert !called
60
+ end
61
+ assert called
62
+ end
63
+
64
+ def test_translating_id_attributes
65
+ objects = []
66
+ @loader.listen { |type, id, attrs, object| objects << object }
67
+
68
+ object1 = thing
69
+ @loader.feed object1.class, object1.id, object1.attributes
70
+ object2 = thing('related' => [:id, 'Replicate::Object', object1.id])
71
+ @loader.feed object2.class, object2.id, object2.attributes
72
+
73
+ assert_equal 2, objects.size
74
+ assert_equal objects[0].id, objects[1].related
75
+ end
76
+
77
+ def test_translating_multiple_id_attributes
78
+ objects = []
79
+ @loader.listen { |type, id, attrs, object| objects << object }
80
+
81
+ members = (0..9).map { |i| thing('number' => i) }
82
+ members.each do |member|
83
+ @loader.feed member.class, member.id, member.attributes
84
+ end
85
+
86
+ ids = members.map { |m| m.id }
87
+ referencer = thing('related' => [:id, 'Replicate::Object', ids])
88
+ @loader.feed referencer.class, referencer.id, referencer.attributes
89
+
90
+ assert_equal 11, objects.size
91
+ assert_equal 10, objects.last.related.size
92
+ end
93
+ end
@@ -0,0 +1,10 @@
1
+ require 'test/unit'
2
+
3
+ class ReplicateTest < Test::Unit::TestCase
4
+ def test_auto_loading
5
+ require 'replicate'
6
+ Replicate::Dumper
7
+ Replicate::Loader
8
+ Replicate::Status
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: replicate
3
+ version: !ruby/object:Gem::Version
4
+ hash: 15
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ version: "1.0"
10
+ platform: ruby
11
+ authors:
12
+ - Ryan Tomayko
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-09-08 00:00:00 Z
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: activerecord
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ hash: 7
28
+ segments:
29
+ - 2
30
+ - 2
31
+ version: "2.2"
32
+ type: :development
33
+ version_requirements: *id001
34
+ description: Dump and load relational objects between Ruby environments.
35
+ email: r@tomayko.com
36
+ executables:
37
+ - replicate
38
+ extensions: []
39
+
40
+ extra_rdoc_files: []
41
+
42
+ files:
43
+ - COPYING
44
+ - README.md
45
+ - Rakefile
46
+ - bin/replicate
47
+ - lib/replicate.rb
48
+ - lib/replicate/active_record.rb
49
+ - lib/replicate/dumper.rb
50
+ - lib/replicate/emitter.rb
51
+ - lib/replicate/loader.rb
52
+ - lib/replicate/object.rb
53
+ - lib/replicate/status.rb
54
+ - test/active_record_test.rb
55
+ - test/dumper_test.rb
56
+ - test/loader_test.rb
57
+ - test/replicate_test.rb
58
+ homepage: http://github.com/rtomayko/replicate/
59
+ licenses: []
60
+
61
+ post_install_message:
62
+ rdoc_options: []
63
+
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ hash: 3
72
+ segments:
73
+ - 0
74
+ version: "0"
75
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ hash: 3
81
+ segments:
82
+ - 0
83
+ version: "0"
84
+ requirements: []
85
+
86
+ rubyforge_project:
87
+ rubygems_version: 1.8.6
88
+ signing_key:
89
+ specification_version: 2
90
+ summary: Dump and load relational objects between Ruby environments.
91
+ test_files:
92
+ - test/active_record_test.rb
93
+ - test/dumper_test.rb
94
+ - test/loader_test.rb
95
+ - test/replicate_test.rb