mongify 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +20 -0
- data/CHANGELOG.rdoc +22 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +68 -0
- data/LICENSE +20 -0
- data/README.rdoc +86 -0
- data/Rakefile +73 -0
- data/bin/mongify +14 -0
- data/features/options.feature +46 -0
- data/features/print.feature +10 -0
- data/features/process.feature +19 -0
- data/features/step_definitions/mongify_steps.rb +42 -0
- data/features/step_definitions/mongo_steps.rb +7 -0
- data/features/support/env.rb +28 -0
- data/lib/mongify.rb +22 -0
- data/lib/mongify/cli.rb +8 -0
- data/lib/mongify/cli/application.rb +43 -0
- data/lib/mongify/cli/help_command.rb +16 -0
- data/lib/mongify/cli/options.rb +94 -0
- data/lib/mongify/cli/report.rb +11 -0
- data/lib/mongify/cli/version_command.rb +18 -0
- data/lib/mongify/cli/worker_command.rb +93 -0
- data/lib/mongify/configuration.rb +46 -0
- data/lib/mongify/database.rb +5 -0
- data/lib/mongify/database/base_connection.rb +78 -0
- data/lib/mongify/database/column.rb +81 -0
- data/lib/mongify/database/no_sql_connection.rb +62 -0
- data/lib/mongify/database/sql_connection.rb +61 -0
- data/lib/mongify/database/table.rb +74 -0
- data/lib/mongify/exceptions.rb +13 -0
- data/lib/mongify/translation.rb +55 -0
- data/lib/mongify/translation/printer.rb +20 -0
- data/lib/mongify/translation/process.rb +61 -0
- data/lib/mongify/ui.rb +45 -0
- data/lib/mongify/version.rb +3 -0
- data/mongify.gemspec +42 -0
- data/spec/default.watch +199 -0
- data/spec/files/base_configuration.rb +9 -0
- data/spec/files/empty_translation.rb +0 -0
- data/spec/files/simple_translation.rb +26 -0
- data/spec/mongify/cli/application_spec.rb +19 -0
- data/spec/mongify/cli/help_command_spec.rb +18 -0
- data/spec/mongify/cli/options_spec.rb +62 -0
- data/spec/mongify/cli/version_command_spec.rb +24 -0
- data/spec/mongify/cli/worker_command_spec.rb +115 -0
- data/spec/mongify/configuration_spec.rb +25 -0
- data/spec/mongify/database/base_connection_spec.rb +59 -0
- data/spec/mongify/database/column_spec.rb +103 -0
- data/spec/mongify/database/no_sql_connection_spec.rb +131 -0
- data/spec/mongify/database/sql_connection_spec.rb +91 -0
- data/spec/mongify/database/table_spec.rb +120 -0
- data/spec/mongify/translation/printer_spec.rb +34 -0
- data/spec/mongify/translation/process_spec.rb +68 -0
- data/spec/mongify/translation_spec.rb +59 -0
- data/spec/mongify/ui_spec.rb +73 -0
- data/spec/mongify_spec.rb +15 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +22 -0
- data/spec/support/config_reader.rb +21 -0
- data/spec/support/database.example +17 -0
- data/spec/support/database_output.txt +27 -0
- data/spec/support/generate_database.rb +91 -0
- metadata +370 -0
@@ -0,0 +1,25 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe Mongify::Configuration do
|
4
|
+
before(:each) do
|
5
|
+
Mongify::Translation.stub(:parse)
|
6
|
+
@translation_file = File.expand_path(File.dirname(__FILE__) + '/../files/empty_translation.rb')
|
7
|
+
@configuration_file = File.expand_path(File.dirname(__FILE__) + '/../files/base_configuration.rb')
|
8
|
+
end
|
9
|
+
it "should parse file for transaltion" do
|
10
|
+
Mongify::Translation.should_receive(:parse).and_return(true)
|
11
|
+
Mongify::Configuration.parse_translation(@translation_file)
|
12
|
+
end
|
13
|
+
|
14
|
+
context "configuration file" do
|
15
|
+
it "should parse confg file" do
|
16
|
+
Mongify::Configuration.should_receive(:parse).and_return(true)
|
17
|
+
Mongify::Configuration.parse_configuration(@configuration_file)
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should validate file exists" do
|
21
|
+
lambda { Mongify::Configuration.parse_configuration("../missing_file.rb") }.should raise_error(Mongify::FileNotFound)
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongify::Database::BaseConnection do
|
4
|
+
before(:each) do
|
5
|
+
@base_connection = Mongify::Database::BaseConnection.new
|
6
|
+
end
|
7
|
+
it "should set any variable name that's passed" do
|
8
|
+
@base_connection = Mongify::Database::BaseConnection.new(:apple => 'blue', :car => 'good')
|
9
|
+
@base_connection.instance_variables.should =~ ['@apple', '@car']
|
10
|
+
end
|
11
|
+
|
12
|
+
context "validation" do
|
13
|
+
it "should be true" do
|
14
|
+
@base_connection.host 'localhost'
|
15
|
+
@base_connection.host.should == "localhost"
|
16
|
+
@base_connection.should be_valid
|
17
|
+
end
|
18
|
+
|
19
|
+
it "should be false" do
|
20
|
+
@base_connection.should_not be_valid
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
it "should raise error when trying to call has_connection?" do
|
25
|
+
lambda { @base_connection.has_connection? }.should raise_error(NotImplementedError)
|
26
|
+
end
|
27
|
+
it "should raise error when trying to call setup_connection_adapter" do
|
28
|
+
lambda { @base_connection.setup_connection_adapter}.should raise_error(NotImplementedError)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should raise error on setting unknown variable setting" do
|
32
|
+
lambda{@base_connection.connection = "localhost"}.should raise_error
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should respond to available settings" do
|
36
|
+
@base_connection.respond_to?(:host).should be_true
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should force adaptor to a string" do
|
40
|
+
@base_connection.adapter :sqlite
|
41
|
+
@base_connection.adapter.should == 'sqlite'
|
42
|
+
end
|
43
|
+
|
44
|
+
context "hash" do
|
45
|
+
before(:each) do
|
46
|
+
@adapter = 'baseDB'
|
47
|
+
@host = '127.0.0.1'
|
48
|
+
@database = 'test_database'
|
49
|
+
end
|
50
|
+
it "should give settings in a hash" do
|
51
|
+
@sql_connection = Mongify::Database::BaseConnection.new(:adapter => @adapter, :host => @host, :database => @database)
|
52
|
+
@sql_connection.to_hash.should == {:adapter => @adapter, :host => @host, :database => @database}
|
53
|
+
end
|
54
|
+
it "should setup from constructor hash" do
|
55
|
+
@sql_connection = Mongify::Database::BaseConnection.new(:adapter => @adapter, :host => @host, :database => @database)
|
56
|
+
@sql_connection.to_hash.should == {:adapter => @adapter, :host => @host, :database => @database}
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongify::Database::Column do
|
4
|
+
before(:each) do
|
5
|
+
@column = Mongify::Database::Column.new('first_name')
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should have name" do
|
9
|
+
@column.name.should == 'first_name'
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should get setup options" do
|
13
|
+
@column = Mongify::Database::Column.new('account_id', :integer, :references => 'accounts')
|
14
|
+
@column.options.should == {'references' => 'accounts'}
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should force type to string if nil" do
|
18
|
+
@column = Mongify::Database::Column.new('first_name', nil)
|
19
|
+
@column.type.should == :string
|
20
|
+
end
|
21
|
+
|
22
|
+
context "key" do
|
23
|
+
it "should be true" do
|
24
|
+
@column = Mongify::Database::Column.new('id', :key)
|
25
|
+
@column.should be_a_key
|
26
|
+
end
|
27
|
+
|
28
|
+
it "should be true" do
|
29
|
+
@column = Mongify::Database::Column.new('first_name', :string)
|
30
|
+
@column.should_not be_a_key
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
context "options" do
|
35
|
+
it "should allow to be set by name" do
|
36
|
+
@column = Mongify::Database::Column.new('first_name')
|
37
|
+
@column.references = "users"
|
38
|
+
@column.references.should == "users"
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should not allow to be set unless they're in the AVAILABLE_OPTIONS" do
|
42
|
+
@column = Mongify::Database::Column.new('first_name')
|
43
|
+
lambda { @column.unknown = "users" }.should raise_error(NoMethodError)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
context "auto_detect" do
|
48
|
+
context "id" do
|
49
|
+
it "should type to key" do
|
50
|
+
@column = Mongify::Database::Column.new('id', :integer)
|
51
|
+
@column.type.should == :key
|
52
|
+
end
|
53
|
+
it "should not set type to key if original type is not integer" do
|
54
|
+
@column = Mongify::Database::Column.new('id', :string)
|
55
|
+
@column.type.should == :string
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should detect references" do
|
60
|
+
@column = Mongify::Database::Column.new('user_id', :integer)
|
61
|
+
@column.references.should == "users"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
context :to_print do
|
66
|
+
before(:each) do
|
67
|
+
@column = Mongify::Database::Column.new('first_name', :string)
|
68
|
+
end
|
69
|
+
it "should output column name and type" do
|
70
|
+
@column.to_print.should == %Q[column "first_name", :string]
|
71
|
+
end
|
72
|
+
it "should detect references" do
|
73
|
+
@column = Mongify::Database::Column.new('user_id', :integer)
|
74
|
+
@column.to_print.should == %Q[column "user_id", :integer, :references => "users"]
|
75
|
+
end
|
76
|
+
it "should output nil options" do
|
77
|
+
@column.default = nil
|
78
|
+
@column.to_print.should == %Q[column "first_name", :string]
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context :reference? do
|
83
|
+
it "should be true" do
|
84
|
+
@column = Mongify::Database::Column.new('user_id', :integer, :references => 'users')
|
85
|
+
@column.should be_a_reference
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
context :translate do
|
90
|
+
it "should return a hash with the new translation" do
|
91
|
+
@column = Mongify::Database::Column.new('first_name', :string)
|
92
|
+
@column.translate('bob').should == {'first_name' => 'bob'}
|
93
|
+
end
|
94
|
+
it "should return a datetime format" do
|
95
|
+
@column = Mongify::Database::Column.new('created_at', :datetime)
|
96
|
+
@column.translate('2011-01-14 21:23:39').should == {'created_at' => Time.utc(2011, 01, 14, 21, 23,39)}
|
97
|
+
end
|
98
|
+
it "should return pre_mongified_id when type is a key" do
|
99
|
+
@column = Mongify::Database::Column.new('id', :key)
|
100
|
+
@column.translate(123123).should == {"pre_mongified_id" => 123123}
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,131 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongify::Database::NoSqlConnection do
|
4
|
+
before(:each) do
|
5
|
+
@host = '127.0.0.1'
|
6
|
+
@database = 'mongify_test'
|
7
|
+
@mongodb_connection = Mongify::Database::NoSqlConnection.new
|
8
|
+
end
|
9
|
+
|
10
|
+
context "valid?" do
|
11
|
+
it "should be true" do
|
12
|
+
Mongify::Database::NoSqlConnection.new(:host => 'localhost', :database => 'blue').should be_valid
|
13
|
+
end
|
14
|
+
it "should be false without any params" do
|
15
|
+
Mongify::Database::NoSqlConnection.new().should_not be_valid
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should be false without database" do
|
19
|
+
Mongify::Database::NoSqlConnection.new(:host => 'localhost').should_not be_valid
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should be false without host" do
|
23
|
+
Mongify::Database::NoSqlConnection.new(:database => 'blue').should_not be_valid
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context "connection string" do
|
28
|
+
before(:each) do
|
29
|
+
@mongodb_connection.host @host
|
30
|
+
@mongodb_connection.database @database
|
31
|
+
end
|
32
|
+
|
33
|
+
context "without username or password" do
|
34
|
+
it "should render correctly" do
|
35
|
+
@mongodb_connection.connection_string.should == "mongodb://#{@host}"
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should include port" do
|
39
|
+
@mongodb_connection.port 10101
|
40
|
+
@mongodb_connection.connection_string.should == "mongodb://#{@host}:10101"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "connection" do
|
46
|
+
before(:each) do
|
47
|
+
@mock_connection = mock(:connected? => true)
|
48
|
+
Mongo::Connection.stub(:new).and_return(@mock_connection)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should only create a connection once" do
|
52
|
+
Mongo::Connection.should_receive(:new).once
|
53
|
+
@mongodb_connection.connection
|
54
|
+
@mongodb_connection.connection
|
55
|
+
end
|
56
|
+
|
57
|
+
it "should add_auth if username && password is present" do
|
58
|
+
@mock_connection.should_receive(:add_auth)
|
59
|
+
@mongodb_connection.username "bob"
|
60
|
+
@mongodb_connection.password "secret"
|
61
|
+
@mongodb_connection.connection
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should reset connection on reset" do
|
65
|
+
Mongo::Connection.should_receive(:new).twice
|
66
|
+
@mongodb_connection.connection
|
67
|
+
@mongodb_connection.reset!
|
68
|
+
@mongodb_connection.connection
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
context "database action:" do
|
74
|
+
before(:each) do
|
75
|
+
@collection = mock
|
76
|
+
@db = mock
|
77
|
+
@db.stub(:[]).with('users').and_return(@collection)
|
78
|
+
@mongodb_connection.stub(:db).and_return(@db)
|
79
|
+
end
|
80
|
+
context "insert_into" do
|
81
|
+
it "should insert into a table using the mongo driver" do
|
82
|
+
@collection.should_receive(:insert).with({'first_name' => 'bob'}, anything)
|
83
|
+
@mongodb_connection.insert_into('users', {'first_name' => 'bob'})
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
context "get_id_using_pre_mongified_id" do
|
88
|
+
it "should return new id" do
|
89
|
+
@collection.should_receive(:find_one).with({"pre_mongified_id"=>1}).and_return({'_id' => '123'})
|
90
|
+
@mongodb_connection.get_id_using_pre_mongified_id('users', 1).should == '123'
|
91
|
+
end
|
92
|
+
it "should return nil if nothing is found" do
|
93
|
+
@collection.should_receive(:find_one).with({"pre_mongified_id"=>1}).and_return(nil)
|
94
|
+
@mongodb_connection.get_id_using_pre_mongified_id('users', 1).should == nil
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
context "select_rows" do
|
99
|
+
it "should return all records" do
|
100
|
+
@collection.should_receive(:find).with().and_return([])
|
101
|
+
@mongodb_connection.select_rows('users')
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context "update" do
|
106
|
+
it "should update the record" do
|
107
|
+
attributes = {'post_id' => 123}
|
108
|
+
@collection.should_receive(:update).with({"_id" => 1}, attributes)
|
109
|
+
@mongodb_connection.update('users', 1, attributes)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
|
115
|
+
describe "working connection" do
|
116
|
+
before(:each) do
|
117
|
+
@mongodb_connection = GenerateDatabase.mongo_connection
|
118
|
+
end
|
119
|
+
|
120
|
+
it "should work" do
|
121
|
+
@mongodb_connection.should be_valid
|
122
|
+
@mongodb_connection.should have_connection
|
123
|
+
end
|
124
|
+
|
125
|
+
it "should return a db" do
|
126
|
+
@mongodb_connection.db.should be_a Mongify::Database::NoSqlConnection::DB
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
end
|
131
|
+
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongify::Database::SqlConnection do
|
4
|
+
before(:all) do
|
5
|
+
@db_path = GenerateDatabase.sqlite
|
6
|
+
end
|
7
|
+
|
8
|
+
before(:each) do
|
9
|
+
@sql_connection = Mongify::Database::SqlConnection.new(:adapter => 'sqlite3', :database => @db_path)
|
10
|
+
end
|
11
|
+
|
12
|
+
context "Sqlite 3 config" do
|
13
|
+
before(:each) do
|
14
|
+
@adapter = 'sqlite3'
|
15
|
+
@sql_connection = Mongify::Database::SqlConnection.new(:adapter => @adapter, :database => @db_path)
|
16
|
+
end
|
17
|
+
|
18
|
+
context "valid?" do
|
19
|
+
it "should be true" do
|
20
|
+
@sql_connection.should be_valid
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "testing connection" do
|
25
|
+
it "should work" do
|
26
|
+
@sql_connection.should have_connection
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "MySql config" do
|
32
|
+
before(:each) do
|
33
|
+
@sql_connection = GenerateDatabase.mysql_connection
|
34
|
+
end
|
35
|
+
|
36
|
+
context "valid?" do
|
37
|
+
it "should be true" do
|
38
|
+
Mongify::Database::SqlConnection.new(:adapter => 'mysql', :host => 'localhost', :database => 'blue').should be_valid
|
39
|
+
end
|
40
|
+
it "should be false" do
|
41
|
+
Mongify::Database::SqlConnection.new(:adapter => 'mysql').should_not be_valid
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
context "testing connection" do
|
46
|
+
it "should call setup_connection_adapter before testing connection" do
|
47
|
+
@sql_connection.should_receive(:setup_connection_adapter)
|
48
|
+
@sql_connection.has_connection?
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should work" do
|
52
|
+
@sql_connection.should have_connection
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context "Sqlite connection" do
|
58
|
+
context "testing connection" do
|
59
|
+
it "should call setup_connection_adapter before testing connection" do
|
60
|
+
@sql_connection.should_receive(:setup_connection_adapter)
|
61
|
+
@sql_connection.has_connection?
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should work" do
|
65
|
+
@sql_connection.should have_connection
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
context "tables" do
|
70
|
+
it "should be able to get a list" do
|
71
|
+
@sql_connection.tables.sort.should == ['comments', 'posts', 'users'].sort
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context "columns" do
|
76
|
+
it "should see columns for a table" do
|
77
|
+
@sql_connection.columns_for(:users).map{ |column| column.name }.sort.should == ['id', 'first_name', 'last_name', 'created_at', 'updated_at'].sort
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
context "select_all" do
|
83
|
+
it "should generate correct select statement" do
|
84
|
+
@mock_conn = mock
|
85
|
+
@mock_conn.should_receive(:select_all).with('SELECT * FROM users')
|
86
|
+
@sql_connection.stub(:connection).and_return(@mock_conn)
|
87
|
+
@sql_connection.select_rows('users')
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
@@ -0,0 +1,120 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Mongify::Database::Table do
|
4
|
+
before(:each) do
|
5
|
+
@table = Mongify::Database::Table.new('users')
|
6
|
+
end
|
7
|
+
|
8
|
+
it "should have name" do
|
9
|
+
@table.name.should == "users"
|
10
|
+
end
|
11
|
+
it "should allow you to change table name" do
|
12
|
+
@table.name = 'accounts'
|
13
|
+
@table.name.should == 'accounts'
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should get setup options" do
|
17
|
+
@table = Mongify::Database::Table.new('users', :embed_in => 'accounts', :as => 'users')
|
18
|
+
@table.options.should == {'embed_in' => 'accounts', 'as' => 'users'}
|
19
|
+
end
|
20
|
+
|
21
|
+
context "column_index (find_column)" do
|
22
|
+
it "should add column index on column creation" do
|
23
|
+
@table.should_receive(:add_column_index).with('first_name', 0)
|
24
|
+
@table.column('first_name', :string)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "column" do
|
29
|
+
it "should add to count" do
|
30
|
+
lambda { @table.column 'name' }.should change{@table.columns.count}.by(1)
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should work without a type" do
|
34
|
+
col = @table.column 'name', :default => '123'
|
35
|
+
col.type.should == :string
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should be able to find" do
|
39
|
+
@table.column 'another'
|
40
|
+
col = @table.column 'dark'
|
41
|
+
@table.find_column('dark').should == col
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should return nil if not found" do
|
45
|
+
@table.column 'dark'
|
46
|
+
@table.find_column('blue').should be_nil
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context "add_column" do
|
51
|
+
it "should require Mongify::Database::Column" do
|
52
|
+
lambda { @table.add_column("Not a column") }.should raise_error(Mongify::DatabaseColumnExpected)
|
53
|
+
end
|
54
|
+
it "shold except Mongify::Database::Column as a parameter" do
|
55
|
+
lambda { @table.add_column(Mongify::Database::Column.new('test')) }.should_not raise_error(Mongify::DatabaseColumnExpected)
|
56
|
+
end
|
57
|
+
|
58
|
+
it "should add to the column count" do
|
59
|
+
lambda { @table.add_column(Mongify::Database::Column.new('test')) }.should change{@table.columns.count}.by(1)
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should be indexed" do
|
63
|
+
col = Mongify::Database::Column.new('test')
|
64
|
+
@table.add_column(col)
|
65
|
+
@table.find_column('test').should == col
|
66
|
+
end
|
67
|
+
|
68
|
+
context "on initialization" do
|
69
|
+
before(:each) do
|
70
|
+
@columns = [Mongify::Database::Column.new('test1'), Mongify::Database::Column.new('test2')]
|
71
|
+
@table = Mongify::Database::Table.new('users', :columns => @columns)
|
72
|
+
end
|
73
|
+
it "should add columns" do
|
74
|
+
@table.columns.should have(2).columns
|
75
|
+
end
|
76
|
+
it "should remove columns from the options" do
|
77
|
+
@table.options.should_not have_key('columns')
|
78
|
+
end
|
79
|
+
it "should be indexed" do
|
80
|
+
@table.find_column('test1').should == @columns[0]
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context "reference_colums" do
|
86
|
+
before(:each) do
|
87
|
+
@col1 = Mongify::Database::Column.new('user_id', :integer, :referneces => :users)
|
88
|
+
@col2 = Mongify::Database::Column.new('post_id', :integer, :references => 'posts')
|
89
|
+
@columns = [@col1,
|
90
|
+
Mongify::Database::Column.new('body'),
|
91
|
+
@col2]
|
92
|
+
@table = Mongify::Database::Table.new('comments', :columns => @columns)
|
93
|
+
end
|
94
|
+
it "should return an array of columns" do
|
95
|
+
@table.reference_columns.should == [@col1, @col2]
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
context "translate" do
|
100
|
+
before(:each) do
|
101
|
+
@column1 = mock(:translate => {'first_name' => 'Timmy'}, :name => 'first_name')
|
102
|
+
@column2 = mock(:translate => {'last_name' => 'Zuza'}, :name => 'last_name')
|
103
|
+
@table.stub(:find_column).with(anything).and_return(nil)
|
104
|
+
@table.stub(:find_column).with('first_name').and_return(@column1)
|
105
|
+
@table.stub(:find_column).with('last_name').and_return(@column2)
|
106
|
+
end
|
107
|
+
it "should return a correct hash" do
|
108
|
+
@table.translate({'first_name' => 'Timmy', 'last_name' => 'Zuza'}).should == {'first_name' => 'Timmy', 'last_name' => 'Zuza'}
|
109
|
+
end
|
110
|
+
it "should send translate to both columns with the given value" do
|
111
|
+
@column1.should_receive(:translate).with('Timmy').and_return({'first_name' => 'Timmy'})
|
112
|
+
@column2.should_receive(:translate).with('Zuza').and_return({'last_name' => 'Zuza'})
|
113
|
+
@table.translate({'first_name' => 'Timmy', 'last_name' => 'Zuza'})
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should return same values if column doesn't exist in the translation" do
|
117
|
+
@table.translate({'age' => 18}).should == {'age' => 18}
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|