]> git.decadent.org.uk Git - maypole.git/blob - t/crud.t
tested and it seems to work
[maypole.git] / t / crud.t
1 #!/usr/bin/perl -w
2 use Test::More;
3 use lib 'ex'; # Where BeerDB should live
4 BEGIN {
5     $ENV{BEERDB_DEBUG} = 2;
6
7     eval { require BeerDB };
8     Test::More->import( skip_all =>
9         "SQLite not working or BeerDB module could not be loaded: $@"
10     ) if $@;
11
12     plan tests =>21;
13     
14 }
15 use Maypole::CLI qw(BeerDB);
16 use Maypole::Constants;
17 $ENV{MAYPOLE_TEMPLATES} = "t/templates";
18
19 isa_ok( (bless {},"BeerDB") , "Maypole");
20
21
22
23 # Test create missing required 
24 like(BeerDB->call_url("http://localhost/beerdb/brewery/do_edit?name=&url=www.sammysmiths.com&notes=Healthy Brew"), qr/name' => 'This field is required/, "Required fields necessary to create ");
25
26 # Test create with all  required
27 like(BeerDB->call_url("http://localhost/beerdb/brewery/do_edit?name=Samuel Smiths&url=www.sammysmiths.com&notes=Healthy Brew"), qr/^# view/, "Created a brewery");
28      
29 ($brewery,@other) = BeerDB::Brewery->search(name=>'Samuel Smiths'); 
30
31
32 SKIP: {
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");
35
36         #-------- Test updating printable fields ------------------   
37
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");
40
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"); 
43
44         # Test update no columns 
45         like(BeerDB->call_url("http://localhost/beerdb/brewery/do_edit/".$brewery->id), qr/^# view/, "Updated no columns"); 
46         
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"); 
49
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" );
52
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");
57
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.");
60 };
61
62 #-----------------  Test other types of  fields --------------
63
64 $style = BeerDB::Style->insert({name => 'Stout', notes => 'Rich, dark, creamy, mmmmmm.'});
65
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&notes=Healthy Brew&price=5.00&tasted=2000-12-01"),  qr/^# view/, "Created a beer with date, integer and printable fields");
68
69 ($beer, @other) = BeerDB::Beer->search(name=>'Oatmeal Stout');
70
71 SKIP: {
72         skip "Could not create and retrieve Beer", 7 unless $beer;
73
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=&notes=Healthy Brew&price=5.00"),  qr/^# view/, "Updated a beer");
76
77         # TEST update actually cleared out a the integer field
78         $val  = $beer->score ;
79     if ($val eq '') { $val = undef }; 
80         is($val, undef, "Verified non required integer field was cleared");
81
82         
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");
85
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=&notes=Healthy Brew&price=5.00"),  qr/^# view/, "Updated a beer");
88
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.");
93
94         # TEST invalid date 
95         like(BeerDB->call_url("http://localhost/beerdb/beer/do_edit/".$beer->id."?name=Oatmeal Stout&brewery=".$brewery->id."&style=".$style->id."&tasted=baddate&notes=Healthy Brew&price=5.00"),  qr/tasted' => 'Please provide a valid value/, "Date field invalid");
96
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");
99         
100         # TEST negative value actually got stored
101         like($beer->price, qr/-5(\.00)?/, "Negative value for required field stored in database") 
102 };
103  
104 $beer_id = $beer->id;
105 $beer->delete;
106
107 # TEST delete
108 $beer = BeerDB::Beer->retrieve($beer_id);
109 is($beer, undef, "Deleted Beer");
110
111 $brewery->delete;
112 $style->delete;