Skip to content

Commit 3dc56b6

Browse files
author
dcalixto
committed
Update database setup specs to use TestHelper database
1 parent 77fbe94 commit 3dc56b6

File tree

5 files changed

+76
-73
lines changed

5 files changed

+76
-73
lines changed

spec/db_setup_spec.cr

+25-24
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,46 @@
11
require "./spec_helper"
22

3+
require "./spec_helper"
4+
35
describe "DB Setup" do
46
describe "database connection" do
57
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
8+
db = TestHelper.database
9+
bag = PunchingBag::Tracker.new(db)
10+
bag.setup_database
911

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
12+
result = db.query_all(<<-SQL, as: {column_name: String, data_type: String})
13+
SELECT column_name, data_type
14+
FROM information_schema.columns
15+
WHERE table_name = 'punches'
16+
ORDER BY ordinal_position
17+
SQL
1618

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
19+
result.should contain({column_name: "id", data_type: "bigint"})
20+
result.should contain({column_name: "punchable_type", data_type: "character varying"})
21+
result.should contain({column_name: "hits", data_type: "integer"})
22+
result.should contain({column_name: "created_at", data_type: "timestamp with time zone"})
2223
end
2324
end
24-
25-
describe "table schema" do
26-
it "has the expected columns" do
27-
DB.open(PunchingBag::Configuration.database_url) do |db|
28-
result = db.query_all(<<-SQL, as: {column_name: String, data_type: String})
25+
end
26+
describe "table schema" do
27+
it "has the expected columns" do
28+
DB.open(PunchingBag::Configuration.database_url) do |db|
29+
result = db.query_all(<<-SQL, as: {column_name: String, data_type: String})
2930
SELECT column_name, data_type
3031
FROM information_schema.columns
3132
WHERE table_name = 'punches'
3233
ORDER BY ordinal_position
3334
SQL
3435

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"})
39-
end
36+
result.should contain({column_name: "id", data_type: "bigint"})
37+
result.should contain({column_name: "punchable_type", data_type: "character varying"})
38+
result.should contain({column_name: "hits", data_type: "integer"})
39+
result.should contain({column_name: "created_at", data_type: "timestamp with time zone"})
4040
end
4141
end
4242
end
43+
4344
describe PunchingBag do
4445
before_each do
4546
File.delete("./punching_bag.db") if File.exists?("./punching_bag.db")

spec/punching_bag_spec.cr

+9-24
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
require "../src/punching_bag"
2-
require "spec"
1+
require "./spec_helper"
32

43
describe PunchingBag do
54
it "tracks and retrieves hits correctly" do
6-
bag = PunchingBag::Tracker.new(PunchingBag.db)
5+
bag = PunchingBag::Tracker.new(TestHelper.database)
76
bag.clear
87

98
# Insert data for the test
@@ -19,7 +18,7 @@ describe PunchingBag do
1918
end
2019

2120
it "calculates average time correctly for multiple punches" do
22-
bag = PunchingBag::Tracker.new(PunchingBag.db)
21+
bag = PunchingBag::Tracker.new(TestHelper.database)
2322
bag.clear
2423

2524
now = Time.utc
@@ -31,7 +30,7 @@ describe PunchingBag do
3130
end
3231

3332
it "returns UTC time when no punches exist" do
34-
bag = PunchingBag::Tracker.new(PunchingBag.db.not_nil!)
33+
bag = PunchingBag::Tracker.new(TestHelper.database)
3534
bag.clear
3635

3736
now = Time.utc
@@ -41,7 +40,7 @@ describe PunchingBag do
4140
end
4241

4342
it "calculates average time for single punch" do
44-
bag = PunchingBag::Tracker.new(PunchingBag.db)
43+
bag = PunchingBag::Tracker.new(TestHelper.database)
4544
bag.clear
4645

4746
specific_time = Time.utc
@@ -50,33 +49,18 @@ describe PunchingBag do
5049
result = bag.average_time("Post", 1_i64)
5150
result.should be_close(specific_time, 1.second)
5251
end
53-
54-
it "retrieves most hit items within a time range" do
55-
bag = PunchingBag::Tracker.new(PunchingBag.db.not_nil!)
56-
bag.clear
57-
58-
bag.punch("Post", 1_i64, 5)
59-
bag.punch("Post", 2_i64, 3)
60-
bag.punch("Comment", 1_i64, 1)
61-
62-
since = Time.utc - 1.day
63-
most_hit = bag.most_hit(since)
64-
65-
most_hit[0][:punchable_id].should eq(1_i64)
66-
most_hit[0][:total_hits].should eq(5_i64)
67-
end
6852
end
69-
7053
describe "Database Setup" do
7154
it "creates punches table with correct schema" do
55+
db = TestHelper.database
7256
sql = <<-SQL
7357
SELECT column_name, data_type, is_nullable
7458
FROM information_schema.columns
7559
WHERE table_name = 'punches'
7660
ORDER BY ordinal_position
7761
SQL
7862

