Skip to content

Commit cbc5e86

Browse files
author
Stefanie Grunwald
authored
Merge pull request #6 from moertel/special-col-support
Add support for jsonb and arrays
2 parents 7ba6a41 + 89615b5 commit cbc5e86

File tree

3 files changed

+159
-5
lines changed

3 files changed

+159
-5
lines changed

lib/squcumber-postgres/mock/database.rb

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,31 @@ def _get_create_table_statement(schema, table)
117117

118118
def _get_column_definitions(table_definition)
119119
table_definition.map do |definition|
120-
if definition['data_type'].eql?('character')
121-
definition['data_type'] = "#{definition['data_type']}(#{definition['character_maximum_length'].to_s})"
120+
schema_column_type = ''
121+
is_array = false
122+
if definition['data_type'].eql?('ARRAY')
123+
is_array = true
124+
schema_column_type = definition['udt_name'].gsub(/^\_/, '')
125+
else
126+
schema_column_type = definition['data_type']
122127
end
123-
"#{definition['column_name']} #{definition['data_type']} default null"
128+
129+
# Deal with (var)chars
130+
if definition['character_maximum_length']
131+
schema_column_type = schema_column_type + "(#{definition['character_maximum_length'].to_s})"
132+
end
133+
134+
# Deal with decimals
135+
if definition['udt_name'].eql?('numeric') and definition['numeric_precision'] and definition['numeric_scale']
136+
schema_column_type = schema_column_type + "(#{definition['numeric_precision'].to_s},#{definition['numeric_scale'].to_s})"
137+
end
138+
139+
# Deal with arrays
140+
if is_array
141+
schema_column_type = schema_column_type + '[]'
142+
end
143+
144+
"#{definition['column_name']} #{schema_column_type} default null"
124145
end
125146
end
126147
end

spec/squcumber-postgres/mock/database_spec.rb

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,139 @@ module Squcumber::Postgres::Mock
335335
end
336336
end
337337

338+
describe '#_get_column_definitions' do
339+
let(:column_definition_integer) do
340+
{
341+
'table_schema' => 'some_schema',
342+
'table_name' => 'some_table',
343+
'column_name' => 'some_column',
344+
'udt_name' => 'int4',
345+
'data_type' => 'integer',
346+
'character_maximum_length' => nil,
347+
'numeric_precision' => 32,
348+
'numeric_scale' => 0
349+
}
350+
end
351+
let(:column_definition_varchar) do
352+
{
353+
'table_schema' => 'some_schema',
354+
'table_name' => 'some_table',
355+
'column_name' => 'some_column',
356+
'udt_name' => 'varchar',
357+
'data_type' => 'character varying',
358+
'character_maximum_length' => 255,
359+
'numeric_precision' => nil,
360+
'numeric_scale' => nil
361+
}
362+
end
363+
let(:column_definition_varchar_array) do
364+
{
365+
'table_schema' => 'some_schema',
366+
'table_name' => 'some_table',
367+
'column_name' => 'some_column',
368+
'udt_name' => '_varchar',
369+
'data_type' => 'ARRAY',
370+
'character_maximum_length' => nil,
371+
'numeric_precision' => nil,
372+
'numeric_scale' => nil
373+
}
374+
end
375+
let(:column_definition_char) do
376+
{
377+
'table_schema' => 'some_schema',
378+
'table_name' => 'some_table',
379+
'column_name' => 'some_column',
380+
'udt_name' => '_bpchar',
381+
'data_type' => 'character',
382+
'character_maximum_length' => 50,
383+
'numeric_precision' => nil,
384+
'numeric_scale' => nil
385+
}
386+
end
387+
let(:column_definition_jsonb) do
388+
{
389+
'table_schema' => 'some_schema',
390+
'table_name' => 'some_table',
391+
'column_name' => 'some_column',
392+
'udt_name' => 'jsonb',
393+
'data_type' => 'jsonb',
394+
'character_maximum_length' => nil,
395+
'numeric_precision' => nil,
396+
'numeric_scale' => nil
397+
}
398+
end
399+
let(:column_definition_timestamp) do
400+
{
401+
'table_schema' => 'some_schema',
402+
'table_name' => 'some_table',
403+
'column_name' => 'some_column',
404+
'udt_name' => 'timestamp',
405+
'data_type' => 'timestamp without time zone',
406+
'character_maximum_length' => nil,
407+
'numeric_precision' => nil,
408+
'numeric_scale' => nil
409+
}
410+
end
411+
let(:column_definition_numeric) do
412+
{
413+
'table_schema' => 'some_schema',
414+
'table_name' => 'some_table',
415+
'column_name' => 'some_column',
416+
'udt_name' => 'numeric',
417+
'data_type' => 'numeric',
418+
'character_maximum_length' => nil,
419+
'numeric_precision' => 6,
420+
'numeric_scale' => 2
421+
}
422+
end
423+
424+
before(:each) do
425+
@dummy = described_class.new(production_database)
426+
end
427+
428+
it 'parses integer columns correctly' do
429+
expect(@dummy.send(:_get_column_definitions, [column_definition_integer])).to match([
430+
'some_column integer default null'
431+
])
432+
end
433+
434+
it 'parses varchar columns correctly' do
435+
expect(@dummy.send(:_get_column_definitions, [column_definition_varchar])).to match([
436+
'some_column character varying(255) default null'
437+
])
438+
end
439+
440+
it 'parses array varchar columns correctly' do
441+
expect(@dummy.send(:_get_column_definitions, [column_definition_varchar_array])).to match([
442+
'some_column varchar[] default null'
443+
])
444+
end
445+
446+
it 'parses char columns correctly' do
447+
expect(@dummy.send(:_get_column_definitions, [column_definition_char])).to match([
448+
'some_column character(50) default null'
449+
])
450+
end
451+
452+
it 'parses jsonb columns correctly' do
453+
expect(@dummy.send(:_get_column_definitions, [column_definition_jsonb])).to match([
454+
'some_column jsonb default null'
455+
])
456+
end
457+
458+
it 'parses timestamp columns correctly' do
459+
expect(@dummy.send(:_get_column_definitions, [column_definition_timestamp])).to match([
460+
'some_column timestamp without time zone default null'
461+
])
462+
end
463+
464+
it 'parses numeric columns correctly' do
465+
expect(@dummy.send(:_get_column_definitions, [column_definition_numeric])).to match([
466+
'some_column numeric(6,2) default null'
467+
])
468+
end
469+
end
470+
338471
describe '#_get_create_table_statement' do
339472
let(:schema) { 'some_schema' }
340473
let(:table) { 'some_table' }

squcumber-postgres.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
Gem::Specification.new do |s|
22
s.name = 'squcumber-postgres'
3-
s.version = '0.0.4'
3+
s.version = '0.0.5'
44
s.default_executable = 'squcumber-postgres'
55

66
s.licenses = ['MIT']
77
s.required_ruby_version = '>= 2.0'
88
s.required_rubygems_version = Gem::Requirement.new('>= 0') if s.respond_to? :required_rubygems_version=
99
s.authors = ['Stefanie Grunwald']
10-
s.date = %q{2018-09-03}
10+
s.date = %q{2018-09-05}
1111
s.email = %q{steffi@physics.org}
1212
s.files = [
1313
'Rakefile',

0 commit comments

Comments
 (0)