Help language development. Donate to The Perl Foundation

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

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

class Book is Model {
  submethod BUILD {
    self.validate: 'title', { :presence }
    self.validate: 'pages', { :presence, numericality => { lt => 400 } }
    self.validate: 'sentences', { :presence, numericality => { gt => 1000 } }
    self.validate: 'words', { :presence, numericality => { in => 2000..5000 } }
    self.validate: 'periods', { :presence, numericality => { gte => 1000 } }
    self.validate: 'commas', { :presence, numericality => { lte => 200 } }
  }
}

plan 19;

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

my Book $book;

$book = Book.create({title => 'Book Title', pages => 399, sentences => 1001, words => 2000, periods => 1000, commas => 200});
ok $book.is-valid;

$book = Book.build({title => 'Book Title', pages => 399, sentences => 1001, words => 2000, periods => 1000, commas => 200});
ok $book.is-valid;

$book.pages = 400;
ok $book.is-invalid;
ok $book.errors.pages[0] eq 'less than 400 required';

$book = Book.build({title => 'Book Title', pages => 399, sentences => 1001, words => 2000, periods => 1000, commas => 200});
ok $book.is-valid;

$book.sentences = 1000;
ok $book.is-invalid;
ok $book.errors.sentences[0] eq 'more than 1000 required';

$book = Book.build({title => 'Book Title', pages => 399, sentences => 1001, words => 2000, periods => 1000, commas => 200});
ok $book.is-valid;

$book.words = 1999;
ok $book.is-invalid;
ok $book.errors.words[0] eq '2000 to 5000 required';

$book = Book.build({title => 'Book Title', pages => 399, sentences => 1001, words => 2000, periods => 1000, commas => 200});
ok $book.is-valid;

$book.words = 5001;
ok $book.is-invalid;
ok $book.errors.words[0] eq '2000 to 5000 required';

$book = Book.build({title => 'Book Title', pages => 399, sentences => 1001, words => 2000, periods => 1000, commas => 200});
ok $book.is-valid;

$book.periods = 999;
ok $book.is-invalid;
ok $book.errors.periods[0] eq '1000 or more required';

$book = Book.build({title => 'Book Title', pages => 399, sentences => 1001, words => 2000, periods => 1000, commas => 200});
ok $book.is-valid;

$book.commas = 201;
ok $book.is-invalid;
ok $book.errors.commas[0] eq '200 or less required';

Book.destroy-all;