#!/usr/bin/perl
use strict;
use warnings;
use Test::More qw(no_plan);

# Same as "use RegistrarData;", but traps errors
BEGIN { 
  use_ok('dggRegistrarData'); 
}

`./init_db`;  # make sure database is in initial state.

my $data;
ok($data = dggRegistrarData->new,       "create registrarData object");
my $person;
ok($person = $data->getNextPerson,   "get first person");
ok(ref($person) eq 'Person',         ".. which is a Person");
ok($person->name  eq 'A. Einstein',  ".. whose name is Einstein");
ok($person->phone eq '222-9999',     ".. whose phone is 222-9999");

$data->reset;  # start over
my $count=0;   # count how many people we can get.
$count++ while $data->getNextPerson;
ok($count == 8,                      "8 people in the test database");

$data->addPerson(name => 'S. Claus', phone => '000-0000');

$data->reset;  # start over
$count=0;
$count++ while $data->getNextPerson;
ok($count == 9,                      "9 people after adding one.");

$data->reset;
my $course;
ok($course = $data->getNextCourse,   "get first course");
ok(ref($course) eq 'Course',         ".. which is a Course");
ok($course->title eq 'calculus',     ".. whose title is calculus");
ok($course->time eq 'MWF 10:00am',   ".. which meets MWF 10:00am");
ok($course->term eq 'Fall 2004',     ".. whose term is Fall 2004");
ok($course->place eq 'Sci217',       ".. which meets in Sci217");
ok($course->credits eq '3',          ".. which is worth 3 credits");

$data->reset;
$count=0; #reset counter
$count++ while $data->getNextCourse;
ok($count == 4,                      "4 courses in the test database");

$data->addCourse(title => 'Chess', time => 'Wed. 4:00pm', term => 'Fall 2004',
                 place => 'Dinning Hall', credits => '1');

$data->reset;
$count=0;
$count++ while $data->getNextCourse;
ok($count == 5,                      "5 courses after adding one.");

# print " data->{dbh} = '" . $data->{dbh} . "'\n";
# print " data->{sth} = '" . $data->{sth} . "'\n";
# print " data->{error} = '" . $data->{error} . "'\n";

$count=0;
$count++ while $data->getNextCourse;
ok($count == 5,                      "ran through course list again without reset");

