Skip to content

Commit

Permalink
fix: test suite drop database before all
Browse files Browse the repository at this point in the history
  • Loading branch information
sjdonado committed Jul 31, 2024
1 parent 98f103f commit a931894
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
require "uuid"
require "file_utils"

require "spec-kemal"
require "micrate"

require "dotenv"
Dotenv.load ".env.#{ENV["ENV"]}"

require "../bit"

Spec.before_suite do
# Delete the SQLite database file if it exists
db_file_path = ENV["DATABASE_URL"].split("sqlite3://").last.split("?").first
if File.exists?(db_file_path)
File.delete(db_file_path)
end

Micrate::DB.connection_url = ENV["DATABASE_URL"]
Micrate::Cli.run_up

Expand All @@ -20,7 +30,8 @@ def create_test_user

changeset = App::Lib::Database.insert(user)
if !changeset.valid?
raise "Test user creation failed"
error_messages = changeset.errors.map { |error| "#{error}" }.join(", ")
raise "Test user creation failed #{error_messages}"
end

user
Expand All @@ -29,13 +40,14 @@ end
def create_test_link(user, url)
link = App::Models::Link.new
link.id = UUID.v4.to_s
link.slug = App::Services::SlugService.shorten_url(url)
link.url = url
link.slug = Random::Secure.urlsafe_base64(4)
link.user = user

changeset = App::Lib::Database.insert(link)
if !changeset.valid?
raise "Test link creation failed"
unless changeset.valid?
error_messages = changeset.errors.map { |error| "#{error}" }.join(", ")
raise "Test link creation failed: #{error_messages} #{url} #{link.slug}"
end

link.clicks = [] of App::Models::Click
Expand Down

0 comments on commit a931894

Please sign in to comment.