Skip to content
This repository
Browse code

Added testing role and some more bacis DBIC tests.

  • Loading branch information...
commit a858cd6decc29774af0286474bfdc10c528daca1 1 parent 8b1b80a
Dave Cross authored January 29, 2014
20  t/Test/Lystyng/Schema.pm
@@ -2,28 +2,18 @@ package Test::Lystyng::Schema;
2 2
 
3 3
 use base 'Test::Class';
4 4
 use Test::More;
  5
+use Moose;
5 6
 
6 7
 use lib 'lib';
7 8
 
8  
-sub check_connect : Test(2) {
9  
-  my @errors;
10  
-  foreach (qw[LYSTYNG_DB_SERVER LYSTYNG_DB_NAME
11  
-              LYSTYNG_DB_USER LYSTYNG_DB_PASS]) {
12  
-    push @errors, $_ unless defined $ENV{$_};
13  
-  }
  9
+with 'Test::Role::WithSchema';
14 10
 
15  
-  if (@errors) {
16  
-    BAIL_OUT("Missing connection info: @errors");
17  
-  }
  11
+sub check_connect : Tests {
18 12
 
19 13
   use_ok('Lystyng::Schema');
20 14
 
21  
-  my $sch = Lystyng::Schema->connect(
22  
-    "dbi:mysql:hostname=$ENV{LYSTYNG_DB_SERVER};database=$ENV{LYSTYNG_DB_NAME}",
23  
-    $ENV{LYSTYNG_DB_USER}, $ENV{LYSTYNG_DB_PASS}
24  
-  );
25  
-
26  
-  ok($sch);
  15
+  ok(my $sch = shift->schema);
  16
+  isa_ok($sch, 'Lystyng::Schema');
27 17
 }
28 18
 
29 19
 1;
47  t/Test/Lystyng/Schema/Result/User.pm
@@ -4,33 +4,9 @@ use base qw(Test::Class);
4 4
 use Test::More;
5 5
 use Moose;
6 6
 
7  
-use Lystyng::Schema;
8  
-
9  
-has schema => (
10  
-  is      => 'rw',
11  
-  isa     => 'Lystyng::Schema',
12  
-  lazy    => 1,
13  
-  builder => '_build_schema',
14  
-);
15  
-
16  
-sub _build_schema {
17  
-  my @errors;
18  
-  foreach (qw[LYSTYNG_DB_SERVER LYSTYNG_DB_NAME
19  
-              LYSTYNG_DB_USER LYSTYNG_DB_PASS]) {
20  
-    push @errors, $_ unless defined $ENV{$_};
21  
-  }
22  
-
23  
-  if (@errors) {
24  
-    BAIL_OUT("Missing connection info: @errors");
25  
-  }
  7
+with 'Test::Role::WithSchema';
26 8
 
27  
-  return Lystyng::Schema->connect(
28  
-    "dbi:mysql:hostname=$ENV{LYSTYNG_DB_SERVER};database=$ENV{LYSTYNG_DB_NAME}",
29  
-    $ENV{LYSTYNG_DB_USER}, $ENV{LYSTYNG_DB_PASS}
30  
-  );
31  
-}
32  
-
33  
-sub create : Tests {
  9
+sub basic : Tests {
34 10
   my $self = shift;
35 11
 
36 12
   my $user_rs = $self->schema->resultset('User');
@@ -42,6 +18,25 @@ sub create : Tests {
42 18
 
43 19
   ok($user, 'Got a user');
44 20
   isa_ok($user, 'Lystyng::Schema::Result::User');
  21
+
  22
+  my ($user2) = $user_rs->search({
  23
+    username => 'user',
  24
+  });
  25
+
  26
+  ok($user, 'Got another user');
  27
+  isa_ok($user, 'Lystyng::Schema::Result::User');
  28
+
  29
+  foreach (qw[username password email]) {
  30
+    is($user->$_, $user2->$_, "$_ is correct");
  31
+  }
  32
+
  33
+  $user2->password('another password');
  34
+  $user2->update;
  35
+  # re-read from db
  36
+  $user2->discard_changes;
  37
+  is($user2->password, 'another password', 'Updated password is correct');
  38
+
  39
+  $user_rs->delete;
45 40
 }
46 41
 
47 42
 1;
35  t/Test/Role/WithSchema.pm
... ...
@@ -0,0 +1,35 @@
  1
+package Test::Role::WithSchema;
  2
+
  3
+use strict;
  4
+use warnings;
  5
+use 5.010;
  6
+
  7
+use Moose::Role;
  8
+
  9
+use Lystyng::Schema;
  10
+
  11
+has schema => (
  12
+  is      => 'rw',
  13
+  isa     => 'Lystyng::Schema',
  14
+  lazy    => 1,
  15
+  builder => '_build_schema',
  16
+);
  17
+
  18
+sub _build_schema {
  19
+  my @errors;
  20
+  foreach (qw[LYSTYNG_DB_SERVER LYSTYNG_DB_NAME
  21
+              LYSTYNG_DB_USER LYSTYNG_DB_PASS]) {
  22
+    push @errors, $_ unless defined $ENV{$_};
  23
+  }
  24
+
  25
+  if (@errors) {
  26
+    BAIL_OUT("Missing connection info: @errors");
  27
+  }
  28
+
  29
+  return Lystyng::Schema->connect(
  30
+    "dbi:mysql:hostname=$ENV{LYSTYNG_DB_SERVER};database=$ENV{LYSTYNG_DB_NAME}",
  31
+    $ENV{LYSTYNG_DB_USER}, $ENV{LYSTYNG_DB_PASS}
  32
+  );
  33
+}
  34
+
  35
+1;

0 notes on commit a858cd6

Please sign in to comment.
Something went wrong with that request. Please try again.