Release a new CPAN version of Analysis
[scpubgit/stemmatology.git] / persistence / t / text_tradition_user_collapse.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More 'no_plan';
7 use File::Temp;
8 use Text::Tradition;
9
10 use_ok('Text::Tradition::Directory');
11
12 my $fh   = File::Temp->new();
13 my $file = $fh->filename;
14 $fh->close;
15 my $dsn = "dbi:SQLite:dbname=$file";
16
17
18 my $uuid;
19 my $email = 'john@doe.com';
20 {
21   my $user_store = Text::Tradition::Directory->new(
22     'dsn'        => $dsn,
23     'extra_args' => { 'create' => 1 }
24   );
25
26   my $scope = $user_store->new_scope;
27
28 ## create user
29   my $new_user = $user_store->add_user(
30     { username => 'fred',
31       password => 'bloggspass'
32     }
33   );
34
35   my $t = Text::Tradition->new(
36     'name'  => 'inline',
37     'input' => 'Tabular',
38     'file'  => 't/data/simple.txt',
39   );
40
41   $uuid = $user_store->save($t);
42   $new_user->add_tradition($t);
43   $new_user->email($email);
44   ok( $t->user, 'sets user' );
45   $user_store->update($new_user);
46   $user_store->save($t);
47 }
48
49 {
50   my $user_store = Text::Tradition::Directory->new(
51     'dsn'        => $dsn,
52     'extra_args' => { 'create' => 1 }
53   );
54   my $scope = $user_store->new_scope;
55
56   # change attribute in the user object
57   my $user = $user_store->find_user( { username => 'fred' } );
58   $user->email('foo@bar.baz');
59   $user_store->update($user);
60   is( scalar @{ $user->traditions }, 1 );
61   ok( $user->traditions->[0]->user,
62     'reinflated tradition object from user points to user' );
63 }
64
65 {
66   my $user_store = Text::Tradition::Directory->new(
67     'dsn'        => $dsn,
68     'extra_args' => { 'create' => 1 }
69   );
70   my $scope = $user_store->new_scope;
71
72   # refetch tradition
73   my $fetched_t = $user_store->tradition($uuid);
74
75   # assert that the associated user also changed
76   is( $fetched_t->user->email, 'foo@bar.baz' );
77 }
78
79 {
80   my $user_store = Text::Tradition::Directory->new(
81     'dsn'        => $dsn,
82     'extra_args' => { 'create' => 1 }
83   );
84   my $scope = $user_store->new_scope;
85   my $user = $user_store->find_user( { username => 'fred' } );
86
87   # change the email back to what it was
88   $user->email($email);
89   $user_store->update($user);
90 }
91
92 {
93   my $user_store = Text::Tradition::Directory->new(
94     'dsn'        => $dsn,
95     'extra_args' => { 'create' => 1 }
96   );
97   my $scope = $user_store->new_scope;
98
99   # refetch tradition
100   my $fetched_t = $user_store->tradition($uuid);
101
102   # assert that email has actually been reverted
103   is( $fetched_t->user->email, $email );
104 }