Skip to content

Commit

Permalink
feature: add tests for Baserow API integration with VCR
Browse files Browse the repository at this point in the history
  • Loading branch information
yhru committed Oct 15, 2024
1 parent 6bfc634 commit 9263d18
Show file tree
Hide file tree
Showing 9 changed files with 276 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,6 @@ public/vite-test

app/graphql/schema.json
public/graphql/schema

# ASDF
.tool-versions
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ group :test do
gem 'simplecov-cobertura', require: false
gem 'timecop'
gem 'vcr'
gem 'pry'
gem 'webmock'
end

Expand Down
12 changes: 9 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ GEM
chunky_png (1.4.0)
clamav-client (3.2.0)
cmdparse (3.0.7)
coderay (1.1.3)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
concurrent-ruby (1.2.3)
Expand Down Expand Up @@ -461,7 +462,6 @@ GEM
rake
mini_magick (4.12.0)
mini_mime (1.1.5)
mini_portile2 (2.8.6)
minitest (5.23.0)
msgpack (1.7.2)
multi_json (1.15.0)
Expand All @@ -479,8 +479,9 @@ GEM
net-smtp (0.5.0)
net-protocol
nio4r (2.7.3)
nokogiri (1.16.5)
mini_portile2 (~> 2.8.2)
nokogiri (1.16.5-x86_64-darwin)
racc (~> 1.4)
nokogiri (1.16.5-x86_64-linux)
racc (~> 1.4)
openid_connect (2.3.0)
activemodel
Expand Down Expand Up @@ -528,6 +529,9 @@ GEM
premailer (~> 1.7, >= 1.7.9)
prometheus-client (4.2.2)
promise.rb (0.7.4)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
psych (5.1.2)
stringio
public_suffix (5.0.5)
Expand Down Expand Up @@ -910,6 +914,7 @@ GEM
zxcvbn-ruby (1.2.0)

PLATFORMS
x86_64-darwin-22
x86_64-linux

DEPENDENCIES
Expand Down Expand Up @@ -1006,6 +1011,7 @@ DEPENDENCIES
prawn-rails
prawn-table
premailer-rails
pry
puma
pundit
rack-attack
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"debounce": "^1.2.1",
"dom4": "^2.1.6",
"email-butler": "^1.0.13",
"esbuild": "0.19.9",
"geojson": "^0.5.0",
"graphiql": "^3.1.1",
"graphql": "^16.8.1",
Expand Down
59 changes: 59 additions & 0 deletions spec/models/table_row_selector/baserow_api_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
require 'rails_helper'
require 'webmock/rspec'
require 'vcr'

RSpec.describe TableRowSelector::BaserowAPI, type: :model do
describe '#search' do
it 'search in a Baserow table and return the result', :vcr do
allow(TableRowSelector::BaserowAPI).to receive(:config).and_return({
'Table' => '202600',
'Champ de recherche' => 'search_field',
'Token' => ENV['API_BASEROW_TOKEN'].to_s
})

VCR.use_cassette('baserow_search') do
results = TableRowSelector::BaserowAPI.search(123, "term")

expect(results).to be_an(Array)
expect(results.length).to be > 0
expect(results.first).to have_key(:name)
expect(results.first).to have_key(:id)
end
end
end

describe '#fetch_row' do
it 'fetch a specific line in a Baserow table', :vcr do
allow(TableRowSelector::BaserowAPI).to receive(:config).and_return({
'Table' => '202600',
'Token' => ENV['API_BASEROW_TOKEN'].to_s
})

VCR.use_cassette('baserow_fetch_row') do
result = TableRowSelector::BaserowAPI.fetch_row(123, 1)

expect(result[:row]["Nom"]).to eq("Communes de polynésie")
expect(result[:row]["Notes"]).to eq("")
expect(result[:row]["Actif"]).to eq(true)
expect(result[:row]["Table"]).to eq("202578")
expect(result[:row]["Champs usager"]).to eq("1391747,1391750,1391755,1391756")
expect(result[:row]["Champ de recherche"]).to eq("1391747")
expect(result[:row]["Champs instructeur"]).to eq("1391747,1391750,1391755,1391756")
end
end
end

describe '#available_tables' do
it 'fetch the available tables', :vcr do
allow(TableRowSelector::BaserowAPI).to receive(:config)

VCR.use_cassette('baserow_available_tables') do
results = TableRowSelector::BaserowAPI.available_tables

expect(results).to be_an(Array)
expect(results.first[:name]).to eq("Communes de polynésie")
expect(results.first[:id]).to eq(1)
end
end
end
end
8 changes: 8 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
require 'shoulda-matchers'
require 'view_component/test_helpers'
require "rack_session_access/capybara"
require 'vcr'

# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
Expand Down Expand Up @@ -121,6 +122,13 @@
end
end

VCR.configure do |config|
config.cassette_library_dir = 'spec/vcr_cassettes'
config.hook_into :webmock
config.configure_rspec_metadata!
config.allow_http_connections_when_no_cassette = false
end

config.include ActiveSupport::Testing::TimeHelpers
config.include Shoulda::Matchers::ActiveRecord, type: :model
config.include Shoulda::Matchers::ActiveModel, type: :model
Expand Down
49 changes: 49 additions & 0 deletions spec/vcr_cassettes/baserow_available_tables.yml

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

97 changes: 97 additions & 0 deletions spec/vcr_cassettes/baserow_fetch_row.yml

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

49 changes: 49 additions & 0 deletions spec/vcr_cassettes/baserow_search.yml

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

0 comments on commit 9263d18

Please sign in to comment.