DBIx-Class-Valiant

 view release on metacpan or  search on metacpan

t/dbic/basic.t  view on Meta::CPAN

  # Basic create test.
  ok my $person = Schema
    ->resultset('Person')
    ->create({
      __context => 'registration',
      username => 'jjn',
      first_name => 'john',
      last_name => 'napiorkowski',
      password => 'abc123',
    }), 'created fixture';

  ok $person->invalid, 'attempted record invalid';
  ok !$person->in_storage, 'record was not saved';

  is_deeply +{$person->errors->to_hash(full_messages=>1)}, +{
    password => [
      "Password is too short (minimum is 8 characters)",
    ],
    password_confirmation => [
      "Password Confirmation doesn't match 'Password'",
    ],
  }, 'Got expected errors';
}

{
  # Basic update test.
  ok my $person = Schema
    ->resultset('Person')
    ->create({
      __context => ['registration'],
      username => 'jjn',
      first_name => 'john',
      last_name => 'napiorkowski',
      password => 'abc123aaaaaa',
      password_confirmation => 'abc123aaaaaa',
    }), 'created fixture';

  ok $person->valid, 'attempted record valid';
  ok $person->in_storage, 'record was saved';

  $person->password('aaa');
  $person->update({last_name=>'1', __context => 'registration'});

  ok $person->invalid, 'attempted record invalid with context';

  is_deeply +{$person->errors->to_hash(full_messages=>1)}, +{
    last_name => [
      "Last Name is too short (minimum is 2 characters)",
    ],
    password => [
      "Password is too short (minimum is 8 characters)",
    ],
    password_confirmation => [
      "Password Confirmation doesn't match 'Password'",
    ],
  }, 'Got expected errors';

  $person->discard_changes;
  ok $person->password eq 'abc123aaaaaa', 'original not altered';

  # Make sure real updates are not blocked
  $person->password('890xyzgreen59');
  $person->password_confirmation('890xyzgreen59');

  $person->update({ __context => 'registration'});

  ok $person->valid, 'attempted record valid';
  ok $person->in_storage, 'saved';

  # This is maybe a TODO since DBIC won't pass the non field confirmation via
  # update args
  #$person->password('890xyzgreen59123');
  #$person->update({
  #    password_confirmation => '890xyzgreen59123', 
  #    __context => 'registration'
  #});

  #ok $person->valid, 'attempted record valid';
  #ok $person->in_storage, 'saved';
}

{
  # Basic multicreate test. (might have /has one)
  ok my $person = Schema
    ->resultset('Person')
    ->create({
      __context => ['registration','profile'],
      username => 'jjn2',
      first_name => 'john',
      last_name => 'napiorkowski',
      password => 'abc123',
      password_confirmation => 'abc123',
      profile => {
        zip => "78621",
        city => 'Elgin',
      },
      credit_cards => [
        {card_number=>'asdasd', expiration=>'ddw'},
      ],
    }), 'created fixture';

  is $person->model_name->param_key, 'person';
  is $person->profile->model_name->param_key, 'profile';

  ok $person->invalid, 'attempted record invalid multi context';
  ok !$person->in_storage, 'record was not saved';

  is_deeply +{$person->errors->to_hash(full_messages=>1)}, +{
    credit_cards => [
      "Credit Cards has too few rows (minimum is 2)",
      "Credit Cards Are Invalid",
    ],
    "credit_cards[0].card_number" => [
      "Credit Cards Card Number is too short (minimum is 13 characters)",
      "Credit Cards Card Number does not look like a credit card",
    ],
    "credit_cards[0].expiration" => [
      "Credit Cards Expiration does not look like a datetime value",
    ],
    password => [
      "Password is too short (minimum is 8 characters)",



( run in 0.604 second using v1.01-cache-2.11-cpan-800906f7e73 )