All Downloads are FREE. Search and download functionalities are using the official Maven repository.

core.pure.tds.testTdsSchema.pure Maven / Gradle / Ivy

There is a newer version: 4.57.1
Show newest version
// Copyright 2023 Goldman Sachs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import meta::pure::functions::tests::model::*;
import meta::pure::extension::*;
import meta::pure::router::extension::*;
import meta::pure::tds::extensions::*;
import meta::pure::tds::schema::tests::*;
import meta::pure::tests::model::simple::*;

function <> meta::pure::tds::schema::tests::tdsFunc(date:Date[1]):TabularDataSet[1]
{
      Person.all()
         ->project([
               col(p|$p.firstName,'firstName'),
               col(p|$date,'date')
            ])
}

function <> meta::pure::tds::schema::tests::addFullName(objects: meta::pure::tds::TabularDataSet[1]): meta::pure::tds::TabularDataSet[1]
{
  $objects->extend(
    col(r: TDSRow[1]|$r.getString('firstName') + ' ' + $r.getString('lastName'), 'fullName')
  )
}

function <> meta::pure::tds::schema::tests::filter(objects: meta::pure::tds::TabularDataSet[1], name:String[1]): meta::pure::tds::TabularDataSet[1]
{
  $objects->filter(r | $r.getString('fullName') == $name);
}

function <> meta::pure::tds::schema::tests::addFullNameAndFilter(objects: meta::pure::tds::TabularDataSet[1], name:String[1]): meta::pure::tds::TabularDataSet[1]
{
  $objects->addFullName()->filter(r | $r.getString('fullName') == $name);
}

