Sha256: 75bd839d42ce3a8763c5059fb25cb878836d3bcfcefd7cabf0870116cfad9902

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

require "#{File.dirname(__FILE__)}/../spec_helper"

describe SQLTree, 'parsing and generating SQL' do

  it "should parse and generate SQL fo a simple list query" do
    SQLTree["SELECT * FROM table"].to_sql.should == 'SELECT * FROM "table"'
  end

  it "should parse and generate the DISTINCT keyword" do
    SQLTree["SELECT DISTINCT * FROM table"].to_sql.should == 'SELECT DISTINCT * FROM "table"'
  end

  it 'should parse and generate table aliases' do
    SQLTree["SELECT a.* FROM table AS a"].to_sql.should == 'SELECT "a".* FROM "table" AS "a"'
  end

  it "should parse and generate an ORDER BY clause" do
    SQLTree["SELECT * FROM table ORDER BY field1, field2"].to_sql.should ==
            'SELECT * FROM "table" ORDER BY "field1", "field2"'
  end

  it "should parse and generate an expression in the SELECT clause" do
    SQLTree['SELECT MD5( a)  AS  a,    b  > 0  AS  test  FROM  table'].to_sql.should ==
            'SELECT MD5("a") AS "a", ("b" > 0) AS "test" FROM "table"'
  end

  it "should parse and generate a complex FROM clause" do
    SQLTree['SELECT * FROM  a  LEFT JOIN  b  ON ( a.id    = b.a_id),      c  AS  d'].to_sql.should ==
            'SELECT * FROM "a" LEFT JOIN "b" ON ("a"."id" = "b"."a_id"), "c" AS "d"'
  end

  it "should parse and generate a WHERE clause" do
    SQLTree['SELECT * FROM  t  WHERE (   field  > 4  OR  NOW() >  timestamp)   AND   other_field  IS NOT NULL'].to_sql.should ==
            'SELECT * FROM "t" WHERE ((("field" > 4) OR (NOW() > "timestamp")) AND ("other_field" IS NOT NULL))'
  end

  it "should parse and generate a GROUP BY and HAVING clause" do
    SQLTree['SELECT SUM( field1 ) FROM  t  GROUP BY  field1,  MD5( field2 ) HAVING  SUM( field1 ) > 10'].to_sql.should ==
            'SELECT SUM("field1") FROM "t" GROUP BY "field1", MD5("field2") HAVING (SUM("field1") > 10)'
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
sql_tree-0.0.3 spec/integration/full_queries_spec.rb