spec/unit/projection_dsl_spec.rb in rom-sql-1.0.0.beta1 vs spec/unit/projection_dsl_spec.rb in rom-sql-1.0.0.beta2

- old
+ new

@@ -1,16 +1,16 @@ require 'spec_helper' -RSpec.describe ROM::SQL::ProjectionDSL, :sqlite, helpers: true do +RSpec.describe ROM::SQL::ProjectionDSL, :postgres, helpers: true do include_context 'database setup' subject(:dsl) do ROM::SQL::ProjectionDSL.new(schema) end let(:schema) do - define_schema(:users, id: ROM::SQL::Types::Serial, name: ROM::SQL::Types::String) + define_schema(:users, id: ROM::SQL::Types::Serial, name: ROM::SQL::Types::String, meta: ROM::SQL::Types::PG::JSONB) end let(:ds) do conn[:users] end @@ -19,10 +19,50 @@ it 'evaluates the block and returns an array with attribute types' do literals = dsl .call { int::count(id).as(:count) } .map { |attr| attr.sql_literal(ds) } - expect(literals).to eql(["count(`id`) AS 'count'"]) + expect(literals).to eql([%(COUNT("id") AS "count")]) + end + + it 'supports chaining attribute db functions' do + literals = dsl + .call { meta.pg_jsonb.get_text("name").as(:name) } + .map { |attr| attr.sql_literal(ds) } + + expect(literals).to eql([%{("meta" ->> 'name') AS "name"}]) + end + + it 'supports functions with args and chaining with other functions' do + literals = dsl + .call { int::count(id.qualified).distinct } + .map { |attr| attr.sql_literal(ds) } + + expect(literals).to eql([%(COUNT(DISTINCT "users"."id"))]) + end + + it 'supports functions with args and chaining with other functions and an alias' do + literals = dsl + .call { int::count(id.qualified).distinct.as(:count) } + .map { |attr| attr.sql_literal(ds) } + + expect(literals).to eql([%(COUNT(DISTINCT "users"."id") AS "count")]) + end + + it 'supports functions with arg being an attribute' do + literals = dsl + .call { int::count(id).as(:count) } + .map { |attr| attr.sql_literal(ds) } + + expect(literals).to eql([%(COUNT("id") AS "count")]) + end + + it 'supports functions with arg being a qualified attribute' do + literals = dsl + .call { int::count(id.qualified).as(:count) } + .map { |attr| attr.sql_literal(ds) } + + expect(literals).to eql([%(COUNT("users"."id") AS "count")]) end end describe '#method_missing' do it 'responds to methods matching attribute names' do