Skip to content
Merged
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
20 changes: 19 additions & 1 deletion config/database.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
database_name = "avram_dev"

Avram::Repo.configure do |settings|
class TestDatabase < Avram::Database
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is how to create a new Database

end

class DatabaseWithIncorrectSettings < Avram::Database
end

TestDatabase.configure do |settings|
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Configure databases

settings.url = ENV["DATABASE_URL"]? || Avram::PostgresURL.build(
hostname: "db",
database: database_name,
username: "lucky",
password: "developer"
)
end

DatabaseWithIncorrectSettings.configure do |settings|
settings.url = Avram::PostgresURL.build(
hostname: "db",
database: database_name,
username: "incorrect"
)
end

Avram.configure do |settings|
settings.database_to_migrate = TestDatabase.new
end
2 changes: 1 addition & 1 deletion spec/bool_criteria_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./spec_helper"

private class QueryMe < Avram::Model
private class QueryMe < BaseModel
COLUMN_SQL = "users.id, users.created_at, users.updated_at, users.admin"

table users do
Expand Down
10 changes: 5 additions & 5 deletions spec/connection_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ require "./spec_helper"

describe Avram::Connection do
it "displays a helpful error when failing to connect" do
conn = Avram::Connection.new("postgres://root:root@localhost:5432/tacoman")
message = Regex.new("Failed to connect to databse 'tacoman' with username 'root'.")
conn = Avram::Connection.new("postgres://root:root@localhost:5432/tacoman", TestDatabase)
message = Regex.new("TestDatabase: Failed to connect to database 'tacoman' with username 'root'.")
expect_raises(Avram::ConnectionError, message) do
conn.try_connection!
conn.open
end
end

it "suggests trying a password when no password supplied and connection fails" do
conn = Avram::Connection.new("postgres://root@localhost:5432/tacoman")
conn = Avram::Connection.new("postgres://root@localhost:5432/tacoman", TestDatabase)
message = Regex.new("You didn't supply a password, did you mean to?")
expect_raises(Avram::ConnectionError, message) do
conn.try_connection!
conn.open
end
end
end
2 changes: 1 addition & 1 deletion spec/criteria_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./spec_helper"

private class QueryMe < Avram::Model
private class QueryMe < BaseModel
COLUMN_SQL = "users.id, users.created_at, users.updated_at, users.age, users.nickname"

table users do
Expand Down
4 changes: 2 additions & 2 deletions spec/migrator/migration_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe Avram::Migrator::Migration::V1 do
MigrationThatPartiallyWorks::V999.new.up
end

exists = Avram::Repo.run do |db|
exists = TestDatabase.run do |db|
db.query_one? "SELECT EXISTS (SELECT 1 FROM pg_tables WHERE tablename = 'fake_things');", as: Bool
end
exists.should be_false
Expand Down Expand Up @@ -65,5 +65,5 @@ private def get_column_names(table_name)
AND table_name = '#{table_name}'
SQL

Avram::Repo.run { |db| db.query_all statement, as: String }
TestDatabase.run { |db| db.query_all statement, as: String }
end
16 changes: 8 additions & 8 deletions spec/model_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./spec_helper"

private class QueryMe < Avram::Model
private class QueryMe < BaseModel
COLUMN_SQL = "users.id, users.created_at, users.updated_at, users.email, users.age"

table :users do
Expand All @@ -9,40 +9,40 @@ private class QueryMe < Avram::Model
end
end

private class ModelWithMissingButSimilarlyNamedColumn < Avram::Model
private class ModelWithMissingButSimilarlyNamedColumn < BaseModel
table :users do
column mickname : String
end
end

private class ModelWithOptionalAttributeOnRequiredColumn < Avram::Model
private class ModelWithOptionalAttributeOnRequiredColumn < BaseModel
table :users do
column name : String?
end
end

private class ModelWithRequiredAttributeOnOptionalColumn < Avram::Model
private class ModelWithRequiredAttributeOnOptionalColumn < BaseModel
table :users do
column nickname : String
end
end

private class MissingTable < Avram::Model
private class MissingTable < BaseModel
table :definitely_a_missing_table do
end
end

private class MissingButSimilarlyNamedTable < Avram::Model
private class MissingButSimilarlyNamedTable < BaseModel
table :uusers do
end
end

