From b9a44b7fc7cbc27de21b01ff0fc735995d4ec76e Mon Sep 17 00:00:00 2001 From: Paul Poulain Date: Mon, 11 Jun 2012 15:45:31 +0200 Subject: [PATCH] DBIx::Class Branch.pm, first try This package, and the test implement CRUD for Branches table. Very basic, but proves we can do many things in a few lines ! http://bugs.koha-community.org/show_bug.cgi?id=8309 --- Koha/DB/Branch.pm | 36 ++++++++++++++++++++++++++ t/db_dependent/lib/KohaTest/Koha/DB/Branch.t | 27 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 Koha/DB/Branch.pm create mode 100755 t/db_dependent/lib/KohaTest/Koha/DB/Branch.t diff --git a/Koha/DB/Branch.pm b/Koha/DB/Branch.pm new file mode 100644 index 0000000..bf3b9d7 --- /dev/null +++ b/Koha/DB/Branch.pm @@ -0,0 +1,36 @@ +package Koha::DB::Branch; + +use Modern::Perl; +use C4::Context; #FIXME = create a Koha package for KOHA_CONF reading + +use Koha::Schema; + +use Moose; + +my $schema = Koha::Schema->connect( + 'dbi:mysql:dbname='.C4::Context->config("database"), + C4::Context->config("user"), + C4::Context->config("pass"), + { AutoCommit => 1 }, + ); + +sub create { + my ($self, $branch) = @_; + $schema->resultset('Branch')->create($branch); +} + +sub read { + my ($self, $branch) = @_; + return $schema->resultset('Branch')->search($branch); +} + +sub update { + my ($self, $branch) = @_; + return $schema->resultset('Branch')->search({branchcode => $branch->{branchcode}})->update($branch); +} + +sub delete { + my ($self,$branch) = @_; + $schema->resultset('Branch')->search($branch)->delete; +} + diff --git a/t/db_dependent/lib/KohaTest/Koha/DB/Branch.t b/t/db_dependent/lib/KohaTest/Koha/DB/Branch.t new file mode 100755 index 0000000..50dd221 --- /dev/null +++ b/t/db_dependent/lib/KohaTest/Koha/DB/Branch.t @@ -0,0 +1,27 @@ +#!/usr/bin/perl + +use v5.10.0; +use Modern::Perl; + +use Test::More tests => 5; + +BEGIN { + use_ok('Koha::DB::Branch', 'Loading Koha::DB::Branch OK'); +} + +# open a handler to the branch +my $branch = Koha::DB::Branch->new(); + +# CREATE +ok($branch->create({'branchcode' => 'TEST','branchname' => 'TEST NAME'})->id eq 'TEST','Creating a branch OK'); + +# READ +ok(my $branch_f = $branch->read({'branchcode' => 'TEST'})->first->branchname eq 'TEST NAME','Reading a branch OK'); + +# UPDATE +$branch->update({'branchcode' => 'TEST','branchname' => 'TEST UPDATED'}); +ok($branch_f = $branch->read({'branchcode' => 'TEST'})->first->branchname eq 'TEST UPDATED','Updating a branch OK'); + +# DELETE +ok($branch->delete({'branchcode' => 'TEST'}),'Deleting a branch OK'); + -- 1.7.9.5