Skip to content

Commit

Permalink
adding some test for /schema/:table POST
Browse files Browse the repository at this point in the history
  • Loading branch information
fernyb committed Jun 22, 2011
1 parent 7e2e8dd commit 5f6f927
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 26 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.DS_Store
Build/
Build/
coverage/
2 changes: 2 additions & 0 deletions server/ruby/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ GEM
rack (1.3.0)
rack-test (0.6.0)
rack (>= 1.0)
rcov (0.9.9)
rspec (2.6.0)
rspec-core (~> 2.6.0)
rspec-expectations (~> 2.6.0)
Expand All @@ -29,6 +30,7 @@ DEPENDENCIES
json (= 1.5.1)
mysql (= 2.8.1)
rack-test (= 0.6.0)
rcov (= 0.9.9)
rspec (= 2.6.0)
shotgun (= 0.9)
sinatra (= 1.2.0)
2 changes: 2 additions & 0 deletions server/ruby/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ task :spec do
RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = "spec/**/*_spec.rb"
t.rspec_opts = ['--options', "#{File.dirname(__FILE__)}/spec/spec.opts"]
t.rcov = true
t.rcov_opts = %w{--rails --exclude osx\/objc,gems\/,spec\/,features\/}
t.verbose = true
end
end
2 changes: 1 addition & 1 deletion server/ruby/ext/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
def to_query_string
params = []
self.each_pair do |k,v|
params << "#{URI.escape(k, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}=#{URI.escape(v, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}"
params << "#{URI.escape(k.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}=#{URI.escape(v, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))}"
end
params.join("&")
end
Expand Down
1 change: 1 addition & 0 deletions server/ruby/gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ end
group :test do
gem 'rspec', '2.6.0'
gem 'rack-test', '0.6.0'
gem 'rcov', '0.9.9'
end
187 changes: 163 additions & 24 deletions server/ruby/spec/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,34 +198,173 @@ def json
end

describe '/schema/:table' do
it 'returns the schema for table_name' do
@mysql.should_receive(:query).with("SHOW COLUMNS FROM `table_name`").and_return([
["id", "int(11) unsigned zerofill", "NO", "PRI", nil, "auto_increment"],
["user_id", "int(11)", "YES", "MUL", nil, ""],
["name", "varchar(255)", "YES", "", nil, ""],
["description", "text", "YES", "", nil, ""],
["latitude", "float", "YES", "MUL", nil, ""],
["longitude", "float", "YES", "MUL", nil, ""],
["created_at", "datetime", "YES", "MUL", nil, ""],
["updated_at", "datetime", "YES", "", nil, ""],
["test", "varchar(255)", "YES", "MUL", nil, ""]
])

get '/schema/table_name'
describe "GET" do
it 'returns the schema for table_name' do
@mysql.should_receive(:query).with("SHOW COLUMNS FROM `table_name`").and_return([
["id", "int(11) unsigned zerofill", "NO", "PRI", nil, "auto_increment"],
["user_id", "int(11)", "YES", "MUL", nil, ""],
["name", "varchar(255)", "YES", "", nil, ""],
["description", "text", "YES", "", nil, ""],
["latitude", "float", "YES", "MUL", nil, ""],
["longitude", "float", "YES", "MUL", nil, ""],
["created_at", "datetime", "YES", "MUL", nil, ""],
["updated_at", "datetime", "YES", "", nil, ""],
["test", "varchar(255)", "YES", "MUL", nil, ""]
])

get '/schema/table_name'

json['path'].should == '/schema/table_name'
json['error'].should == ''
json['fields'].size == 9
json['fields'].first.keys.should include('Field', 'Type', 'Length', 'Unsigned', 'Zerofill', 'Binary', 'Allow Null', 'Key', 'Default')
end

it 'returns error message for Mysql Error' do
@mysql.should_receive(:query).and_raise(Mysql::Error.new('There is an error.'))
get '/schema/table_name'

json['path'].should == '/schema/table_name'
json['error'].should == ''
json['fields'].size == 9
json['fields'].first.keys.should include('Field', 'Type', 'Length', 'Unsigned', 'Zerofill', 'Binary', 'Allow Null', 'Key', 'Default')
json['path'].should == '/schema/table_name'
json['error'].should == 'There is an error.'
json['fields'].size == 0
end
end

it 'returns error message for Mysql Error' do
@mysql.should_receive(:query).and_raise(Mysql::Error.new('There is an error.'))
get '/schema/table_name'
describe "POST" do
before do
@query = {
port: '3306',
database: 'skatr_development',
host: 'localhost',
password: 'password',
username: 'root',
field: 'id',
type: 'int',
length: '11',
unsigned: 'NO',
name: 'unsigned',
extra: 'auto_increment',
null: 'NO'
}
end

it "updates the id field usigned attribute when unsigned is NO" do
@mysql.should_receive(:query).with("ALTER TABLE `checkins` CHANGE `id` `id` int(11) NOT NULL auto_increment").and_return nil
@mysql.should_receive(:query).with("SHOW COLUMNS FROM `checkins`").and_return([
["id", "int(11) zerofill", "NO", "PRI", nil, "auto_increment"],
["user_id", "int(11)", "YES", "MUL", nil, ""],
["name", "varchar(255)", "YES", "", nil, ""],
["description", "text", "YES", "", nil, ""],
["latitude", "float", "YES", "MUL", nil, ""],
["longitude", "float", "YES", "MUL", nil, ""],
["created_at", "datetime", "YES", "MUL", nil, ""],
["updated_at", "datetime", "YES", "", nil, ""],
["test", "varchar(255)", "YES", "MUL", nil, ""]
])

