#!perl -T
use 5.006;
use strict;
use warnings FATAL => 'all';
use Test::More;

use Mojo::Util::Collection;
use Mojo::Util::Model::User;

my $collection = Mojo::Util::Collection->new(
    items => [
        {
            id         => 1,
            first_name => 'Joe',
            last_name  => 'Doe',
            age        => 19
        },
        {
            id         => 2,
            first_name => 'Jane',
            last_name  => 'Doe',
            age        => 21
        },
        {
            id         => 3,
            first_name => 'John',
            last_name  => 'Doe',
            age        => 22
        },
        {
            id         => 4,
            first_name => 'Jill',
            last_name  => 'Doe',
            age        => 23
        },
    ],
    model => Mojo::Util::Model::User->new
);

my $user = $collection->firstOrNew({ first_name => 'Katy' });

is($user->id, 1);
isa_ok($user, 'Mojo::Util::Model::User');

my $other_collection = Mojo::Util::Collection->new(
    items => [],
    model => Mojo::Util::Model::User->new
);

my $other_user = $other_collection->firstOrNew({ first_name => 'Katy' });

is($other_user->id, undef);
is($other_user->first_name, 'Katy');
isa_ok($other_user, 'Mojo::Util::Model::User');

done_testing();
