Skip to content

Commit

Permalink
add integration tests; force generic query compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
imdrasil committed Apr 3, 2018
1 parent bc9d7a5 commit 0581a7b
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 15 deletions.
10 changes: 1 addition & 9 deletions .travis.setup.sh
@@ -1,10 +1,5 @@
set -exo pipefail

if [ "$DB" == 'postgres' ] || [ "$PAIR" == '1' ]; then
# Create database for postgres
psql -c 'create database jennifer_test;' -U postgres
fi

if [ "$DB" == 'mysql' ] || [ "$PAIR" == '1' ]; then
# Install newer MySQL
sudo apt-key adv --recv-key --keyserver keyserver.ubuntu.com A4A9406876FCBD3C456770C88C718D3B5072E1F5
Expand All @@ -14,9 +9,6 @@ if [ "$DB" == 'mysql' ] || [ "$PAIR" == '1' ]; then
sudo apt-get install -q -y -o Dpkg::Options::=--force-confnew mysql-server
sudo mysql_upgrade -u root --force
sudo service mysql restart

# Create database for mysql
crystal ./examples/run.cr -- db:create
fi

crystal ./examples/run.cr -- db:migrate
crystal ./examples/run.cr -- db:setup
7 changes: 7 additions & 0 deletions .travis.spec.sh
@@ -0,0 +1,7 @@
#!/bin/bash

if [ "$INTEGRATION" == '1' ]; then
crystal spec spec/**/*_test.cr
else
crystal spec
fi
4 changes: 4 additions & 0 deletions .travis.yml
Expand Up @@ -12,4 +12,8 @@ env:
- DB=postgres DB_USER=postgres DB_PASSWORD="" PAIR=1
- DB=mysql DB_USER=root DB_PASSWORD="" PAIR=1
- DB=postgres DB_USER=postgres DB_PASSWORD="" LEGACY_INSERT=1
# integration tests
- DB=postgres INTEGRATION=1
- DB=mysql INTEGRATION=1
before_script: bash .travis.setup.sh
script: bash .travis.spec.sh
22 changes: 22 additions & 0 deletions spec/integration/sam/blank_application.cr
@@ -0,0 +1,22 @@
require "../spec_helper"
require "sam"
require "../../../src/jennifer/sam"

Jennifer::Config.configure do |conf|
conf.logger.level = Logger::DEBUG
conf.host = "localhost"
conf.adapter = Spec.adapter
conf.migration_files_path = "./examples/migrations"
conf.db = "jennifer_integration_test"

case Spec.adapter
when "mysql"
conf.user = ENV["DB_USER"]? || "root"
conf.password = ""
when "postgres"
conf.user = ENV["DB_USER"]? || "developer"
conf.password = ENV["DB_PASSWORD"]? || "1qazxsw2"
end
end

Sam.help
84 changes: 84 additions & 0 deletions spec/integration/sam_test.cr
@@ -0,0 +1,84 @@
require "./spec_helper.cr"
require "spec"

DEFAULT_DB = "jennifer_integration_test"

def execute(command, options)
io = IO::Memory.new

status = Process.run("#{command} \"${@}\"", options, shell: true, output: io, error: io).exit_status
{status, io.to_s}
end

def clean(db = DEFAULT_DB)
yield
ensure
config = Jennifer::Config
command =
case Spec.adapter
when "postgres"
user = ENV["DB_USER"]? || "developer"
password = ENV["DB_PASSWORD"]? || "1qazxsw2"
"PGPASSWORD=#{password} dropdb #{db} -U#{user}"
when "mysql"
user = ENV["DB_USER"]? || "root"
password = ENV["DB_PASSWORD"]?
common_command = "echo \"drop database #{db};\" | mysql -u#{user}"
common_command += " -p#{password}" if password
common_command
else
raise "Unknown adapter"
end
Process.run(command, shell: true)
end

def create_db(db = DEFAULT_DB)
config = Jennifer::Config
command =
case Spec.adapter
when "postgres"
user = ENV["DB_USER"]? || "developer"
password = ENV["DB_PASSWORD"]? || "1qazxsw2"
"PGPASSWORD=#{password} createdb #{db} -U#{user}"
when "mysql"
user = ENV["DB_USER"]? || "root"
password = ENV["DB_PASSWORD"]?
common_command = "echo \"create database #{db};\" | mysql -u#{user}"
common_command += " -p#{password}" if password
common_command
else
raise "Unknown adapter"
end
Process.run(command, shell: true)
end

# NOTE: drop existing db before running tests
clean {}

describe "Blank application" do
describe "db:create" do
it do
clean do
execute("crystal spec/integration/sam/blank_application.cr", ["db:create"])[0].should eq(0)
end
end
end

describe "db:drop" do
it do
clean do
create_db
execute("crystal spec/integration/sam/blank_application.cr", ["db:drop"])[0].should eq(0)
end
end
end

describe "db:migrate" do
it do
clean do
create_db
execute("crystal spec/integration/sam/blank_application.cr", ["db:migrate"])[0].should eq(0)
end
end
end
end
24 changes: 24 additions & 0 deletions spec/integration/spec_helper.cr
@@ -0,0 +1,24 @@
require "../../src/jennifer"

module Spec
@@adapter = ""

def self.adapter
@@adapter
end

def self.adapter=(v)
@@adapter = v
end
end

{% if env("DB") == "mysql" %}
require "../../src/jennifer/adapter/mysql"
Spec.adapter = "mysql"
{% elsif env("DB") == "sqlite3" %}
require "../../src/jennifer/adapter/sqlite3"
Spec.adapter = "sqlite3"
{% else %}
require "../../src/jennifer/adapter/postgres"
Spec.adapter = "postgres"
{% end %}
2 changes: 1 addition & 1 deletion spec/spec_helper.cr
Expand Up @@ -16,7 +16,7 @@ require "./config"
require "./models.cr"
require "./factories.cr"

# Callbaks =======================
# Callbacks =======================

Spec.before_each do
Jennifer::Adapter.adapter.begin_transaction
Expand Down
5 changes: 5 additions & 0 deletions src/jennifer.cr
Expand Up @@ -83,3 +83,8 @@ end
::Jennifer.after_load_hook

I18n.load_path << File.join(__DIR__, "jennifer/locale")

# NOTE: This is needed to compile query generic class, otherwise
# `!query` at src/jennifer/adapter/base_sql_generator.cr:137:12 has no type
# is raised
Jennifer::Migration::Version.all
10 changes: 5 additions & 5 deletions src/jennifer/migration/runner.cr
Expand Up @@ -12,10 +12,10 @@ module Jennifer

pending = interpolation.keys - Version.all.pluck(:version).map(&.as(String))
return if pending.empty?
brocken = Version.where { _version.in(pending) }.pluck(:version).map(&.as(String))
unless brocken.empty?
puts "Can't run migrations because some of them are older then relase version.\nThey are:"
brocken.sort.each do |v|
broken = Version.where { _version.in(pending) }.pluck(:version).map(&.as(String))
unless broken.empty?
puts "Can't run migrations because some of them are older then master version.\nThey are:"
broken.sort.each do |v|
puts "- #{v}"
end
return
Expand Down Expand Up @@ -83,7 +83,7 @@ module Jennifer
klass.new.down
Version.all.where { _version == v }.delete
processed = true
puts "Droped migration #{v}"
puts "Dropped migration #{v}"
end
rescue e
puts e.message
Expand Down

0 comments on commit 0581a7b

Please sign in to comment.