3 use lib 'examples'; # Where BeerDB should live
5 $ENV{BEERDB_DEBUG} = 2;
7 eval { require BeerDB };
8 Test::More->import( skip_all =>
9 "SQLite not working or BeerDB module could not be loaded: $@"
15 use Maypole::CLI qw(BeerDB);
16 use Maypole::Constants;
17 $ENV{MAYPOLE_TEMPLATES} = "t/templates";
19 isa_ok( (bless {},"BeerDB") , "Maypole");
23 # Test create missing required
24 like(BeerDB->call_url("http://localhost/beerdb/brewery/do_edit?name=&url=www.sammysmiths.com¬es=Healthy Brew"), qr/name' => 'This field is required/, "Required fields necessary to create ");
26 # Test create with all required
27 like(BeerDB->call_url("http://localhost/beerdb/brewery/do_edit?name=Samuel Smiths&url=www.sammysmiths.com¬es=Healthy Brew"), qr/^# view/, "Created a brewery");
29 ($brewery,@other) = BeerDB::Brewery->search(name=>'Samuel Smiths');
33 skip "Could not create and retrieve Brewery", 8 unless $brewery;
34 like(eval {$brewery->name}, qr/Samuel Smiths/, "Retrieved Brewery, $brewery, we just created");
36 #-------- Test updating printable fields ------------------
38 # TEST clearing out required printable column
39 like(BeerDB->call_url("http://localhost/beerdb/brewery/do_edit/".$brewery->id."?name="), qr/name' => 'This field is required/, "Required printable field can not be cleared on update");
41 # Test cgi update errors hanging around from last request
42 unlike(BeerDB->call_url("http://localhost/beerdb/brewery/do_edit/".$brewery->id), qr/name' => 'This field is required/, "cgi_update_errors did not persist");
44 # Test update no columns
45 like(BeerDB->call_url("http://localhost/beerdb/brewery/do_edit/".$brewery->id), qr/^# view/, "Updated no columns");
47 # Test only updating one non required column
48 like(BeerDB->call_url("http://localhost/beerdb/brewery/do_edit/".$brewery->id."?notes="), qr/^# view/, "Updated a single non required column");
50 # TEST empty input for non required printable
51 like(BeerDB->call_url("http://localhost/beerdb/brewery/do_edit/".$brewery->id."?notes=&name=Sammy Smiths"), qr/^# view/, "Updated brewery" );
53 # TEST update actually cleared out a printable field
54 $val = $brewery->notes ;
55 if ($val eq '') { $val = undef };
56 is($val, undef, "Verified non required printable field was cleared");
58 # TEST update did not change a field not in parameter list
59 is($brewery->url, 'www.sammysmiths.com', "A field not in parameter list is not updated.");
62 #----------------- Test other types of fields --------------
64 $style = BeerDB::Style->insert({name => 'Stout', notes => 'Rich, dark, creamy, mmmmmm.'});
66 # TEST create with integer, date, printable fields
67 like(BeerDB->call_url("http://localhost/beerdb/beer/do_edit?name=Oatmeal Stout&brewery=".$brewery->id."&style=".$style->id."&score=5¬es=Healthy Brew&price=5.00&tasted=2000-12-01"), qr/^# view/, "Created a beer with date, integer and printable fields");
69 ($beer, @other) = BeerDB::Beer->search(name=>'Oatmeal Stout');
72 skip "Could not create and retrieve Beer", 7 unless $beer;
74 # TEST wiping out an integer field
75 like(BeerDB->call_url("http://localhost/beerdb/beer/do_edit/".$beer->id."?name=Oatmeal Stout&brewery=".$brewery->id."&style=".$style->id."&score=¬es=Healthy Brew&price=5.00"), qr/^# view/, "Updated a beer");
77 # TEST update actually cleared out a the integer field
79 if ($val eq '') { $val = undef };
80 is($val, undef, "Verified non required integer field was cleared");
83 # TEST invalid integer field
84 like(BeerDB->call_url("http://localhost/beerdb/beer/do_edit/".$beer->id."?name=Oatmeal Stout&brewery=".$brewery->id."&style=Stout&price=5.00"), qr/style' => 'Please provide a valid value/, "Integer field invalid");
86 # TEST update with empty date field
87 like(BeerDB->call_url("http://localhost/beerdb/beer/do_edit/".$beer->id."?name=Oatmeal Stout&brewery=".$brewery->id."&style=".$style->id."&tasted=¬es=Healthy Brew&price=5.00"), qr/^# view/, "Updated a beer");
89 # TEST update actually cleared out a date field
90 $tasted = $beer->tasted ;
91 if ($tasted eq '') { $tasted = undef };
92 is($tasted, undef, "Verified non required date field was cleared.");
95 like(BeerDB->call_url("http://localhost/beerdb/beer/do_edit/".$beer->id."?name=Oatmeal Stout&brewery=".$brewery->id."&style=".$style->id."&tasted=baddate¬es=Healthy Brew&price=5.00"), qr/tasted' => 'Please provide a valid value/, "Date field invalid");
97 # TEST negative value allowed for required field
98 like(BeerDB->call_url("http://localhost/beerdb/beer/do_edit/".$beer->id."?name=Oatmeal Stout&brewery=".$brewery->id."&price=-5.00"), qr/^# view/, "Negative values allowed for required field");
100 # TEST negative value actually got stored
101 like($beer->price, qr/-5(\.00)?/, "Negative value for required field stored in database")
104 $beer_id = $beer->id;
108 $beer = BeerDB::Beer->retrieve($beer_id);
109 is($beer, undef, "Deleted Beer");