[Catalyst-commits] r8375 - in
CatalystX-CRUD/CatalystX-CRUD/trunk/t: . lib/MyApp/Controller
lib/MyApp/Model
karpet at dev.catalyst.perl.org
karpet at dev.catalyst.perl.org
Tue Sep 9 20:14:11 BST 2008
Author: karpet
Date: 2008-09-09 20:14:11 +0100 (Tue, 09 Sep 2008)
New Revision: 8375
Added:
CatalystX-CRUD/CatalystX-CRUD/trunk/t/04-query.t
CatalystX-CRUD/CatalystX-CRUD/trunk/t/lib/MyApp/Controller/Search.pm
CatalystX-CRUD/CatalystX-CRUD/trunk/t/lib/MyApp/Model/FileSearch.pm
Log:
add query tests
Added: CatalystX-CRUD/CatalystX-CRUD/trunk/t/04-query.t
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/t/04-query.t (rev 0)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/t/04-query.t 2008-09-09 19:14:11 UTC (rev 8375)
@@ -0,0 +1,137 @@
+use Test::More tests => 14;
+use strict;
+use lib qw( lib t/lib );
+use_ok('CatalystX::CRUD::Model::File');
+use_ok('CatalystX::CRUD::Object::File');
+
+use Catalyst::Test 'MyApp';
+use Data::Dump qw( dump );
+use HTTP::Request::Common;
+
+ok( my $res = request('/search/search'), "response for /search/search" );
+
+#dump( $response->headers );
+
+is( $res->headers->{status}, '200', "response Ok" );
+
+ok( $res = request('/search/search?file=bar'), "?file=bar" );
+
+#warn $res->content;
+
+is( $res->content, qq/{
+ limit => 50,
+ offset => 0,
+ plain_query => { file => ["bar"] },
+ plain_query_str => "(file='bar')",
+ query => ["file", "bar"],
+ sort_by => "file DESC",
+ sort_order => [{ file => "DESC" }],
+}/, "?file=bar"
+);
+
+ok( $res = request('/search/search?file=bar&content=foo'),
+ "?file=bar&content=foo" );
+
+#warn $res->content;
+
+is( $res->content, qq/{
+ limit => 50,
+ offset => 0,
+ plain_query => { content => ["foo"], file => ["bar"] },
+ plain_query_str => "(file='bar') AND (content='foo')",
+ query => ["AND", ["file", "bar", "content", "foo"]],
+ sort_by => "file DESC",
+ sort_order => [{ file => "DESC" }],
+}/, "?file=bar&content=foo"
+);
+
+ok( $res
+ = request(
+ '/search/search?file=bar&file=foo&content=green&content=red'),
+ '?file=bar&file=foo&content=green&content=red'
+);
+
+#warn $res->content;
+
+is( $res->content, qq/{
+ limit => 50,
+ offset => 0,
+ plain_query => { content => ["green", "red"], file => ["bar", "foo"] },
+ plain_query_str => "(file='bar' OR file='foo') AND (content='green' OR content='red')",
+ query => [
+ "AND",
+ [
+ "OR",
+ ["file", "bar", "file", "foo"],
+ "OR",
+ ["content", "green", "content", "red"],
+ ],
+ ],
+ sort_by => "file DESC",
+ sort_order => [{ file => "DESC" }],
+}/, '?file=bar&file=foo&content=green&content=red'
+);
+
+ok( $res = request(
+ '/search/search?file=bar&file=foo&content=green&content=red&cxc-op=OR'
+ ),
+ '?file=bar&file=foo&content=green&content=red&cxc-op=OR'
+);
+
+#warn $res->content;
+
+is( $res->content, qq/{
+ limit => 50,
+ offset => 0,
+ plain_query => { content => ["green", "red"], file => ["bar", "foo"] },
+ plain_query_str => "(file='bar' OR file='foo') OR (content='green' OR content='red')",
+ query => [
+ "OR",
+ [
+ "OR",
+ ["file", "bar", "file", "foo"],
+ "OR",
+ ["content", "green", "content", "red"],
+ ],
+ ],
+ sort_by => "file DESC",
+ sort_order => [{ file => "DESC" }],
+}/, '?file=bar&file=foo&content=green&content=red&cxc-op=OR'
+);
+
+ok( $res = request(
+ POST(
+ '/search/search',
+ [ 'cxc-query' =>
+ "(file='bar' OR file='foo') OR (content='green' OR content='red')"
+ ]
+ )
+ ),
+ qq/?cxc-query="(file='bar' OR file='foo') OR (content='green' OR content='red')"/
+);
+
+#warn $res->content;
+
+is( $res->content, qq/{
+ limit => 50,
+ offset => 0,
+ plain_query => {
+ "cxc-query" => [
+ "(file='bar' OR file='foo') OR (content='green' OR content='red')",
+ ],
+ },
+ plain_query_str => "(file='bar' OR file='foo') OR (content='green' OR content='red')",
+ query => [
+ "OR",
+ [
+ "OR",
+ ["file", "bar", "file", "foo"],
+ "OR",
+ ["content", "green", "content", "red"],
+ ],
+ ],
+ sort_by => "file DESC",
+ sort_order => [{ file => "DESC" }],
+}/, '?cxc-query=content = green OR red OR file = bar OR foo'
+);
+
Added: CatalystX-CRUD/CatalystX-CRUD/trunk/t/lib/MyApp/Controller/Search.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/t/lib/MyApp/Controller/Search.pm (rev 0)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/t/lib/MyApp/Controller/Search.pm 2008-09-09 19:14:11 UTC (rev 8375)
@@ -0,0 +1,27 @@
+package MyApp::Controller::Search;
+use strict;
+use base qw( CatalystX::CRUD::Test::Controller );
+use Carp;
+use Data::Dump qw( dump );
+use File::Temp;
+use MyApp::Form;
+
+__PACKAGE__->config(
+ primary_key => 'absolute',
+ form_class => 'MyApp::Form',
+ form_fields => [qw( file content )],
+ model_name => 'FileSearch',
+ primary_key => 'file',
+ init_form => 'init_with_file',
+ init_object => 'file_from_form',
+);
+
+sub end : Private {
+ my ( $self, $c ) = @_;
+ $c->log->debug( "resp status = " . $c->res->status ) if $c->debug;
+ delete $c->stash->{query}->{query_obj};
+ $c->res->body( Data::Dump::dump( $c->stash->{query} ) );
+ 1;
+}
+
+1;
Added: CatalystX-CRUD/CatalystX-CRUD/trunk/t/lib/MyApp/Model/FileSearch.pm
===================================================================
--- CatalystX-CRUD/CatalystX-CRUD/trunk/t/lib/MyApp/Model/FileSearch.pm (rev 0)
+++ CatalystX-CRUD/CatalystX-CRUD/trunk/t/lib/MyApp/Model/FileSearch.pm 2008-09-09 19:14:11 UTC (rev 8375)
@@ -0,0 +1,23 @@
+package MyApp::Model::FileSearch;
+use strict;
+use base qw(
+ CatalystX::CRUD::Model::File
+ CatalystX::CRUD::Model::Utils
+);
+use MyApp::File;
+__PACKAGE__->config( object_class => 'MyApp::File' );
+use Class::C3;
+
+sub make_query {
+ my ($self) = @_;
+ my $q = $self->make_sql_query( $self->context->controller->form_fields );
+
+ # we test $q in 04-query.t
+ $self->context->stash( query => $q );
+
+ # but File model expects a sub ref
+ return $self->next::method;
+}
+
+1;
+
More information about the Catalyst-commits
mailing list