Skip to content

Commit

Permalink
build: run samples during tests
Browse files Browse the repository at this point in the history
Automatically run all samples as part of the tests.
  • Loading branch information
olavloite committed Dec 20, 2024
1 parent a49ecf9 commit 1223669
Show file tree
Hide file tree
Showing 69 changed files with 103 additions and 769 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/samples.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
on:
pull_request:
name: samples
jobs:
test:
runs-on: ubuntu-latest

strategy:
max-parallel: 4
matrix:
ruby: ["3.1", "3.2", "3.3"]
ar: ["~> 6.1.0", "~> 7.0.0", "~> 7.1.0", "~> 7.2.0", "~> 8.0.0"]
# Exclude combinations that are not supported.
exclude:
- ruby: "3.1"
ar: "~> 8.0.0"
env:
AR_VERSION: ${{ matrix.ar }}
steps:
- uses: actions/checkout@v4
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
bundler-cache: false
ruby-version: ${{ matrix.ruby }}
- name: Install dependencies
run: bundle install
- name: Run samples
run: bundle exec rake all
working-directory: examples/snippets
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ gem "activerecord", ar_version
gem "ostruct"
gem "minitest", "~> 5.25.0"
gem "minitest-rg", "~> 5.3.0"
gem "pry", "~> 0.13.0"
gem "pry-byebug", "~> 3.9.0"
gem "pry", "~> 0.14.2"
gem "pry-byebug", "~> 3.10.1"
# Add sqlite3 for testing for compatibility with other adapters.
gem 'sqlite3'

Expand Down
32 changes: 27 additions & 5 deletions examples/snippets/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,37 @@
require_relative "config/environment"
require "docker"

desc "Lists all available samples."
task :list do
samples = Dir.entries(".").select do |entry|
def fetch_samples
Dir.entries(".").select do |entry|
File.directory?(File.join(".", entry)) \
&& !%w[. ..].include?(entry) \
&& File.exist?(File.join(".", entry, "application.rb"))
&& !%w[. ..].include?(entry) \
&& File.exist?(File.join(".", entry, "application.rb"))
end
end

desc "Lists all available samples."
task :list do
samples = fetch_samples
puts "Available samples: "
samples.sort.each { |dir| puts " #{dir}" }
puts ""
puts "Run a sample with the command `bundle exec rake run\\[<sample-name>\\]`"
end

desc "Runs all samples."
task :all do
samples = fetch_samples
ar_version = ENV.fetch "AR_VERSION", "~> 7.1.0"
less_than_7_1 = ar_version.dup.to_s.sub("~>", "").strip < "7.1.0"
samples.delete "interleaved-tables" if less_than_7_1
samples.delete "interleaved-tables-before-7.1" unless less_than_7_1
samples.delete "bit-reversed-sequence" if less_than_7_1
samples.delete "query-logs" if less_than_7_1
samples.each do |sample|
run_sample sample
end
end

desc "Runs a simple ActiveRecord tutorial on a Spanner emulator."
task :run, [:sample] do |_t, args|
sample = args[:sample]
Expand All @@ -28,7 +46,11 @@ task :run, [:sample] do |_t, args|
puts ""
sample = "quickstart"
end
run_sample sample
end

def run_sample sample
puts "Running #{sample}"
puts "Downloading Spanner emulator image..."
Docker::Image.create "fromImage" => "gcr.io/cloud-spanner-emulator/emulator:latest"
puts "Creating Spanner emulator container..."
Expand Down
6 changes: 1 addition & 5 deletions examples/snippets/array-data-type/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
require_relative "models/entity_with_array_types"

class Application
def self.run # rubocop:disable Metrics/AbcSize
def self.run
# Create a record with all array types.
record = EntityWithArrayTypes.create \
col_array_string: ["value1", "value2", "value3"],
Expand All @@ -35,10 +35,6 @@ def self.run # rubocop:disable Metrics/AbcSize
puts "Bytes array: #{record.col_array_bytes.map(&:read)}"
puts "Date array: #{record.col_array_date}"
puts "Timestamp array: #{record.col_array_timestamp}"

puts ""
puts "Press any key to end the application"
$stdin.getch
end
end

Expand Down
1 change: 1 addition & 0 deletions examples/snippets/array-data-type/config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ development:
database: testdb
pool: 5
timeout: 5000
schema_dump: false
31 changes: 0 additions & 31 deletions examples/snippets/array-data-type/db/schema.rb

