require "spec_helper" describe "Schema definition" do describe "elements" do before(:each) do @schema = Mondrian::OLAP::Schema.new end describe "root element" do it "should render to XML" do @schema.to_xml.should be_like <<-XML XML end it "should render to XML with attributes" do @schema.define('FoodMart') do description 'Demo "FoodMart" schema' end @schema.to_xml.should be_like <<-XML XML end it "should render to XML using class method" do schema = Mondrian::OLAP::Schema.define('FoodMart') schema.to_xml.should be_like <<-XML XML end end describe "Cube" do it "should render to XML" do @schema.define do cube 'Sales' do default_measure 'Unit Sales' description 'Sales cube' cache false enabled true end end @schema.to_xml.should be_like <<-XML XML end it "should render to XML using options hash" do @schema.define do cube 'Sales', :default_measure => 'Unit Sales', :description => 'Sales cube', :cache => false, :enabled => true end @schema.to_xml.should be_like <<-XML XML end end describe "Table" do it "should render to XML" do @schema.define do cube 'Sales' do table 'sales_fact', :alias => 'sales' end end @schema.to_xml.should be_like <<-XML XML end it "should render table name in uppercase when using Oracle or LucidDB driver" do @schema.define do cube 'Sales' do table 'sales_fact', :alias => 'sales', :schema => 'facts' end end %w(oracle luciddb).each do |driver| @schema.to_xml(:driver => driver).should be_like <<-XML
XML end end it "should render table name in uppercase when :upcase_data_dictionary option is set to true" do @schema.define :upcase_data_dictionary => true do cube 'Sales' do table 'sales_fact', :alias => 'sales', :schema => 'facts' end end @schema.to_xml.should be_like <<-XML
XML end it "should render table name in lowercase when using Oracle or LucidDB driver but with :upcase_data_dictionary set to false" do @schema.define :upcase_data_dictionary => false do cube 'Sales' do table 'sales_fact', :alias => 'sales', :schema => 'facts' end end %w(oracle luciddb).each do |driver| @schema.to_xml(:driver => driver).should be_like <<-XML
XML end end end describe "Dimension" do it "should render to XML" do @schema.define do cube 'Sales' do dimension 'Gender' do foreign_key 'customer_id' hierarchy do has_all true all_member_name 'All Genders' primary_key 'customer_id' table 'customer' level 'Gender', :column => 'gender', :unique_members => true end end end end @schema.to_xml.should be_like <<-XML
XML end it "should render time dimension" do @schema.define do cube 'Sales' do dimension 'Time' do foreign_key 'time_id' hierarchy do has_all false primary_key 'time_id' table 'time_by_day' level 'Year', :column => 'the_year', :type => 'Numeric', :unique_members => true level 'Quarter', :column => 'quarter', :unique_members => false level 'Month', :column => 'month_of_year', :type => 'Numeric', :unique_members => false end end end end @schema.to_xml.should be_like <<-XML
XML end it "should render dimension hierarchy with join" do @schema.define do cube 'Sales' do dimension 'Products', :foreign_key => 'product_id' do hierarchy :has_all => true, :all_member_name => 'All Products', :primary_key => 'product_id', :primary_key_table => 'product' do join :left_key => 'product_class_id', :right_key => 'product_class_id' do table 'product' table 'product_class' end level 'Product Family', :table => 'product_class', :column => 'product_family', :unique_members => true level 'Brand Name', :table => 'product', :column => 'brand_name', :unique_members => false level 'Product Name', :table => 'product', :column => 'product_name', :unique_members => true end end end end @schema.to_xml.should be_like <<-XML
XML end it "should render table and column names in uppercase when using Oracle driver" do @schema.define do cube 'Sales' do dimension 'Products', :foreign_key => 'product_id' do hierarchy :has_all => true, :all_member_name => 'All Products', :primary_key => 'product_id', :primary_key_table => 'product' do join :left_key => 'product_class_id', :right_key => 'product_class_id' do table 'product' table 'product_class' end level 'Product Family', :table => 'product_class', :column => 'product_family', :unique_members => true level 'Brand Name', :table => 'product', :column => 'brand_name', :unique_members => false level 'Product Name', :table => 'product', :column => 'product_name', :unique_members => true end end end end @schema.to_xml(:driver => 'oracle').should be_like <<-XML
XML end end describe "Measure" do it "should render XML" do @schema.define do cube 'Sales' do measure 'Unit Sales' do column 'unit_sales' aggregator 'sum' end end end @schema.to_xml.should be_like <<-XML XML end it "should render column name in uppercase when using Oracle driver" do @schema.define do cube 'Sales' do measure 'Unit Sales' do column 'unit_sales' aggregator 'sum' end end end @schema.to_xml(:driver => 'oracle').should be_like <<-XML XML end end describe "Calculated Member" do it "should render XML" do @schema.define do cube 'Sales' do calculated_member 'Profit' do dimension 'Measures' formula '[Measures].[Store Sales] - [Measures].[Store Cost]' format_string '#,##0.00' end end end @schema.to_xml.should be_like <<-XML [Measures].[Store Sales] - [Measures].[Store Cost] XML end end end describe "connection with schema" do before(:all) do @schema = Mondrian::OLAP::Schema.define do cube 'Sales' do table 'sales' dimension 'Gender', :foreign_key => 'customer_id' do hierarchy :has_all => true, :primary_key => 'id' do table 'customers' level 'Gender', :column => 'gender', :unique_members => true end end dimension 'Time', :foreign_key => 'time_id' do hierarchy :has_all => false, :primary_key => 'id' do table 'time' level 'Year', :column => 'the_year', :type => 'Numeric', :unique_members => true level 'Quarter', :column => 'quarter', :unique_members => false level 'Month', :column => 'month_of_year', :type => 'Numeric', :unique_members => false end end measure 'Unit Sales', :column => 'unit_sales', :aggregator => 'sum' measure 'Store Sales', :column => 'store_sales', :aggregator => 'sum' end end @olap = Mondrian::OLAP::Connection.create(CONNECTION_PARAMS.merge :schema => @schema) end it "should connect" do @olap.should be_connected end it "should execute query" do @olap.from('Sales'). columns('[Measures].[Unit Sales]', '[Measures].[Store Sales]'). rows('descendants([Time].[2010].[Q1])'). where('[Gender].[F]'). execute.should be_a(Mondrian::OLAP::Result) end end end