post "/schema/checkins?#{@query.to_query_string}"

json['path'].should == '/schema/checkins'
json['error'].should == ''
json['fields'].size.should == 9
json['fields'].first['Field'].should == 'id'
json['fields'].first['Unsigned'].should be_false
end

it "updates the id field usigned attribute when unsigned is YES" do
@query[:name] = 'unsigned'
@query[:unsigned] = 'YES'

@mysql.should_receive(:query).with("ALTER TABLE `checkins` CHANGE `id` `id` int(11) UNSIGNED NOT NULL auto_increment").and_return nil
@mysql.should_receive(:query).with("SHOW COLUMNS FROM `checkins`").and_return([
["id", "int(11) unsigned zerofill", "NO", "PRI", nil, "auto_increment"],
["user_id", "int(11)", "YES", "MUL", nil, ""],
["name", "varchar(255)", "YES", "", nil, ""],
["description", "text", "YES", "", nil, ""],
["latitude", "float", "YES", "MUL", nil, ""],
["longitude", "float", "YES", "MUL", nil, ""],
["created_at", "datetime", "YES", "MUL", nil, ""],
["updated_at", "datetime", "YES", "", nil, ""],
["test", "varchar(255)", "YES", "MUL", nil, ""]
])

post "/schema/checkins?#{@query.to_query_string}"

json['path'].should == '/schema/checkins'
json['error'].should == ''
json['fields'].size.should == 9
json['fields'].first['Field'].should == 'id'
json['fields'].first['Unsigned'].should be_true
end

it "updates the extra field, auto_increment" do
@query[:name] = ''
@query[:unsigned] = 'YES'
@query[:extra] = 'auto_increment'

@mysql.should_receive(:query).with("ALTER TABLE `checkins` CHANGE `id` `id` int(11) UNSIGNED NOT NULL auto_increment").and_return nil
@mysql.should_receive(:query).with("SHOW COLUMNS FROM `checkins`").and_return([
["id", "int(11) unsigned zerofill", "NO", "PRI", nil, "auto_increment"],
["user_id", "int(11)", "YES", "MUL", nil, ""],
["name", "varchar(255)", "YES", "", nil, ""],
["description", "text", "YES", "", nil, ""],
["latitude", "float", "YES", "MUL", nil, ""],
["longitude", "float", "YES", "MUL", nil, ""],
["created_at", "datetime", "YES", "MUL", nil, ""],
["updated_at", "datetime", "YES", "", nil, ""],
["test", "varchar(255)", "YES", "MUL", nil, ""]
])

post "/schema/checkins?#{@query.to_query_string}"

json['path'].should == '/schema/checkins'
json['error'].should == ''
json['fields'].size.should == 9
json['fields'].first['Field'].should == 'id'
json['fields'].first['Extra'].should.should == 'auto_increment'
end

it "updates the extra field, to be none and should not be unsigned" do
@query[:name] = ''
@query[:unsigned] = 'NO'
@query[:extra] = 'none'

@mysql.should_receive(:query).with("ALTER TABLE `checkins` CHANGE `id` `id` int(11) NOT NULL").and_return nil
@mysql.should_receive(:query).with("SHOW COLUMNS FROM `checkins`").and_return([
["id", "int(11) zerofill", "NO", "PRI", nil, ""],
["user_id", "int(11)", "YES", "MUL", nil, ""],
["name", "varchar(255)", "YES", "", nil, ""],
["description", "text", "YES", "", nil, ""],
["latitude", "float", "YES", "MUL", nil, ""],
["longitude", "float", "YES", "MUL", nil, ""],
["created_at", "datetime", "YES", "MUL", nil, ""],
["updated_at", "datetime", "YES", "", nil, ""],
["test", "varchar(255)", "YES", "MUL", nil, ""]
])

post "/schema/checkins?#{@query.to_query_string}"

json['path'].should == '/schema/checkins'
json['error'].should == ''
json['fields'].size.should == 9
json['fields'].first['Field'].should == 'id'
json['fields'].first['Extra'].should.should == ''
json['fields'].first['Unsigned'].should be_false
end

it "can handle MySQL errors" do
@query[:name] = ''
@query[:unsigned] = 'NO'
@query[:extra] = 'none'

@mysql.should_receive(:query).with("ALTER TABLE `checkins` CHANGE `id` `id` int(11) NOT NULL").and_raise(Mysql::Error.new("There is an error..."))

post "/schema/checkins?#{@query.to_query_string}"

json['path'].should == '/schema/table_name'
json['error'].should == 'There is an error.'
json['fields'].size == 0
json['path'].should == '/schema/checkins'
json['error'].should == 'There is an error...'
json['fields'].size.should == 0
end
end
end

Expand Down

0 comments on commit 5f6f927

Please sign in to comment.