Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add dummy rails application #85

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ jobs:
BUNDLE_GEMFILE: gemfiles/${{ matrix.rails }}.gemfile
DATABASE_URL: ${{ matrix.adapter }}
WITH_ADVISORY_LOCK_PREFIX: ${{ github.run_id }}
run: bundle exec rake
run: bin/rails db:reset test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ test/version_tmp
tmp
*.iml
test/sqlite.db
.env
.env
/test/dummy/log/
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
source 'https://rubygems.org'

gemspec

gem 'railties'

gem 'pg'
gem 'sqlite3'
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ YARD::Rake::YardocTask.new do |t|
t.files = ['lib/**/*.rb', 'README.md']
end

APP_RAKEFILE = File.expand_path("test/dummy/Rakefile", __dir__)
load APP_RAKEFILE
require 'rake/testtask'

Rake::TestTask.new do |t|
Expand Down
13 changes: 13 additions & 0 deletions bin/rails
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#!/usr/bin/env ruby
# This command will automatically be run when you run "rails" with Rails gems
# installed from the root of your application.

ENGINE_ROOT = File.expand_path("..", __dir__)
APP_PATH = File.expand_path("../test/dummy/config/application", __dir__)
ENV["RAILS_ENV"] ||= "test"
# Set up gems listed in the Gemfile.
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__)
require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"])

require "rails/all"
require "rails/engine/commands"
3 changes: 3 additions & 0 deletions gemfiles/activerecord_6.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

source "https://rubygems.org"

gem "railties"
gem "pg"
gem "sqlite3"
gem "activerecord", "~> 6.1.0"

platforms :ruby do
Expand Down
3 changes: 3 additions & 0 deletions gemfiles/activerecord_7.0.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

source "https://rubygems.org"

gem "railties"
gem "pg"
gem "sqlite3"
gem "activerecord", "~> 7.0.0"

platforms :ruby do
Expand Down
3 changes: 3 additions & 0 deletions gemfiles/activerecord_7.1.gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

source "https://rubygems.org"

gem "railties"
gem "pg"
gem "sqlite3"
gem "activerecord", "~> 7.1.0"

platforms :ruby do
Expand Down
2 changes: 1 addition & 1 deletion lib/with_advisory_lock/database_adapter_support.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def postgresql?
end

def sqlite?
@sym_name == :sqlite3
[:sqlite3, :sqlite].include? @sym_name
end
end
end
5 changes: 5 additions & 0 deletions test/dummy/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

require_relative 'config/application'

Rails.application.load_tasks
5 changes: 5 additions & 0 deletions test/dummy/app/models/application_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
Empty file.
4 changes: 4 additions & 0 deletions test/dummy/app/models/label.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class Label < ApplicationRecord
end
8 changes: 8 additions & 0 deletions test/dummy/app/models/tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class Tag < ApplicationRecord
after_save do
TagAudit.create(tag_name: name)
Label.create(name: name)
end
end
4 changes: 4 additions & 0 deletions test/dummy/app/models/tag_audit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class TagAudit < ApplicationRecord
end
6 changes: 6 additions & 0 deletions test/dummy/bin/rails
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

APP_PATH = File.expand_path('../config/application', __dir__)
require_relative '../config/boot'
require 'rails/commands'
6 changes: 6 additions & 0 deletions test/dummy/bin/rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require_relative '../config/boot'
require 'rake'
Rake.application.run
35 changes: 35 additions & 0 deletions test/dummy/bin/setup
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'fileutils'

# path to your application root.
APP_ROOT = File.expand_path('..', __dir__)

def system!(*args)
system(*args, exception: true)
end

FileUtils.chdir APP_ROOT do
# This script is a way to set up or update your development environment automatically.
# This script is idempotent, so that you can run it at any time and get an expectable outcome.
# Add necessary setup steps to this file.

puts '== Installing dependencies =='
system! 'gem install bundler --conservative'
system('bundle check') || system!('bundle install')

# puts "\n== Copying sample files =="
# unless File.exist?("config/database.yml")
# FileUtils.cp "config/database.yml.sample", "config/database.yml"
# end

puts "\n== Preparing database =="
system! 'bin/rails db:prepare'

puts "\n== Removing old logs and tempfiles =="
system! 'bin/rails log:clear tmp:clear'

puts "\n== Restarting application server =="
system! 'bin/rails restart'
end
6 changes: 6 additions & 0 deletions test/dummy/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

require_relative 'config/environment'

run Rails.application
Rails.application.load_server
14 changes: 14 additions & 0 deletions test/dummy/config/application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

require_relative 'boot'

require 'rails'
require 'active_record/railtie'
Bundler.require(*Rails.groups)
require 'with_advisory_lock'

module Dummy
class Application < Rails::Application
config.load_defaults Rails::VERSION::STRING.to_f
end
end
7 changes: 7 additions & 0 deletions test/dummy/config/boot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../../Gemfile', __dir__)

