Help language development. Donate to The Perl Foundation

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

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

class Person is Model {
  submethod BUILD {
    self.validate: 'username', { :presence, exclusion => { in => <admin superuser> } }
  }
}

plan 9;

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

Person.destroy-all;

my @errors;
my Person $person;

$person = Person.build({});
ok $person.is-invalid;
@errors = ['must be present', 'is invalid'];
ok @errors.grep: $person.errors.username[0];
ok @errors.grep: $person.errors.username[1];

$person = Person.build({username => 'admin'});
ok $person.is-invalid;
ok $person.errors.username[0] eq 'is invalid';

$person = Person.build({username => 'superuser'});
ok $person.is-invalid;
ok $person.errors.username[0] eq 'is invalid';

$person = Person.create({username => 'alfred'});
ok $person.is-valid;
nok $person.errors.username;

Person.destroy-all;