function  <> meta::pure::tds::schema::tests::resolveSchemaTest() : Boolean[1]
{
   assertSchemaRoundTripEquality({|
      Person.all()
         ->project(
               col(p|$p.firstName,'firstName')
            )
   });

   assertSchemaRoundTripEquality({| meta::pure::tds::schema::tests::tdsFunc(%2023-01-01)});

   assertSchemaRoundTripEquality({|
      Person.all()
         ->project([
               col(p|$p.firstName,'firstName') ,
               col(p|$p.lastName,'lastName')
            ])
         ->meta::pure::tds::schema::tests::addFullName()
   });

   assertSchemaRoundTripEquality({|
      Person.all()
         ->project([
               col(p|$p.firstName,'firstName') ,
               col(p|$p.lastName,'lastName')
            ])
         ->meta::pure::tds::schema::tests::addFullNameAndFilter('john doe')->filter('john doe')
   });   

   assertSchemaRoundTripEquality(
    [
      ^TDSColumn(name='firstName', offset = 0, type = String),
      ^TDSColumn(name='date', offset = 1, type = Date)
    ],
    {date:Date[1] | meta::pure::tds::schema::tests::tdsFunc($date)});

   assertSchemaRoundTripEquality({|
    let const = 1;
      Person.all()
         ->project([
               col(p|$const, 'const'),
               col(p|$p.firstName,'firstName')
            ]);
   });

   assertSchemaRoundTripEquality({|
      Person.all()
         ->project([
               col(p|$p.firstName,'firstName') ,
               col(p|$p.lastName,'lastName')
            ])
   });

   assertSchemaRoundTripEquality({|
      Person.all()
         ->project([
               col(p|$p.firstName,'firstName') ,
               col(p|$p.lastName,'lastName')
            ])
         ->meta::pure::tds::groupBy(['firstName'], [agg('count',r|$r.getString('firstName'), c|$c->size())])
   });

   assertSchemaRoundTripEquality({|
      Person.all()->projectWithColumnSubset([
      col(x|$x.firstName, 'first'),
      col(x|$x.lastName, 'last'),
      col(x|$x.age, 'age'),
      col(x|$x.firm.legalName, 'firmName'),
      col(x|$x.firm->toOne().isFirmXGroup(), 'isFirmX')
   ], ['first', 'last', 'firmName'])});

   assertSchemaRoundTripEquality({|
      Person.all()
         ->project([
               col(p|$p.firstName,'firstName') ,
               col(p|$p.lastName + ' you','lastName')
            ])
         ->limit(10)
         ->filter(row|$row.getString('firstName') != 'hello')
   });

   assertSchemaRoundTripEquality({|
      Person.all()
         ->project([
               col(p|$p.firstName,'firstName') ,
               col(p|$p.lastName + ' you','lastName'),
               col(p|$p.locations.type, 'locationsType'),
               col(p|$p.locations.type->first(), 'firstLocationType')
            ])
         ->limit(10)
         ->filter(row|$row.getString('firstName') != 'hello')
   });

   assertSchemaRoundTripEquality({|
      Firm.all()->project([f|$f.employees.age->average()], ['a'])
   });

   assertSchemaRoundTripEquality({|
         Firm.all()->project([
               f|$f.employees.lastName->count(),
               f|$f.employees.age->average()
               ],
               ['b','a'])
   });

   assertSchemaRoundTripEquality({|
         Firm.all()->project([
               col(f|$f.legalName,'a'),
               col(f|$f.employees->map(e|$e.lastName),'b'),
               col(f|$f.employees->map(e|2 + $e.locations.place->count()),'c')
               ])
      });

    assertSchemaRoundTripEquality({|
         Firm.all()->project([
               col(f|$f.employees->map(e|2 + $e.locations.place->count()),'c')
               ])
                ->extend(col(row:TDSRow[1] | $row.getInteger('c')->toString(), 'c2'))
                ->restrict('c2')
                ->extend(col(row:TDSRow[1]| $row.getString('c2'), 'c'))
                ->restrict('c')
   });

   assertSchemaRoundTripEquality({|
         Person.all()
            ->project(col(p|$p.age, 'age'))
            ->sort(asc('age'))
            ->limit(3)
            ->filter(r|$r.getInteger('age') > 25)
      });

   assertSchemaRoundTripEquality({|
         Person.all()
            ->project(col(p|$p.age->first(), 'age'))
            ->sort(asc('age'))
            ->limit(3)
            ->filter(r|$r.getInteger('age')->toOne() > 25)
      });

   assertSchemaRoundTripEquality({|
         Person.all()
            ->project(col(p|$p.age, 'age'))
            ->sort(asc('age'))
            ->limit(3)
            ->filter(r|$r.getInteger('age') > 25)
      });

   assertSchemaRoundTripEquality({|
         Address.all()
            ->project([a|$a.name, a|$a.type],['name', 'type'])
            ->extend(col(row:TDSRow[1]| DurationUnit.DAYS, 'typeduplicated'))
      });

    assertSchemaRoundTripEquality({|
         Address.all()
            ->project([a|$a.name, a|$a.type],['name', 'type'])
            ->extend(^BasicColumnSpecification(name='typeduplicated', func={row:TDSRow[1]|DurationUnit.DAYS}))
      });

    assertSchemaRoundTripEquality({|
         Address.all()
            ->project([a|$a.name, a|$a.type],['name', 'type'])
            ->extend(^BasicColumnSpecification(name='typeduplicated', func={row:TDSRow[1]|DurationUnit.DAYS}))
            ->restrict(['typeduplicated', 'name'])
      });

   assertSchemaRoundTripEquality({|
      Person.all()
         ->project([
               col(p|'hello','constString'),
               col(p|%2018-12-12,'constDate'),
               col(p|true,'constBoolean'),
               col(p|1,'constInteger'),
               col(p|1.5,'constFloat')
            ])
   });

   assertSchemaRoundTripEquality(
      [
         ^TDSColumn(name = 'constString', offset= 0, type = String),
         ^TDSColumn(name = 'constDate', offset= 1, type = StrictDate)
      ],
      {|
      Person.all()
         ->project([
               col(p|'hello','constString'),
               col(p|%2018-12-12,'constDate')
            ])
         ->distinct()
   });


   assertSchemaRoundTripEquality({|
         Trade.all()
            ->groupBy([x|$x.date->adjust(0, DurationUnit.DAYS)],
               [ agg(x | $x.quantity, y | $y->sum()), agg(x | $x.quantity, y | $y->sum())],
               ['tradeDate', 'quantityA', 'quantityB']
               )
   });

   assertSchemaRoundTripEquality({|
         Person.all()
            ->project([
               col(p|$p.age->first(), 'age1'),
               col(p|$p.age->first(), 'age2')
               ])
            ->renameColumns(pair('age1', 'ageOne'))
      });

   assertSchemaRoundTripEquality(
      [
         ^TDSColumn(name = 'constString', offset= 0, type = String),
         ^TDSColumn(name = 'constDate', offset= 1, type = StrictDate),
         ^TDSColumn(name = 'rowNumber', offset= 2, type = Integer)
      ],
      {|
      Person.all()
         ->project([
               col(p|'hello','constString'),
               col(p|%2018-12-12,'constDate')
            ])
         ->olapGroupBy('constString',asc('constString'),y|$y->meta::pure::functions::math::olap::rowNumber(),'rowNumber')
   });

   let t = ^TabularDataSet(rows = [], columns = ^TDSColumn(name = 'aCol', offset= 0, type = String));
   assertSchemaRoundTripEquality(
      [
         ^TDSColumn(name = 'aCol', offset= 0, type = String)
      ],
      {|
         $t
      });

   assertSchemaRoundTripEquality({|
         {| Person.all()
            ->project([col(p|$p.age->first(), 'age1')])
         }->eval()});

   assertSchemaRoundTripEquality({|
         {a:String[1] | Person.all()
            ->project([col(p|$p.age->first(), 'age1')])
         }->eval('a')});

   assertSchemaRoundTripEquality({|
         {a:String[1], b:String[1] | Person.all()
            ->project([col(p|$p.age->first(), 'age1')])
         }->eval('a', 'b')});

   assertSchemaRoundTripEquality({|
         {a:String[1], b:String[1], c:String[1] | Person.all()
            ->project([col(p|$p.age->first(), 'age1')])
         }->eval('a', 'b', 'c')});

   assertSchemaRoundTripEquality({|
         {a:String[1], b:String[1], c:String[1], d:String[1] | Person.all()
            ->project([col(p|$p.age->first(), 'age1')])
         }->eval('a', 'b', 'c', 'd')});

   assertSchemaRoundTripEquality({|
         {a:String[1], b:String[1], c:String[1], d:String[1], e:String[1] | Person.all()
            ->project([col(p|$p.age->first(), 'age1')])
         }->eval('a', 'b', 'c', 'd', 'e')});

   assertSchemaRoundTripEquality({|
         {a:String[1], b:String[1], c:String[1], d:String[1], e:String[1], f:String[1] | Person.all()
            ->project([col(p|$p.age->first(), 'age1')])
         }->eval('a', 'b', 'c', 'd', 'e', 'f')});

   assertSchemaRoundTripEquality({|
         {a:String[1], b:String[1], c:String[1], d:String[1], e:String[1], f:String[1], g:String[1] | Person.all()
            ->project([col(p|$p.age->first(), 'age1')])
         }->eval('a', 'b', 'c', 'd', 'e', 'f', 'g')});

   assertSchemaRoundTripEquality(
         ^TDSColumn(name='age1', offset = 0, type = Integer),
         {a:String[1], b:String[1], c:String[1], d:String[1], e:String[1], f:String[1], g:String[1] | Person.all()
            ->project([col(p|$p.age->first(), 'age1')])
         });
}