79-
results = PunchingBag.db.query_all(sql) do |rs|
63+
results = db.query_all(sql) do |rs|
8064
{
8165
name: rs.read(String),
8266
type: rs.read(String),
@@ -93,7 +77,8 @@ describe "Database Setup" do
9377
end
9478

9579
it "creates required indexes" do
96-
results = PunchingBag.db.query_all(<<-SQL, as: {name: String})
80+
db = TestHelper.database
81+
results = db.query_all(<<-SQL, as: {name: String})
9782
SELECT indexname as name
9883
FROM pg_indexes
9984
WHERE tablename = 'punches'

spec/spec_helper.cr

+26-22
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
require "spec"
2-
require "webmock"
32
require "../src/punching_bag"
3+
require "./test_helper"
44

5-
ENV["DATABASE_URL"] = "postgres://localhost/punching_bag_test" # Or a different test database
5+
PunchingBag.configure do |config|
6+
config.database_url = "postgres://postgres:postgres@localhost:5432/punching_bag_test"
7+
end
68

7-
module TestHelper
8-
# DB_URL = "postgres://daniel:password@localhost/punching_bag_test"
9-
@@database : DB::Database? = nil
9+
Spec.before_each do
10+
TestHelper.database.exec "DROP TABLE IF EXISTS punches"
1011

11-
def self.database
12-
@@database ||= DB.open(ENV["DATABASE_URL"]? || "postgres://postgres:postgres@localhost:5432/punching_bag_test")
13-
end
12+
TestHelper.database.exec <<-SQL
13+
CREATE TABLE punches (
14+
id BIGSERIAL PRIMARY KEY,
15+
punchable_type VARCHAR(255),
16+
punchable_id BIGINT,
17+
hits INTEGER DEFAULT 1,
18+
created_at TIMESTAMP WITH TIME ZONE,
19+
starts_at TIMESTAMP WITH TIME ZONE,
20+
ends_at TIMESTAMP WITH TIME ZONE
21+
)
22+
SQL
1423

15-
def self.setup_database
16-
@@database = DB.open(ENV["DATABASE_URL"]? || "postgres://postgres:postgres@localhost:5432/punching_bag_test")
17-
PunchingBag.db = database
18-
database.exec "DROP TABLE IF EXISTS punches CASCADE" # Assuming this is your table
19-
end
24+
TestHelper.database.exec <<-SQL
25+
CREATE INDEX punchable_index ON punches (punchable_type, punchable_id)
26+
SQL
2027

21-
def self.cleanup_database
22-
@@database.try &.close
23-
@@database = nil
24-
end
28+
TestHelper.database.exec <<-SQL
29+
CREATE INDEX idx_punches_created_at ON punches (created_at)
30+
SQL
31+
end
32+
Spec.after_suite do
33+
TestHelper.database.close
2534
end
26-
27-
Spec.before_suite { TestHelper.setup_database }
28-
Spec.after_suite { TestHelper.cleanup_database }
29-
# Mocking setup
30-
WebMock.allow_net_connect = false

spec/test_helper.cr

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module TestHelper
2+
@@db : DB::Database?
3+
4+
def self.database
5+
@@db ||= DB.open(PunchingBag::Configuration.database_url)
6+
end
7+
end

src/punching_bag/configuration.cr

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
module PunchingBag
22
class Configuration
3-
class_property database_url : String = "postgres://localhost/punching_bag_test"
3+
class_property database_url : String = "postgres://postgres:postgres@localhost:5432/punching_bag_test"
4+
5+
@@db : DB::Database?
6+
7+
def self.db
8+
@@db ||= DB.open(database_url)
9+
end
410
end
511

612
def self.configure
713
yield Configuration
814
end
915

10-
def self.db : DB::Database
11-
DB.open(Configuration.database_url)
16+
def self.db
17+
Configuration.db
1218
end
1319
end

0 commit comments

Comments
 (0)