Help language development. Donate to The Perl Foundation

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

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

class User is Model {
  submethod BUILD {
    self.validate: 'fname', { :presence, length => { min => 4, max => 32 } }
    self.validate: 'lname', { :presence, length => { min => 4, max => 32 } }
  }
}

plan 17;

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

my @errors;
my User $user;

$user = User.build;
ok $user.is-invalid;
nok $user.is-valid;
nok $user.id;
@errors = ['at least 4 characters required', 'must be present'];
ok @errors.grep: $user.errors.fname[0];
ok @errors.grep: $user.errors.fname[1];

$user = User.build({});
ok $user.is-invalid;
nok $user.is-valid;
nok $user.id;
ok @errors.grep: $user.errors.fname[0];
ok @errors.grep: $user.errors.fname[1];

$user = User.build({fname => 'x' x 33});
ok $user.is-invalid;
nok $user.is-valid;
nok $user.id;
ok $user.errors.fname[0] eq 'only 32 characters allowed';

$user = User.build({fname => 'Greg', lname => 'Donald'});
nok $user.id;
nok $user.is-invalid;
ok $user.is-valid;

User.destroy-all;