Help language development. Donate to The Perl Foundation

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

t/002-validate-presence-on-update.t6
use v6.d;
use lib 'lib';
use ORM::ActiveRecord::Model;
use Test;

class Game is Model {
  submethod BUILD {
    self.validate: 'name', { :presence, on => { :update } }
    self.validate: 'year', { :presence, :numericality }
  }
}

plan 22;

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

my @errors;
my $game;

$game = Game.create({year => 1987});
ok $game.id;
ok $game.name eq '';
ok $game.is-invalid;
nok $game.save;

$game = Game.create({year => 1987});
ok $game.id;
ok $game.name eq '';
ok $game.is-invalid;
nok $game.save;

$game = Game.create({name => Nil, year => 1987});
ok $game.id;
nok $game.name;
ok $game.is-invalid;
nok $game.save;
ok $game.is-invalid;

$game = Game.create({name => 'Frogger', year => 1987});
ok $game.name eq 'Frogger';

$game.name = 'Super Metroid';
ok $game.name eq 'Super Metroid';
ok $game.save;
ok $game.name eq 'Super Metroid';
nok $game.errors.fname;

$game = Game.create({year => 1987});
$game.name = 'Super Metroid';
ok $game.name eq 'Super Metroid';
ok $game.save;
ok $game.name eq 'Super Metroid';
nok $game.errors.name;

Game.destroy-all;