Skip to content

Commit a03d3db

Browse files
committed
Coderrabit suggestions applied. Tests updated. Linter fixes
1 parent bca7033 commit a03d3db

6 files changed

Lines changed: 33 additions & 102 deletions

File tree

lib/bas/bot/base.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ class Base
1515
attr_accessor :read_response, :process_response, :write_response
1616

1717
def initialize(options, shared_storage_reader, shared_storage_writer = nil)
18-
@process_options = options || { close_connections_after_process: true }
18+
default_options = { close_connections_after_process: true }
19+
@process_options = default_options.merge(options || {})
1920
@shared_storage_reader = shared_storage_reader
2021
@shared_storage_writer = shared_storage_writer || shared_storage_reader
2122
end

lib/bas/shared_storage/POSTGRES.md

Lines changed: 0 additions & 91 deletions
This file was deleted.

lib/bas/shared_storage/base.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,7 @@ def set_in_process; end
2121

2222
def set_processed; end
2323

24-
def close_connections
25-
# TODO: Leave this method empty after testing
26-
puts "Closing connection for #{self.class.name}"
27-
end
24+
def close_connections; end
2825

2926
protected
3027

lib/bas/utils/postgres/connection.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ def query(query)
1616
results = if query.is_a? String
1717
@connection.exec(query)
1818
else
19-
sentence, params = query
19+
validate_query(query)
2020

21+
sentence, params = query
2122
@connection.exec_params(sentence, params)
2223
end
2324

@@ -28,6 +29,14 @@ def finish
2829
@connection&.finish
2930
@connection = nil
3031
end
32+
33+
private
34+
35+
def validate_query(query)
36+
return if query.is_a?(Array) && query.size == 2 && query[0].is_a?(String) && query[1].is_a?(Array)
37+
38+
raise ArgumentError, "Parameterized query must be an array of [sentence (String), params (Array)]"
39+
end
3140
end
3241
end
3342
end

spec/bas/bot/base_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
RSpec.describe Bas::Bot::Base do
77
before do
8-
@options = {}
8+
@options = { close_connections_after_process: true }
99
@shared_storage_reader = double(:shared_storage_reader, set_in_process: "in-process", set_processed: "processed",
1010
close_connections: true)
1111
@shared_storage_writer = double(:shared_storage_writer, close_connections: true)

spec/bas/utils/postgres/connection_spec.rb

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
it "creates a new connection with the provided parameters" do
3838
expect(PG::Connection).to receive(:new).with(connection_params).and_return(pg_connection)
3939

40-
described_class.new({ connection: connection_params })
40+
described_class.new(connection_params)
4141
end
4242

4343
it "raises an error when connection parameters are invalid" do
@@ -158,11 +158,26 @@
158158

159159
expect do
160160
connection.query(123)
161-
end.to raise_error(NoMethodError)
161+
end.to raise_error(ArgumentError)
162162
end
163163

164-
# NOTE: The implementation doesn't validate array structure, so malformed arrays
165-
# don't raise errors - they just behave unexpectedly
164+
it "raises ArgumentError for parameterized query with wrong array size" do
165+
expect do
166+
connection.query(["SELECT * FROM users WHERE id = $1"])
167+
end.to raise_error(ArgumentError, "Parameterized query must be an array of [sentence (String), params (Array)]")
168+
end
169+
170+
it "raises ArgumentError for parameterized query with non-string sentence" do
171+
expect do
172+
connection.query([1, [1]])
173+
end.to raise_error(ArgumentError, "Parameterized query must be an array of [sentence (String), params (Array)]")
174+
end
175+
176+
it "raises ArgumentError for parameterized query with non-array params" do
177+
expect do
178+
connection.query(["SELECT * FROM users WHERE id = $1", 1])
179+
end.to raise_error(ArgumentError, "Parameterized query must be an array of [sentence (String), params (Array)]")
180+
end
166181
end
167182
end
168183

0 commit comments

Comments
 (0)