Help language development. Donate to The Perl Foundation

ORM::ActiveRecord cpan:GDONALD last updated on 2019-10-24

t/001-basic.t6
use v6.d;
use lib 'lib';
use ORM::ActiveRecord::Model;
use Test;

class Page {...} # stub

class User is Model {
  submethod BUILD {
    self.has-many: pages => class => Page;

    self.validate: 'fname', { :presence, length => { min => 4, max => 32 } }
    self.validate: 'lname', { :presence, length => { min => 4, max => 32 } }
  }

  method fullname {
    self.fname ~ ' ' ~ self.lname;
  }
}

class Page is Model {
  submethod BUILD {
    self.belongs-to: user => class => User;

    self.validate: 'name', { :presence, length => { min => 4, max => 32 } }
  }
}

plan 5;

%*ENV<DISABLE-SQL-LOG> = True;

my $user = User.create({fname => 'Greg', lname => 'Donald'});
ok $user.id;

Page.create({:$user, name => 'Raku'});

my $page = $user.pages.first;
ok $page.name eq 'Raku';
ok $page.user.fullname eq 'Greg Donald';

my $alfred = User.create({fname => 'Alfred E.', lname => 'Neuman'});
ok $alfred.id;

$page.update({user => $alfred});
ok $page.user.fullname eq 'Alfred E. Neuman';

Page.destroy-all;
User.destroy-all;