rename restrict by user
[dbsrgits/DBIx-Class-Schema-RestrictWithObject.git] / t / 05restrict.t
CommitLineData
772c89c1 1use strict;
2use warnings;
3use Test::More;
4
5use Scalar::Util;
6
7plan (tests => 17);
8
9use lib qw(t/lib);
10
11use RestrictByUserTest;
12my $schema = RestrictByUserTest->init_schema;
13ok($schema, "Connected successfully");
14
15my $user1 = $schema->resultset('Users')->create({name => 'user1'});
16my $user2 = $schema->resultset('Users')->create({name => 'user2'});
17ok(ref $user1 && ref $user2, "Successfully created mock users");
18
19ok($user1->notes->create({name => 'note 1-1'}), "Successfully created 1-1 note");
20ok($user1->notes->create({name => 'note 1-2'}), "Successfully created 1-2 note");
21
22ok($user2->notes->create({name => 'note 2-1'}), "Successfully created 2-1 note");
23ok($user2->notes->create({name => 'note 2-2'}), "Successfully created 2-2 note");
24ok($user2->notes->create({name => 'note 2-3'}), "Successfully created 2-3 note");
25ok($user2->notes->create({name => 'note 2-4'}), "Successfully created 2-4 note");
26
27my $u1_schema = $schema->restrict_by_user($user1);
28my $u2_schema = $schema->restrict_by_user($user2, "MY");
29my $u3_schema = $schema->restrict_by_user($user2, "BUNK");
30
31is($u1_schema->user->id, $user1->id, "Correct restriction for user 1");
32is($u2_schema->user->id, $user2->id, "Correct restriction for user 2");
33is($u2_schema->restricted_prefix, "MY", "Correct prefix for user 2");
34
35ok(Scalar::Util::refaddr($u1_schema) ne Scalar::Util::refaddr($u2_schema),
36 "Successful clones");
37
38is($schema->resultset('Notes')->count, 6, 'Correct un resticted count');
39is($u1_schema->resultset('Notes')->count, 2, 'Correct resticted count');
40is($u2_schema->resultset('Notes')->count, 4, 'Correct resticted count using prefix');
41is($u2_schema->resultset('Notes')->count, 4,
42 'Correct resticted count using prefix and fallback');
43
44is($u2_schema->resultset('Users')->count, 2, 'Unrestricted resultsets work');
45
46
471;