lluminary 0.1.1 → 0.1.2
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 +4 -4
- data/lib/lluminary/config.rb +11 -1
- data/lib/lluminary/field_description.rb +34 -29
- data/lib/lluminary/provider_error.rb +2 -1
- data/lib/lluminary/providers/base.rb +5 -1
- data/lib/lluminary/providers/bedrock.rb +32 -30
- data/lib/lluminary/providers/openai.rb +28 -21
- data/lib/lluminary/providers/test.rb +9 -14
- data/lib/lluminary/result.rb +5 -2
- data/lib/lluminary/schema.rb +9 -16
- data/lib/lluminary/schema_model.rb +13 -10
- data/lib/lluminary/task.rb +84 -59
- data/lib/lluminary/validation_error.rb +2 -1
- data/lib/lluminary/version.rb +3 -2
- data/lib/lluminary.rb +23 -7
- data/spec/examples/analyze_text_spec.rb +7 -4
- data/spec/examples/color_analyzer_spec.rb +22 -22
- data/spec/examples/content_analyzer_spec.rb +27 -44
- data/spec/examples/historical_event_analyzer_spec.rb +18 -15
- data/spec/examples/price_analyzer_spec.rb +22 -28
- data/spec/examples/quote_task_spec.rb +9 -8
- data/spec/examples/sentiment_analysis_spec.rb +13 -10
- data/spec/examples/summarize_text_spec.rb +7 -4
- data/spec/lluminary/config_spec.rb +28 -26
- data/spec/lluminary/field_description_spec.rb +19 -21
- data/spec/lluminary/providers/base_spec.rb +11 -8
- data/spec/lluminary/providers/bedrock_spec.rb +47 -57
- data/spec/lluminary/providers/openai_spec.rb +27 -35
- data/spec/lluminary/providers/test_spec.rb +21 -16
- data/spec/lluminary/result_spec.rb +17 -10
- data/spec/lluminary/schema_model_spec.rb +31 -22
- data/spec/lluminary/schema_spec.rb +95 -107
- data/spec/lluminary/task_spec.rb +366 -300
- data/spec/spec_helper.rb +7 -2
- metadata +35 -19
@@ -1,135 +1,133 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "spec_helper"
|
2
3
|
|
3
4
|
RSpec.describe Lluminary::Schema do
|
4
5
|
let(:schema) { described_class.new }
|
5
6
|
|
6
|
-
describe
|
7
|
-
it
|
7
|
+
describe "#initialize" do
|
8
|
+
it "creates an empty fields hash" do
|
8
9
|
expect(schema.fields).to eq({})
|
9
10
|
end
|
10
11
|
end
|
11
12
|
|
12
|
-
describe
|
13
|
-
it
|
13
|
+
describe "#string" do
|
14
|
+
it "adds a string field to the schema" do
|
14
15
|
schema.string(:name)
|
15
16
|
expect(schema.fields).to eq({ name: { type: :string, description: nil } })
|
16
17
|
end
|
17
18
|
|
18
|
-
it
|
19
|
+
it "adds a string field with description" do
|
19
20
|
schema.string(:name, description: "The user's full name")
|
20
|
-
expect(schema.fields).to eq(
|
21
|
-
name: {
|
22
|
-
|
23
|
-
description: "The user's full name"
|
24
|
-
}
|
25
|
-
})
|
21
|
+
expect(schema.fields).to eq(
|
22
|
+
{ name: { type: :string, description: "The user's full name" } }
|
23
|
+
)
|
26
24
|
end
|
27
25
|
end
|
28
26
|
|
29
|
-
describe
|
30
|
-
it
|
27
|
+
describe "#integer" do
|
28
|
+
it "adds an integer field to the schema" do
|
31
29
|
schema.integer(:count)
|
32
|
-
expect(schema.fields).to eq(
|
30
|
+
expect(schema.fields).to eq(
|
31
|
+
{ count: { type: :integer, description: nil } }
|
32
|
+
)
|
33
33
|
end
|
34
34
|
|
35
|
-
it
|
35
|
+
it "adds an integer field with description" do
|
36
36
|
schema.integer(:count, description: "The total number of items")
|
37
|
-
expect(schema.fields).to eq(
|
38
|
-
count: {
|
39
|
-
|
40
|
-
description: "The total number of items"
|
41
|
-
}
|
42
|
-
})
|
37
|
+
expect(schema.fields).to eq(
|
38
|
+
{ count: { type: :integer, description: "The total number of items" } }
|
39
|
+
)
|
43
40
|
end
|
44
41
|
end
|
45
42
|
|
46
|
-
describe
|
47
|
-
it
|
43
|
+
describe "#boolean" do
|
44
|
+
it "adds a boolean field to the schema" do
|
48
45
|
schema.boolean(:active)
|
49
|
-
expect(schema.fields).to eq(
|
46
|
+
expect(schema.fields).to eq(
|
47
|
+
{ active: { type: :boolean, description: nil } }
|
48
|
+
)
|
50
49
|
end
|
51
50
|
|
52
|
-
it
|
51
|
+
it "adds a boolean field with description" do
|
53
52
|
schema.boolean(:active, description: "Whether the item is active")
|
54
|
-
expect(schema.fields).to eq(
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
53
|
+
expect(schema.fields).to eq(
|
54
|
+
{
|
55
|
+
active: {
|
56
|
+
type: :boolean,
|
57
|
+
description: "Whether the item is active"
|
58
|
+
}
|
59
|
+
}
|
60
|
+
)
|
60
61
|
end
|
61
62
|
end
|
62
63
|
|
63
|
-
describe
|
64
|
-
it
|
64
|
+
describe "#float" do
|
65
|
+
it "adds a float field to the schema" do
|
65
66
|
schema.float(:price)
|
66
67
|
expect(schema.fields).to eq({ price: { type: :float, description: nil } })
|
67
68
|
end
|
68
69
|
|
69
|
-
it
|
70
|
+
it "adds a float field with description" do
|
70
71
|
schema.float(:price, description: "The price of the item")
|
71
|
-
expect(schema.fields).to eq(
|
72
|
-
price: {
|
73
|
-
|
74
|
-
description: "The price of the item"
|
75
|
-
}
|
76
|
-
})
|
72
|
+
expect(schema.fields).to eq(
|
73
|
+
{ price: { type: :float, description: "The price of the item" } }
|
74
|
+
)
|
77
75
|
end
|
78
76
|
end
|
79
77
|
|
80
|
-
describe
|
81
|
-
it
|
78
|
+
describe "#datetime" do
|
79
|
+
it "adds a datetime field to the schema" do
|
82
80
|
schema.datetime(:start_time)
|
83
|
-
expect(schema.fields).to eq(
|
81
|
+
expect(schema.fields).to eq(
|
82
|
+
{ start_time: { type: :datetime, description: nil } }
|
83
|
+
)
|
84
84
|
end
|
85
85
|
|
86
|
-
it
|
86
|
+
it "adds a datetime field with description" do
|
87
87
|
schema.datetime(:start_time, description: "When the event starts")
|
88
|
-
expect(schema.fields).to eq(
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
88
|
+
expect(schema.fields).to eq(
|
89
|
+
{
|
90
|
+
start_time: {
|
91
|
+
type: :datetime,
|
92
|
+
description: "When the event starts"
|
93
|
+
}
|
94
|
+
}
|
95
|
+
)
|
94
96
|
end
|
95
97
|
end
|
96
98
|
|
97
|
-
describe
|
98
|
-
it
|
99
|
+
describe "#fields" do
|
100
|
+
it "returns the fields hash" do
|
99
101
|
schema.string(:name)
|
100
102
|
expect(schema.fields).to eq({ name: { type: :string, description: nil } })
|
101
103
|
end
|
102
104
|
|
103
|
-
it
|
105
|
+
it "returns the same hash instance" do
|
104
106
|
schema.string(:name)
|
105
107
|
first_call = schema.fields
|
106
108
|
second_call = schema.fields
|
107
109
|
expect(first_call).to be(second_call)
|
108
110
|
end
|
109
111
|
|
110
|
-
context
|
111
|
-
let(:schema)
|
112
|
-
described_class.new.tap do |s|
|
113
|
-
s.datetime(:start_time)
|
114
|
-
end
|
115
|
-
end
|
112
|
+
context "with datetime fields" do
|
113
|
+
let(:schema) { described_class.new.tap { |s| s.datetime(:start_time) } }
|
116
114
|
|
117
|
-
it
|
115
|
+
it "accepts DateTime values" do
|
118
116
|
errors = schema.validate(start_time: DateTime.now)
|
119
117
|
expect(errors).to be_empty
|
120
118
|
end
|
121
119
|
|
122
|
-
it
|
120
|
+
it "accepts nil values" do
|
123
121
|
errors = schema.validate(start_time: nil)
|
124
122
|
expect(errors).to be_empty
|
125
123
|
end
|
126
124
|
|
127
|
-
it
|
125
|
+
it "returns errors for non-DateTime values" do
|
128
126
|
errors = schema.validate(start_time: "2024-01-01")
|
129
127
|
expect(errors).to contain_exactly("Start time must be a DateTime")
|
130
128
|
end
|
131
129
|
|
132
|
-
it
|
130
|
+
it "can be required using presence validation" do
|
133
131
|
schema.validates :start_time, presence: true
|
134
132
|
errors = schema.validate(start_time: nil)
|
135
133
|
expect(errors).to contain_exactly("Start time can't be blank")
|
@@ -137,7 +135,7 @@ RSpec.describe Lluminary::Schema do
|
|
137
135
|
end
|
138
136
|
end
|
139
137
|
|
140
|
-
describe
|
138
|
+
describe "#validate" do
|
141
139
|
let(:schema) do
|
142
140
|
described_class.new.tap do |s|
|
143
141
|
s.string(:name)
|
@@ -145,12 +143,12 @@ RSpec.describe Lluminary::Schema do
|
|
145
143
|
end
|
146
144
|
end
|
147
145
|
|
148
|
-
it
|
146
|
+
it "returns no errors when all values match their field types" do
|
149
147
|
errors = schema.validate(name: "John", age: 30)
|
150
148
|
expect(errors).to be_empty
|
151
149
|
end
|
152
150
|
|
153
|
-
it
|
151
|
+
it "returns errors for type mismatches" do
|
154
152
|
errors = schema.validate(name: 123, age: "30")
|
155
153
|
expect(errors).to contain_exactly(
|
156
154
|
"Name must be a String",
|
@@ -158,95 +156,83 @@ RSpec.describe Lluminary::Schema do
|
|
158
156
|
)
|
159
157
|
end
|
160
158
|
|
161
|
-
context
|
162
|
-
let(:schema)
|
163
|
-
described_class.new.tap do |s|
|
164
|
-
s.boolean(:active)
|
165
|
-
end
|
166
|
-
end
|
159
|
+
context "with boolean fields" do
|
160
|
+
let(:schema) { described_class.new.tap { |s| s.boolean(:active) } }
|
167
161
|
|
168
|
-
it
|
162
|
+
it "accepts true values" do
|
169
163
|
errors = schema.validate(active: true)
|
170
164
|
expect(errors).to be_empty
|
171
165
|
end
|
172
166
|
|
173
|
-
it
|
167
|
+
it "accepts false values" do
|
174
168
|
errors = schema.validate(active: false)
|
175
169
|
expect(errors).to be_empty
|
176
170
|
end
|
177
171
|
|
178
|
-
it
|
172
|
+
it "accepts nil values" do
|
179
173
|
errors = schema.validate(active: nil)
|
180
174
|
expect(errors).to be_empty
|
181
175
|
end
|
182
176
|
|
183
|
-
it
|
184
|
-
errors = schema.validate(active:
|
177
|
+
it "returns errors for non-boolean values" do
|
178
|
+
errors = schema.validate(active: "true")
|
185
179
|
expect(errors).to contain_exactly("Active must be true or false")
|
186
180
|
|
187
181
|
errors = schema.validate(active: 1)
|
188
182
|
expect(errors).to contain_exactly("Active must be true or false")
|
189
183
|
end
|
190
184
|
|
191
|
-
it
|
185
|
+
it "can be required using presence validation" do
|
192
186
|
schema.validates :active, presence: true
|
193
187
|
errors = schema.validate(active: nil)
|
194
188
|
expect(errors).to contain_exactly("Active can't be blank")
|
195
189
|
end
|
196
190
|
end
|
197
191
|
|
198
|
-
context
|
199
|
-
let(:schema)
|
200
|
-
described_class.new.tap do |s|
|
201
|
-
s.string(:name)
|
202
|
-
end
|
203
|
-
end
|
192
|
+
context "with string fields" do
|
193
|
+
let(:schema) { described_class.new.tap { |s| s.string(:name) } }
|
204
194
|
|
205
|
-
it
|
195
|
+
it "accepts string values" do
|
206
196
|
errors = schema.validate(name: "John")
|
207
197
|
expect(errors).to be_empty
|
208
198
|
end
|
209
199
|
|
210
|
-
it
|
200
|
+
it "accepts nil values" do
|
211
201
|
errors = schema.validate(name: nil)
|
212
202
|
expect(errors).to be_empty
|
213
203
|
end
|
214
204
|
|
215
|
-
it
|
205
|
+
it "returns errors for non-string values" do
|
216
206
|
errors = schema.validate(name: 123)
|
217
207
|
expect(errors).to contain_exactly("Name must be a String")
|
218
208
|
end
|
219
209
|
|
220
|
-
it
|
210
|
+
it "can be required using presence validation" do
|
221
211
|
schema.validates :name, presence: true
|
222
212
|
errors = schema.validate(name: nil)
|
223
213
|
expect(errors).to contain_exactly("Name can't be blank")
|
224
214
|
end
|
225
215
|
end
|
226
216
|
|
227
|
-
context
|
228
|
-
let(:schema)
|
229
|
-
described_class.new.tap do |s|
|
230
|
-
s.integer(:age)
|
231
|
-
end
|
232
|
-
end
|
217
|
+
context "with integer fields" do
|
218
|
+
let(:schema) { described_class.new.tap { |s| s.integer(:age) } }
|
233
219
|
|
234
|
-
it
|
220
|
+
it "accepts integer values" do
|
235
221
|
errors = schema.validate(age: 30)
|
236
222
|
expect(errors).to be_empty
|
237
223
|
end
|
238
224
|
|
239
|
-
it
|
225
|
+
it "accepts nil values" do
|
240
226
|
errors = schema.validate(age: nil)
|
241
227
|
expect(errors).to be_empty
|
242
228
|
end
|
243
229
|
|
244
|
-
it
|
230
|
+
it "returns errors for non-integer values" do
|
245
231
|
errors = schema.validate(age: "30")
|
246
232
|
expect(errors).to contain_exactly("Age must be an Integer")
|
247
233
|
end
|
248
234
|
|
249
|
-
it
|
235
|
+
it "can be required using presence validation" do
|
250
236
|
schema.validates :age, presence: true
|
251
237
|
errors = schema.validate(age: nil)
|
252
238
|
expect(errors).to contain_exactly("Age can't be blank")
|
@@ -254,7 +240,7 @@ RSpec.describe Lluminary::Schema do
|
|
254
240
|
end
|
255
241
|
end
|
256
242
|
|
257
|
-
describe
|
243
|
+
describe "ActiveModel validations" do
|
258
244
|
let(:schema) do
|
259
245
|
described_class.new.tap do |s|
|
260
246
|
s.string(:name)
|
@@ -265,12 +251,12 @@ RSpec.describe Lluminary::Schema do
|
|
265
251
|
end
|
266
252
|
end
|
267
253
|
|
268
|
-
it
|
254
|
+
it "generates a class that includes ActiveModel::Validations" do
|
269
255
|
schema_model = schema.schema_model
|
270
256
|
expect(schema_model.ancestors).to include(ActiveModel::Validations)
|
271
257
|
end
|
272
258
|
|
273
|
-
it
|
259
|
+
it "adds accessors for defined fields" do
|
274
260
|
schema_model = schema.schema_model
|
275
261
|
instance = schema_model.new
|
276
262
|
instance.name = "John"
|
@@ -279,24 +265,26 @@ RSpec.describe Lluminary::Schema do
|
|
279
265
|
expect(instance.age).to eq(30)
|
280
266
|
end
|
281
267
|
|
282
|
-
it
|
268
|
+
it "validates presence" do
|
283
269
|
schema_model = schema.schema_model
|
284
270
|
instance = schema_model.new
|
285
271
|
expect(instance.valid?).to be false
|
286
272
|
expect(instance.errors.full_messages).to include("Name can't be blank")
|
287
273
|
end
|
288
274
|
|
289
|
-
it
|
275
|
+
it "validates numericality" do
|
290
276
|
schema_model = schema.schema_model
|
291
277
|
instance = schema_model.new(name: "John", age: 0)
|
292
278
|
expect(instance.valid?).to be false
|
293
|
-
expect(instance.errors.full_messages).to include(
|
279
|
+
expect(instance.errors.full_messages).to include(
|
280
|
+
"Age must be greater than 0"
|
281
|
+
)
|
294
282
|
end
|
295
283
|
|
296
|
-
it
|
284
|
+
it "returns true for valid instances" do
|
297
285
|
schema_model = schema.schema_model
|
298
286
|
instance = schema_model.new(name: "John", age: 30)
|
299
287
|
expect(instance.valid?).to be true
|
300
288
|
end
|
301
289
|
end
|
302
|
-
end
|
290
|
+
end
|