maglevrecord 0.0.3 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (161) hide show
  1. data/.gitignore +35 -0
  2. data/.travis.yml +19 -0
  3. data/Gemfile +13 -0
  4. data/README.md +89 -0
  5. data/Rakefile +22 -0
  6. data/install.sh +238 -0
  7. data/lib/maglev_record/base.rb +10 -62
  8. data/lib/maglev_record/enumerable.rb +13 -24
  9. data/lib/maglev_record/errors.rb +10 -0
  10. data/lib/maglev_record/integration.rb +7 -0
  11. data/lib/maglev_record/maglev_record.rb +13 -0
  12. data/lib/maglev_record/maglev_support/active_support_patch.rb +31 -0
  13. data/lib/maglev_record/maglev_support/concern.rb +19 -0
  14. data/lib/maglev_record/maglev_support/maglev_support.rb +15 -0
  15. data/lib/maglev_record/maglev_support/secure_password.rb +39 -0
  16. data/lib/maglev_record/migration/loader.rb +38 -0
  17. data/lib/maglev_record/migration/migration.rb +142 -0
  18. data/lib/maglev_record/migration/migrator.rb +40 -0
  19. data/lib/maglev_record/migration/operations.rb +92 -0
  20. data/lib/maglev_record/migration.rb +5 -0
  21. data/lib/maglev_record/persistence.rb +23 -48
  22. data/lib/maglev_record/raketasks.rb +9 -0
  23. data/lib/maglev_record/read_write.rb +20 -10
  24. data/lib/maglev_record/rooted_base.rb +8 -0
  25. data/lib/maglev_record/rooted_enumerable.rb +25 -0
  26. data/lib/maglev_record/rooted_persistence.rb +36 -0
  27. data/lib/maglev_record/sensible.rb +28 -0
  28. data/lib/maglev_record/snapshot/change.rb +53 -0
  29. data/lib/maglev_record/snapshot/snapshot.rb +67 -0
  30. data/lib/maglev_record/snapshot/snapshotable.rb +58 -0
  31. data/lib/maglev_record/snapshot.rb +5 -0
  32. data/lib/maglev_record/tools/object_reference.rb +189 -0
  33. data/lib/maglev_record/tools/submodule_finder.rb +42 -0
  34. data/lib/maglev_record/tools.rb +2 -0
  35. data/lib/maglev_record.rb +60 -4
  36. data/lib/tasks/database.rake +38 -0
  37. data/maglevrecord.gemspec +17 -0
  38. data/rails/init.rb +1 -0
  39. data/test/_test_object_reference.rb +70 -0
  40. data/test/example_model.rb +26 -13
  41. data/test/migration/dummy_migrations/migration_1.rb +8 -0
  42. data/test/migration/dummy_migrations/migration_2.rb +8 -0
  43. data/test/migration/dummy_migrations/migration_3.rb +8 -0
  44. data/test/migration/migrations/migration_1.rb +12 -0
  45. data/test/migration/migrations/migration_2.rb +12 -0
  46. data/test/migration/migrations/migration_3.rb +12 -0
  47. data/test/migration/operation_setup.rb +91 -0
  48. data/test/migration/projects/project1/.gitignore +15 -0
  49. data/test/migration/projects/project1/Gemfile +43 -0
  50. data/test/migration/projects/project1/Gemfile.lock +114 -0
  51. data/test/migration/projects/project1/README.rdoc +261 -0
  52. data/test/migration/projects/project1/Rakefile +17 -0
  53. data/test/migration/projects/project1/app/assets/images/rails.png +0 -0
  54. data/test/migration/projects/project1/app/assets/javascripts/application.js +15 -0
  55. data/test/migration/projects/project1/app/assets/stylesheets/application.css +13 -0
  56. data/test/migration/projects/project1/app/controllers/application_controller.rb +3 -0
  57. data/test/migration/projects/project1/app/helpers/application_helper.rb +2 -0
  58. data/test/migration/projects/project1/app/mailers/.gitkeep +0 -0
  59. data/test/migration/projects/project1/app/models/.gitkeep +0 -0
  60. data/test/migration/projects/project1/app/views/layouts/application.html.erb +14 -0
  61. data/test/migration/projects/project1/config/application.rb +62 -0
  62. data/test/migration/projects/project1/config/boot.rb +6 -0
  63. data/test/migration/projects/project1/config/database.yml +25 -0
  64. data/test/migration/projects/project1/config/environment.rb +5 -0
  65. data/test/migration/projects/project1/config/environments/development.rb +37 -0
  66. data/test/migration/projects/project1/config/environments/production.rb +67 -0
  67. data/test/migration/projects/project1/config/initializers/backtrace_silencers.rb +7 -0
  68. data/test/migration/projects/project1/config/initializers/inflections.rb +15 -0
  69. data/test/migration/projects/project1/config/initializers/mime_types.rb +5 -0
  70. data/test/migration/projects/project1/config/initializers/secret_token.rb +7 -0
  71. data/test/migration/projects/project1/config/initializers/session_store.rb +8 -0
  72. data/test/migration/projects/project1/config/initializers/wrap_parameters.rb +14 -0
  73. data/test/migration/projects/project1/config/locales/en.yml +5 -0
  74. data/test/migration/projects/project1/config/routes.rb +58 -0
  75. data/test/migration/projects/project1/config.ru +4 -0
  76. data/test/migration/projects/project1/db/seeds.rb +7 -0
  77. data/test/migration/projects/project1/lib/assets/.gitkeep +0 -0
  78. data/test/migration/projects/project1/lib/tasks/.gitkeep +0 -0
  79. data/test/migration/projects/project1/log/.gitkeep +0 -0
  80. data/test/migration/projects/project1/public/404.html +26 -0
  81. data/test/migration/projects/project1/public/422.html +26 -0
  82. data/test/migration/projects/project1/public/500.html +25 -0
  83. data/test/migration/projects/project1/public/favicon.ico +0 -0
  84. data/test/migration/projects/project1/public/index.html +241 -0
  85. data/test/migration/projects/project1/public/robots.txt +5 -0
  86. data/test/migration/projects/project1/script/rails +6 -0
  87. data/test/migration/projects/project2/.gitignore +15 -0
  88. data/test/migration/projects/project2/Gemfile +44 -0
  89. data/test/migration/projects/project2/Gemfile.lock +115 -0
  90. data/test/migration/projects/project2/README.rdoc +261 -0
  91. data/test/migration/projects/project2/Rakefile +17 -0
  92. data/test/migration/projects/project2/app/assets/images/rails.png +0 -0
  93. data/test/migration/projects/project2/app/assets/javascripts/application.js +15 -0
  94. data/test/migration/projects/project2/app/assets/stylesheets/application.css +13 -0
  95. data/test/migration/projects/project2/app/controllers/application_controller.rb +3 -0
  96. data/test/migration/projects/project2/app/helpers/application_helper.rb +2 -0
  97. data/test/migration/projects/project2/app/mailers/.gitkeep +0 -0
  98. data/test/migration/projects/project2/app/models/.gitkeep +0 -0
  99. data/test/migration/projects/project2/app/models/project_model.rb +8 -0
  100. data/test/migration/projects/project2/app/views/layouts/application.html.erb +14 -0
  101. data/test/migration/projects/project2/config/application.rb +62 -0
  102. data/test/migration/projects/project2/config/boot.rb +6 -0
  103. data/test/migration/projects/project2/config/database.yml +25 -0
  104. data/test/migration/projects/project2/config/environment.rb +5 -0
  105. data/test/migration/projects/project2/config/environments/development.rb +37 -0
  106. data/test/migration/projects/project2/config/environments/production.rb +67 -0
  107. data/test/migration/projects/project2/config/initializers/backtrace_silencers.rb +7 -0
  108. data/test/migration/projects/project2/config/initializers/inflections.rb +15 -0
  109. data/test/migration/projects/project2/config/initializers/mime_types.rb +5 -0
  110. data/test/migration/projects/project2/config/initializers/secret_token.rb +7 -0
  111. data/test/migration/projects/project2/config/initializers/session_store.rb +8 -0
  112. data/test/migration/projects/project2/config/initializers/wrap_parameters.rb +14 -0
  113. data/test/migration/projects/project2/config/locales/en.yml +5 -0
  114. data/test/migration/projects/project2/config/routes.rb +58 -0
  115. data/test/migration/projects/project2/config.ru +4 -0
  116. data/test/migration/projects/project2/db/seeds.rb +7 -0
  117. data/test/migration/projects/project2/lib/assets/.gitkeep +0 -0
  118. data/test/migration/projects/project2/lib/tasks/.gitkeep +0 -0
  119. data/test/migration/projects/project2/log/.gitkeep +0 -0
  120. data/test/migration/projects/project2/migrations/migration_2013-04-Apr-23_17.31.38.rb +16 -0
  121. data/test/migration/projects/project2/migrations/migration_2013-04-Apr-23_17.31.52.rb +16 -0
  122. data/test/migration/projects/project2/migrations/migration_2013-04-Apr-23_17.32.07.rb +16 -0
  123. data/test/migration/projects/project2/public/404.html +26 -0
  124. data/test/migration/projects/project2/public/422.html +26 -0
  125. data/test/migration/projects/project2/public/500.html +25 -0
  126. data/test/migration/projects/project2/public/favicon.ico +0 -0
  127. data/test/migration/projects/project2/public/index.html +241 -0
  128. data/test/migration/projects/project2/public/robots.txt +5 -0
  129. data/test/migration/projects/project2/script/rails +6 -0
  130. data/test/migration/test_loader.rb +78 -0
  131. data/test/migration/test_migration.rb +127 -0
  132. data/test/migration/test_nonexistent_classes.rb +120 -0
  133. data/test/migration/test_operations_fit_together.rb +68 -0
  134. data/test/migration/test_project.rb +105 -0
  135. data/test/migration/test_project1.slow.rb +58 -0
  136. data/test/migration/test_project2.slow.rb +34 -0
  137. data/test/migration/test_remove.rb +219 -0
  138. data/test/migration/test_rename.rb +238 -0
  139. data/test/migration/test_scenario.rb +37 -0
  140. data/test/migration/todo.txt +17 -0
  141. data/test/more_asserts.rb +63 -0
  142. data/test/snapshot/test_snapshot.rb +99 -0
  143. data/test/snapshot/test_snapshot_attributes.slow.rb +57 -0
  144. data/test/snapshot/test_snapshot_classes.slow.rb +32 -0
  145. data/test/snapshot/test_snapshotable.rb +53 -0
  146. data/test/test_accessors.rb +46 -0
  147. data/test/test_active_model_like_interface.rb +1 -1
  148. data/test/test_model_timestamps.rb +22 -0
  149. data/test/test_more_asserts.rb +68 -0
  150. data/test/test_naming.rb +22 -0
  151. data/test/test_persistence.rb +127 -0
  152. data/test/test_query_interface.rb +20 -25
  153. data/test/test_sensibles.rb +38 -0
  154. data/test/test_validation.rb +20 -57
  155. data/todo.txt +1 -0
  156. data/update.sh +180 -0
  157. metadata +156 -12
  158. data/lib/maglev_record/model_not_saved_or_reset.rb +0 -15
  159. data/test/test_dirty_object.rb +0 -50
  160. data/test/test_maglev_record_base.rb +0 -22
  161. data/test/test_maglev_simple_persistance.rb +0 -45
