mongify 1.0.1 → 1.1.0
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.
- checksums.yaml +4 -4
- data/.gitignore +3 -0
- data/.yardopts +3 -0
- data/CHANGELOG.rdoc +5 -0
- data/Gemfile.lock +52 -6
- data/LICENSE +1 -1
- data/README.rdoc +29 -11
- data/Rakefile +37 -9
- data/features/options.feature +2 -0
- data/features/print.feature +1 -1
- data/features/process.feature +10 -1
- data/features/step_definitions/process_steps.rb +11 -1
- data/features/support/env.rb +1 -1
- data/lib/mongify/cli/application.rb +7 -7
- data/lib/mongify/cli/command/worker.rb +18 -14
- data/lib/mongify/cli/options.rb +2 -1
- data/lib/mongify/configuration.rb +5 -5
- data/lib/mongify/database/base_connection.rb +6 -6
- data/lib/mongify/database/column.rb +40 -40
- data/lib/mongify/database/data_row.rb +9 -9
- data/lib/mongify/database/no_sql_connection.rb +61 -35
- data/lib/mongify/database/sql_connection.rb +44 -15
- data/lib/mongify/database/table.rb +62 -46
- data/lib/mongify/exceptions.rb +5 -5
- data/lib/mongify/progressbar.rb +15 -15
- data/lib/mongify/status.rb +8 -8
- data/lib/mongify/translation.rb +19 -17
- data/lib/mongify/translation/process.rb +16 -123
- data/lib/mongify/translation/processor_common.rb +132 -0
- data/lib/mongify/translation/sync.rb +112 -0
- data/lib/mongify/ui.rb +9 -9
- data/lib/mongify/version.rb +1 -1
- data/mongify.gemspec +4 -2
- data/spec/files/deleting_fields_from_embedding_parent_translation.rb +19 -0
- data/spec/files/embedded_parent_translation.rb +1 -1
- data/spec/mongify/cli/application_spec.rb +1 -1
- data/spec/mongify/cli/options_spec.rb +1 -1
- data/spec/mongify/cli/worker_command_spec.rb +46 -17
- data/spec/mongify/database/column_spec.rb +21 -21
- data/spec/mongify/database/data_row_spec.rb +11 -11
- data/spec/mongify/database/no_sql_connection_spec.rb +61 -27
- data/spec/mongify/database/sql_connection_spec.rb +62 -2
- data/spec/mongify/database/table_spec.rb +53 -29
- data/spec/mongify/translation/printer_spec.rb +2 -2
- data/spec/mongify/translation/process_spec.rb +50 -34
- data/spec/mongify/translation/sync_spec.rb +184 -0
- data/spec/mongify/translation_spec.rb +8 -8
- data/spec/mongify/ui_spec.rb +12 -12
- data/spec/mongify_spec.rb +1 -1
- data/spec/spec_helper.rb +8 -1
- data/spec/support/config_reader.rb +2 -2
- data/spec/support/database_generator.rb +68 -25
- data/spec/support/database_output.txt +17 -0
- metadata +41 -6
@@ -8,7 +8,7 @@ describe Mongify::Database::DataRow do
|
|
8
8
|
it "should have method access to hash values" do
|
9
9
|
@datarow.first_name.should == 'Timmy'
|
10
10
|
end
|
11
|
-
|
11
|
+
|
12
12
|
it "should dup the hash" do
|
13
13
|
@hash = {:first_name => 'Bob'}
|
14
14
|
@hash.should_receive(:dup).and_return(@hash)
|
@@ -20,11 +20,11 @@ describe Mongify::Database::DataRow do
|
|
20
20
|
@hash.should_receive(:stringify_keys!)
|
21
21
|
dr = Mongify::Database::DataRow.new(@hash)
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
it "should have a working include? method" do
|
25
25
|
@datarow.should include('first_name')
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
28
|
it "should be able to set a value" do
|
29
29
|
@datarow.first_name = 'Bob'
|
30
30
|
@datarow.first_name.should == 'Bob'
|
@@ -34,15 +34,15 @@ describe Mongify::Database::DataRow do
|
|
34
34
|
@datarow.should include('height')
|
35
35
|
@datarow.height.should == 6
|
36
36
|
end
|
37
|
-
|
37
|
+
|
38
38
|
it "should to_hash" do
|
39
39
|
@datarow.to_hash.should == @hash
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
it "should allow inspect" do
|
43
43
|
@datarow.inspect.should == @hash.inspect
|
44
44
|
end
|
45
|
-
|
45
|
+
|
46
46
|
context "delete" do
|
47
47
|
it "should delete key" do
|
48
48
|
@datarow.delete('age')
|
@@ -53,16 +53,16 @@ describe Mongify::Database::DataRow do
|
|
53
53
|
@datarow.delete('age').should == age
|
54
54
|
end
|
55
55
|
end
|
56
|
-
|
56
|
+
|
57
57
|
it "should work with an empty hash" do
|
58
58
|
dr = Mongify::Database::DataRow.new({})
|
59
59
|
dr.keys.should be_empty
|
60
60
|
end
|
61
|
-
|
61
|
+
|
62
62
|
it "should return all keys in object" do
|
63
63
|
@datarow.keys.should == @hash.keys
|
64
64
|
end
|
65
|
-
|
65
|
+
|
66
66
|
context "respond_to" do
|
67
67
|
it "should be true for first_name" do
|
68
68
|
@datarow.respond_to?('first_name').should be_true
|
@@ -71,7 +71,7 @@ describe Mongify::Database::DataRow do
|
|
71
71
|
@datarow.respond_to?('first_name=').should be_true
|
72
72
|
end
|
73
73
|
end
|
74
|
-
|
74
|
+
|
75
75
|
context "read_attributes" do
|
76
76
|
it "should read attributes" do
|
77
77
|
@datarow.read_attribute('first_name').should == @hash['first_name']
|
@@ -84,7 +84,7 @@ describe Mongify::Database::DataRow do
|
|
84
84
|
@datarow.read_attributes('monkey').should be_nil
|
85
85
|
end
|
86
86
|
end
|
87
|
-
|
87
|
+
|
88
88
|
context "write_attribute" do
|
89
89
|
it "should write attributes" do
|
90
90
|
@datarow.write_attribute('first_name', 'Sam')
|
@@ -6,7 +6,7 @@ describe Mongify::Database::NoSqlConnection do
|
|
6
6
|
@database = 'mongify_test'
|
7
7
|
@mongodb_connection = Mongify::Database::NoSqlConnection.new
|
8
8
|
end
|
9
|
-
|
9
|
+
|
10
10
|
context "valid?" do
|
11
11
|
it "should be true" do
|
12
12
|
Mongify::Database::NoSqlConnection.new(:host => 'localhost', :database => 'blue').should be_valid
|
@@ -14,60 +14,60 @@ describe Mongify::Database::NoSqlConnection do
|
|
14
14
|
it "should be false without any params" do
|
15
15
|
Mongify::Database::NoSqlConnection.new().should_not be_valid
|
16
16
|
end
|
17
|
-
|
17
|
+
|
18
18
|
it "should be false without database" do
|
19
19
|
Mongify::Database::NoSqlConnection.new(:host => 'localhost').should_not be_valid
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
22
|
it "should be false without host" do
|
23
23
|
Mongify::Database::NoSqlConnection.new(:database => 'blue').should_not be_valid
|
24
24
|
end
|
25
25
|
end
|
26
|
-
|
26
|
+
|
27
27
|
it "should rename mongo to mongodb for adapter" do
|
28
28
|
Mongify::Database::NoSqlConnection.new(:host => 'localhost', :database => 'blue', :adapter => 'mongo').adapter.should == 'mongodb'
|
29
29
|
end
|
30
|
-
|
30
|
+
|
31
31
|
context "connection string" do
|
32
32
|
before(:each) do
|
33
33
|
@mongodb_connection.host @host
|
34
34
|
@mongodb_connection.database @database
|
35
35
|
end
|
36
|
-
|
36
|
+
|
37
37
|
context "without username or password" do
|
38
38
|
it "should render correctly" do
|
39
39
|
@mongodb_connection.connection_string.should == "mongodb://#{@host}"
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
it "should include port" do
|
43
43
|
@mongodb_connection.port 10101
|
44
44
|
@mongodb_connection.connection_string.should == "mongodb://#{@host}:10101"
|
45
45
|
end
|
46
46
|
end
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
context "connection" do
|
50
50
|
before(:each) do
|
51
51
|
@mock_connection = mock(:connected? => true)
|
52
52
|
Mongo::Connection.stub(:new).and_return(@mock_connection)
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
it "should only create a connection once" do
|
56
56
|
Mongo::Connection.should_receive(:new).once
|
57
57
|
@mongodb_connection.connection
|
58
58
|
@mongodb_connection.connection
|
59
59
|
end
|
60
|
-
|
60
|
+
|
61
61
|
it "should add_auth if username && password is present" do
|
62
62
|
@mock_connection.should_receive(:add_auth)
|
63
63
|
@mongodb_connection.username "bob"
|
64
64
|
@mongodb_connection.password "secret"
|
65
65
|
@mongodb_connection.connection
|
66
66
|
end
|
67
|
-
|
67
|
+
|
68
68
|
end
|
69
|
-
|
70
|
-
|
69
|
+
|
70
|
+
|
71
71
|
context "database action:" do
|
72
72
|
before(:each) do
|
73
73
|
@collection = mock
|
@@ -81,7 +81,7 @@ describe Mongify::Database::NoSqlConnection do
|
|
81
81
|
@mongodb_connection.insert_into('users', {'first_name' => 'bob'})
|
82
82
|
end
|
83
83
|
end
|
84
|
-
|
84
|
+
|
85
85
|
context "get_id_using_pre_mongified_id" do
|
86
86
|
it "should return new id" do
|
87
87
|
@collection.should_receive(:find_one).with({"pre_mongified_id"=>1}).and_return({'_id' => '123'})
|
@@ -92,14 +92,22 @@ describe Mongify::Database::NoSqlConnection do
|
|
92
92
|
@mongodb_connection.get_id_using_pre_mongified_id('users', 1).should == nil
|
93
93
|
end
|
94
94
|
end
|
95
|
-
|
95
|
+
|
96
96
|
context "select_rows" do
|
97
97
|
it "should return all records" do
|
98
98
|
@collection.should_receive(:find).with().and_return([])
|
99
99
|
@mongodb_connection.select_rows('users')
|
100
100
|
end
|
101
101
|
end
|
102
|
-
|
102
|
+
|
103
|
+
context "select_by_query" do
|
104
|
+
it "should return some records according to a query" do
|
105
|
+
query = {"dummy" => true}
|
106
|
+
@collection.should_receive(:find).with(query).and_return([])
|
107
|
+
@mongodb_connection.select_by_query('users', query)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
103
111
|
context "update" do
|
104
112
|
it "should update the record" do
|
105
113
|
attributes = {'post_id' => 123}
|
@@ -107,7 +115,33 @@ describe Mongify::Database::NoSqlConnection do
|
|
107
115
|
@mongodb_connection.update('users', 1, attributes)
|
108
116
|
end
|
109
117
|
end
|
110
|
-
|
118
|
+
|
119
|
+
context "upsert" do
|
120
|
+
it "should update the record if its pre_mongified_id exists" do
|
121
|
+
attributes = {'pre_mongified_id' => 1, 'post_id' => 123}
|
122
|
+
id = 10
|
123
|
+
duplicate = mock
|
124
|
+
duplicate.stub(:[]).with(:_id).and_return(id)
|
125
|
+
@mongodb_connection.stub(:find_one).with('users', {"pre_mongified_id" => 1}).and_return(duplicate)
|
126
|
+
@mongodb_connection.should_receive(:find_one).with('users', {"pre_mongified_id" => 1})
|
127
|
+
@mongodb_connection.should_receive(:update).with('users', id, attributes)
|
128
|
+
@mongodb_connection.upsert('users', attributes)
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should insert a new record if no record having the same pre_mongified_id exists" do
|
132
|
+
attributes = {'pre_mongified_id' => 1, 'post_id' => 123}
|
133
|
+
@mongodb_connection.should_receive(:find_one).with('users', {"pre_mongified_id" => 1})
|
134
|
+
@mongodb_connection.should_receive(:insert_into).with('users', attributes)
|
135
|
+
@mongodb_connection.upsert('users', attributes)
|
136
|
+
end
|
137
|
+
|
138
|
+
it "should delegate the upsert to the save method of Mongo if no pre_mongified_id to match with the _id" do
|
139
|
+
attributes = {'post_id' => 123}
|
140
|
+
@collection.should_receive(:save).with(attributes)
|
141
|
+
@mongodb_connection.upsert('users', attributes)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
|
111
145
|
context "find_one" do
|
112
146
|
it "should call find_one on collection" do
|
113
147
|
query= {'pre_mongified_id' => 1}
|
@@ -115,12 +149,12 @@ describe Mongify::Database::NoSqlConnection do
|
|
115
149
|
@mongodb_connection.find_one('users', query)
|
116
150
|
end
|
117
151
|
end
|
118
|
-
|
152
|
+
|
119
153
|
it "should create index for pre_mongified_id" do
|
120
154
|
@collection.should_receive(:create_index).with([["pre_mongified_id", Mongo::ASCENDING]]).and_return(true)
|
121
155
|
@mongodb_connection.create_pre_mongified_id_index('users')
|
122
156
|
end
|
123
|
-
|
157
|
+
|
124
158
|
context "remove_pre_mongified_ids" do
|
125
159
|
before(:each) do
|
126
160
|
@collection.stub(:index_information).and_return('pre_mongified_id_1' => 'something')
|
@@ -137,7 +171,7 @@ describe Mongify::Database::NoSqlConnection do
|
|
137
171
|
end
|
138
172
|
end
|
139
173
|
end
|
140
|
-
|
174
|
+
|
141
175
|
context "force" do
|
142
176
|
before(:each) do
|
143
177
|
@mock_connection = mock(:connected? => true, :drop_database => true)
|
@@ -151,12 +185,12 @@ describe Mongify::Database::NoSqlConnection do
|
|
151
185
|
it "should be false" do
|
152
186
|
Mongify::Database::NoSqlConnection.new(:host => 'localhost', :database => 'blue', :force => false).should_not be_forced
|
153
187
|
end
|
154
|
-
|
188
|
+
|
155
189
|
it "should drop database" do
|
156
190
|
@mongodb_connection.connection.should_receive(:drop_database).with('blue').and_return(true)
|
157
191
|
@mongodb_connection.send(:drop_database)
|
158
192
|
end
|
159
|
-
|
193
|
+
|
160
194
|
context "ask permission" do
|
161
195
|
it "should ask to drop database" do
|
162
196
|
Mongify::UI.should_receive(:ask).and_return(false)
|
@@ -165,7 +199,7 @@ describe Mongify::Database::NoSqlConnection do
|
|
165
199
|
it "should not drop database if permission is declined" do
|
166
200
|
Mongify::UI.should_receive(:ask).and_return(false)
|
167
201
|
@mongodb_connection.should_receive(:drop_database).never
|
168
|
-
@mongodb_connection.send(:ask_to_drop_database)
|
202
|
+
@mongodb_connection.send(:ask_to_drop_database)
|
169
203
|
end
|
170
204
|
it "should drop database if permission is granted" do
|
171
205
|
Mongify::UI.should_receive(:ask).and_return(true)
|
@@ -175,21 +209,21 @@ describe Mongify::Database::NoSqlConnection do
|
|
175
209
|
end
|
176
210
|
end
|
177
211
|
|
178
|
-
|
212
|
+
|
179
213
|
describe "working connection" do
|
180
214
|
before(:each) do
|
181
215
|
@mongodb_connection = DatabaseGenerator.mongo_connection
|
182
216
|
end
|
183
|
-
|
217
|
+
|
184
218
|
it "should work" do
|
185
219
|
@mongodb_connection.should be_valid
|
186
220
|
@mongodb_connection.should have_connection
|
187
221
|
end
|
188
|
-
|
222
|
+
|
189
223
|
it "should return a db" do
|
190
224
|
@mongodb_connection.db.should be_a Mongify::Database::NoSqlConnection::DB
|
191
225
|
end
|
192
226
|
end
|
193
|
-
|
227
|
+
|
194
228
|
end
|
195
229
|
|
@@ -6,6 +6,7 @@ describe Mongify::Database::SqlConnection do
|
|
6
6
|
|
7
7
|
let(:sqlite_connection){Mongify::Database::SqlConnection.new(:adapter => 'sqlite3', :database => @db_path)}
|
8
8
|
let(:mysql_connection){@sql_connection = DatabaseGenerator.mysql_connection}
|
9
|
+
let(:postgresql_connection){@sql_connection = DatabaseGenerator.postgresql_connection}
|
9
10
|
|
10
11
|
context "Sqlite 3 config" do
|
11
12
|
context "valid?" do
|
@@ -42,6 +43,27 @@ describe Mongify::Database::SqlConnection do
|
|
42
43
|
end
|
43
44
|
end
|
44
45
|
|
46
|
+
context "Postgres config" do
|
47
|
+
context "valid?" do
|
48
|
+
it "should be true" do
|
49
|
+
Mongify::Database::SqlConnection.new(:adapter => 'postgresql', :host => 'localhost', :database => 'mongify_test').should be_valid
|
50
|
+
end
|
51
|
+
it "should be false" do
|
52
|
+
Mongify::Database::SqlConnection.new(:adapter => 'postgresql').should_not be_valid
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
context "testing connection" do
|
57
|
+
it "should work" do
|
58
|
+
postgresql_connection.should have_connection
|
59
|
+
end
|
60
|
+
it "should call setup_connection_adapter before testing connection" do
|
61
|
+
postgresql_connection.should_receive(:setup_connection_adapter)
|
62
|
+
postgresql_connection.has_connection?
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
45
67
|
context "Sqlite connection" do
|
46
68
|
context "testing connection" do
|
47
69
|
it "should call setup_connection_adapter before testing connection" do
|
@@ -56,7 +78,7 @@ describe Mongify::Database::SqlConnection do
|
|
56
78
|
|
57
79
|
context "tables" do
|
58
80
|
it "should be able to get a list" do
|
59
|
-
sqlite_connection.tables.should =~
|
81
|
+
sqlite_connection.tables.should =~ %w(comments notes posts preferences users teams coaches)
|
60
82
|
end
|
61
83
|
end
|
62
84
|
|
@@ -66,7 +88,7 @@ describe Mongify::Database::SqlConnection do
|
|
66
88
|
end
|
67
89
|
end
|
68
90
|
end
|
69
|
-
|
91
|
+
|
70
92
|
context "select_all" do
|
71
93
|
it "should generate correct select statement" do
|
72
94
|
@mock_conn = mock
|
@@ -75,5 +97,43 @@ describe Mongify::Database::SqlConnection do
|
|
75
97
|
sqlite_connection.select_rows('users')
|
76
98
|
end
|
77
99
|
end
|
100
|
+
|
101
|
+
context "select_by_query" do
|
102
|
+
it "should select rows based on a query" do
|
103
|
+
query = "SELECT * FROM users WHERE true"
|
104
|
+
@mock_conn = mock
|
105
|
+
@mock_conn.should_receive(:select_all).with(query)
|
106
|
+
sqlite_connection.stub(:connection).and_return(@mock_conn)
|
107
|
+
sqlite_connection.select_by_query(query)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
context "count" do
|
112
|
+
it "should get count of all rows in a table" do
|
113
|
+
query = "SELECT COUNT(*) FROM users"
|
114
|
+
@mock_conn = mock
|
115
|
+
@mock_conn.should_receive(:select_value).with(query)
|
116
|
+
sqlite_connection.stub(:connection).and_return(@mock_conn)
|
117
|
+
sqlite_connection.count('users')
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should get count of rows in a table filtered by a query" do
|
121
|
+
query = "SELECT COUNT(*) FROM users WHERE true"
|
122
|
+
@mock_conn = mock
|
123
|
+
@mock_conn.should_receive(:select_value).with(query)
|
124
|
+
sqlite_connection.stub(:connection).and_return(@mock_conn)
|
125
|
+
sqlite_connection.count('users', 'true')
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
context "execute" do
|
130
|
+
it "should execute an arbitrary query" do
|
131
|
+
query = "CREATE TABLE x(int y);"
|
132
|
+
@mock_conn = mock
|
133
|
+
@mock_conn.should_receive(:execute).with(query)
|
134
|
+
sqlite_connection.stub(:connection).and_return(@mock_conn)
|
135
|
+
sqlite_connection.execute(query)
|
136
|
+
end
|
137
|
+
end
|
78
138
|
end
|
79
139
|
|
@@ -4,7 +4,7 @@ describe Mongify::Database::Table do
|
|
4
4
|
before(:each) do
|
5
5
|
@table = Mongify::Database::Table.new('users')
|
6
6
|
end
|
7
|
-
|
7
|
+
|
8
8
|
it "should have name" do
|
9
9
|
@table.name.should == "users"
|
10
10
|
end
|
@@ -15,17 +15,17 @@ describe Mongify::Database::Table do
|
|
15
15
|
@table.name = 'accounts'
|
16
16
|
@table.name.should == 'accounts'
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
19
|
it "should be ingored" do
|
20
20
|
table = Mongify::Database::Table.new('users', :ignore => true)
|
21
21
|
table.should be_ignored
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
24
|
it "should get setup options" do
|
25
25
|
@table = Mongify::Database::Table.new('users', :embed_in => 'accounts', :as => 'users')
|
26
26
|
@table.options.should == {'embed_in' => 'accounts', 'as' => 'users'}
|
27
27
|
end
|
28
|
-
|
28
|
+
|
29
29
|
context "rename_to" do
|
30
30
|
before(:each) do
|
31
31
|
@table = Mongify::Database::Table.new('users', :rename_to => 'people')
|
@@ -38,53 +38,60 @@ describe Mongify::Database::Table do
|
|
38
38
|
@table.sql_name.should == "users"
|
39
39
|
end
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
context "column_index (find_column)" do
|
43
43
|
it "should add column index on column creation" do
|
44
44
|
@table.should_receive(:add_and_index_column)
|
45
45
|
@table.column('first_name', :string)
|
46
46
|
end
|
47
47
|
end
|
48
|
-
|
48
|
+
|
49
49
|
context "column" do
|
50
50
|
it "should add to count" do
|
51
51
|
lambda { @table.column 'name' }.should change{@table.columns.count}.by(1)
|
52
52
|
end
|
53
|
-
|
53
|
+
|
54
54
|
it "should work without a type" do
|
55
55
|
col = @table.column 'name'
|
56
56
|
col.type.should == :string
|
57
57
|
end
|
58
|
-
|
58
|
+
|
59
59
|
it "should work without a type with options" do
|
60
60
|
col = @table.column 'name', :rename_to => 'first_name'
|
61
61
|
col.type.should == :string
|
62
62
|
col.should be_renamed
|
63
63
|
end
|
64
|
-
|
64
|
+
|
65
65
|
it "should be able to find" do
|
66
66
|
@table.column 'another'
|
67
67
|
col = @table.column 'dark'
|
68
68
|
@table.find_column('dark').should == col
|
69
69
|
end
|
70
|
-
|
70
|
+
|
71
71
|
it "should be able to find (case sensitive)" do
|
72
72
|
col = @table.column 'geoCode'
|
73
73
|
@table.column 'filler'
|
74
74
|
@table.find_column('geoCode').should == col
|
75
75
|
end
|
76
|
-
|
76
|
+
|
77
77
|
it "should be searchable with sql_name only" do
|
78
78
|
col = @table.column 'surname', :string, :rename_to => 'last_name'
|
79
79
|
@table.find_column('surname').should == col
|
80
80
|
end
|
81
|
-
|
81
|
+
|
82
82
|
it "should return nil if not found" do
|
83
83
|
@table.column 'dark'
|
84
84
|
@table.find_column('blue').should be_nil
|
85
85
|
end
|
86
|
+
|
87
|
+
it "should be able to find the key column" do
|
88
|
+
@table.column 'name'
|
89
|
+
col = @table.column 'id', :key, :as => :integer
|
90
|
+
@table.column 'address'
|
91
|
+
@table.key_column.should == col
|
92
|
+
end
|
86
93
|
end
|
87
|
-
|
94
|
+
|
88
95
|
context "add_column" do
|
89
96
|
it "should require Mongify::Database::Column" do
|
90
97
|
lambda { @table.add_column("Not a column") }.should raise_error(Mongify::DatabaseColumnExpected)
|
@@ -92,17 +99,17 @@ describe Mongify::Database::Table do
|
|
92
99
|
it "shold except Mongify::Database::Column as a parameter" do
|
93
100
|
lambda { @table.add_column(Mongify::Database::Column.new('test')) }.should_not raise_error(Mongify::DatabaseColumnExpected)
|
94
101
|
end
|
95
|
-
|
102
|
+
|
96
103
|
it "should add to the column count" do
|
97
104
|
lambda { @table.add_column(Mongify::Database::Column.new('test')) }.should change{@table.columns.count}.by(1)
|
98
105
|
end
|
99
|
-
|
106
|
+
|
100
107
|
it "should be indexed" do
|
101
108
|
col = Mongify::Database::Column.new('test')
|
102
109
|
@table.add_column(col)
|
103
110
|
@table.find_column('test').should == col
|
104
111
|
end
|
105
|
-
|
112
|
+
|
106
113
|
context "on initialization" do
|
107
114
|
before(:each) do
|
108
115
|
@columns = [Mongify::Database::Column.new('test1'), Mongify::Database::Column.new('test2')]
|
@@ -119,7 +126,7 @@ describe Mongify::Database::Table do
|
|
119
126
|
end
|
120
127
|
end
|
121
128
|
end
|
122
|
-
|
129
|
+
|
123
130
|
context "reference_colums" do
|
124
131
|
before(:each) do
|
125
132
|
@col1 = Mongify::Database::Column.new('user_id', :integer, :references => 'users')
|
@@ -133,7 +140,7 @@ describe Mongify::Database::Table do
|
|
133
140
|
@table.reference_columns.should =~ [@col1, @col2]
|
134
141
|
end
|
135
142
|
end
|
136
|
-
|
143
|
+
|
137
144
|
context "dealing with embedding," do
|
138
145
|
context "embed_on" do
|
139
146
|
it "should return embed_on option" do
|
@@ -173,7 +180,7 @@ describe Mongify::Database::Table do
|
|
173
180
|
Mongify::Database::Table.new('users').should_not be_embedded
|
174
181
|
end
|
175
182
|
end
|
176
|
-
|
183
|
+
|
177
184
|
context "embed_on" do
|
178
185
|
it "should be post_id" do
|
179
186
|
Mongify::Database::Table.new('comments', :embed_in => 'posts', :on => 'post_id').embed_on.should == 'post_id'
|
@@ -186,7 +193,7 @@ describe Mongify::Database::Table do
|
|
186
193
|
end
|
187
194
|
end
|
188
195
|
end
|
189
|
-
|
196
|
+
|
190
197
|
context "before_save" do
|
191
198
|
before(:each) do
|
192
199
|
@table = Mongify::Database::Table.new('users')
|
@@ -210,6 +217,13 @@ describe Mongify::Database::Table do
|
|
210
217
|
Mongify::Database::DataRow.should_receive(:new).and_return(dr)
|
211
218
|
@table.send(:run_before_save, row)
|
212
219
|
end
|
220
|
+
it "should preserve the pre_mongified_id even if user deletes it" do
|
221
|
+
row = {'first_name' => 'Bob', 'pre_mongified_id' => 1}
|
222
|
+
@table.before_save do |row, _|
|
223
|
+
row.delete('pre_mongified_id')
|
224
|
+
end
|
225
|
+
@table.send(:run_before_save, row).should == row
|
226
|
+
end
|
213
227
|
end
|
214
228
|
it "should work" do
|
215
229
|
@table.translate({'permission' => 51}).should == {'admin' => true}
|
@@ -225,16 +239,26 @@ describe Mongify::Database::Table do
|
|
225
239
|
end
|
226
240
|
end
|
227
241
|
it "should work" do
|
228
|
-
@table.translate({'preferences' => 'yes'}, {}).should == [{}, {'preferences' => 'yes'}]
|
242
|
+
@table.translate({'preferences' => 'yes'}, {}).should == [{}, {'preferences' => 'yes'}, {}]
|
229
243
|
end
|
230
244
|
it "should return blank hash if parent is unchanged" do
|
231
245
|
@table.remove_before_save_filter!
|
232
|
-
@table.translate({'preferences' => "true"}, {}).should == [{'preferences' => "true"}, {}]
|
246
|
+
@table.translate({'preferences' => "true"}, {}).should == [{'preferences' => "true"}, {}, {}]
|
247
|
+
end
|
248
|
+
it 'should return a hash containing the keys that have been deleted on the parent row' do
|
249
|
+
@parent_table.column('field_1')
|
250
|
+
@parent_table.column('field_2')
|
251
|
+
@table.before_save do |_, parent|
|
252
|
+
parent.delete('field_1')
|
253
|
+
end
|
254
|
+
@table.translate({'preferences' => 'true'},{'field_1' => 'a', 'field_2' => 'b'}).should ==
|
255
|
+
[{'preferences' => 'true'}, {'field_2' => 'b'}, {'field_1' => '1'}]
|
233
256
|
end
|
257
|
+
|
234
258
|
end
|
235
|
-
|
236
|
-
|
237
|
-
|
259
|
+
|
260
|
+
|
261
|
+
|
238
262
|
context "polymorphic" do
|
239
263
|
before(:each) do
|
240
264
|
@table = Mongify::Database::Table.new('comments', :polymorphic => :commentable)
|
@@ -248,7 +272,7 @@ describe Mongify::Database::Table do
|
|
248
272
|
it "should return 'commentable'" do
|
249
273
|
@table.polymorphic_as.should == 'commentable'
|
250
274
|
end
|
251
|
-
|
275
|
+
|
252
276
|
context "embed" do
|
253
277
|
before(:each) do
|
254
278
|
@table = Mongify::Database::Table.new('comments', :polymorphic => :commentable, :embed_in => true)
|
@@ -256,10 +280,10 @@ describe Mongify::Database::Table do
|
|
256
280
|
it "should be embedded" do
|
257
281
|
@table.should be_embedded
|
258
282
|
end
|
259
|
-
|
283
|
+
|
260
284
|
end
|
261
285
|
end
|
262
|
-
|
286
|
+
|
263
287
|
context "translate" do
|
264
288
|
before(:each) do
|
265
289
|
@column1 = mock(:translate => {'first_name' => 'Timmy'}, :name => 'first_name')
|
@@ -276,7 +300,7 @@ describe Mongify::Database::Table do
|
|
276
300
|
@column2.should_receive(:translate).with('Zuza').and_return({'last_name' => 'Zuza'})
|
277
301
|
@table.translate({'first_name' => 'Timmy', 'last_name' => 'Zuza'})
|
278
302
|
end
|
279
|
-
|
303
|
+
|
280
304
|
it "should not return any value if column doesn't exist in the translation" do
|
281
305
|
@table.translate({'age' => 18}).should == {}
|
282
306
|
end
|