|
| 1 | +# spec/lib/gooddata/cloud_resources/postgresql/postgresql_client_spec.rb |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | +require 'gooddata/cloud_resources/postgresql/postgresql_client' |
| 5 | +require 'rspec' |
| 6 | +require 'csv' |
| 7 | +require 'benchmark' |
| 8 | + |
| 9 | +describe GoodData::CloudResources::PostgresClient do |
| 10 | + before do |
| 11 | + @postresql_host = ENV['POSTGRES_HOST'] || 'localhost' |
| 12 | + @postresql_port = ENV['POSTGRES_PORT'] || 5432 |
| 13 | + @postresql_database = ENV['POSTGRES_DB'] || 'postgres' |
| 14 | + @postgres_schema = ENV['POSTGRES_SCHEMA'] || 'public' |
| 15 | + @postresql_user = ENV['POSTGRES_USER'] || 'postgres' |
| 16 | + @postresql_password = ENV['POSTGRES_SECRET'] || 'postgres' |
| 17 | + end |
| 18 | + |
| 19 | + let(:valid_options) do |
| 20 | + { |
| 21 | + 'postgresql_client' => { |
| 22 | + 'connection' => { |
| 23 | + 'database' => @postresql_database, |
| 24 | + 'schema' => @postgres_schema, |
| 25 | + 'authentication' => { |
| 26 | + 'basic' => { |
| 27 | + 'userName' => @postresql_user, |
| 28 | + 'password' => @postresql_password |
| 29 | + } |
| 30 | + }, |
| 31 | + 'sslMode' => 'prefer', |
| 32 | + 'url' => "jdbc:postgresql://#{@postresql_host}:#{@postresql_port}/#{@postresql_database}" |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + end |
| 37 | + |
| 38 | + describe '.accept?' do |
| 39 | + it 'returns true for postgresql type' do |
| 40 | + expect(GoodData::CloudResources::PostgresClient.accept?('postgresql')).to be true |
| 41 | + end |
| 42 | + |
| 43 | + it 'returns false for other types' do |
| 44 | + expect(GoodData::CloudResources::PostgresClient.accept?('mysql')).to be false |
| 45 | + end |
| 46 | + end |
| 47 | + |
| 48 | + describe '#initialize' do |
| 49 | + it 'raises an error if postgresql_client is missing' do |
| 50 | + options = {} |
| 51 | + expect { GoodData::CloudResources::PostgresClient.new(options) }.to raise_error(RuntimeError, "Data Source needs a client to Postgres to be able to query the storage but 'postgresql_client' is empty.") |
| 52 | + end |
| 53 | + |
| 54 | + it 'raises an error if connection info is missing' do |
| 55 | + options = { 'postgresql_client' => {} } |
| 56 | + expect { GoodData::CloudResources::PostgresClient.new(options) }.to raise_error(RuntimeError, 'Missing connection info for Postgres client') |
| 57 | + end |
| 58 | + |
| 59 | + it 'raises an error if sslMode is invalid' do |
| 60 | + invalid_options = valid_options.dup |
| 61 | + invalid_options['postgresql_client']['connection']['sslMode'] = 'invalid' |
| 62 | + expect { GoodData::CloudResources::PostgresClient.new(invalid_options) }.to raise_error(RuntimeError, 'SSL Mode should be prefer, require and verify-full') |
| 63 | + end |
| 64 | + |
| 65 | + it 'initializes with valid options' do |
| 66 | + client = GoodData::CloudResources::PostgresClient.new(valid_options) |
| 67 | + expect(client).not_to be_nil |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + describe '#build_url' do |
| 72 | + it 'builds the correct URL' do |
| 73 | + client = GoodData::CloudResources::PostgresClient.new(valid_options) |
| 74 | + expected_url = "jdbc:postgresql://#{@postresql_host}:#{@postresql_port}/#{@postresql_database}?sslmode=prefer" |
| 75 | + expect(client.send(:build_url, "jdbc:postgresql://#{@postresql_host}:#{@postresql_port}")).to eq(expected_url) |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + describe '#connect' do |
| 80 | + it 'sets up the connection' do |
| 81 | + client = GoodData::CloudResources::PostgresClient.new(valid_options) |
| 82 | + client.connect |
| 83 | + expect(client.instance_variable_get(:@connection)).not_to be_nil |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + describe '#realize_query_version' do |
| 88 | + it 'executes the query with postgres version and writes results to a CSV file' do |
| 89 | + client = GoodData::CloudResources::PostgresClient.new(valid_options) |
| 90 | + client.connect |
| 91 | + output_file = client.realize_query('SELECT version();', {}) |
| 92 | + expect(File).to exist(output_file) |
| 93 | + expect(File.read(output_file)).to include('PostgreSQL') |
| 94 | + File.delete(output_file) |
| 95 | + end |
| 96 | + end |
| 97 | +end |
0 commit comments