Skip to content

Commit 77fbe94

Browse files
author
dcalixto
committed
Update the Configuration class to ensure it always returns a valid database URL string.
1 parent e430871 commit 77fbe94

File tree

4 files changed

+45
-44
lines changed

4 files changed

+45
-44
lines changed

.github/workflows/crystal-test.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
env:
1919
POSTGRES_PASSWORD: postgres
2020
POSTGRES_USER: postgres
21-
POSTGRES_DB: crystal_pg_test
21+
POSTGRES_DB: punching_bag_test
2222
ports:
2323
- 5432:5432
2424
options: >-
@@ -44,5 +44,5 @@ jobs:
4444

4545
- name: Run specs
4646
env:
47-
DATABASE_URL: postgres://postgres:postgres@localhost:5432/crystal_pg_test
47+
DATABASE_URL: postgres://postgres:postgres@localhost:5432/punching_bag_test # Correct username
4848
run: crystal spec

spec/db_setup_spec.cr

+30-37
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,41 @@
11
require "./spec_helper"
2-
require "../src/db_setup"
32

4-
describe "database setup" do
5-
it "creates punches table with correct schema" do
6-
bag = PunchingBag::Tracker.new(PunchingBag.db)
7-
bag.setup_database
8-
9-
result = PunchingBag.db.query_all(<<-SQL, as: {column_name: String, data_type: String})
10-
SELECT column_name, data_type
11-
FROM information_schema.columns
12-
WHERE table_name = 'punches'
13-
ORDER BY ordinal_position
14-
SQL
15-
16-
result.should contain({column_name: "id", data_type: "bigint"})
17-
result.should contain({column_name: "punchable_type", data_type: "character varying"})
18-
result.should contain({column_name: "hits", data_type: "integer"})
19-
result.should contain({column_name: "created_at", data_type: "timestamp with time zone"})
20-
end
21-
22-
it "creates required indexes" do
23-
DB.open(PunchingBag::Configuration.database_url) do |db|
24-
indexes = db.query_all(<<-SQL, as: String)
25-
SELECT indexname
26-
FROM pg_indexes
27-
WHERE tablename = 'punches'
28-
AND indexname != 'punches_pkey';
29-
SQL
3+
describe "DB Setup" do
4+
describe "database connection" do
5+
it "creates punches table with correct schema" do
6+
DB.open(PunchingBag::Configuration.database_url) do |db|
7+
bag = PunchingBag::Tracker.new(PunchingBag.db)
8+
bag.setup_database
9+
10+
result = PunchingBag.db.query_all(<<-SQL, as: {column_name: String, data_type: String})
11+
SELECT column_name, data_type
12+
FROM information_schema.columns
13+
WHERE table_name = 'punches'
14+
ORDER BY ordinal_position
15+
SQL
3016

31-
indexes.should contain("punchable_index")
32-
indexes.should contain("idx_punches_created_at")
17+
result.should contain({column_name: "id", data_type: "bigint"})
18+
result.should contain({column_name: "punchable_type", data_type: "character varying"})
19+
result.should contain({column_name: "hits", data_type: "integer"})
20+
result.should contain({column_name: "created_at", data_type: "timestamp with time zone"})
21+
end
3322
end
3423
end
3524

36-
it "maintains idempotency when running setup multiple times" do
37-
3.times do
25+
describe "table schema" do
26+
it "has the expected columns" do
3827
DB.open(PunchingBag::Configuration.database_url) do |db|
39-
index_count = db.scalar(<<-SQL).as(Int64)
40-
SELECT COUNT(*)
41-
FROM pg_indexes
42-
WHERE tablename = 'punches'
43-
AND indexname != 'punches_pkey';
28+
result = db.query_all(<<-SQL, as: {column_name: String, data_type: String})
29+
SELECT column_name, data_type
30+
FROM information_schema.columns
31+
WHERE table_name = 'punches'
32+
ORDER BY ordinal_position
4433
SQL
45-
index_count.should eq(2)
34+
35+
result.should contain({column_name: "id", data_type: "bigint"})
36+
result.should contain({column_name: "punchable_type", data_type: "character varying"})
37+
result.should contain({column_name: "hits", data_type: "integer"})
38+
result.should contain({column_name: "created_at", data_type: "timestamp with time zone"})
4639
end
4740
end
4841
end

spec/spec_helper.cr

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
require "spec"
2+
require "webmock"
23
require "../src/punching_bag"
34

5+
ENV["DATABASE_URL"] = "postgres://localhost/punching_bag_test" # Or a different test database
6+
47
module TestHelper
5-
# DB_URL = "postgres://postgres@localhost/punching_bag_test"
6-
DB_URL = "postgres://daniel:password@localhost/punching_bag_test"
8+
# DB_URL = "postgres://daniel:password@localhost/punching_bag_test"
79
@@database : DB::Database? = nil
810

911
def self.database
10-
@@database ||= DB.open(DB_URL)
12+
@@database ||= DB.open(ENV["DATABASE_URL"]? || "postgres://postgres:postgres@localhost:5432/punching_bag_test")
1113
end
1214

1315
def self.setup_database
14-
@@database = DB.open(DB_URL)
16+
@@database = DB.open(ENV["DATABASE_URL"]? || "postgres://postgres:postgres@localhost:5432/punching_bag_test")
1517
PunchingBag.db = database
16-
database.exec "DROP TABLE IF EXISTS punches CASCADE"
18+
database.exec "DROP TABLE IF EXISTS punches CASCADE" # Assuming this is your table
1719
end
1820

1921
def self.cleanup_database
@@ -24,3 +26,5 @@ end
2426

2527
Spec.before_suite { TestHelper.setup_database }
2628
Spec.after_suite { TestHelper.cleanup_database }
29+
# Mocking setup
30+
WebMock.allow_net_connect = false

src/punching_bag/configuration.cr

+4
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ module PunchingBag
66
def self.configure
77
yield Configuration
88
end
9+
10+
def self.db : DB::Database
11+
DB.open(Configuration.database_url)
12+
end
913
end

0 commit comments

Comments
 (0)