making tradition user become a weak ref
[scpubgit/stemmatology.git] / 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 my $user_store = Text::Tradition::Directory->new(
18   'dsn'        => $dsn,
19   'extra_args' => { 'create' => 1 }
20 );
21
22 my $uuid;
23 my $email = 'john@doe.com';
24 {
25   my $scope = $user_store->new_scope;
26
27 ## create user
28   my $new_user = $user_store->add_user(
29     { username => 'fred',
30       password => 'bloggspass'
31     }
32   );
33
34   my $t = Text::Tradition->new(
35     'name'  => 'inline',
36     'input' => 'Tabular',
37     'file'  => 't/data/simple.txt',
38   );
39
40   $uuid = $user_store->save($t);
41   $new_user->add_tradition($t);
42   $new_user->email($email);
43   $user_store->update($new_user);
44 }
45
46 {
47   my $scope = $user_store->new_scope;
48
49   # change attribute in the user object
50   my $user = $user_store->find_user( { username => 'fred' } );
51   $user->email('foo@bar.baz');
52   $user_store->update($user);
53   is(scalar @{$user->traditions}, 1);
54 }
55
56 {
57   my $scope = $user_store->new_scope;
58
59   # refetch tradition
60   my $fetched_t = $user_store->tradition($uuid);
61
62   # assert that the associated user also changed
63   is( $fetched_t->user->email, 'foo@bar.baz' );
64 }
65
66 {
67   my $scope = $user_store->new_scope;
68   my $user = $user_store->find_user( { username => 'fred' } );
69
70   # change the email back to what it was
71   $user->email($email);
72   $user_store->update($user);
73 }
74
75 {
76   my $scope = $user_store->new_scope;
77
78   # refetch tradition
79   my $fetched_t = $user_store->tradition( $uuid );
80
81   # assert that email has actually been reverted
82   is( $fetched_t->user->email, $email );
83 }