Skip to content
Browse files

Added a few tests

  • Loading branch information...
1 parent 62ea68b commit 9b37d9f42f4378f9a24448105a976a88fd800358 @benvanstaveren committed Apr 27, 2011
Showing with 128 additions and 0 deletions.
  1. +80 −0 t/02-functional-helper.t
  2. +48 −0 t/03-functional-nohelper.t
View
80 t/02-functional-helper.t
@@ -0,0 +1,80 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+# Disable IPv6, epoll and kqueue
+BEGIN { $ENV{MOJO_NO_IPV6} = $ENV{MOJO_POLL} = 1 }
+
+use Test::More;
+
+if(!$ENV{TEST_MONGODB}) {
+ plan skip_all => 'Please set the TEST_MONGODB variable to a MongoDB connection string (host:port) in order to test';
+} else {
+ plan tests => 21;
+}
+
+# testing code starts here
+use Mojolicious::Lite;
+use Test::Mojo;
+
+my ($host, $port) = split(/:/, $ENV{TEST_MONGODB});
+$host ||= 'localhost';
+$port ||= 27017;
+
+my $dbname = 'mojolicious_plugin_mongodb_test_' . $$;
+my $dbname2 = 'mojolicious_plugin_mongodb_test_2' . $$;
+
+plugin 'mongodb', {
+ 'host' => $host,
+ 'port' => $port,
+ 'database' => $dbname,
+ 'helper' => 'foo',
+ };
+
+get '/defaultdb' => sub {
+ my $self = shift;
+ $self->render(text => $self->app->defaultdb );
+};
+
+get '/connection' => sub {
+ my $self = shift;
+ $self->render(text => ref($self->app->mongodb_connection));
+};
+
+get '/getdb' => sub {
+ my $self = shift;
+ $self->render(text => $self->foo->name);
+};
+
+get '/getotherdb' => sub {
+ my $self = shift;
+ $self->render(text => $self->foo($dbname2)->name);
+};
+
+get '/db-get-collection/:cname' => sub {
+ my $self = shift;
+ my $cname = $self->stash('cname');
+ $self->render(text => $self->foo->get_collection($cname)->name);
+};
+
+get '/db-coll/:cname' => sub {
+ my $self = shift;
+ my $cname = $self->stash('cname');
+ $self->render(text => $self->coll($cname)->name);
+};
+
+get '/db-coll-full/:cname' => sub {
+ my $self = shift;
+ my $cname = $self->stash('cname');
+ $self->render(text => $self->coll($cname, $dbname2)->full_name);
+};
+
+my $t = Test::Mojo->new;
+
+$t->get_ok('/defaultdb')->status_is(200)->content_is($dbname);
+$t->get_ok('/connection')->status_is(200)->content_is('MongoDB::Connection');
+$t->get_ok('/getdb')->status_is(200)->content_is($dbname);
+$t->get_ok('/getotherdb')->status_is(200)->content_is($dbname2);
+$t->get_ok('/db-get-collection/test1')->status_is(200)->content_is('test1');
+$t->get_ok('/db-coll/test1')->status_is(200)->content_is('test1');
+$t->get_ok('/db-coll-full/test1')->status_is(200)->content_is("$dbname2.test1");
View
48 t/03-functional-nohelper.t
@@ -0,0 +1,48 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+
+# Disable IPv6, epoll and kqueue
+BEGIN { $ENV{MOJO_NO_IPV6} = $ENV{MOJO_POLL} = 1 }
+
+use Test::More;
+use Try::Tiny;
+
+if(!$ENV{TEST_MONGODB}) {
+ plan skip_all => 'Please set the TEST_MONGODB variable to a MongoDB connection string (host:port) in order to test';
+} else {
+ plan tests => 3;
+}
+
+# testing code starts here
+use Mojolicious::Lite;
+use Test::Mojo;
+
+my ($host, $port) = split(/:/, $ENV{TEST_MONGODB});
+$host ||= 'localhost';
+$port ||= 27017;
+
+
+plugin 'mongodb', {
+ 'host' => $host,
+ 'port' => $port,
+ 'database' => 'froofypants_' . $$,
+ 'nohelper' => 1,
+ };
+
+get '/test' => sub {
+ my $self = shift;
+ my $rv = 0;
+
+ try {
+ my $foo = $self->db;
+ } catch {
+ $rv = 1;
+ };
+ $self->render(text => ($rv == 1) ? 'ok' : 'fail');
+};
+
+
+my $t = Test::Mojo->new;
+
+$t->get_ok('/test')->status_is(200)->content_is('ok');

0 comments on commit 9b37d9f

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