|
| 1 | +# /Users/milandufek/dev/gooddata/gooddata-ruby/spec/integration/drivers/mysql_client_test.rb |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | +require 'gooddata/cloud_resources/mysql/mysql_client' |
| 5 | +require 'rspec' |
| 6 | +require 'csv' |
| 7 | +require 'benchmark' |
| 8 | + |
| 9 | +describe GoodData::CloudResources::MysqlClient do |
| 10 | + before do |
| 11 | + @mysql_host = ENV['MYSQL_HOST'] || 'localhost' |
| 12 | + @mysql_port = ENV['MYSQL_PORT'] || 3306 |
| 13 | + @mysql_database = ENV['MYSQL_DB'] || 'mysql' |
| 14 | + @mysql_user = ENV['MYSQL_USER'] || 'root' |
| 15 | + @mysql_password = ENV['MYSQL_SECRET'] || 'root' |
| 16 | + end |
| 17 | + |
| 18 | + let(:valid_options) do |
| 19 | + { |
| 20 | + 'mysql_client' => { |
| 21 | + 'connection' => { |
| 22 | + 'database' => @mysql_database, |
| 23 | + 'schema' => 'public', |
| 24 | + 'authentication' => { |
| 25 | + 'basic' => { |
| 26 | + 'userName' => @mysql_user, |
| 27 | + 'password' => @mysql_password |
| 28 | + } |
| 29 | + }, |
| 30 | + 'sslMode' => 'prefer', |
| 31 | + 'url' => "jdbc:mysql://#{@mysql_host}:#{@mysql_port}/#{@mysql_database}" |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + end |
| 36 | + |
| 37 | + describe '.accept?' do |
| 38 | + it 'returns true for mysql type' do |
| 39 | + expect(GoodData::CloudResources::MysqlClient.accept?('mysql')).to be true |
| 40 | + end |
| 41 | + |
| 42 | + it 'returns false for other types' do |
| 43 | + expect(GoodData::CloudResources::MysqlClient.accept?('postgresql')).to be false |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + describe '#initialize' do |
| 48 | + it 'raises an error if mysql_client is missing' do |
| 49 | + options = {} |
| 50 | + expect { GoodData::CloudResources::MysqlClient.new(options) }.to raise_error(RuntimeError, "Data Source needs a client to Mysql to be able to query the storage but 'mysql_client' is empty.") |
| 51 | + end |
| 52 | + |
| 53 | + it 'raises an error if connection info is missing' do |
| 54 | + options = { 'mysql_client' => {} } |
| 55 | + expect { GoodData::CloudResources::MysqlClient.new(options) }.to raise_error(RuntimeError, 'Missing connection info for Mysql client') |
| 56 | + end |
| 57 | + |
| 58 | + it 'raises an error if sslMode is invalid' do |
| 59 | + invalid_options = valid_options.dup |
| 60 | + invalid_options['mysql_client']['connection']['sslMode'] = 'invalid' |
| 61 | + expect { GoodData::CloudResources::MysqlClient.new(invalid_options) }.to raise_error(RuntimeError, 'SSL Mode should be prefer, require and verify-full') |
| 62 | + end |
| 63 | + |
| 64 | + it 'initializes with valid options' do |
| 65 | + client = GoodData::CloudResources::MysqlClient.new(valid_options) |
| 66 | + expect(client).not_to be_nil |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + describe '#build_url' do |
| 71 | + it 'builds the correct URL' do |
| 72 | + client = GoodData::CloudResources::MysqlClient.new(valid_options) |
| 73 | + expected_url = "jdbc:mysql://#{@mysql_host}:#{@mysql_port}/#{@mysql_database}?&useSSL=true&requireSSL=false&verifyServerCertificate=false&useCursorFetch=true&enabledTLSProtocols=TLSv1.2" |
| 74 | + expect(client.send(:build_url, "jdbc:mysql://#{@mysql_host}:#{@mysql_port}")).to eq(expected_url) |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + describe '#connect' do |
| 79 | + it 'sets up the connection' do |
| 80 | + client = GoodData::CloudResources::MysqlClient.new(valid_options) |
| 81 | + client.connect |
| 82 | + expect(client.instance_variable_get(:@connection)).not_to be_nil |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + describe '#realize_query' do |
| 87 | + it 'executes the query and writes results to a CSV file' do |
| 88 | + client = GoodData::CloudResources::MysqlClient.new(valid_options) |
| 89 | + client.connect |
| 90 | + output_file = client.realize_query('SELECT 123;', {}) |
| 91 | + expect(File).to exist(output_file) |
| 92 | + expect(File.read(output_file)).to include('123') |
| 93 | + File.delete(output_file) |
| 94 | + end |
| 95 | + end |
| 96 | + |
| 97 | + describe '#fetch_size' do |
| 98 | + it 'returns the correct fetch size for MySQL' do |
| 99 | + client = GoodData::CloudResources::MysqlClient.new(valid_options) |
| 100 | + expect(client.fetch_size).to eq(GoodData::CloudResources::MysqlClient::MYSQL_FETCH_SIZE) |
| 101 | + end |
| 102 | + |
| 103 | + it 'returns the correct fetch size for MongoDB BI Connector' do |
| 104 | + mongo_options = valid_options.dup |
| 105 | + mongo_options['mysql_client']['connection']['databaseType'] = GoodData::CloudResources::MysqlClient::MONGO_BI_TYPE |
| 106 | + client = GoodData::CloudResources::MysqlClient.new(mongo_options) |
| 107 | + expect(client.fetch_size).to eq(GoodData::CloudResources::MysqlClient::MONGO_BI_FETCH_SIZE) |
| 108 | + end |
| 109 | + end |
| 110 | + |
| 111 | + describe '#get_ssl_mode' do |
| 112 | + it 'returns the correct SSL mode for prefer' do |
| 113 | + client = GoodData::CloudResources::MysqlClient.new(valid_options) |
| 114 | + expect(client.send(:get_ssl_mode, 'prefer')).to eq(GoodData::CloudResources::MysqlClient::PREFER) |
| 115 | + end |
| 116 | + |
| 117 | + it 'returns the correct SSL mode for require' do |
| 118 | + client = GoodData::CloudResources::MysqlClient.new(valid_options) |
| 119 | + expect(client.send(:get_ssl_mode, 'require')).to eq(GoodData::CloudResources::MysqlClient::REQUIRE) |
| 120 | + end |
| 121 | + |
| 122 | + it 'returns the correct SSL mode for verify-full' do |
| 123 | + client = GoodData::CloudResources::MysqlClient.new(valid_options) |
| 124 | + expect(client.send(:get_ssl_mode, 'verify-full')).to eq(GoodData::CloudResources::MysqlClient::VERIFY_FULL) |
| 125 | + end |
| 126 | + end |
| 127 | + |
| 128 | + describe '#add_extended' do |
| 129 | + it 'returns the correct extended parameters for MongoDB BI Connector' do |
| 130 | + mongo_options = valid_options.dup |
| 131 | + mongo_options['mysql_client']['connection']['databaseType'] = GoodData::CloudResources::MysqlClient::MONGO_BI_TYPE |
| 132 | + client = GoodData::CloudResources::MysqlClient.new(mongo_options) |
| 133 | + expect(client.send(:add_extended)).to eq('&authenticationPlugins=org.mongodb.mongosql.auth.plugin.MongoSqlAuthenticationPlugin&useLocalTransactionState=true') |
| 134 | + end |
| 135 | + |
| 136 | + it 'returns an empty string for MySQL' do |
| 137 | + client = GoodData::CloudResources::MysqlClient.new(valid_options) |
| 138 | + expect(client.send(:add_extended)).to eq('') |
| 139 | + end |
| 140 | + end |
| 141 | +end |
0 commit comments