private class EmptyModelCompilesOk < Avram::Model
private class EmptyModelCompilesOk < BaseModel
table :no_fields do
end
end

private class InferredTableNameModel < Avram::Model
private class InferredTableNameModel < BaseModel
COLUMN_SQL = "inferred_table_name_models.id, inferred_table_name_models.created_at, inferred_table_name_models.updated_at"

table do
Expand Down
24 changes: 24 additions & 0 deletions spec/multi_database_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require "./spec_helper"

private class ModelWithBadDatabase < BaseModel
table do
end

# Use a different database for this fake model
def self.database
DatabaseWithIncorrectSettings
end
end

describe "Configuring and connecting to different databases" do
it "tries to connect to the configured database" do
# It will fail to connect which is what we expect since we configured
# a database with an incorrect URL
#
# If it does not raise an error then the connection is good,
# which is not what we configured
expect_raises Avram::ConnectionError do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this raise an exception error message letting us know which one it wasn't able to connect to? Our app could have up to 4 PG connections technically (a bit absurd, I know), so if for some reason one of them didn't connection, it would be nice to see Unable to connect with DatabaseWithIncorrectSettings or something.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes great point!

ModelWithBadDatabase::BaseQuery.new.select_count
end
end
end
2 changes: 1 addition & 1 deletion spec/query_builder_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ describe "Avram::QueryBuilder" do

query.statement.should eq "SELECT * FROM users ORDER BY name ASC, id DESC"

Avram::Repo.run do |db|
TestDatabase.run do |db|
db.exec query.statement
end
end
Expand Down
6 changes: 3 additions & 3 deletions spec/save_operation_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private class SaveLineItem < LineItem::SaveOperation
permit_columns :name
end

private class ValueColumnModel < Avram::Model
private class ValueColumnModel < BaseModel
table :value_column_model do
column value : String
end
Expand Down Expand Up @@ -317,7 +317,7 @@ describe "Avram::SaveOperation" do
it "logs the failure if a logger is set" do
log_io = IO::Memory.new
logger = Dexter::Logger.new(log_io)
Avram::Repo.temp_config(logger: logger) do |settings|
Avram.temp_config(logger: logger) do |settings|
SaveUser.create(name: "", age: 30) { |form, record| :unused }
log_io.to_s.should contain(%("failed_to_save":"SaveUser","validation_errors":"name is required. joined_at is required"))
end
Expand Down Expand Up @@ -443,7 +443,7 @@ describe "Avram::SaveOperation" do
user = UserQuery.new.first
log_io = IO::Memory.new
logger = Dexter::Logger.new(log_io)
Avram::Repo.temp_config(logger: logger) do |settings|
Avram.temp_config(logger: logger) do |settings|
SaveUser.update(user, name: "") { |form, record| :unused }
log_io.to_s.should contain(%("failed_to_save":"SaveUser","validation_errors":"name is required"))
end
Expand Down
3 changes: 2 additions & 1 deletion spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
require "spec"
require "../src/avram"
require "./support/base_model"
require "./support/**"
require "../config/database"

Db::Create.new.call
Db::Migrate.new.call

Spec.before_each do
Avram::Repo.truncate
TestDatabase.truncate
end
2 changes: 1 addition & 1 deletion spec/string_criteria_spec.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "./spec_helper"

private class QueryMe < Avram::Model
private class QueryMe < BaseModel
COLUMN_SQL = "users.id, users.created_at, users.updated_at, users.name"

table users do
Expand Down
2 changes: 1 addition & 1 deletion spec/support/admin.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Admin < Avram::Model
class Admin < BaseModel
COLUMN_SQL = "admins.id, admins.created_at, admins.updated_at, admins.name"

table do
Expand Down
5 changes: 5 additions & 0 deletions spec/support/base_model.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class BaseModel < Avram::Model
def self.database
TestDatabase
end
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is how you set what database a model uses. Can be done on the abstract class or could be overridden per model

end
2 changes: 1 addition & 1 deletion spec/support/blob.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Blob < Avram::Model
class Blob < BaseModel
table do
column doc : JSON::Any?
end
Expand Down
6 changes: 3 additions & 3 deletions spec/support/business.cr
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
class Business < Avram::Model
class Business < BaseModel
table do
column name : String
has_one tax_id : TaxId
has_one email_address : EmailAddress
end
end

