queryclerk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +2 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +10 -0
- data/LICENSE +21 -0
- data/README.md +153 -0
- data/Rakefile +8 -0
- data/docs/DEVELOPER_GUIDE.md +144 -0
- data/docs/HOME.md +13 -0
- data/lib/queryclerk/ai_adapter.rb +46 -0
- data/lib/queryclerk/client.rb +21 -0
- data/lib/queryclerk/configuration.rb +70 -0
- data/lib/queryclerk/dsl_parser.rb +158 -0
- data/lib/queryclerk/errors.rb +21 -0
- data/lib/queryclerk/prompt_builder.rb +51 -0
- data/lib/queryclerk/providers/anthropic.rb +52 -0
- data/lib/queryclerk/providers/base.rb +99 -0
- data/lib/queryclerk/providers/openai.rb +47 -0
- data/lib/queryclerk/providers.rb +34 -0
- data/lib/queryclerk/query_builder.rb +104 -0
- data/lib/queryclerk/railtie.rb +12 -0
- data/lib/queryclerk/safety_guard.rb +54 -0
- data/lib/queryclerk/schema_introspector.rb +65 -0
- data/lib/queryclerk/version.rb +5 -0
- data/lib/queryclerk.rb +67 -0
- data/sig/queryclerk.rbs +3 -0
- data/spec/ai_adapter_spec.rb +28 -0
- data/spec/client_spec.rb +48 -0
- data/spec/configuration_spec.rb +47 -0
- data/spec/providers_spec.rb +298 -0
- data/spec/queryclerk_spec.rb +151 -0
- data/spec/spec_helper.rb +63 -0
- metadata +145 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
RSpec.describe Queryclerk::Configuration do
|
|
6
|
+
subject(:config) { described_class.new }
|
|
7
|
+
|
|
8
|
+
it "defaults to the OpenAI provider with its model and base URL" do
|
|
9
|
+
expect(config.provider).to eq(:openai)
|
|
10
|
+
expect(config.model_name).to eq("gpt-4o-mini")
|
|
11
|
+
expect(config.api_base_url).to eq("https://api.openai.com/v1")
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
it "resolves Anthropic defaults when the provider is :anthropic" do
|
|
15
|
+
config.provider = :anthropic
|
|
16
|
+
|
|
17
|
+
expect(config.model_name).to eq("claude-sonnet-4-6")
|
|
18
|
+
expect(config.api_base_url).to eq("https://api.anthropic.com/v1")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it "normalizes provider strings and casing to a symbol" do
|
|
22
|
+
config.provider = "Anthropic"
|
|
23
|
+
|
|
24
|
+
expect(config.provider).to eq(:anthropic)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it "prefers explicitly assigned model_name and api_base_url over provider defaults" do
|
|
28
|
+
config.provider = :anthropic
|
|
29
|
+
config.model_name = "claude-opus-4-8"
|
|
30
|
+
config.api_base_url = "https://gateway.internal/v1"
|
|
31
|
+
|
|
32
|
+
expect(config.model_name).to eq("claude-opus-4-8")
|
|
33
|
+
expect(config.api_base_url).to eq("https://gateway.internal/v1")
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it "falls back to OpenAI defaults for an unrecognized provider" do
|
|
37
|
+
config.provider = :unknown
|
|
38
|
+
|
|
39
|
+
expect(config.model_name).to eq("gpt-4o-mini")
|
|
40
|
+
expect(config.api_base_url).to eq("https://api.openai.com/v1")
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "exposes Anthropic-specific defaults" do
|
|
44
|
+
expect(config.max_tokens).to eq(1024)
|
|
45
|
+
expect(config.anthropic_version).to eq("2023-06-01")
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
require "json"
|
|
5
|
+
|
|
6
|
+
RSpec.describe Queryclerk::Providers do
|
|
7
|
+
# Wires a provider to a Faraday test connection that records the outgoing
|
|
8
|
+
# request into `captured` and replies with a canned response. The connection
|
|
9
|
+
# is built from the configured base URL so the relative endpoint path is
|
|
10
|
+
# resolved exactly as it would be in production.
|
|
11
|
+
def stubbed(klass, configuration, expected_path:, response: nil, raw_body: nil, status: 200)
|
|
12
|
+
captured = {}
|
|
13
|
+
stubs = Faraday::Adapter::Test::Stubs.new
|
|
14
|
+
stubs.post(expected_path) do |env|
|
|
15
|
+
captured[:url] = env.url.to_s
|
|
16
|
+
captured[:headers] = env.request_headers.to_h
|
|
17
|
+
captured[:body] = JSON.parse(env.body)
|
|
18
|
+
[status, { "Content-Type" => "application/json" }, raw_body || JSON.generate(response)]
|
|
19
|
+
end
|
|
20
|
+
connection = Faraday.new(url: configuration.api_base_url) { |f| f.adapter :test, stubs }
|
|
21
|
+
[klass.new(configuration: configuration, connection: connection), captured]
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def config_for(provider)
|
|
25
|
+
Queryclerk::Configuration.new.tap do |c|
|
|
26
|
+
c.api_key = "secret-key"
|
|
27
|
+
c.provider = provider
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
describe Queryclerk::Providers::OpenAI do
|
|
32
|
+
it "posts an OpenAI chat-completions request and extracts the message content" do
|
|
33
|
+
provider, captured = stubbed(
|
|
34
|
+
described_class, config_for(:openai),
|
|
35
|
+
expected_path: "/v1/chat/completions",
|
|
36
|
+
response: { "choices" => [{ "message" => { "content" => '{"filters":[]}' } }] }
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
result = provider.chat_completion(system_prompt: "SYS", user_prompt: "USR")
|
|
40
|
+
|
|
41
|
+
expect(result).to eq('{"filters":[]}')
|
|
42
|
+
# Guards the latent bug: a leading-slash path drops the "/v1" prefix.
|
|
43
|
+
expect(captured[:url]).to eq("https://api.openai.com/v1/chat/completions")
|
|
44
|
+
expect(captured[:headers]["Authorization"]).to eq("Bearer secret-key")
|
|
45
|
+
expect(captured[:body]).to eq(
|
|
46
|
+
"model" => "gpt-4o-mini",
|
|
47
|
+
"temperature" => 0.0,
|
|
48
|
+
"messages" => [
|
|
49
|
+
{ "role" => "system", "content" => "SYS" },
|
|
50
|
+
{ "role" => "user", "content" => "USR" }
|
|
51
|
+
]
|
|
52
|
+
)
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
it "raises AIResponseError on a non-2xx response, surfacing the API message" do
|
|
56
|
+
provider, = stubbed(
|
|
57
|
+
described_class, config_for(:openai),
|
|
58
|
+
expected_path: "/v1/chat/completions",
|
|
59
|
+
status: 401,
|
|
60
|
+
response: { "error" => { "message" => "Invalid API key" } }
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
expect { provider.chat_completion(system_prompt: "s", user_prompt: "u") }
|
|
64
|
+
.to raise_error(Queryclerk::AIResponseError, /Invalid API key/)
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it "raises AIResponseError when the response has no choices" do
|
|
68
|
+
provider, = stubbed(
|
|
69
|
+
described_class, config_for(:openai),
|
|
70
|
+
expected_path: "/v1/chat/completions",
|
|
71
|
+
response: { "unexpected" => true }
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
expect { provider.chat_completion(system_prompt: "s", user_prompt: "u") }
|
|
75
|
+
.to raise_error(Queryclerk::AIResponseError, /missing choices/)
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
it "raises AIResponseError when choices is an empty array" do
|
|
79
|
+
provider, = stubbed(
|
|
80
|
+
described_class, config_for(:openai),
|
|
81
|
+
expected_path: "/v1/chat/completions",
|
|
82
|
+
response: { "choices" => [] }
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
expect { provider.chat_completion(system_prompt: "s", user_prompt: "u") }
|
|
86
|
+
.to raise_error(Queryclerk::AIResponseError, /missing choices/)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
it "raises AIResponseError when content is null (tool_calls-only reply)" do
|
|
90
|
+
provider, = stubbed(
|
|
91
|
+
described_class, config_for(:openai),
|
|
92
|
+
expected_path: "/v1/chat/completions",
|
|
93
|
+
response: { "choices" => [{ "message" => { "role" => "assistant", "content" => nil, "tool_calls" => [] } }] }
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
expect { provider.chat_completion(system_prompt: "s", user_prompt: "u") }
|
|
97
|
+
.to raise_error(Queryclerk::AIResponseError, /missing choices/)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
it "raises AIResponseError when the first choice has no message" do
|
|
101
|
+
provider, = stubbed(
|
|
102
|
+
described_class, config_for(:openai),
|
|
103
|
+
expected_path: "/v1/chat/completions",
|
|
104
|
+
response: { "choices" => [{ "finish_reason" => "stop" }] }
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
expect { provider.chat_completion(system_prompt: "s", user_prompt: "u") }
|
|
108
|
+
.to raise_error(Queryclerk::AIResponseError, /missing choices/)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
it "surfaces the HTTP status when an error response has no message" do
|
|
112
|
+
provider, = stubbed(
|
|
113
|
+
described_class, config_for(:openai),
|
|
114
|
+
expected_path: "/v1/chat/completions",
|
|
115
|
+
status: 429,
|
|
116
|
+
response: { "error" => { "type" => "rate_limit_error" } }
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
expect { provider.chat_completion(system_prompt: "s", user_prompt: "u") }
|
|
120
|
+
.to raise_error(Queryclerk::AIResponseError, /HTTP 429/)
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
it "surfaces the HTTP status when an error body is not JSON" do
|
|
124
|
+
provider, = stubbed(
|
|
125
|
+
described_class, config_for(:openai),
|
|
126
|
+
expected_path: "/v1/chat/completions",
|
|
127
|
+
status: 502,
|
|
128
|
+
raw_body: "<html>Bad Gateway</html>"
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
expect { provider.chat_completion(system_prompt: "s", user_prompt: "u") }
|
|
132
|
+
.to raise_error(Queryclerk::AIResponseError, /HTTP 502/)
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
it "raises AIResponseError when a successful body is not valid JSON" do
|
|
136
|
+
provider, = stubbed(
|
|
137
|
+
described_class, config_for(:openai),
|
|
138
|
+
expected_path: "/v1/chat/completions",
|
|
139
|
+
raw_body: "not json at all"
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
expect { provider.chat_completion(system_prompt: "s", user_prompt: "u") }
|
|
143
|
+
.to raise_error(Queryclerk::AIResponseError, /not valid JSON/)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
it "wraps Faraday transport errors as AIResponseError" do
|
|
147
|
+
cfg = config_for(:openai)
|
|
148
|
+
stubs = Faraday::Adapter::Test::Stubs.new
|
|
149
|
+
stubs.post("/v1/chat/completions") { raise Faraday::TimeoutError }
|
|
150
|
+
connection = Faraday.new(url: cfg.api_base_url) { |f| f.adapter :test, stubs }
|
|
151
|
+
provider = described_class.new(configuration: cfg, connection: connection)
|
|
152
|
+
|
|
153
|
+
expect { provider.chat_completion(system_prompt: "s", user_prompt: "u") }
|
|
154
|
+
.to raise_error(Queryclerk::AIResponseError, /AI request failed/)
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
describe Queryclerk::Providers::Anthropic do
|
|
159
|
+
it "posts a native Messages request and extracts the text block" do
|
|
160
|
+
provider, captured = stubbed(
|
|
161
|
+
described_class, config_for(:anthropic),
|
|
162
|
+
expected_path: "/v1/messages",
|
|
163
|
+
response: { "content" => [{ "type" => "text", "text" => '{"filters":[]}' }] }
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
result = provider.chat_completion(system_prompt: "SYS", user_prompt: "USR")
|
|
167
|
+
|
|
168
|
+
expect(result).to eq('{"filters":[]}')
|
|
169
|
+
expect(captured[:url]).to eq("https://api.anthropic.com/v1/messages")
|
|
170
|
+
expect(captured[:headers]["x-api-key"]).to eq("secret-key")
|
|
171
|
+
expect(captured[:headers]["anthropic-version"]).to eq("2023-06-01")
|
|
172
|
+
expect(captured[:headers]).not_to have_key("Authorization")
|
|
173
|
+
expect(captured[:body]).to eq(
|
|
174
|
+
"model" => "claude-sonnet-4-6",
|
|
175
|
+
"max_tokens" => 1024,
|
|
176
|
+
"temperature" => 0.0,
|
|
177
|
+
"system" => "SYS",
|
|
178
|
+
"messages" => [{ "role" => "user", "content" => "USR" }]
|
|
179
|
+
)
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
it "skips non-text blocks and returns the first text block" do
|
|
183
|
+
provider, = stubbed(
|
|
184
|
+
described_class, config_for(:anthropic),
|
|
185
|
+
expected_path: "/v1/messages",
|
|
186
|
+
response: {
|
|
187
|
+
"content" => [
|
|
188
|
+
{ "type" => "thinking", "thinking" => "..." },
|
|
189
|
+
{ "type" => "text", "text" => "the answer" }
|
|
190
|
+
]
|
|
191
|
+
}
|
|
192
|
+
)
|
|
193
|
+
|
|
194
|
+
expect(provider.chat_completion(system_prompt: "s", user_prompt: "u")).to eq("the answer")
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
it "returns the first text block when several are present" do
|
|
198
|
+
provider, = stubbed(
|
|
199
|
+
described_class, config_for(:anthropic),
|
|
200
|
+
expected_path: "/v1/messages",
|
|
201
|
+
response: {
|
|
202
|
+
"content" => [
|
|
203
|
+
{ "type" => "text", "text" => "first" },
|
|
204
|
+
{ "type" => "text", "text" => "second" }
|
|
205
|
+
]
|
|
206
|
+
}
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
expect(provider.chat_completion(system_prompt: "s", user_prompt: "u")).to eq("first")
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
it "raises AIResponseError when the content array is empty" do
|
|
213
|
+
provider, = stubbed(
|
|
214
|
+
described_class, config_for(:anthropic),
|
|
215
|
+
expected_path: "/v1/messages",
|
|
216
|
+
response: { "content" => [] }
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
expect { provider.chat_completion(system_prompt: "s", user_prompt: "u") }
|
|
220
|
+
.to raise_error(Queryclerk::AIResponseError, /missing content text block/)
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
it "raises AIResponseError when a text block has no text value" do
|
|
224
|
+
provider, = stubbed(
|
|
225
|
+
described_class, config_for(:anthropic),
|
|
226
|
+
expected_path: "/v1/messages",
|
|
227
|
+
response: { "content" => [{ "type" => "text" }] }
|
|
228
|
+
)
|
|
229
|
+
|
|
230
|
+
expect { provider.chat_completion(system_prompt: "s", user_prompt: "u") }
|
|
231
|
+
.to raise_error(Queryclerk::AIResponseError, /missing content text block/)
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
it "raises AIResponseError surfacing the Anthropic error message" do
|
|
235
|
+
provider, = stubbed(
|
|
236
|
+
described_class, config_for(:anthropic),
|
|
237
|
+
expected_path: "/v1/messages",
|
|
238
|
+
status: 400,
|
|
239
|
+
response: { "type" => "error", "error" => { "type" => "invalid_request_error", "message" => "max_tokens: required" } }
|
|
240
|
+
)
|
|
241
|
+
|
|
242
|
+
expect { provider.chat_completion(system_prompt: "s", user_prompt: "u") }
|
|
243
|
+
.to raise_error(Queryclerk::AIResponseError, /max_tokens: required/)
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
it "raises AIResponseError when no text block is present" do
|
|
247
|
+
provider, = stubbed(
|
|
248
|
+
described_class, config_for(:anthropic),
|
|
249
|
+
expected_path: "/v1/messages",
|
|
250
|
+
response: { "content" => [{ "type" => "tool_use", "id" => "x" }] }
|
|
251
|
+
)
|
|
252
|
+
|
|
253
|
+
expect { provider.chat_completion(system_prompt: "s", user_prompt: "u") }
|
|
254
|
+
.to raise_error(Queryclerk::AIResponseError, /missing content text block/)
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
it "respects custom max_tokens and anthropic_version" do
|
|
258
|
+
cfg = config_for(:anthropic)
|
|
259
|
+
cfg.max_tokens = 256
|
|
260
|
+
cfg.anthropic_version = "2024-10-22"
|
|
261
|
+
provider, captured = stubbed(
|
|
262
|
+
described_class, cfg,
|
|
263
|
+
expected_path: "/v1/messages",
|
|
264
|
+
response: { "content" => [{ "type" => "text", "text" => "x" }] }
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
provider.chat_completion(system_prompt: "s", user_prompt: "u")
|
|
268
|
+
|
|
269
|
+
expect(captured[:body]["max_tokens"]).to eq(256)
|
|
270
|
+
expect(captured[:headers]["anthropic-version"]).to eq("2024-10-22")
|
|
271
|
+
end
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
describe ".build" do
|
|
275
|
+
it "returns the OpenAI provider for :openai" do
|
|
276
|
+
expect(described_class.build(configuration: config_for(:openai)))
|
|
277
|
+
.to be_a(Queryclerk::Providers::OpenAI)
|
|
278
|
+
end
|
|
279
|
+
|
|
280
|
+
it "returns the Anthropic provider for :anthropic" do
|
|
281
|
+
expect(described_class.build(configuration: config_for(:anthropic)))
|
|
282
|
+
.to be_a(Queryclerk::Providers::Anthropic)
|
|
283
|
+
end
|
|
284
|
+
|
|
285
|
+
it "raises ConfigurationError for an unknown provider" do
|
|
286
|
+
expect { described_class.build(configuration: config_for(:gemini)) }
|
|
287
|
+
.to raise_error(Queryclerk::ConfigurationError, /Unknown provider/)
|
|
288
|
+
end
|
|
289
|
+
|
|
290
|
+
it "raises ConfigurationError when the API key is missing" do
|
|
291
|
+
cfg = Queryclerk::Configuration.new
|
|
292
|
+
cfg.provider = :anthropic
|
|
293
|
+
|
|
294
|
+
expect { described_class.build(configuration: cfg) }
|
|
295
|
+
.to raise_error(Queryclerk::ConfigurationError, /Missing API key/)
|
|
296
|
+
end
|
|
297
|
+
end
|
|
298
|
+
end
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "spec_helper"
|
|
4
|
+
|
|
5
|
+
RSpec.describe Queryclerk do
|
|
6
|
+
let(:adapter) { instance_double("AIAdapter") }
|
|
7
|
+
|
|
8
|
+
before do
|
|
9
|
+
user_1 = User.create!(name: "Alex", email: "alex@example.com", account_id: 10)
|
|
10
|
+
user_2 = User.create!(name: "Sam", email: "sam@example.com", account_id: 10)
|
|
11
|
+
user_3 = User.create!(name: "Taylor", email: "taylor@example.com", account_id: 99)
|
|
12
|
+
|
|
13
|
+
Invoice.create!(user_id: user_1.id, account_id: 10, amount_cents: 80_000, status: "unpaid", issued_at: Time.now)
|
|
14
|
+
Invoice.create!(user_id: user_2.id, account_id: 10, amount_cents: 10_000, status: "paid", issued_at: Time.now)
|
|
15
|
+
Invoice.create!(user_id: user_3.id, account_id: 99, amount_cents: 90_000, status: "unpaid", issued_at: Time.now)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it "returns an ActiveRecord::Relation for a valid query" do
|
|
19
|
+
allow(adapter).to receive(:call).and_return(
|
|
20
|
+
{
|
|
21
|
+
"filters" => [
|
|
22
|
+
{ "field" => "status", "operator" => "eq", "value" => "unpaid" },
|
|
23
|
+
{ "field" => "amount_cents", "operator" => "gt", "value" => 50_000 }
|
|
24
|
+
],
|
|
25
|
+
"limit" => 50,
|
|
26
|
+
"sort" => { "field" => "created_at", "direction" => "desc" }
|
|
27
|
+
}
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
relation = described_class.query(
|
|
31
|
+
prompt: "Unpaid invoices above 50000",
|
|
32
|
+
model: Invoice,
|
|
33
|
+
ai_adapter: adapter
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
expect(relation).to be_a(ActiveRecord::Relation)
|
|
37
|
+
expect(relation.count).to eq(1)
|
|
38
|
+
expect(relation.first.account_id).to eq(10)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "raises InvalidDSL for invalid field" do
|
|
42
|
+
allow(adapter).to receive(:call).and_return(
|
|
43
|
+
{
|
|
44
|
+
"filters" => [
|
|
45
|
+
{ "field" => "drop_table", "operator" => "eq", "value" => "x" }
|
|
46
|
+
],
|
|
47
|
+
"limit" => 10
|
|
48
|
+
}
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
expect do
|
|
52
|
+
described_class.query(prompt: "bad", model: Invoice, ai_adapter: adapter)
|
|
53
|
+
end.to raise_error(Queryclerk::InvalidDSL, /Unknown field/)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it "raises InvalidDSL when limit exceeds configured max" do
|
|
57
|
+
allow(adapter).to receive(:call).and_return(
|
|
58
|
+
{
|
|
59
|
+
"filters" => [],
|
|
60
|
+
"limit" => 1_000
|
|
61
|
+
}
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
expect do
|
|
65
|
+
described_class.query(prompt: "all", model: Invoice, ai_adapter: adapter)
|
|
66
|
+
end.to raise_error(Queryclerk::InvalidDSL, /limit must be between 1 and 100/)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
it "raises InvalidDSL for unknown operator" do
|
|
70
|
+
allow(adapter).to receive(:call).and_return(
|
|
71
|
+
{
|
|
72
|
+
"filters" => [
|
|
73
|
+
{ "field" => "amount_cents", "operator" => "delete", "value" => 1 }
|
|
74
|
+
],
|
|
75
|
+
"limit" => 10
|
|
76
|
+
}
|
|
77
|
+
)
|
|
78
|
+
|
|
79
|
+
expect do
|
|
80
|
+
described_class.query(prompt: "bad", model: Invoice, ai_adapter: adapter)
|
|
81
|
+
end.to raise_error(Queryclerk::InvalidDSL, /Unknown operator/)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
it "raises UnauthorizedModel for non-whitelisted models" do
|
|
85
|
+
class AuditLog < ActiveRecord::Base
|
|
86
|
+
self.table_name = "users"
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
allow(adapter).to receive(:call).and_return({ "filters" => [], "limit" => 5 })
|
|
90
|
+
|
|
91
|
+
expect do
|
|
92
|
+
described_class.query(prompt: "anything", model: AuditLog, ai_adapter: adapter)
|
|
93
|
+
end.to raise_error(Queryclerk::UnauthorizedModel)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
it "raises InvalidDSL for JSON injection attempt with unknown key" do
|
|
97
|
+
allow(adapter).to receive(:call).and_return(
|
|
98
|
+
{
|
|
99
|
+
"filters" => [
|
|
100
|
+
{ "field" => "status", "operator" => "eq", "value" => "unpaid" }
|
|
101
|
+
],
|
|
102
|
+
"limit" => 10,
|
|
103
|
+
"sql" => "SELECT * FROM invoices; DROP TABLE invoices;"
|
|
104
|
+
}
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
expect do
|
|
108
|
+
described_class.query(prompt: "inject", model: Invoice, ai_adapter: adapter)
|
|
109
|
+
end.to raise_error(Queryclerk::InvalidDSL, /Unknown DSL keys/)
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
it "supports filtering by associated model fields" do
|
|
113
|
+
allow(adapter).to receive(:call).and_return(
|
|
114
|
+
{
|
|
115
|
+
"filters" => [
|
|
116
|
+
{ "field" => "user.email", "operator" => "like", "value" => "alex@" },
|
|
117
|
+
{ "field" => "status", "operator" => "eq", "value" => "unpaid" }
|
|
118
|
+
],
|
|
119
|
+
"limit" => 10
|
|
120
|
+
}
|
|
121
|
+
)
|
|
122
|
+
|
|
123
|
+
relation = described_class.query(
|
|
124
|
+
prompt: "Unpaid invoices for users with email containing alex@",
|
|
125
|
+
model: Invoice,
|
|
126
|
+
ai_adapter: adapter
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
expect(relation.count).to eq(1)
|
|
130
|
+
expect(relation.first.user.email).to eq("alex@example.com")
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
it "rejects associated-field filter when join is not allowed by policy" do
|
|
134
|
+
Queryclerk.configure do |c|
|
|
135
|
+
c.allowed_associations = {}
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
allow(adapter).to receive(:call).and_return(
|
|
139
|
+
{
|
|
140
|
+
"filters" => [
|
|
141
|
+
{ "field" => "user.email", "operator" => "eq", "value" => "alex@example.com" }
|
|
142
|
+
],
|
|
143
|
+
"limit" => 10
|
|
144
|
+
}
|
|
145
|
+
)
|
|
146
|
+
|
|
147
|
+
expect do
|
|
148
|
+
described_class.query(prompt: "Invoices for alex@example.com", model: Invoice, ai_adapter: adapter)
|
|
149
|
+
end.to raise_error(Queryclerk::InvalidDSL, /Join not allowed by policy: user/)
|
|
150
|
+
end
|
|
151
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "active_record"
|
|
5
|
+
require "sqlite3"
|
|
6
|
+
require "rspec"
|
|
7
|
+
require_relative "../lib/queryclerk"
|
|
8
|
+
|
|
9
|
+
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
|
|
10
|
+
|
|
11
|
+
ActiveRecord::Schema.define do
|
|
12
|
+
create_table :users, force: true do |t|
|
|
13
|
+
t.string :name
|
|
14
|
+
t.string :email
|
|
15
|
+
t.integer :account_id
|
|
16
|
+
t.timestamps
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
create_table :invoices, force: true do |t|
|
|
20
|
+
t.integer :user_id
|
|
21
|
+
t.integer :account_id
|
|
22
|
+
t.integer :amount_cents
|
|
23
|
+
t.string :status
|
|
24
|
+
t.datetime :issued_at
|
|
25
|
+
t.timestamps
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
class User < ActiveRecord::Base
|
|
30
|
+
has_many :invoices
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
class Invoice < ActiveRecord::Base
|
|
34
|
+
belongs_to :user
|
|
35
|
+
|
|
36
|
+
enum :status, { unpaid: "unpaid", paid: "paid", voided: "voided" }
|
|
37
|
+
|
|
38
|
+
def self.current_account_id
|
|
39
|
+
10
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def self.current_tenant_scope
|
|
43
|
+
where(account_id: current_account_id)
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
RSpec.configure do |config|
|
|
48
|
+
config.before do
|
|
49
|
+
User.delete_all
|
|
50
|
+
Invoice.delete_all
|
|
51
|
+
Queryclerk.reset_configuration!
|
|
52
|
+
|
|
53
|
+
Queryclerk.configure do |c|
|
|
54
|
+
c.api_key = "test-api-key"
|
|
55
|
+
c.model_name = "gpt-4o-mini"
|
|
56
|
+
c.max_limit = 100
|
|
57
|
+
c.allowed_models = [Invoice, User]
|
|
58
|
+
c.allowed_associations = {
|
|
59
|
+
"Invoice" => ["user"]
|
|
60
|
+
}
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|