require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
$LOAD_PATH.unshift File.expand_path('../../../lib', __dir__)
4 changes: 4 additions & 0 deletions test/dummy/config/database.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
test:
url: <%= ENV.fetch('DATABASE_URL', "sqlite3://#{Dir.tmpdir}/with_advisory_lock-#{Rails.gem_version}-#{RUBY_VERSION}.sqlite3") %>
properties:
allowPublicKeyRetrieval: true
7 changes: 7 additions & 0 deletions test/dummy/config/environment.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

# Load the Rails application.
require_relative 'application'

# Initialize the Rails application.
Rails.application.initialize!
44 changes: 44 additions & 0 deletions test/dummy/config/environments/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# frozen_string_literal: true

require 'active_support/core_ext/integer/time'

# The test environment is used exclusively to run your application's
# test suite. You never need to work with it otherwise. Remember that
# your test database is "scratch space" for the test suite and is wiped
# and recreated between test runs. Don't rely on the data there!

Rails.application.configure do
# Settings specified here will take precedence over those in config/application.rb.

# While tests run files are not watched, reloading is not necessary.
config.enable_reloading = false

# Eager loading loads your entire application. When running a single test locally,
# this is usually not necessary, and can slow down your test suite. However, it's
# recommended that you enable it in continuous integration systems to ensure eager
# loading is working properly before deploying your code.
config.eager_load = ENV['CI'].present?

# Configure public file server for tests with Cache-Control for performance.
config.public_file_server.enabled = true
config.public_file_server.headers = {
'Cache-Control' => "public, max-age=#{1.hour.to_i}"
}

# Show full error reports and disable caching.
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.cache_store = :null_store

# Render exception templates for rescuable exceptions and raise for other exceptions.
config.action_dispatch.show_exceptions = :rescuable

# Print deprecation notices to the stderr.
config.active_support.deprecation = :stderr

# Raise exceptions for disallowed deprecations.
config.active_support.disallowed_deprecation = :raise

# Tell Active Support which deprecation messages to disallow.
config.active_support.disallowed_deprecation_warnings = []
end
13 changes: 13 additions & 0 deletions test/dummy/db/migrate/0_initial_schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

class InitialSchema < ActiveRecord::Migration[6.1]
create_table 'tags' do |t|
t.string 'name'
end
create_table 'tag_audits', id: false do |t|
t.string 'tag_name'
end
create_table 'labels', id: false do |t|
t.string 'name'
end
end
30 changes: 30 additions & 0 deletions test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 0) do
# These are extensions that must be enabled in order to support this database
enable_extension 'plpgsql'

create_table 'labels', id: false, force: :cascade do |t|
t.string 'name'
end

create_table 'tag_audits', id: false, force: :cascade do |t|
t.string 'tag_name'
end

create_table 'tags', force: :cascade do |t|
t.string 'name'
end
end
Empty file added test/dummy/log/.keep
Empty file.
40 changes: 5 additions & 35 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,13 @@
# frozen_string_literal: true

require 'erb'
require 'active_record'
require 'with_advisory_lock'
require 'tmpdir'
require 'securerandom'
begin
require 'activerecord-trilogy-adapter'
ActiveSupport.on_load(:active_record) do
require "trilogy_adapter/connection"
ActiveRecord::Base.public_send :extend, TrilogyAdapter::Connection
end
rescue LoadError
# do nothing
end
ENV['RAILS_ENV'] = 'test'

ActiveRecord::Base.configurations = {
default_env: {
url: ENV.fetch('DATABASE_URL', "sqlite3://#{Dir.tmpdir}/with_advisory_lock_test#{RUBY_VERSION}-#{ActiveRecord.gem_version}.sqlite3"),
properties: { allowPublicKeyRetrieval: true } # for JRuby madness
}
}
require_relative 'dummy/config/environment'
ActiveRecord::Migrator.migrations_paths = [File.expand_path('../test/dummy/db/migrate', __dir__)]
require 'rails/test_help'

ENV['WITH_ADVISORY_LOCK_PREFIX'] ||= SecureRandom.hex

ActiveRecord::Base.establish_connection

def env_db
@env_db ||= ActiveRecord::Base.connection_db_config.adapter.to_sym
end

ActiveRecord::Migration.verbose = false

require 'test_models'
require 'minitest'
require 'maxitest/autorun'
require 'mocha/minitest'

class GemTestCase < ActiveSupport::TestCase
Expand All @@ -49,14 +22,11 @@ def is_postgresql_adapter?; adapter_support.postgresql?; end

setup do
ENV['FLOCK_DIR'] = Dir.mktmpdir if is_sqlite3_adapter?
Tag.delete_all
TagAudit.delete_all
Label.delete_all
end

teardown do
FileUtils.remove_entry_secure(ENV['FLOCK_DIR'], true) if is_sqlite3_adapter?
end
end

puts "Testing with #{env_db} database, ActiveRecord #{ActiveRecord.gem_version} and #{RUBY_ENGINE} #{RUBY_ENGINE_VERSION} as #{RUBY_VERSION}"
# puts "Testing with #{env_db} database, ActiveRecord #{ActiveRecord.gem_version} and #{RUBY_ENGINE} #{RUBY_ENGINE_VERSION} as #{RUBY_VERSION}"
30 changes: 0 additions & 30 deletions test/test_models.rb

This file was deleted.

Loading
Loading