spec/mondrian_spec.rb in mondrian-olap-1.2.0 vs spec/mondrian_spec.rb in mondrian-olap-1.3.0

- old
+ new

@@ -37,10 +37,11 @@ level 'Country', :column => 'country', :unique_members => true level 'State Province', :column => 'state_province', :unique_members => true level 'City', :column => 'city', :unique_members => false level 'Name', :column => 'fullname', :unique_members => true do property 'Related name', :column => 'related_fullname', :type => "String" + property 'Birthdate', :column => 'birthdate', :type => "String" end end hierarchy 'ID', :has_all => true, :all_member_name => 'All Customers', :primary_key => 'id' do table 'customers' level 'ID', :column => 'id', :type => 'Numeric', :internal_type => 'long', :unique_members => true do @@ -52,20 +53,34 @@ hierarchy :has_all => false, :primary_key => 'id' do table 'time' level 'Year', :column => 'the_year', :type => 'Numeric', :unique_members => true, :level_type => 'TimeYears' level 'Quarter', :column => 'quarter', :unique_members => false, :level_type => 'TimeQuarters' level 'Month', :column => 'month_of_year', :type => 'Numeric', :unique_members => false, :level_type => 'TimeMonths' + level 'Day', :column => 'day_of_month', :type => 'Numeric', :unique_members => false, :level_type => 'TimeDays' do + property 'Date', :column => 'the_date', :type => "String" + end end hierarchy 'Weekly', :has_all => false, :primary_key => 'id' do table 'time' level 'Year', :column => 'the_year', :type => 'Numeric', :unique_members => true, :level_type => 'TimeYears' level 'Week', :column => 'weak_of_year', :type => 'Numeric', :unique_members => false, :level_type => 'TimeWeeks' end end measure 'Unit Sales', :column => 'unit_sales', :aggregator => 'sum' measure 'Store Sales', :column => 'store_sales', :aggregator => 'sum' end + + user_defined_function 'IsDirty' do + ruby do + returns :scalar + syntax :function + def call_with_evaluator(evaluator) + evaluator.isDirty + end + end + end + end @olap = Mondrian::OLAP::Connection.create(CONNECTION_PARAMS.merge :schema => @schema) end # test for http://jira.pentaho.com/browse/MONDRIAN-1050 @@ -118,6 +133,123 @@ MDX expect { @olap.execute mdx }.not_to raise_error end + # Test for https://jira.pentaho.com/browse/MONDRIAN-2714 + it "should return datetime property as java.sql.Timestamp" do + full_name = '[2010].[Q1].[1].[1]' + member = @olap.cube('Sales').member(full_name) + member.property_value('Date').should be_a(java.sql.Timestamp) + + result = @olap.from('Sales'). + with_member('[Measures].[date]').as("#{full_name}.Properties('Date')", format_string: 'dd.mm.yyyy'). + columns('[Measures].[date]').execute + result.values.first.should be_a(java.sql.Timestamp) + result.formatted_values.first.should == '01.01.2010' + end + + it "should return date property as java.sql.Date" do + expected_date_class = + case MONDRIAN_DRIVER + when 'oracle' + java.sql.Timestamp + else + java.sql.Date + end + + member = @olap.cube('Sales').hierarchy('Customers').level('Name').members.first + date_value = member.property_value('Birthdate') + date_value.should be_a(expected_date_class) + + result = @olap.from('Sales'). + with_member('[Measures].[date]').as("#{member.full_name}.Properties('Birthdate')", format_string: 'dd.mm.yyyy'). + columns('[Measures].[date]').execute + result.values.first.should be_a(expected_date_class) + result.formatted_values.first.should == Date.parse(date_value.to_s).strftime("%d.%m.%Y") + end + + describe "optimized Aggregate" do + def expected_value(crossjoin_members = nil) + query = @olap.from('Sales').columns('[Measures].[Unit Sales]') + query = query.crossjoin(crossjoin_members) if crossjoin_members + query.rows('[Customers].[USA].[CA]', '[Customers].[USA].[OR]'). + execute.values.map(&:first).inject(&:+) + end + + it "should aggregate stored members" do + result = @olap.from('Sales'). + with_member('[Customers].[CA and OR]').as("Aggregate({[Customers].[USA].[CA], [Customers].[USA].[OR]})"). + columns('[Measures].[Unit Sales]'). + rows('[Customers].[CA and OR]').execute + result.values[0][0].should == expected_value + end + + it "should aggregate stored members from several dimensions" do + result = @olap.from('Sales'). + with_member('[Customers].[CA and OR]'). + as("Aggregate({[Gender].[F]} * {[Customers].[USA].[CA], [Customers].[USA].[OR]})"). + columns('[Measures].[Unit Sales]'). + rows('[Customers].[CA and OR]').execute + result.values[0][0].should == expected_value('[Gender].[F]') + end + + it "should aggregate stored members and a measure" do + result = @olap.from('Sales'). + with_member('[Measures].[CA and OR]'). + as("Aggregate({[Customers].[USA].[CA], [Customers].[USA].[OR]} * {[Measures].[Unit Sales]})"). + columns('[Measures].[CA and OR]').execute + result.values[0].should == expected_value + end + + it "should aggregate stored members with expression" do + result = @olap.from('Sales'). + with_member('[Measures].[CA and OR twice]'). + as("Aggregate({[Customers].[USA].[CA], [Customers].[USA].[OR]}, [Measures].[Unit Sales] * 2)"). + columns('[Measures].[CA and OR twice]').execute + result.values[0].should == expected_value * 2 + end + + it "should aggregate calculated aggregate members" do + result = @olap.from('Sales'). + with_member('[Customers].[CA calculated]').as("Aggregate({[Customers].[USA].[CA]})"). + with_member('[Customers].[OR calculated]').as("Aggregate({[Customers].[USA].[OR]})"). + with_member('[Customers].[CA and OR]').as("Aggregate({[Customers].[CA calculated], [Customers].[OR calculated]})"). + columns('[Measures].[Unit Sales]'). + rows('[Customers].[CA and OR]').execute + result.values[0][0].should == expected_value + end + end + + it "should call evaluator isDirty method" do + result = @olap.from('Sales'). + with_member('[Measures].[is dirty]').as('IsDirty()'). + columns('[Measures].[is dirty]').execute + result.values[0].should == false + end + + it "should support multiple values IN expression" do + lambda do + @olap.from('Sales'). + columns('[Measures].[Unit Sales]'). + where('[Time].[2011].[Q1]', '[Time].[2011].[Q2]'). + execute + end.should_not raise_error + end + + describe "functions with double argument" do + it "should get Abs with decimal measure" do + result = @olap.from('Sales'). + with_member('[Measures].[Abs Store Sales]').as('Abs([Measures].[Store Sales])'). + columns('[Measures].[Store Sales]', '[Measures].[Abs Store Sales]').execute + result.values[0].should == result.values[1] + end + + it "should get Round with decimal measure" do + result = @olap.from('Sales'). + with_member('[Measures].[Round Store Sales]').as('Round([Measures].[Store Sales])'). + columns('[Measures].[Store Sales]', '[Measures].[Round Store Sales]'). + where('[Customers].[USA].[CA]').execute + result.values[0].round.should == result.values[1] + end + end end