Sha256: 3045b9f331a7ddd71cdd79b377f2a1958ee1454eabee29a8f046d72130c64d25

Contents?: true

Size: 1.87 KB

Versions: 3

Compression:

Stored size: 1.87 KB

Contents

require "spec_helper"

module Scenic
  describe Definition do
    describe "to_sql" do
      it "returns the content of a view definition" do
        sql_definition = "SELECT text 'Hi' as greeting"
        allow(File).to receive(:read).and_return(sql_definition)

        definition = Definition.new("searches", 1)

        expect(definition.to_sql).to eq sql_definition
      end

      it "raises an error if the file is empty" do
        allow(File).to receive(:read).and_return("")

        expect do
          Definition.new("searches", 1).to_sql
        end.to raise_error RuntimeError
      end
    end

    describe "path" do
      it "returns a sql file in db/views with padded version and view name" do
        expected = "db/views/searches_v01.sql"

        definition = Definition.new("searches", 1)

        expect(definition.path).to eq expected
      end

      it "handles schema qualified view names" do
        definition = Definition.new("non_public.searches", 1)

        expect(definition.path).to eq "db/views/non_public_searches_v01.sql"
      end

      it "handles active record view prefix and suffixing" do
        with_affixed_tables(prefix: "foo_", suffix: "_bar") do
          definition = Definition.new("foo_searches_bar", 1)

          expect(definition.path).to eq "db/views/searches_v01.sql"
        end
      end
    end

    describe "full_path" do
      it "joins the path with Rails.root" do
        definition = Definition.new("searches", 15)

        expect(definition.full_path).to eq Rails.root.join(definition.path)
      end
    end

    describe "version" do
      it "pads the version number with 0" do
        definition = Definition.new(:_, 1)

        expect(definition.version).to eq "01"
      end

      it "doesn't pad more than 2 characters" do
        definition = Definition.new(:_, 15)

        expect(definition.version).to eq "15"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
scenic-1.8.0 spec/scenic/definition_spec.rb
scenic-1.7.0 spec/scenic/definition_spec.rb
scenic-1.6.0 spec/scenic/definition_spec.rb