function meta::pure::tds::schema::tests::assertSchemaRoundTripEquality(query : FunctionDefinition<{->TabularDataSet[1]}>[1]) : Boolean[1]
{
   assertSchemaRoundTripEquality($query, defaultExtensions());
}

function meta::pure::tds::schema::tests::assertSchemaRoundTripEquality(query : FunctionDefinition<{->TabularDataSet[1]}>[1], extensions:Extension[*]) : Boolean[1]
{
   let expected = $query->eval().columns;//->map(c|if($c.offset->isEmpty(), |^$c(offset = 0), | $c));
   assertSchemaRoundTripEquality($expected, $query, $extensions);
}

function meta::pure::tds::schema::tests::assertSchemaRoundTripEquality(expected:TDSColumn[*], query : FunctionDefinition[1]) : Boolean[1]
{
   assertSchemaRoundTripEquality($expected, $query, defaultExtensions());
}

function meta::pure::tds::schema::tests::assertSchemaRoundTripEquality(expected:TDSColumn[*], query : FunctionDefinition[1], extensions:Extension[*]) : Boolean[1]
{
   let actual = $query->meta::pure::tds::schema::resolveSchema($extensions);

   assertSchemaEquality($expected, $actual);
}

function meta::pure::tds::schema::tests::assertSchemaEquality(expected:TDSColumn[*], actual : TDSColumn[*]) : Boolean[1]
{
   let areEqual = ($expected->size() == $actual->size())
      && ($expected->zip($actual)->forAll(p|

            let theProperties = TDSColumn->getAllTypeGeneralisations()->remove(Any)->map(t|$t->match([c:Class[1]|$c, a:Any[*]|[]])).properties->removeAll(Class.properties);

            $theProperties->forAll(property|
               $property->eval($p.first) == $property->eval($p.second);
               );
            ));

   if ($areEqual,
       | $areEqual,
       |
         println('expected:');
         println($expected->map(c|$c->simpleToString())->joinStrings('[', ',', ']'));
         println('actual:');
         println($actual->map(c|$c->simpleToString())->joinStrings('[', ',', ']'));

         assertEquals($expected, $actual);
       );
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy