From 405f41b18434f74c33bfeeb66c66b324920903c6 Mon Sep 17 00:00:00 2001 From: Sergio Bayona Date: Wed, 21 Feb 2024 09:13:15 -0500 Subject: [PATCH] specs for builder and model --- lib/esquema/builder.rb | 7 +++- spec/esquema/builder_spec.rb | 69 ++++++++++++++++++++++++++++++++++++ spec/esquema/model_spec.rb | 22 ++++++++++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 spec/esquema/builder_spec.rb diff --git a/lib/esquema/builder.rb b/lib/esquema/builder.rb index 06f4087..20f53b9 100644 --- a/lib/esquema/builder.rb +++ b/lib/esquema/builder.rb @@ -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 @@ -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. diff --git a/spec/esquema/builder_spec.rb b/spec/esquema/builder_spec.rb new file mode 100644 index 0000000..5ee2a47 --- /dev/null +++ b/spec/esquema/builder_spec.rb @@ -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 diff --git a/spec/esquema/model_spec.rb b/spec/esquema/model_spec.rb index 870ee53..e08a6fd 100644 --- a/spec/esquema/model_spec.rb +++ b/spec/esquema/model_spec.rb @@ -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",