use Data::Dumper;
use Storable;
-use constant {
- REL_FRIEND => "friends",
- REL_FREAK => "freaks",
- REL_FAN => "fans",
- REL_FOE => "foes",
- REL_FOF => "fofs",
- REL_EOF => "eofs",
- };
-
-my $fake_ipid = "0123456789abcdef0123456789abcdef";
-my $fake_subnetid = "0123456789abcdef0123456789abcdef";
+use Newslash::Util::Test qw(create_user delete_user
+ create_story delete_story
+ create_comments
+ create_admin_user
+ create_users
+ get_anonymous_user
+ );
my $t = Test::Mojo->new('Newslash::Web');
-my $users = $t->app->model('users');
my $relations = $t->app->model('relations');
-
-subtest 'get relations hash' => sub {
- plan skip_all => "mode is 'test'" if ($t->app->mode eq 'test');
-
- my $people = $relations->hash(4);
- ok($people, "retrive people");
-
- my $info = $users->select(target => 'info', uid => 4);
- ok($info, "retrive from users_info");
-
- my $people_raw = $info->{people};
- my $people_info = Storable::thaw($people_raw);
- ok($people_info, "nfreeze serialized people data");
-
- is_deeply($people, $people_info, "check people from people table and from users_info table");
-};
-
-
-subtest 'manipulate relations' => sub {
- plan skip_all => "mode is not 'test'" if ($t->app->mode ne 'test');
- # at first, create test users.
- my @testers = qw(people01 people02 people03 people04);
- my $test_users = {};
- for my $name (@testers) {
- my $user = $users->create($name, $name . '@example.com');
- ok($user && $user->{error} == 0, "create user $name");
- diag("$name: $user->{uid} - $user->{password}");
- $test_users->{$name} = $users->select(uid => $user->{uid});
- ok($test_users->{$name}, "select user $name");
- $test_users->{$name}->{ipid} = $fake_ipid;
- $test_users->{$name}->{subnetid} = $fake_subnetid;
- }
-
- my ($uid01, $uid02, $uid03, $uid04) = ($test_users->{people01}->{uid},
- $test_users->{people02}->{uid},
- $test_users->{people03}->{uid},
- $test_users->{people04}->{uid});
-
- my $rs = $relations->create($uid01, $uid02, "friend");
- ok($rs, "create relation 1");
-
- $rs = $relations->create($uid02, $uid03, "friend");
- ok($rs, "create relation 2");
-
- $rs = $relations->create($uid02, $uid04, "foe");
- ok($rs, "create relation 3");
-
-
- my $r1 = $relations->hash($uid01);
- is_deeply($r1, {
- REL_FRIEND() => { $uid02 => 1, },
- REL_FOF() => { $uid03 => { $uid02 => 1, }, },
- REL_EOF() => { $uid04 => { $uid02 => 1, }, },
- }, "people 01's relations");
-
- my $r2 = $relations->hash($uid02);
- is_deeply($r2, {
- REL_FAN() => { $uid01 => 1, },
- REL_FRIEND() => { $uid03 => 1, },
- REL_FOE() => { $uid04 => 1, },
- }, "people 02's relations");
-
- my $r3 = $relations->hash($uid03);
- is_deeply($r3, {
- REL_FAN() => { $uid02 => 1, },
- }, "people 03's relations");
-
- my $r4 = $relations->hash($uid04);
- is_deeply($r4, {
- REL_FREAK() => { $uid02 => 1, },
- }, "people 04's relations");
+ok($relations, "get Relations instance");
- # finished. do cleanup...
- # delete test users
- for my $name (@testers) {
- my $rs = $users->hard_delete($test_users->{$name}->{uid});
- ok($rs, "delete user $name");
- }
-};
- done_testing();
+done_testing();
get_anonymous_user
);
-my $t = Test::Mojo->new('Newslash::Web');
-my $test_users;
+use constant {
+ REL_FRIEND => "friends",
+ REL_FREAK => "freaks",
+ REL_FAN => "fans",
+ REL_FOE => "foes",
+ REL_FOF => "fofs",
+ REL_EOF => "eofs",
+ };
+my $t = Test::Mojo->new('Newslash::Web');
+my $users = $t->app->model('users');
my $relations = $t->app->model('relations');
+ok($users, "get Users instance");
ok($relations, "get Relations instance");
+
+my $test_users;
+
# prepare for test
if ($t->app->mode eq 'test') {
# at first, create test users.
- $test_users = create_users($t->app, "reltest", 2);
+ $test_users = create_users($t->app, "reltest", 4);
}
};
+subtest 'get relations hash' => sub {
+ plan skip_all => "mode is 'test'" if ($t->app->mode eq 'test');
+
+ my $people = $relations->hash(4);
+ ok($people, "retrive people");
+
+ my $info = $users->select(target => 'info', uid => 4);
+ ok($info, "retrive from users_info");
+
+ my $people_raw = $info->{people};
+ my $people_info = Storable::thaw($people_raw);
+ ok($people_info, "nfreeze serialized people data");
+
+ is_deeply($people, $people_info, "check people from people table and from users_info table");
+};
+
+
+subtest 'manipulate relations' => sub {
+ plan skip_all => "mode is not 'test'" if ($t->app->mode ne 'test');
+
+ # at first, create test users.
+ my ($uid01, $uid02, $uid03, $uid04) = ($test_users->{reltest1}->{uid},
+ $test_users->{reltest2}->{uid},
+ $test_users->{reltest3}->{uid},
+ $test_users->{reltest4}->{uid});
+
+ my $rs = $relations->create($uid01, $uid02, "friend");
+ ok($rs, "create relation 1");
+
+ $rs = $relations->create($uid02, $uid03, "friend");
+ ok($rs, "create relation 2");
+
+ $rs = $relations->create($uid02, $uid04, "foe");
+ ok($rs, "create relation 3");
+
+
+ my $r1 = $relations->hash($uid01);
+ is_deeply($r1, {
+ REL_FRIEND() => { $uid02 => 1, },
+ REL_FOF() => { $uid03 => { $uid02 => 1, }, },
+ REL_EOF() => { $uid04 => { $uid02 => 1, }, },
+ }, "reltest1's relations");
+
+ my $r2 = $relations->hash($uid02);
+ is_deeply($r2, {
+ REL_FAN() => { $uid01 => 1, },
+ REL_FRIEND() => { $uid03 => 1, },
+ REL_FOE() => { $uid04 => 1, },
+ }, "reltest2's relations");
+
+ my $r3 = $relations->hash($uid03);
+ is_deeply($r3, {
+ REL_FAN() => { $uid02 => 1, },
+ }, "reltest3's relations");
+
+ my $r4 = $relations->hash($uid04);
+ is_deeply($r4, {
+ REL_FREAK() => { $uid02 => 1, },
+ }, "reltest4's relations");
+
+};
+
+
# cleanup
if ($t->app->mode eq 'test') {
for my $u (keys %$test_users) {