Skip to content

Commit

Permalink
specs for builder and model
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiobayona committed Feb 21, 2024
1 parent 1114b66 commit 405f41b
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/esquema/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module Esquema
# The Builder class is responsible for building a schema for an ActiveRecord model.
class Builder
attr_reader :model, :required_properties, :schema
attr_reader :model, :required_properties

def initialize(model)
raise ArgumentError, "Class is not an ActiveRecord model" unless model.ancestors.include? ActiveRecord::Base
Expand All @@ -28,6 +28,11 @@ def build_schema
}.compact
end

# @return [Hash] The schema for the ActiveRecord model.
def schema
build_schema
end

# Builds the properties for the schema.
#
# @return [Hash] The built properties.
Expand Down
69 changes: 69 additions & 0 deletions spec/esquema/builder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
RSpec.describe Esquema::Builder do
let(:model) { double("Model") }
let(:builder) { described_class.new(model) }

describe "#initialize" do
it "raises an ArgumentError if the class is not an ActiveRecord model" do
expect { described_class.new(Object) }.to raise_error(ArgumentError, "Class is not an ActiveRecord model")
end

it "sets the model and initializes properties" do
allow(model).to receive(:ancestors).and_return([ActiveRecord::Base])
expect(builder.model).to eq(model)
expect(builder.required_properties).to eq([])
end
end

describe "#build_schema" do
before { allow(model).to receive(:ancestors).and_return([ActiveRecord::Base]) }

it "builds the schema for the ActiveRecord model" do
expect(builder).to receive(:build_title).and_return("Title")
expect(builder).to receive(:build_description).and_return("Description")
expect(builder).to receive(:build_type).and_return("object")
expect(builder).to receive(:build_properties).and_return({ property: "value" })
expect(builder).to receive(:required_properties).and_return(["property"])

schema = builder.build_schema

expect(schema).to eq({
title: "Title",
description: "Description",
type: "object",
properties: { property: "value" },
required: ["property"]
})
end
end

describe "#build_properties" do
before { allow(model).to receive(:ancestors).and_return([ActiveRecord::Base]) }

it "adds properties from columns, associations, and virtual properties" do
expect(builder).to receive(:add_properties_from_columns)
expect(builder).to receive(:add_properties_from_associations)
expect(builder).to receive(:add_virtual_properties)

properties = builder.build_properties

expect(properties).to eq(builder.instance_variable_get(:@properties))
end
end

describe "#build_type" do
before { allow(model).to receive(:ancestors).and_return([ActiveRecord::Base]) }

it "returns the type of the model" do
expect(model).to receive(:respond_to?).with(:type).and_return(true)
expect(model).to receive(:type).and_return("type")

expect(builder.build_type).to eq("type")
end

it "returns 'object' if the model does not respond to type" do
expect(model).to receive(:respond_to?).with(:type).and_return(false)

expect(builder.build_type).to eq("object")
end
end
end
22 changes: 22 additions & 0 deletions spec/esquema/model_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,28 @@ def self.name

after { Esquema.configuration.reset }

describe ".schema_enhancements" do
it "returns the schema enhancements" do
expect(user.schema_enhancements).to eq({})
end
end

describe ".enhance_schema" do
it "enhances the schema using the provided block" do
user.enhance_schema do
property :name, title: "Person's Name"
property :email, title: "Person's Mailing Address"
end

expect(user.schema_enhancements).to eq(
properties: {
name: { title: "Person's Name" },
email: { title: "Person's Mailing Address" }
}
)
end
end

it "returns a ruby hash representing the schema_model properties" do
expect(user.json_schema).to include_json({
"title": "User",
Expand Down

0 comments on commit 405f41b

Please sign in to comment.