This file was deleted.

4 changes: 0 additions & 4 deletions examples/snippets/bit-reversed-sequence/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ def self.run

# List all singers, albums and tracks.
list_singers_albums

puts ""
puts "Press any key to end the application"
$stdin.getch
end

def self.find_album singerid, albumid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ development:
database: testdb
pool: 5
timeout: 5000
schema_dump: false
31 changes: 0 additions & 31 deletions examples/snippets/bit-reversed-sequence/db/schema.rb

This file was deleted.

4 changes: 0 additions & 4 deletions examples/snippets/bulk-insert/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ def self.run # rubocop:disable Metrics/AbcSize
puts " with album #{a.title} with id #{a.id}"
end
end

puts ""
puts "Press any key to end the application"
$stdin.getch
end
end

Expand Down
1 change: 1 addition & 0 deletions examples/snippets/bulk-insert/config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ development:
database: testdb
pool: 5
timeout: 5000
schema_dump: false
31 changes: 0 additions & 31 deletions examples/snippets/bulk-insert/db/schema.rb

This file was deleted.

4 changes: 0 additions & 4 deletions examples/snippets/commit-timestamp/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ def self.run # rubocop:disable Metrics/AbcSize
puts "Singer and album updated:"
puts "#{singer.first_name} #{singer.last_name} (Last updated: #{singer.last_updated.strftime format})"
puts " #{album.title} (Last updated: #{album.last_updated.strftime format})"

puts ""
puts "Press any key to end the application"
$stdin.getch
end
end

Expand Down
1 change: 1 addition & 0 deletions examples/snippets/commit-timestamp/config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ development:
database: testdb
pool: 5
timeout: 5000
schema_dump: false
34 changes: 0 additions & 34 deletions examples/snippets/commit-timestamp/db/schema.rb

This file was deleted.

4 changes: 4 additions & 0 deletions examples/snippets/config/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

Dir["../../lib/*.rb"].each { |file| require file }

if ActiveRecord.version >= Gem::Version.create("7.2.0")
ActiveRecord::ConnectionAdapters.register "spanner", "ActiveRecord::ConnectionAdapters::SpannerAdapter"
end

Bundler.require

ActiveRecord::Base.establish_connection(
Expand Down
6 changes: 1 addition & 5 deletions examples/snippets/create-records/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class Application
def self.run
# Creating a single record without an explicit transaction will automatically save it to the database.
# It is not recommended to call Entity.create repeatedly to insert multiple records, as each call will
# use a separate Spanner transaction. Instead multiple records should be created by passing an array of
# use a separate Spanner transaction. Instead, multiple records should be created by passing an array of
# entities to the Entity.create method.
singer = Singer.create first_name: "Dave", last_name: "Allison"
puts ""
Expand All @@ -32,10 +32,6 @@ def self.run
singers.each do |s|
puts " Created singer #{s.first_name} #{s.last_name} with id #{s.id}"
end

puts ""
puts "Press any key to end the application"
$stdin.getch
end
end

Expand Down
1 change: 1 addition & 0 deletions examples/snippets/create-records/config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ development:
database: testdb
pool: 5
timeout: 5000
schema_dump: false
31 changes: 0 additions & 31 deletions examples/snippets/create-records/db/schema.rb

This file was deleted.

6 changes: 1 addition & 5 deletions examples/snippets/date-data-type/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,14 @@ def self.run
puts "#{"#{singer.first_name} #{singer.last_name}".ljust 30}#{singer.birth_date}"
end

# Update the birth date of a random singer using the current system time. Any time and timezone information will be
# Update the birthdate of a random singer using the current system time. Any time and timezone information will be
# lost after saving the record as a DATE only contains the year, month and day-of-month information.
singer = Singer.all.sample
singer.update birth_date: Time.now
singer.reload
puts ""
puts "Updated birth date to current system time:"
puts "#{"#{singer.first_name} #{singer.last_name}".ljust 30}#{singer.birth_date}"

puts ""
puts "Press any key to end the application"
$stdin.getch
end
end

Expand Down
1 change: 1 addition & 0 deletions examples/snippets/date-data-type/config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ development:
database: testdb
pool: 5
timeout: 5000
schema_dump: false
Loading

0 comments on commit 1223669

Please sign in to comment.