@@ -0,0 +1,78 @@
1
+ require "maglev_record"
2
+ require "more_asserts"
3
+
4
+ class TestMigrationLoader < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @loader = MaglevRecord::MigrationLoader.new
8
+ @m1 = <<-MIGRATION1
9
+ Migration.new(Time.utc(2013, 2, 1, 10, 0, 0, 0), "Change book title") do
10
+ def up
11
+ Book.each do |book|
12
+ book.title = "A new book title"
13
+ end
14
+ end
15
+ def down
16
+ Book.each do |book|
17
+ book.title = "Back to old title"
18
+ end
19
+ end
20
+ end
21
+ MIGRATION1
22
+
23
+ @m2 = <<-MIGRATION2
24
+ Migration.new(Time.utc(2014, 2, 1, 10, 0, 0, 0), "Change book title again") do
25
+ def up
26
+ Book.each do |book|
27
+ book.title = "A even newer book title"
28
+ end
29
+ end
30
+ def down
31
+ Book.each do |book|
32
+ book.title = "A new book title"
33
+ end
34
+ end
35
+ end
36
+ MIGRATION2
37
+ end
38
+
39
+ def migration_folder
40
+ File.dirname(__FILE__) + '/migrations/'
41
+ end
42
+
43
+ def test_load_simple_migration
44
+ assert_equal 0, @loader.migration_list.size
45
+ @loader.load_string @m1
46
+ assert_equal 1, @loader.migration_list.size
47
+ assert @loader.migration_list.any? { |m| m.name == "Change book title" }
48
+ end
49
+
50
+ def test_load_several_migrations
51
+ assert_equal @loader.migration_list.size, 0
52
+ @loader.load_string @m1
53
+ @loader.load_string @m2
54
+ assert_equal @loader.migration_list.size, 2
55
+ assert_equal 2013, @loader.migration_list.first.timestamp.year
56
+ assert_equal 2014, @loader.migration_list.last.timestamp.year
57
+ end
58
+
59
+ def test_correct_migration_order
60
+ # load in wrong order, must be in correct order afterwards
61
+ @loader.load_string @m2
62
+ @loader.load_string @m1
63
+ assert_equal 2013, @loader.migration_list.first.timestamp.year
64
+ assert_equal 2014, @loader.migration_list.last.timestamp.year
65
+ end
66
+
67
+ def test_load_from_file
68
+ @loader.load_file(migration_folder + 'migration_1.rb')
69
+ assert @loader.migration_list.any? { |m| m.name == "Change book title" }
70
+ end
71
+
72
+ def test_load_directory
73
+ @loader.load_file(migration_folder + 'migration_1.rb')
74
+ @loader.load_directory(migration_folder)
75
+ assert_equal 3, @loader.migration_list.size
76
+ end
77
+
78
+ end
@@ -0,0 +1,127 @@
1
+ require "maglev_record"
2
+ require "more_asserts"
3
+ require 'time'
4
+
5
+ class TestMigration < Test::Unit::TestCase
6
+ Migration = MaglevRecord::Migration
7
+
8
+ def setup
9
+ @t1 = "2013-01-20 12:01:03"
10
+ @m = Migration.new(@t1, "migration")
11
+ end
12
+
13
+ def test_migration_with_same_timestamp_and_name_is_same_migration
14
+ m1 = Migration.new(@t1, "same")
15
+ m2 = Migration.new(@t1, "same")
16
+ assert_equal m1.id, m2.id
17
+ assert_equal m1, m2
18
+ end
19
+
20
+ def test_id_is_not_object_id
21
+ m = Migration.new(@t1, "migration")
22
+ assert_not_equal m.id, m.object_id
23
+ end
24
+
25
+ def test_migrations_sort_by_timestamp
26
+ parsed_time = Time.parse(@t1)
27
+ m1 = Migration.new(parsed_time + 1, "migration")
28
+ m2 = Migration.new(parsed_time + 2, "migration")
29
+ m3 = Migration.new(parsed_time + 3, "migration")
30
+ m4 = Migration.new(parsed_time + 4, "migration")
31
+ m5 = Migration.new(parsed_time + 5, "migration")
32
+ l = [m2, m4, m1, m3, m5].sort
33
+ assert_equal l, [m1, m2, m3, m4, m5]
34
+ end
35
+
36
+ def test_migrations_sort_by_name_if_timestamp_equal
37
+ parsed_time = Time.parse(@t1)
38
+ m1 = Migration.new(parsed_time + 1, "migration")
39
+ m2 = Migration.new(parsed_time + 1, "zzmigration")
40
+ m3 = Migration.new(parsed_time + 3, "aamigration")
41
+ m4 = Migration.new(parsed_time + 3, "migration")
42
+ m5 = Migration.new(parsed_time + 5, "migration")
43
+ l = [m2, m4, m1, m3, m5].sort
44
+ assert_equal l, [m1, m2, m3, m4, m5]
45
+ end
46
+
47
+ def test_id
48
+ assert_equal "20130120120103migration", @m.id
49
+ end
50
+
51
+ def test_to_s
52
+ assert_equal Migration.name + "<\"2013-01-20 12:01:03\", \"migration\">", @m.to_s
53
+ end
54
+ end
55
+
56
+
57
+ class TestMigrationUpDown < Test::Unit::TestCase
58
+ Migration = MaglevRecord::Migration
59
+ attr_reader :m
60
+
61
+ def self.test_list
62
+ @@test_list
63
+ end
64
+
65
+ def setup
66
+ @@test_list = []
67
+ @t1 = "2013-01-20 12:01:03"
68
+ @m = Migration.new(@t1, 'test') do
69
+ def up
70
+ TestMigrationUpDown.test_list << 1
71
+ end
72
+ def down
73
+ raise "Wrong call to down" unless TestMigrationUpDown.test_list.delete(1) == 1
74
+ end
75
+ end
76
+ end
77
+
78
+ def test_can_be_done
79
+ m.do
80
+ assert m.done?
81
+ end
82
+
83
+ def test_new_app_is_not_done
84
+ assert_not m.done?
85
+ end
86
+
87
+ def test_undo_fail
88
+ m.undo
89
+ end
90
+
91
+ def test_undo_possible
92
+ m.do
93
+ m.undo
94
+ assert_not m.done?
95
+ end
96
+
97
+ def test_do
98
+ m.do
99
+ assert_equal @@test_list, [1]
100
+ m.undo
101
+ assert_equal @@test_list, []
102
+ end
103
+
104
+ def test_do_twice
105
+ # running do twice only executes it once
106
+ m.do
107
+ m.do
108
+ assert_equal @@test_list.size, 1
109
+ end
110
+
111
+ def test_undo_twice
112
+ # assert_equal in down should not throw an error
113
+ m.undo
114
+ m.undo
115
+ end
116
+
117
+ def test_can_do_if_up_not_set
118
+ m1 = Migration.new(@t1, 'migration')
119
+ m1.do # raises no error!, but nothing happens :)
120
+ assert_equal @@test_list.size, 0
121
+ end
122
+
123
+ def test_can_undo_if_down_not_set
124
+ m1 = Migration.new(@t1, 'migration')
125
+ m1.undo # raises no error!, but nothing happens :)
126
+ end
127
+ end
@@ -0,0 +1,120 @@
1
+ ####
2
+ #
3
+ # Test Cases for non existent classes:
4
+ #
5
+ # rename and delete:
6
+ #
7
+ # - instance variables
8
+ #
9
+ # - attributes
10
+ #
11
+ # - classes
12
+ #
13
+
14
+
15
+
16
+
17
+ require "maglev_record"
18
+ require "more_asserts"
19
+ require 'time'
20
+
21
+
22
+ class TestMigrationRenameAttributes < Test::Unit::TestCase
23
+
24
+ def assert_NonExistentClass_not_present
25
+ assert_raise(NameError) {
26
+ NonExistentClass
27
+ }
28
+ end
29
+
30
+ def test_nonexistent_class_is_not_present
31
+ assert_NonExistentClass_not_present
32
+ end
33
+
34
+ ################## rename instance variables
35
+
36
+ def migration_rename_inst_var
37
+ MaglevRecord::Migration.new(Time.now, "rename instance variable") do
38
+ def up
39
+ NonExistentClass.rename_instance_variable(:@lecturer , :@lecturers)
40
+ end
41
+ end
42
+ end
43
+
44
+ def migration_rename_inst_var_block
45
+ MaglevRecord::Migration.new(Time.now, "rename instance variable") do
46
+ def up
47
+ NonExistentClass.rename_instance_variable(:@lecturer , :@lecturers) {
48
+ |lecturer|
49
+ raise "This should never happen"
50
+ }
51
+ end
52
+ end
53
+ end
54
+
55
+ def test_rename_instance_variable_with_block
56
+ migration_rename_inst_var_block.do
57
+ assert_NonExistentClass_not_present
58
+ end
59
+
60
+ def test_rename_instance_variable
61
+ migration_rename_inst_var.do
62
+ assert_NonExistentClass_not_present
63
+ end
64
+
65
+ ################## rename others
66
+
67
+ def test_rename_attribute
68
+ migration = MaglevRecord::Migration.new(Time.now, "rename instance variable") do
69
+ def up
70
+ NonExistentClass.rename_instance_variable(:@lecturer , :@lecturers)
71
+ end
72
+ end
73
+ migration.do
74
+ assert_NonExistentClass_not_present
75
+ end
76
+
77
+ def test_rename_class
78
+ migration = MaglevRecord::Migration.new(Time.now, "rename instance variable") do
79
+ def up
80
+ rename_class NonExistentClass2, :NonExistentClass
81
+ end
82
+ end
83
+ migration.do
84
+ assert_NonExistentClass_not_present
85
+ end
86
+
87
+ ################## remove
88
+
89
+ def test_delete_instance_variable
90
+ migration = MaglevRecord::Migration.new(Time.now, "remove instance variable") do
91
+ def up
92
+ NonExistentClass.delete_instance_variable(:@lecturer)
93
+ end
94
+ end
95
+ migration.do
96
+ assert_NonExistentClass_not_present
97
+ end
98
+
99
+ def test_delete_attribute
100
+ migration = MaglevRecord::Migration.new(Time.now, "remove attribute") do
101
+ def up
102
+ NonExistentClass.delete_attribute(:lecturer)
103
+ end
104
+ end
105
+ migration.do
106
+ assert_NonExistentClass_not_present
107
+ end
108
+
109
+ def test_delete_class
110
+ migration = MaglevRecord::Migration.new(Time.now, "remove class") do
111
+ def up
112
+ delete_class(NonExistentClass)
113
+ end
114
+ end
115
+ migration.do
116
+ assert_NonExistentClass_not_present
117
+ end
118
+
119
+ end
120
+
@@ -0,0 +1,68 @@
1
+ require "maglev_record"
2
+ require "migration/operation_setup"
3
+ require "more_asserts"
4
+ require 'time'
5
+
6
+
7
+
8
+ #
9
+ # make sure renamed classes can be migrated even if they are not yet existent
10
+ #
11
+ # 1) imagine a system with out a class A and the following migrations to be applied:
12
+ #
13
+ # rename A to B
14
+ # rename B to C
15
+ #
16
+ # 3) fail silently vs. do not execute
17
+ # chosen: 3.2
18
+ # 3.1) Migration touches :ModelClass => not executed if not present
19
+ # 3.2) ModelClass contant returns Null Object that can be migrated but does nothing
20
+ #
21
+
22
+ class TestMigrateWithRenamingWorks < Test::Unit::TestCase
23
+
24
+ def setup
25
+ Object.module_eval "
26
+ class A
27
+ include MaglevRecord::RootedBase
28
+ end
29
+ "
30
+ end
31
+
32
+ def migrations
33
+ [
34
+ MaglevRecord::Migration.new(Time.now, "rename A to B") do
35
+ def up
36
+ rename_class A, :B
37
+ end
38
+ end,
39
+ MaglevRecord::Migration.new(Time.now + 1, "rename B to C") do
40
+ def up
41
+ rename_class B, :C
42
+ end
43
+ end,
44
+ MaglevRecord::Migration.new(Time.now + 2, "rename C to D") do
45
+ def up
46
+ rename_class C, :D
47
+ end
48
+ end
49
+ ].sort
50
+ end
51
+
52
+ def do_migrations
53
+ migrations.each { |migration|
54
+ migration.do
55
+ }
56
+ end
57
+
58
+ def test_can_rename_all_migrations
59
+ class_A = A
60
+ do_migrations
61
+ assert_equal class_A, D
62
+ end
63
+
64
+ end
65
+
66
+
67
+
68
+
@@ -0,0 +1,105 @@
1
+ require "more_asserts"
2
+ require "fileutils"
3
+ require "tmpdir"
4
+
5
+ THIS_DIRECTORY = File.expand_path(File.dirname(__FILE__))
6
+
7
+ #
8
+ # this test creates a temporary directory for the test
9
+ #
10
+
11
+ class TempDirTest < Test::Unit::TestCase
12
+ def setup
13
+ # make temporary directory
14
+ # see http://ruby-doc.org/stdlib-2.0/libdoc/tmpdir/rdoc/Dir.html#method-c-mktmpdir
15
+ @tempdir = Dir.mktmpdir
16
+ end
17
+
18
+ def teardown
19
+ # remove the directory.
20
+ FileUtils.remove_dir tempdir
21
+ end
22
+
23
+ def tempdir
24
+ @tempdir
25
+ end
26
+
27
+ # test the project configuration
28
+
29
+ def test_tempdir_exists
30
+ assert File.directory?(tempdir)
31
+ end
32
+
33
+ end
34
+
35
+ class ProjectTest < TempDirTest
36
+ attr_reader :maglev_record_raketask_wd
37
+
38
+ def setup
39
+ super
40
+ @project_name = 'project1' if @project_name.nil?
41
+ # copy the project over to the tempdir
42
+ FileUtils.cp_r(project_source_directory, tempdir)
43
+ # change to the project directory to easyly run rake
44
+ @maglev_record_raketask_wd = FileUtils.getwd
45
+ FileUtils.chdir(project_directory)
46
+ # link maglevrecord for require
47
+ assert `ln -s #{maglev_record_raketask_wd} maglevrecordgem`
48
+ end
49
+
50
+ def teardown
51
+ super
52
+ FileUtils.chdir(@maglev_record_raketask_wd)
53
+ end
54
+
55
+ def project_directory
56
+ File.join(tempdir, @project_name)
57
+ end
58
+
59
+ def project_source_directory
60
+ File.join(THIS_DIRECTORY, 'projects', @project_name)
61
+ end
62
+
63
+ def test_there_is_a_rakefile_in_the_project_directory
64
+ assert File.file?(File.join(project_directory, 'Rakefile'))
65
+ assert File.file?(File.join(project_source_directory, 'Rakefile'))
66
+ end
67
+
68
+ def test_cwd_is_project_directory
69
+ assert_equal FileUtils.getwd, project_directory
70
+ end
71
+
72
+ #
73
+ # rake returns the output of the process so one can print it
74
+ #
75
+ def rake(args)
76
+ exec("bundle exec rake " + args)
77
+ end
78
+
79
+ def rails_c(commands)
80
+ # add the path where maglev_record can be found
81
+ commands = "$LOAD_PATH.unshift './maglevrecordgem'\n" +
82
+ "$LOAD_PATH.unshift './maglevrecordgem/lib'\n" +
83
+ commands +
84
+ "\nexit\n"
85
+ exec('bundle exec rails c', commands)
86
+ end
87
+
88
+ def exec(_command, input = '')
89
+ File.open('input.txt', 'w') {|f| f.write(input) }
90
+ command = "export MAGLEV_OPTS=\"-W0 --stone #{
91
+ Maglev::System.stone_name}\";#{
92
+ _command} < input.txt"
93
+ output = IO.popen(command) { |f|
94
+ s = line = ''
95
+ while not line.nil?
96
+ s += line
97
+ line = f.gets
98
+ end
99
+ output = s
100
+ }
101
+ # p output
102
+ output
103
+ end
104
+ end
105
+
@@ -0,0 +1,58 @@
1
+ require "migration/test_project"
2
+
3
+ class MigrationTestProject1 < ProjectTest
4
+
5
+ def setup
6
+ super
7
+ Maglev.abort_transaction
8
+ Maglev::PERSISTENT_ROOT['test_apply'] = "it was not set"
9
+ Maglev.commit_transaction
10
+ end
11
+
12
+ def test_tasks_are_listed
13
+ output = rake("-T")
14
+ assert_include? output, 'rake migrate:new'
15
+ assert_include? output, 'rake migrate:up'
16
+ end
17
+
18
+ def test_no_folder_on_start
19
+ assert_not File.directory?('./migrations')
20
+ end
21
+
22
+ def test_new
23
+ rake("migrate:new")
24
+ assert File.directory? './migrations'
25
+ assert_not_equal [], Dir[File.join(FileUtils.pwd, 'migrations', '*')]
26
+ end
27
+
28
+ def test_no_apply
29
+ Maglev.abort_transaction
30
+ assert_not_equal 'it was set', Maglev::PERSISTENT_ROOT['test_apply']
31
+ end
32
+
33
+ def test_apply
34
+ upcode = "Maglev::PERSISTENT_ROOT['test_apply'] = 'it was set'"
35
+ Dir.mkdir('./migrations')
36
+ File.open('./migrations/example_migration.rb', 'w') { |file|
37
+ file.write(MaglevRecord::Migration.file_content(
38
+ Time.now, "test_apply_migration",
39
+ upcode)
40
+ )
41
+ }
42
+ s = rake('migrate:up')
43
+ #p '-' * 30
44
+ #p s
45
+ Maglev.abort_transaction
46
+ assert_equal 'it was set', Maglev::PERSISTENT_ROOT['test_apply']
47
+ end
48
+
49
+ def test_can_migrate_autogenerated_migration_without_error
50
+ rake('migrate:new')
51
+ rake('migrate:up')
52
+ end
53
+
54
+ end
55
+
56
+
57
+
58
+
@@ -0,0 +1,34 @@
1
+ require "migration/test_project"
2
+
3
+ class MigrationProject2 < ProjectTest
4
+
5
+ def setup
6
+ @project_name = 'project2'
7
+ super
8
+ Maglev.persistent do
9
+ Object.remove_const :ProjectModel if defined?(ProjectModel)
10
+ end
11
+ Maglev.commit_transaction
12
+ @s = nil
13
+ end
14
+
15
+ def teardown
16
+ super
17
+ puts @s unless @s.nil?
18
+ end
19
+
20
+ def test_no_models
21
+ Maglev.abort_transaction
22
+ assert_raise(NameError) {
23
+ ProjectModel
24
+ }
25
+ end
26
+
27
+ def test_models_appear
28
+ @s = rails_c("puts ProjectModel\nMaglev.commit_transaction")
29
+ Maglev.abort_transaction
30
+ assert_equal ProjectModel.all, []
31
+ @s = nil
32
+ end
33
+
34
+ end