Skip to content

Commit

Permalink
[#20] Adding tests for duplicate_row endpoint and fixing an issue wit…
Browse files Browse the repository at this point in the history
…h table_name, also moved code around to make it easier to test
  • Loading branch information
fernyb committed Jul 15, 2011
1 parent ee4a141 commit 0e2a33a
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 8 deletions.
31 changes: 30 additions & 1 deletion server/ruby/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,35 @@ def generate_where_for_fields fields
end
}.join(" AND ")
end


def generate_copy_query result, table_name
row = {}

result.each_hash do |item|
item.each_pair {|k,v|
row.merge!({
k.to_s => (v.nil? ? 'NULL' : v)
})
}
end

result.fetch_fields.each do |item|
if item.is_pri_key?
row[item.name] = 'NULL'
end
end

keys = row.keys

insert_keys = keys.map {|k| "`#{k}`" }.join(',')
insert_values = keys.map {|k|
v = row[k]
v == 'NULL' ? 'NULL' : "'#{v}'"
}.join(',')

"INSERT INTO `#{table_name}` (#{insert_keys}) VALUES (#{insert_values})"
end

def database_list_tables
begin
Expand Down Expand Up @@ -579,7 +608,7 @@ def handle_api_path
where_fields = generate_where_for_fields params['where_fields']

result = query "SELECT * FROM `#{params[:table]}` WHERE #{where_fields} LIMIT 1"
new_query_str = result.to_copy_query
new_query_str = generate_copy_query result, params[:table]
query new_query_str

rows = table_rows params[:table]
Expand Down
8 changes: 6 additions & 2 deletions server/ruby/ext/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def fields
[]
end

def each_hash(&block)
def each_hash &block
self.each do |item|
block.call(item)
end
Expand All @@ -16,4 +16,8 @@ def each_hash(&block)
def num_rows
self.size
end
}

def fetch_fields
self
end
}
4 changes: 1 addition & 3 deletions server/ruby/ext/mysql.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def rows
rows
end

def to_copy_query
def to_copy_query table_name
row = {}

self.each_hash do |item|
Expand All @@ -25,10 +25,8 @@ def to_copy_query
}
end

table_name = ''
self.fetch_fields.each do |item|
if item.is_pri_key?
table_name = item.table
row[item.name] = 'NULL'
end
end
Expand Down
58 changes: 56 additions & 2 deletions server/ruby/spec/app_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1146,8 +1146,62 @@ def json
end

describe '/duplicate_row/:table' do
it 'can duplicate a row' do
pending
before do
@where_fields = {
id: '4',
name: 'fernyb',
description: 'hello'
}
end

it 'can duplicate a row without Primary Key' do
where_query = @where_fields.to_query_string_with_key('where_fields')
row = {'id' => '4'}
row.should_receive(:is_pri_key?).and_return false
result = [row]

select_qs = "SELECT * FROM `checkins` WHERE `id` = '4' AND `name` = 'fernyb' AND `description` = 'hello' LIMIT 1"
@mysql.should_receive(:query).with(select_qs).and_return(result)

new_sql_str = "INSERT INTO `checkins` (`id`) VALUES ('4')"
@mysql.should_receive(:query).with(new_sql_str)

columns = []
@mysql.should_receive(:query).with("SHOW COLUMNS FROM `checkins`").and_return columns
@mysql.should_receive(:query).with("SELECT * FROM `checkins` LIMIT 0,100").and_return []

post "/duplicate_row/checkins?#{where_query}"
json['query'].should == new_sql_str
end

it 'can duplicate a row having Primary Key' do
where_query = @where_fields.to_query_string_with_key('where_fields')
row = {'id' => '4'}
row.should_receive(:name).and_return 'id'
row.should_receive(:is_pri_key?).and_return true

row2 = {'name' => nil}
row2.should_not_receive(:name)
row2.should_receive(:is_pri_key?).and_return false

row3 = {'age' => '26'}
row3.should_not_receive(:name)
row3.should_receive(:is_pri_key?).and_return false
result = [row, row2, row3]

select_qs = "SELECT * FROM `checkins` WHERE `id` = '4' AND `name` = 'fernyb' AND `description` = 'hello' LIMIT 1"
@mysql.should_receive(:query).with(select_qs).and_return(result)

new_sql_str = "INSERT INTO `checkins` (`id`,`name`,`age`) VALUES (NULL,NULL,'26')"
@mysql.should_receive(:query).with(new_sql_str)

columns = []
@mysql.should_receive(:query).with("SHOW COLUMNS FROM `checkins`").and_return columns
@mysql.should_receive(:query).with("SELECT * FROM `checkins` LIMIT 0,100").and_return []

post "/duplicate_row/checkins?#{where_query}"
json['query'].should == new_sql_str
end

end
end

0 comments on commit 0e2a33a

Please sign in to comment.