class TaxId < Avram::Model
class TaxId < BaseModel
table do
column number : Int32
belongs_to business : Business
end
end

class EmailAddress < Avram::Model
class EmailAddress < BaseModel
table do
column address : String
belongs_to business : Business?
Expand Down
4 changes: 2 additions & 2 deletions spec/support/comment.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Comment < Avram::Model
class Comment < BaseModel
skip_default_columns

table do
Expand All @@ -9,7 +9,7 @@ class Comment < Avram::Model
end
end

class CommentForCustomPost < Avram::Model
class CommentForCustomPost < BaseModel
skip_default_columns

table :comments do
Expand Down
2 changes: 1 addition & 1 deletion spec/support/company.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Company < Avram::Model
class Company < BaseModel
table do
column sales : Int64
column earnings : Float64
Expand Down
2 changes: 1 addition & 1 deletion spec/support/employee.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Employee < Avram::Model
class Employee < BaseModel
table do
column name : String
belongs_to manager : Manager?
Expand Down
2 changes: 1 addition & 1 deletion spec/support/key_holder.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class KeyHolder < Avram::Model
class KeyHolder < BaseModel
table :users do
has_many sign_in_credentials : SignInCredential, foreign_key: :user_id
end
Expand Down
4 changes: 2 additions & 2 deletions spec/support/lazy_load_helpers.cr
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module LazyLoadHelpers
private def with_lazy_load(enabled)
begin
Avram::Repo.configure do |settings|
Avram.configure do |settings|
settings.lazy_load_enabled = enabled
end

yield
ensure
Avram::Repo.configure do |settings|
Avram.configure do |settings|
settings.lazy_load_enabled = true
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/support/line_item.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class LineItem < Avram::Model
class LineItem < BaseModel
skip_default_columns

table do
Expand Down
2 changes: 1 addition & 1 deletion spec/support/line_item_product.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class LineItemProduct < Avram::Model
class LineItemProduct < BaseModel
skip_default_columns

table :line_items_products do
Expand Down
2 changes: 1 addition & 1 deletion spec/support/manager.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Manager < Avram::Model
class Manager < BaseModel
table do
column name : String
has_many employees : Employee
Expand Down
2 changes: 1 addition & 1 deletion spec/support/menu_option.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class MenuOption < Avram::Model
class MenuOption < BaseModel
skip_default_columns

table do
Expand Down
2 changes: 1 addition & 1 deletion spec/support/model_with_different_default_columns.cr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Different primary key name
# No timestamps
class ModelWithDifferentDefaultColumns < Avram::Model
class ModelWithDifferentDefaultColumns < BaseModel
skip_default_columns

table :table_with_different_default_columns do
Expand Down
4 changes: 2 additions & 2 deletions spec/support/post.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Post < Avram::Model
class Post < BaseModel
skip_default_columns

table do
Expand All @@ -16,7 +16,7 @@ end
# This is a regular post, but with a custom table name
# This is to test that 'belongs_to' can accept a 'table' in the
# CommentForCustomPost model
class PostWithCustomTable < Avram::Model
class PostWithCustomTable < BaseModel
skip_default_columns

table :posts do
Expand Down
2 changes: 1 addition & 1 deletion spec/support/price.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Price < Avram::Model
class Price < BaseModel
skip_default_columns

table do
Expand Down
2 changes: 1 addition & 1 deletion spec/support/product.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Product < Avram::Model
class Product < BaseModel
skip_default_columns

table do
Expand Down
2 changes: 1 addition & 1 deletion spec/support/scan.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Scan < Avram::Model
class Scan < BaseModel
skip_default_columns

table do
Expand Down
2 changes: 1 addition & 1 deletion spec/support/sign_in_credential.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class SignInCredential < Avram::Model
class SignInCredential < BaseModel
table do
belongs_to user : User
end
Expand Down
2 changes: 1 addition & 1 deletion spec/support/tag.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Tag < Avram::Model
class Tag < BaseModel
skip_default_columns

table do
Expand Down
2 changes: 1 addition & 1 deletion spec/support/tagging.cr
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class Tagging < Avram::Model
class Tagging < BaseModel
skip_default_columns

table do
Expand Down
Loading