Skip to content

Commit

Permalink
VCR refactor (#22)
Browse files Browse the repository at this point in the history
* refactoring tests to eliminate need for real environment variables during testing

* defaulting required environment variables in test
  • Loading branch information
eebbesen authored Oct 14, 2024
1 parent 913b678 commit 18cfd1b
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 24 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ GitHub Action runs

### testing

RSpec tests require the environment variables checked in [spec_helper.rb](spec/spec_helper.rb). Out of the box your tests that call Google (mocked with VCR) will fail because you do (should) not have access to my test account credentials.
$ rake spec

## Release
Build
Expand Down
7 changes: 6 additions & 1 deletion lib/sheet_zoukas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,16 @@ class Error < StandardError; end

class << self
def retrieve_sheet_json(sheet_id, tab_name, range = nil)
exit 1 unless SheetZoukas::Utils.vars_present?(REQUIRED_VARS, 'required for Google Sheets API calls')
SheetZoukas.exit_program unless SheetZoukas::Utils.vars_present?(REQUIRED_VARS,
'required for Google Sheets API calls')

sheet = GoogleSheets.new
data = sheet.retrieve_sheet(sheet_id, tab_name, range)
DataConverter.new(data.values).convert
end

def exit_program
raise StandardError, 'Error encountered'
end
end
end
9 changes: 8 additions & 1 deletion lib/sheet_zoukas/google_sheets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class GoogleSheets
DEFAULT_SCOPE = 'https://www.googleapis.com/auth/spreadsheets.readonly'

def initialize(scope = DEFAULT_SCOPE)
@authorizer = Google::Auth::ServiceAccountCredentials.from_env(scope: scope)
init_authorizer(scope)
end

def retrieve_sheet(sheet_id, tab_name, range = nil)
Expand All @@ -25,5 +25,12 @@ def retrieve_sheet(sheet_id, tab_name, range = nil)

"#{tab_name}!#{range}"
end

private

# overriden in spec_helper so we don't try to authenticate with Google for tests
def init_authorizer(scope)
@authorizer = Google::Auth::ServiceAccountCredentials.from_env(scope: scope)
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions spec/fixtures/vcr_cassettes/get_spreadsheet_values_range.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion spec/fixtures/vcr_cassettes/retrieve_sheet_json.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions spec/sheet_zoukas/google_sheets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
require 'sheet_zoukas/google_sheets'

RSpec.describe SheetZoukas::GoogleSheets do
before { ENV.store('GOOGLE_API_SPREADSHEET_ID_TEST', 'test_id') }

describe '#initialize' do
it 'uses default scope' do
ret = described_class.new.instance_variable_get(:@authorizer).scope
Expand Down Expand Up @@ -32,14 +34,14 @@
describe '#retrieve_sheet' do
it 'retrieves sheet with range' do
VCR.use_cassette('get_spreadsheet_values_range') do
sheet = described_class.new.retrieve_sheet(ENV.fetch('GOOGLE_API_SPREADSHEET_ID', nil), 'Log', 'A:Z')
sheet = described_class.new.retrieve_sheet(ENV.fetch('GOOGLE_API_SPREADSHEET_ID_TEST', nil), 'Log', 'A:Z')
expect(sheet.values.first).to include('Reward Type')
end
end

it 'retrieves sheet without range' do
VCR.use_cassette('get_spreadsheet_values_no_range') do
sheet = described_class.new.retrieve_sheet(ENV.fetch('GOOGLE_API_SPREADSHEET_ID', nil), 'Log')
sheet = described_class.new.retrieve_sheet(ENV.fetch('GOOGLE_API_SPREADSHEET_ID_TEST', nil), 'Log')
expect(sheet.values.first).to include('Reward Type')
end
end
Expand Down
10 changes: 6 additions & 4 deletions spec/sheet_zoukas_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
end

describe '.retrieve_sheet_json' do
before { ENV.store('GOOGLE_API_SPREADSHEET_ID_TEST', 'test_id') }

it 'retrieves sheet data' do # rubocop:disable RSpec/ExampleLength
VCR.use_cassette('retrieve_sheet_json') do
data = described_class.retrieve_sheet_json(ENV.fetch('GOOGLE_API_SPREADSHEET_ID', nil), 'Log')
data = described_class.retrieve_sheet_json(ENV.fetch('GOOGLE_API_SPREADSHEET_ID_TEST', nil), 'Log')
expect(data[0]).to eq('Place' => 'Slice Brothers',
'Deal' => '2 slices for $5.99',
'Deal Earned' => '',
Expand All @@ -26,9 +28,9 @@
ENV.delete(missing)
expect do
expect do
described_class.retrieve_sheet_json(ENV.fetch('GOOGLE_API_SPREADSHEET_ID', nil), 'Log')
end.to raise_error(SystemExit) do |error| # rubocop:disable Style/MultilineBlockChain
expect(error.status).to eq(1)
described_class.retrieve_sheet_json(ENV.fetch('GOOGLE_API_SPREADSHEET_ID_TEST', nil), 'Log')
end.to raise_error(StandardError) do |error| # rubocop:disable Style/MultilineBlockChain
expect(error.message).to eq('Error encountered')
end
end.to output("⛔️ #{missing} required for Google Sheets API calls.\n").to_stdout
end
Expand Down
27 changes: 21 additions & 6 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,26 @@
VCR.configure do |config|
config.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
config.hook_into :webmock
config.filter_sensitive_data('<GOOGLE_API_KEY>') { ENV.fetch('GOOGLE_API_KEY', nil) }
config.filter_sensitive_data('<SPREADSHEET_ID>') { ENV.fetch('GOOGLE_API_SPREADSHEET_ID', nil) }
end

# make sure all vars required for testing are present
require 'sheet_zoukas/utils'
REQUIRED_VARS_TEST = (SheetZoukas::REQUIRED_VARS + ['GOOGLE_API_SPREADSHEET_ID']).freeze
exit 1 unless SheetZoukas::Utils.vars_present?(REQUIRED_VARS_TEST, 'required for tests to run')
# initialize required environment variables
ENV.store('GOOGLE_ACCOUNT_TYPE', 'service_account')
ENV.store('GOOGLE_API_KEY', 'fake_google_api_key')
ENV.store('GOOGLE_CLIENT_EMAIL', '[email protected]')
ENV.store('GOOGLE_CLIENT_ID', 'fake_google_client_id')
ENV.store('GOOGLE_PRIVATE_KEY', "----BEGIN PRIVATE KEY-----\nfake_google_private_key==\n-----END PRIVATE KEY-----\n")

module SheetZoukas
# for testing don't try to authenticate with Google
# will need to remove when recording new VCR cassettes
class GoogleSheets
Authorizer = Struct.new(:scope)

private

def init_authorizer(scope)
scopes = scope.is_a?(Array) ? scope : [scope]
@authorizer = Authorizer.new(scopes)
end
end
end

0 comments on commit 18cfd1b

Please sign in to comment.