benhutton-mysql2psql 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,121 @@
1
+ require 'test_helper'
2
+
3
+ require 'mysql2psql'
4
+
5
+ class ConvertToFileTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ seed_test_database
9
+ @options=get_test_config_by_label(:localmysql_to_file_convert_all)
10
+ @mysql2psql = Mysql2psql.new([@options.filepath])
11
+ @mysql2psql.convert
12
+ @content = IO.read(@mysql2psql.options.destfile)
13
+ end
14
+ def teardown
15
+ delete_files_for_test_config(@options)
16
+ end
17
+ def content
18
+ @content
19
+ end
20
+
21
+ # verify table creation
22
+ def test_table_creation
23
+ assert_not_nil content.match('DROP TABLE IF EXISTS "numeric_types_basics" CASCADE')
24
+ assert_not_nil content.match(/CREATE TABLE "numeric_types_basics"/)
25
+ end
26
+
27
+ # tests for the conversion of numeric types
28
+ def get_basic_numerics_match(column)
29
+ Regexp.new('CREATE TABLE "numeric_types_basics".*"' + column + '" ([^\n]*).*\)', Regexp::MULTILINE).match( content )
30
+ end
31
+ def test_basic_numerics_tinyint
32
+ match = get_basic_numerics_match( 'f_tinyint' )
33
+ assert_match /smallint/,match[1]
34
+ end
35
+ def test_basic_numerics_tinyint_u
36
+ match = get_basic_numerics_match( 'f_tinyint_u' )
37
+ assert_match /smallint/,match[1]
38
+ end
39
+ def test_basic_numerics_smallint
40
+ match = get_basic_numerics_match( 'f_smallint' )
41
+ assert_match /smallint/,match[1]
42
+ end
43
+ def test_basic_numerics_smallint_u
44
+ match = get_basic_numerics_match( 'f_smallint_u' )
45
+ assert_match /integer/,match[1]
46
+ end
47
+ def test_basic_numerics_mediumint
48
+ match = get_basic_numerics_match( 'f_mediumint' )
49
+ assert_match /integer/,match[1]
50
+ end
51
+ def test_basic_numerics_int
52
+ match = get_basic_numerics_match( 'f_int' )
53
+ assert_match /integer/,match[1]
54
+ end
55
+ def test_basic_numerics_integer
56
+ match = get_basic_numerics_match( 'f_integer' )
57
+ assert_match /integer/,match[1]
58
+ end
59
+ def test_basic_numerics_bigint
60
+ match = get_basic_numerics_match( 'f_bigint' )
61
+ assert_match /bigint/,match[1]
62
+ #assert_not_nil Regexp.new('CREATE TABLE "numeric_types_basics".*"f_bigint" bigint,.*\)', Regexp::MULTILINE).match( content )
63
+ end
64
+ def test_basic_numerics_real
65
+ match = get_basic_numerics_match( 'f_real' )
66
+ assert_match /double precision/,match[1]
67
+ #assert_not_nil Regexp.new('CREATE TABLE "numeric_types_basics".*"f_real" double precision,.*\)', Regexp::MULTILINE).match( content )
68
+ end
69
+ def test_basic_numerics_double
70
+ match = get_basic_numerics_match( 'f_double' )
71
+ assert_match /double precision/,match[1]
72
+ #assert_not_nil Regexp.new('CREATE TABLE "numeric_types_basics".*"f_double" double precision,.*\)', Regexp::MULTILINE).match( content )
73
+ end
74
+ def test_basic_numerics_float
75
+ match = get_basic_numerics_match( 'f_float' )
76
+ assert_match /double precision/,match[1]
77
+ #assert_not_nil Regexp.new('CREATE TABLE "numeric_types_basics".*"f_float" double precision.*\)', Regexp::MULTILINE).match( content )
78
+ end
79
+ def test_basic_numerics_float_u
80
+ match = get_basic_numerics_match( 'f_float_u' )
81
+ assert_match /double precision/,match[1]
82
+ #assert_not_nil Regexp.new('CREATE TABLE "numeric_types_basics".*"f_float_u" double precision.*\)', Regexp::MULTILINE).match( content )
83
+ end
84
+ def test_basic_numerics_decimal
85
+ match = get_basic_numerics_match( 'f_decimal' )
86
+ assert_match /numeric/,match[1]
87
+ #assert_not_nil Regexp.new('CREATE TABLE "numeric_types_basics".*"f_decimal" numeric\(10, 0\),.*\)', Regexp::MULTILINE).match( content )
88
+ end
89
+ def test_basic_numerics_numeric
90
+ match = get_basic_numerics_match( 'f_numeric' )
91
+ assert_match /numeric/,match[1]
92
+ #assert_not_nil Regexp.new('CREATE TABLE "numeric_types_basics".*"f_numeric" numeric\(10, 0\)[\w\n]*\)', Regexp::MULTILINE).match( content )
93
+ end
94
+
95
+ # test boolean conversion
96
+ def get_boolean_column_definition_match(column)
97
+ Regexp.new('CREATE TABLE "test_boolean_conversion".*"' + column + '" ([^\n]*)[^;]*', Regexp::MULTILINE).match(content)[1]
98
+ end
99
+ def test_boolean_default_values
100
+ assert_match /DEFAULT false/, get_boolean_column_definition_match('bit_1_default_0')
101
+ assert_match /DEFAULT false/, get_boolean_column_definition_match('tinyint_1_default_0')
102
+
103
+ assert_match /DEFAULT true/, get_boolean_column_definition_match('bit_1_default_1')
104
+ assert_match /DEFAULT true/, get_boolean_column_definition_match('tinyint_1_default_1')
105
+ assert_match /DEFAULT true/, get_boolean_column_definition_match('tinyint_1_default_2')
106
+ end
107
+
108
+ # test autoincrement handling
109
+ def test_autoincrement
110
+ assert_not_nil Regexp.new('CREATE TABLE "basic_autoincrement".*"auto_id" integer DEFAULT.*\)', Regexp::MULTILINE).match( content )
111
+ end
112
+
113
+ # test view creation (or lack thereof!)
114
+ def test_should_not_copy_views_as_tables
115
+ assert_no_match /CREATE TABLE "test_view"/, content
116
+ end
117
+
118
+ def test_truncate
119
+ assert_match /TRUNCATE/, content
120
+ end
121
+ end
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+
3
+ require 'mysql2psql/converter'
4
+
5
+ class ConverterTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ seed_test_database
9
+ @@options=get_test_config_by_label(:localmysql_to_file_convert_nothing)
10
+ end
11
+ def teardown
12
+ delete_files_for_test_config(@@options)
13
+ end
14
+ def options
15
+ @@options
16
+ end
17
+
18
+ def test_new_converter
19
+ assert_nothing_raised do
20
+ reader=get_test_reader(options)
21
+ writer=get_test_file_writer(options)
22
+ converter=Mysql2psql::Converter.new(reader,writer,options)
23
+ assert_equal 0,converter.convert
24
+ end
25
+ end
26
+
27
+
28
+ end
@@ -0,0 +1,34 @@
1
+ require 'test_helper'
2
+
3
+ require 'mysql2psql/mysql_reader'
4
+
5
+ class MysqlReaderBaseTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ seed_test_database
9
+ @options = get_test_config_by_label(:localmysql_to_file_convert_nothing)
10
+ end
11
+
12
+ def teardown
13
+ delete_files_for_test_config(@options)
14
+ end
15
+
16
+ def test_mysql_connection
17
+ assert_nothing_raised do
18
+ reader = Mysql2psql::MysqlReader.new(@options)
19
+ end
20
+ end
21
+ def test_mysql_reconnect
22
+ assert_nothing_raised do
23
+ reader = Mysql2psql::MysqlReader.new(@options)
24
+ reader.reconnect
25
+ end
26
+ end
27
+ def test_mysql_connection_without_port
28
+ assert_nothing_raised do
29
+ @options.mysqlport = ""
30
+ @options.mysqlsocket = ""
31
+ reader = Mysql2psql::MysqlReader.new(@options)
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,41 @@
1
+ require 'test_helper'
2
+
3
+ class MysqlReaderTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ seed_test_database
7
+ @options = get_test_config_by_label(:localmysql_to_file_convert_nothing)
8
+ @reader=get_test_reader(@options)
9
+ end
10
+ def teardown
11
+ delete_files_for_test_config(@options)
12
+ end
13
+ def reader
14
+ @reader
15
+ end
16
+
17
+ def test_db_connection
18
+ assert_nothing_raised do
19
+ reader.mysql.ping
20
+ end
21
+ end
22
+ def test_tables_collection
23
+ values = reader.tables.select{|t| t.name == 'numeric_types_basics'}
24
+ assert_true values.length==1
25
+ assert_equal 'numeric_types_basics', values[0].name
26
+ end
27
+ def test_paginated_read
28
+ expected_rows=5
29
+ page_size=2
30
+ expected_pages=(1.0 * expected_rows / page_size).ceil
31
+
32
+ row_count=my_row_count=0
33
+ table = reader.tables.select{|t| t.name == 'numeric_types_basics'}[0]
34
+ reader.paginated_read(table, page_size) do |row,counter|
35
+ row_count=counter
36
+ my_row_count+=1
37
+ end
38
+ assert_equal expected_rows, row_count
39
+ assert_equal expected_rows, my_row_count
40
+ end
41
+ end
@@ -0,0 +1,22 @@
1
+ require 'test_helper'
2
+
3
+ require 'mysql2psql/postgres_db_writer'
4
+
5
+ class PostgresDbWriterBaseTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ seed_test_database
9
+ @options = get_test_config_by_label(:localmysql_to_db_convert_nothing)
10
+ end
11
+
12
+ def teardown
13
+ delete_files_for_test_config(@options)
14
+ end
15
+
16
+ def test_pg_connection
17
+ assert_nothing_raised do
18
+ reader = Mysql2psql::PostgresDbWriter.new(@options)
19
+ end
20
+ end
21
+
22
+ end
@@ -0,0 +1,33 @@
1
+ module Test::Unit
2
+
3
+ class TestCase
4
+
5
+ def self.must(name, &block)
6
+ test_name = "test_#{name.gsub(/\s+/,'_')}".to_sym
7
+ defined = instance_method(test_name) rescue false
8
+ raise "#{test_name} is already defined in #{self}" if defined
9
+ if block_given?
10
+ define_method(test_name, &block)
11
+ else
12
+ define_method(test_name) do
13
+ flunk "No implementation provided for #{name}"
14
+ end
15
+ end
16
+ end
17
+
18
+ end
19
+ end
20
+
21
+
22
+ module Test::Unit::Assertions
23
+ def assert_false(object, message="")
24
+ assert_equal(false, object, message)
25
+ end
26
+ def assert_true(object, message="")
27
+ assert_equal(true, object, message)
28
+ end
29
+ def assert_nil(object, message="")
30
+ assert_equal(nil, object, message)
31
+ end
32
+ end
33
+
@@ -0,0 +1,131 @@
1
+ require 'rubygems'
2
+ begin
3
+ gem 'test-unit'
4
+ require "test/unit"
5
+ rescue LoadError
6
+ # assume using stdlib Test:Unit
7
+ require 'test/unit'
8
+ end
9
+
10
+ require 'ext_test_unit'
11
+
12
+ def seed_test_database
13
+ options=get_test_config_by_label(:localmysql_to_file_convert_nothing)
14
+ seedfilepath = "#{File.dirname(__FILE__)}/../fixtures/seed_integration_tests.sql"
15
+ mysql_cmd = `which mysql`.empty? ? 'mysql5' : 'mysql'
16
+ rc=system("#{mysql_cmd} -u#{options.mysqlusername} #{options.mysqldatabase} < #{seedfilepath}")
17
+ raise StandardError unless rc
18
+ return true
19
+ rescue
20
+ raise StandardError.new("Failed to seed integration test db. See README for setup requirements.")
21
+ ensure
22
+ delete_files_for_test_config(options)
23
+ end
24
+
25
+ def get_test_reader(options)
26
+ require 'mysql2psql/mysql_reader'
27
+ Mysql2psql::MysqlReader.new(options)
28
+ rescue
29
+ raise StandardError.new("Failed to initialize integration test db. See README for setup requirements.")
30
+ end
31
+
32
+ def get_test_file_writer(options)
33
+ require 'mysql2psql/postgres_file_writer'
34
+ Mysql2psql::PostgresFileWriter.new(options.destfile)
35
+ rescue => e
36
+ puts e.inspect
37
+ raise StandardError.new("Failed to initialize file writer from #{options.inspect}. See README for setup requirements.")
38
+ end
39
+
40
+ def get_test_converter(options)
41
+ require 'mysql2psql/converter'
42
+ reader=get_test_reader(options)
43
+ writer=get_test_file_writer(options)
44
+ Mysql2psql::Converter.new(reader,writer,options)
45
+ rescue
46
+ raise StandardError.new("Failed to initialize converter from #{options.inspect}. See README for setup requirements.")
47
+ end
48
+
49
+ def get_temp_file(basename)
50
+ require 'tempfile'
51
+ f = Tempfile.new(basename)
52
+ path = f.path
53
+ f.close!()
54
+ path
55
+ end
56
+
57
+ def get_new_test_config(options={})
58
+ require 'mysql2psql/config'
59
+ require 'mysql2psql/config_base'
60
+ if options.delete(:to_file)
61
+ options[:to_filename] = get_temp_file('mysql2psql_tmp_output')
62
+ end
63
+ configtext = Mysql2psql::Config.template(options)
64
+ configfile=get_temp_file('mysql2psql_tmp_config')
65
+ File.open(configfile, 'w') {|f| f.write(configtext) }
66
+ Mysql2psql::ConfigBase.new( configfile )
67
+ rescue
68
+ raise StandardError.new("Failed to initialize options from #{configfile}. See README for setup requirements.")
69
+ end
70
+
71
+ def get_test_config_by_label(name)
72
+ options = case name
73
+ when :localmysql_to_file_convert_nothing
74
+ {
75
+ :to_file => true,
76
+ :include_tables => ['unobtainium'],
77
+ :exclude_tables => ['kryptonite'],
78
+ :suppress_data => true,
79
+ :suppress_ddl => true,
80
+ :supress_sequence_update => false,
81
+ :suppress_indexes => false,
82
+ :force_truncate => false,
83
+ :use_timezones => false
84
+ }
85
+ when :localmysql_to_file_convert_all
86
+ {
87
+ :to_file => true,
88
+ :include_tables => [],
89
+ :exclude_tables => [],
90
+ :suppress_data => false,
91
+ :suppress_ddl => false,
92
+ :supress_sequence_update => false,
93
+ :suppress_indexes => false,
94
+ :force_truncate => true,
95
+ :use_timezones => false
96
+ }
97
+ when :localmysql_to_db_convert_all
98
+ {
99
+ :to_file => false,
100
+ :include_tables => [],
101
+ :exclude_tables => [],
102
+ :suppress_data => false,
103
+ :suppress_ddl => false,
104
+ :supress_sequence_update => false,
105
+ :suppress_indexes => false,
106
+ :force_truncate => false,
107
+ :use_timezones => false
108
+ }
109
+ when :localmysql_to_db_convert_nothing
110
+ {
111
+ :to_file => false,
112
+ :include_tables => ['unobtainium'],
113
+ :exclude_tables => ['kryptonite'],
114
+ :suppress_data => true,
115
+ :suppress_ddl => true,
116
+ :supress_sequence_update => false,
117
+ :suppress_indexes => false,
118
+ :force_truncate => false,
119
+ :use_timezones => false
120
+ }
121
+ else
122
+ raise StandardError.new("Invalid label: #{name}")
123
+ end
124
+ get_new_test_config(options)
125
+ end
126
+
127
+ def delete_files_for_test_config(config)
128
+ File.delete(config.destfile) if File.exists?(config.destfile)
129
+ File.delete(config.filepath) if File.exists?(config.filepath)
130
+ rescue
131
+ end
@@ -0,0 +1,49 @@
1
+ require 'test_helper'
2
+ require 'mysql2psql/config_base'
3
+
4
+ #
5
+ #
6
+ class ConfigBaseTest < Test::Unit::TestCase
7
+ attr_reader :config, :configfilepath
8
+ def setup
9
+ @configfilepath="#{File.dirname(__FILE__)}/../fixtures/config_all_options.yml"
10
+ @config = Mysql2psql::ConfigBase.new( configfilepath )
11
+ end
12
+
13
+ def teardown
14
+ @config = nil
15
+ end
16
+
17
+ def test_config_loaded
18
+ assert_not_nil config.config
19
+ assert_equal configfilepath,config.filepath
20
+ end
21
+
22
+ def test_uninitialized_error_when_not_found_and_no_default
23
+ assert_raises(Mysql2psql::UninitializedValueError) do
24
+ value = @config.not_found(:none)
25
+ end
26
+ end
27
+
28
+ def test_default_when_not_found
29
+ expected = 'defaultvalue'
30
+ value = @config.not_found(expected)
31
+ assert_equal expected,value
32
+ end
33
+
34
+ def test_mysql_hostname
35
+ value = @config.mysqlhostname
36
+ assert_equal 'localhost',value
37
+ end
38
+
39
+ def test_mysql_hostname_array_access
40
+ value = @config[:mysqlhostname]
41
+ assert_equal 'localhost',value
42
+ end
43
+
44
+ def test_dest_file
45
+ value = @config.destfile
46
+ assert_equal 'somefile',value
47
+ end
48
+
49
+ end
@@ -0,0 +1,73 @@
1
+ require 'test_helper'
2
+
3
+ require 'mysql2psql/config'
4
+
5
+ class ConfigTest < Test::Unit::TestCase
6
+ attr_reader :configfile_new, :configfile_all_opts, :configfile_not_found
7
+ def setup
8
+ @configfile_all_opts = "#{File.dirname(__FILE__)}/../fixtures/config_all_options.yml"
9
+ @configfile_not_found = "#{File.dirname(__FILE__)}/../fixtures/config_not_found.yml.do_not_create_this_file"
10
+ @configfile_new = get_temp_file('mysql2psql_test_config')
11
+ end
12
+ def teardown
13
+ File.delete(configfile_new) if File.exists?(configfile_new)
14
+ end
15
+
16
+ def test_config_loaded
17
+ value = Mysql2psql::Config.new(configfile_all_opts, false)
18
+ assert_not_nil value
19
+ end
20
+
21
+ def test_config_file_not_found
22
+ assert_raise(Mysql2psql::ConfigurationFileNotFound) do
23
+ value = Mysql2psql::Config.new(configfile_not_found, false)
24
+ end
25
+ end
26
+
27
+ def test_initialize_new_config_file
28
+ assert_raise(Mysql2psql::ConfigurationFileInitialized) do
29
+ value = Mysql2psql::Config.new(configfile_new, true)
30
+ end
31
+ end
32
+
33
+ def test_config_option_pgdatabase_as_array_index
34
+ expected = 'somename'
35
+ config = Mysql2psql::Config.new(configfile_all_opts, false)
36
+ assert_equal expected,config[:pgdatabase]
37
+ end
38
+ def test_template_option_to_filename
39
+ expected = 'test_filename'
40
+ value = Mysql2psql::Config.template({ :to_filename => expected })
41
+ assert_match /file: #{expected}/,value
42
+ end
43
+ def test_template_option_suppress_data
44
+ expected = true
45
+ value = Mysql2psql::Config.template({ :suppress_data => expected })
46
+ assert_match /suppress_data: #{expected}/,value #NB: option spelling needs fixing
47
+ end
48
+ def test_template_option_suppress_ddl
49
+ expected = true
50
+ value = Mysql2psql::Config.template({ :suppress_ddl => expected })
51
+ assert_match /suppress_ddl: #{expected}/,value #NB: option spelling needs fixing
52
+ end
53
+ def test_template_option_suppress_sequence_update
54
+ expected = true
55
+ value = Mysql2psql::Config.template({ :suppress_sequence_update => expected })
56
+ assert_match /suppress_sequence_update: #{expected}/,value #NB: option spelling needs fixing
57
+ end
58
+ def test_template_option_suppress_indexes
59
+ expected = true
60
+ value = Mysql2psql::Config.template({ :suppress_indexes => expected })
61
+ assert_match /suppress_indexes: #{expected}/,value #NB: option spelling needs fixing
62
+ end
63
+ def test_template_option_force_truncate
64
+ expected = true
65
+ value = Mysql2psql::Config.template({ :force_truncate => expected })
66
+ assert_match /force_truncate: #{expected}/,value
67
+ end
68
+ def test_template_option_use_timezones
69
+ expected = true
70
+ value = Mysql2psql::Config.template({ :use_timezones => expected })
71
+ assert_match /use_timezones: #{expected}/,value
72
+ end
73
+ end
@@ -0,0 +1,29 @@
1
+ require 'test_helper'
2
+
3
+ require 'mysql2psql'
4
+
5
+ class PostgresFileWriterTest < Test::Unit::TestCase
6
+ attr_accessor :destfile
7
+ def setup
8
+ begin
9
+ f = Tempfile.new('mysql2psql_test_destfile')
10
+ @destfile = f.path
11
+ f.close!()
12
+ rescue => e
13
+ raise StandardError.new("Failed to initialize integration test db. See README for setup requirements.")
14
+ end
15
+ end
16
+ def teardown
17
+ File.delete(destfile) if File.exists?(destfile)
18
+ end
19
+
20
+ def test_basic_write
21
+ writer = Mysql2psql::PostgresFileWriter.new(destfile)
22
+ writer.close
23
+ content = IO.read(destfile)
24
+ assert_not_nil content.match("SET client_encoding = 'UTF8'")
25
+ assert_nil content.match("unobtanium")
26
+ end
27
+
28
+
29
+ end