Skip to content

Commit 63c244e

Browse files
authored
Completely reorganize tests (#26)
1 parent f64e072 commit 63c244e

26 files changed

Lines changed: 1239 additions & 1411 deletions

.github/workflows/ci.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ on:
55
branches: [ main ]
66
pull_request:
77
branches: [ main ]
8+
workflow_call:
89

910
jobs:
1011
test:
@@ -15,7 +16,9 @@ jobs:
1516

1617
steps:
1718
- uses: actions/checkout@v4
18-
19+
with:
20+
fetch-depth: 0
21+
1922
- name: Set up Ruby ${{ matrix.ruby-version }}
2023
uses: ruby/setup-ruby@v1
2124
with:

.github/workflows/release.yml

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,7 @@ on:
77

88
jobs:
99
test:
10-
runs-on: ubuntu-latest
11-
strategy:
12-
matrix:
13-
ruby-version: ['3.1', '3.2', '3.3', '3.4']
14-
15-
steps:
16-
- uses: actions/checkout@v4
17-
with:
18-
fetch-depth: 0
19-
20-
- name: Set up Ruby ${{ matrix.ruby-version }}
21-
uses: ruby/setup-ruby@v1
22-
with:
23-
ruby-version: ${{ matrix.ruby-version }}
24-
bundler-cache: true
25-
26-
- name: Run test suite
27-
run: ./bin/test
10+
uses: ./.github/workflows/ci.yml
2811

2912
release:
3013
needs: test

RELEASE.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Release process
2+
3+
1. Bump the version in `lib/ruby_llm/schema/version.rb`
4+
2. Run `bundle install` to update the gemspec
5+
3. Commit the changes with a message like "Bump version to X.Y.Z"
6+
4. Run `bundle exec rake release:prepare` to create a release branch and push it to GitHub
7+
5. Github Actions will run the tests and publish the gem if they pass
8+
6. Delete the release branch: `git branch -d release/<version> && git push origin --delete release/<version>`
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
RSpec.describe RubyLLM::Schema, "class inheritance approach" do
6+
include SchemaBuilders
7+
8+
describe "schema attributes" do
9+
let(:base_schema) { build_schema_class { string :title } }
10+
11+
it "derives schema names from constants, instances, and defaults" do
12+
stub_const("NamedSchema", build_schema_class)
13+
expect(NamedSchema.new.to_json_schema[:name]).to eq("NamedSchema")
14+
15+
expect(base_schema.new.to_json_schema[:name]).to eq("Schema")
16+
expect(base_schema.new("CustomName").to_json_schema[:name]).to eq("CustomName")
17+
end
18+
19+
it "honours description precedence" do
20+
schema_with_description = build_schema_class do
21+
description "Class-level description"
22+
string :title
23+
end
24+
25+
anonymous_output = base_schema.new.to_json_schema
26+
expect(anonymous_output[:description]).to be_nil
27+
28+
class_level_output = schema_with_description.new.to_json_schema
29+
expect(class_level_output[:description]).to eq("Class-level description")
30+
31+
instance_override = schema_with_description.new("Test", description: "Instance description").to_json_schema
32+
expect(instance_override[:description]).to eq("Instance description")
33+
end
34+
35+
it "controls additional properties and strictness" do
36+
default_output = base_schema.new.to_json_schema
37+
expect(default_output[:schema][:additionalProperties]).to eq(false)
38+
expect(default_output[:schema][:strict]).to eq(true)
39+
40+
configured_schema = build_schema_class do
41+
additional_properties true
42+
strict false
43+
string :title
44+
end
45+
46+
configured_output = configured_schema.new.to_json_schema
47+
expect(configured_output[:schema][:additionalProperties]).to eq(true)
48+
expect(configured_output[:schema][:strict]).to eq(false)
49+
end
50+
51+
it "renders structured JSON schema" do
52+
configured_schema = build_schema_class do
53+
description "Test description"
54+
additional_properties false
55+
string :title
56+
integer :count, required: false
57+
end
58+
59+
output = configured_schema.new("ConfiguredSchema").to_json_schema
60+
61+
expect(output).to include(
62+
name: "ConfiguredSchema",
63+
description: "Test description",
64+
schema: hash_including(
65+
type: "object",
66+
properties: {
67+
title: {type: "string"},
68+
count: {type: "integer"}
69+
},
70+
required: [:title],
71+
additionalProperties: false,
72+
strict: true
73+
)
74+
)
75+
end
76+
end
77+
78+
describe "comprehensive scenario" do
79+
let(:schema_class) do
80+
build_schema_class do
81+
description "Comprehensive test schema"
82+
additional_properties true
83+
strict true
84+
85+
string :name, description: "Name field"
86+
integer :count
87+
boolean :active, required: false
88+
89+
object :config do
90+
string :setting
91+
end
92+
93+
array :tags, of: :string
94+
95+
any_of :status do
96+
string
97+
null
98+
end
99+
end
100+
end
101+
102+
it "supports full-feature schemas" do
103+
json_output = schema_class.new("TestSchema").to_json_schema
104+
105+
expect(json_output[:name]).to eq("TestSchema")
106+
expect(json_output[:schema][:additionalProperties]).to eq(true)
107+
expect(json_output[:schema][:strict]).to eq(true)
108+
expect(json_output[:schema][:properties].keys).to contain_exactly(
109+
:name, :count, :active, :config, :tags, :status
110+
)
111+
expect(json_output[:schema][:required]).to contain_exactly(:name, :count, :config, :tags, :status)
112+
end
113+
end
114+
end
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
RSpec.describe RubyLLM::Schema, "factory method (.create) approach" do
6+
include SchemaBuilders
7+
8+
describe "schema attributes" do
9+
let(:base_schema) { build_factory_schema { string :title } }
10+
11+
it "derives schema names from constants, instances, and defaults" do
12+
stub_const("NamedFactorySchema", build_factory_schema { string :title })
13+
expect(NamedFactorySchema.new.to_json_schema[:name]).to eq("NamedFactorySchema")
14+
15+
expect(base_schema.new.to_json_schema[:name]).to eq("Schema")
16+
expect(base_schema.new("CustomName").to_json_schema[:name]).to eq("CustomName")
17+
end
18+
19+
it "honours description precedence" do
20+
schema_with_description = build_factory_schema do
21+
description "Factory description"
22+
string :title
23+
end
24+
25+
anonymous_output = base_schema.new.to_json_schema
26+
expect(anonymous_output[:description]).to be_nil
27+
28+
class_level_output = schema_with_description.new.to_json_schema
29+
expect(class_level_output[:description]).to eq("Factory description")
30+
31+
instance_override = schema_with_description.new("NamedSchema", description: "Instance description").to_json_schema
32+
expect(instance_override[:description]).to eq("Instance description")
33+
end
34+
35+
it "controls additional properties and strictness" do
36+
default_output = base_schema.new.to_json_schema
37+
expect(default_output[:schema][:additionalProperties]).to eq(false)
38+
expect(default_output[:schema][:strict]).to eq(true)
39+
40+
configured_schema = build_factory_schema do
41+
additional_properties true
42+
strict false
43+
string :title
44+
end
45+
46+
configured_output = configured_schema.new.to_json_schema
47+
expect(configured_output[:schema][:additionalProperties]).to eq(true)
48+
expect(configured_output[:schema][:strict]).to eq(false)
49+
end
50+
51+
it "renders structured JSON schema" do
52+
configured_schema = build_factory_schema do
53+
description "Factory test description"
54+
additional_properties false
55+
string :title
56+
integer :count, required: false
57+
end
58+
59+
output = configured_schema.new("FactoryConfiguredSchema").to_json_schema
60+
61+
expect(output).to include(
62+
name: "FactoryConfiguredSchema",
63+
description: "Factory test description",
64+
schema: hash_including(
65+
type: "object",
66+
properties: {
67+
title: {type: "string"},
68+
count: {type: "integer"}
69+
},
70+
required: [:title],
71+
additionalProperties: false,
72+
strict: true
73+
)
74+
)
75+
end
76+
end
77+
78+
describe "comprehensive scenario" do
79+
let(:schema_class) do
80+
build_factory_schema do
81+
description "Comprehensive factory schema"
82+
additional_properties true
83+
strict true
84+
85+
string :name, description: "Name field"
86+
integer :count
87+
boolean :active, required: false
88+
89+
object :config do
90+
string :setting
91+
end
92+
93+
array :tags, of: :string
94+
95+
any_of :status do
96+
string
97+
null
98+
end
99+
end
100+
end
101+
102+
it "supports full-feature schemas" do
103+
json_output = schema_class.new("FactorySchema").to_json_schema
104+
105+
expect(json_output[:name]).to eq("FactorySchema")
106+
expect(json_output[:schema][:additionalProperties]).to eq(true)
107+
expect(json_output[:schema][:strict]).to eq(true)
108+
expect(json_output[:schema][:properties].keys).to contain_exactly(
109+
:name, :count, :active, :config, :tags, :status
110+
)
111+
expect(json_output[:schema][:required]).to contain_exactly(:name, :count, :config, :tags, :status)
112+
end
113+
end
114+
end
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# frozen_string_literal: true
2+
3+
require "spec_helper"
4+
5+
RSpec.describe RubyLLM::Schema, "helpers module approach" do
6+
include SchemaBuilders
7+
8+
describe "schema attributes" do
9+
it "derives schema names from parameters and defaults" do
10+
named_schema = build_helper_schema("ProvidedName") { string :title }
11+
expect(named_schema.to_json_schema[:name]).to eq("ProvidedName")
12+
13+
default_schema = build_helper_schema { string :title }
14+
expect(default_schema.to_json_schema[:name]).to eq("Schema")
15+
end
16+
17+
it "honours description precedence" do
18+
default_output = build_helper_schema("TestSchema") { string :title }.to_json_schema
19+
expect(default_output[:description]).to be_nil
20+
21+
block_description = build_helper_schema("TestSchema") do
22+
description "Block description"
23+
string :title
24+
end.to_json_schema
25+
expect(block_description[:description]).to eq("Block description")
26+
27+
parameter_override = build_helper_schema(
28+
"TestSchema",
29+
description: "Parameter description"
30+
) do
31+
description "Block description"
32+
string :title
33+
end.to_json_schema
34+
expect(parameter_override[:description]).to eq("Parameter description")
35+
end
36+
37+
it "controls additional properties and strictness" do
38+
default_output = build_helper_schema { string :title }.to_json_schema
39+
expect(default_output[:schema][:additionalProperties]).to eq(false)
40+
expect(default_output[:schema][:strict]).to eq(true)
41+
42+
configured_output = build_helper_schema do
43+
additional_properties true
44+
strict false
45+
string :title
46+
end.to_json_schema
47+
48+
expect(configured_output[:schema][:additionalProperties]).to eq(true)
49+
expect(configured_output[:schema][:strict]).to eq(false)
50+
end
51+
52+
it "renders structured JSON schema" do
53+
json_output = build_helper_schema(
54+
"HelperConfiguredSchema",
55+
description: "Helper test description"
56+
) do
57+
additional_properties false
58+
string :title
59+
integer :count, required: false
60+
end.to_json_schema
61+
62+
expect(json_output).to include(
63+
name: "HelperConfiguredSchema",
64+
description: "Helper test description",
65+
schema: hash_including(
66+
type: "object",
67+
properties: {
68+
title: {type: "string"},
69+
count: {type: "integer"}
70+
},
71+
required: [:title],
72+
additionalProperties: false,
73+
strict: true
74+
)
75+
)
76+
end
77+
end
78+
79+
describe "comprehensive scenario" do
80+
it "supports full-feature schemas" do
81+
json_output = build_helper_schema(
82+
"HelperSchema",
83+
description: "Comprehensive helper schema"
84+
) do
85+
additional_properties true
86+
strict true
87+
88+
string :name, description: "Name field"
89+
integer :count
90+
boolean :active, required: false
91+
92+
object :config do
93+
string :setting
94+
end
95+
96+
array :tags, of: :string
97+
98+
any_of :status do
99+
string
100+
null
101+
end
102+
end.to_json_schema
103+
104+
expect(json_output[:name]).to eq("HelperSchema")
105+
expect(json_output[:description]).to eq("Comprehensive helper schema")
106+
expect(json_output[:schema][:additionalProperties]).to eq(true)
107+
expect(json_output[:schema][:strict]).to eq(true)
108+
expect(json_output[:schema][:properties].keys).to contain_exactly(
109+
:name, :count, :active, :config, :tags, :status
110+
)
111+
expect(json_output[:schema][:required]).to contain_exactly(:name, :count, :config, :tags, :status)
112+
end
113+
end
114+
end

0 commit comments

Comments
 (0)