Sha256: 11a7b5bdd885d60431231686cba97bc3bfe2f2e8b7e4f3a4e88d9d3eef6bde4f

Contents?: true

Size: 1.51 KB

Versions: 5

Compression:

Stored size: 1.51 KB

Contents

require File.expand_path('../../support', __FILE__)
require 'couchbase-orm/json_schema/loader'

RSpec.describe CouchbaseOrm::JsonSchema::Loader do
  let(:loader) { described_class.send(:new, "#{Dir.pwd}/spec/json-schema") }

  describe '#extract_type' do
    it 'returns the type from the entity' do
      entity = { type: 'entity_snakecase' }
      expect(loader.extract_type(entity)).to eq('entity_snakecase')

      expect(loader.get_json_schema!(entity)).not_to be_nil
    end
  end

  describe '#get_json_schema' do
    context 'when schemas are present and document type exists' do
      let(:schemas) { { 'user' => { 'properties' => { 'name' => { 'type' => 'string' } } } } }

      before do
        allow(loader).to receive(:schemas).and_return(schemas)
      end

      it 'returns the schema for the given document type' do
        entity = { type: 'user' }
        expect(loader.get_json_schema!(entity)).to eq(schemas['user'])
      end

      it 'raise error if no schema found for the document type' do
        entity = { type: 'post' }
        expect { loader.get_json_schema!(entity) }.to raise_error(CouchbaseOrm::JsonSchema::Loader::Error, /Schema not found for post in .*\/json-schema/)
      end
    end

    context 'when schemas are not present or document type is missing' do
      it 'returns nil' do
        entity = { type: 'user' }
        expect { loader.get_json_schema!(entity) }.to raise_error(CouchbaseOrm::JsonSchema::Loader::Error, /Schema not found for user in .*\/json-schema/)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
couchbase-orm-2.0.4 spec/json-schema/loader_spec.rb
couchbase-orm-2.0.3 spec/json-schema/loader_spec.rb
couchbase-orm-2.0.2 spec/json-schema/loader_spec.rb
couchbase-orm-2.0.1 spec/json-schema/loader_spec.rb
couchbase-orm-2.0.0 spec/json-schema/loader_spec.rb