Allow CDBI objects to be accessed like hashes as people tend to do for
[dbsrgits/DBIx-Class.git] / t / cdbi-t / columns_as_hashes.t
diff --git a/t/cdbi-t/columns_as_hashes.t b/t/cdbi-t/columns_as_hashes.t
new file mode 100644 (file)
index 0000000..8b84337
--- /dev/null
@@ -0,0 +1,39 @@
+#!/usr/bin/perl -w
+
+use strict;
+use Test::More;
+use Test::Warn;
+
+BEGIN {
+  eval "use DBIx::Class::CDBICompat;";
+  plan $@ ? (skip_all => "Class::Trigger and DBIx::ContextualFetch required: $@")
+          : (tests=> 6);
+}
+
+use lib 't/testlib';
+use Film;
+
+my $waves = Film->insert({
+    Title     => "Breaking the Waves",
+    Director  => 'Lars von Trier',
+    Rating    => 'R'
+});
+
+warnings_like {
+    is $waves->{title}, $waves->Title, "columns can be accessed as hashes";
+} qr{^Column 'title' of 'Film/$waves' was accessed as a hash at .*$};
+
+$waves->Rating("G");
+
+warnings_like {
+    is $waves->{rating}, "G", "updating via the accessor updates the hash";
+} qr{^Column 'rating' of 'Film/$waves' was accessed as a hash .*$};
+
+$waves->{rating} = "PG";
+
+warnings_like {
+    $waves->update;
+} qr{^Column 'rating' of 'Film/$waves' was updated as a hash .*$};
+
+my @films = Film->search( Rating => "PG", Title => "Breaking the Waves" );
+is @films, 1, "column updated as hash was saved";