Monday, April 14, 2014

Chapter 7 Exercises

Software Development: An Open Source Approach

Chapter 7: Exercises 1,2,3 

7.1

The dbPersons.php table types all of the data as TEXT. birthday could be typed as a DATE. start_date could be typed as a DATE. This violates criterion 5. It also violates criterion 6 because it doesn't declare a primary key. That means that a single record could be duplicated. If you were to update a duplicated record, only the "first" record would be duplicated and you would have conflicting records.

7.2

The new get_shift_name_from_id($id)  takes care of the checking for the special cases of a “night shift” or “guest chef” and will not call get_shift_start($id) or get_shift_end($id) if either of these special cases are true. Therefore the checks for these special cases in get_shift_start($id) and get_shift_end($id) can be removed from the code:


function get_shift_start($id) {
    if (substr($id, 11, 1) == "-")
       return substr($id, 9, 2);
    else
       return substr($id, 9, 1);
}


function get_shift_end($id) {
    if (substr($id, 11, 1) == "-")
       return substr($id, 12, 2);
    else
       return substr($id, 11, 2);
}


7.3

dbMonths.php

<?php


include_once(dirname(__FILE__) . '/dbinfo.php');
include_once(dirname(__FILE__) . '/../domain/Month.php');


function create_dbMonths() {
    connect();
    mysql_query("DROP TABLE IF EXISTS dbMonths");
    $result = mysql_query("CREATE TABLE dbMonths (id TEXT NOT NULL, dates TEXT, `group` TEXT, status TEXT, end_of_month_timestamp INT)");


    if (!$result) {
       echo mysql_error() . "Error creating dbMonths table<br>";
       echo false;
    }
    mysql_close();
    return true;
}


/*
* add a month to dbMonth: if already there, return false
*/


function insert_dbMonths($month) {
    if (!$month instanceof Month) {
       return false;
    }
    connect();


    $query = "SELECT * FROM dbMonths WHERE id = '" . $month->get_id() . "'";
    $result = mysql_query($query);



    //if there's no entry for this id, add it
    $query = "INSERT INTO dbMonths VALUES ('" .
           $month->get_id() . "','" .
           implode(',', $month->get_dates()) . "','" .
           $month->get_group() . "','" .
           $month->get_status() . "','" .
           $month->get_end_of_month_timestamp() .
    "');";



    $result = mysql_query($query);


    if (!$result) {
       echo (mysql_error() . " unable to insert into dbMonths: " . $month->get_id() . "\n");
       mysql_close();
       return false;
    }
    mysql_close();
    return true;
}


/*
* @return a single row from dbMonths table matching a particular id.
* if not in table, return false
*/


function retrieve_dbMonths($id) {
    connect();
    $query = 'SELECT * FROM dbMonths WHERE id = "' . $id . '"';
    $result = mysql_query($query);
       if (mysql_num_rows($result) != 1) {
       mysql_close();
       return false;
    }


    $result_row = mysql_fetch_assoc($result);
    $theMonth = new Month($result_row['id'], $result_row['group'], $result_row['status']);
    $theMonth->set_end_of_month_timestamp($result_row['end_of_month_timestamp']);


    return $theMonth;
}


function getall_months() {

connect();
    $query = "SELECT * FROM dbMonthsORDER BY end_of_month_timestamp";
    $result = mysql_query($query);
    $theMonths = array();


    while ($result_row = mysql_fetch_assoc($result)) {
       $theMonth = new Month($result_row['id'], $result_row['group'], $result_row['status']);
       $theMonth->set_end_of_month_timestamp($result_row['end_of_month_timestamp']);
       $theMonths[] = $theMonth;
    }


    return $theMonths;
}



function update_dbMonths($month) {
    if (!$month instanceof Month) {
       echo ("Invalid argument for update_dbMonths function call");
       return false;
    }


    if (delete_dbMonths($month->get_id()))
       return insert_dbMonths($month);
    else {
       echo (mysql_error() . "unable to update dbMonths table: " . $month->get_id());
       return false;
    }
}


/*
* remove a month from dbMonths table.  If already there, return false
*/


function delete_dbMonths($id) {
    connect();
    $query = "DELETE FROM dbMonths WHERE id=\"" . $id . "\"";
    $result = mysql_query($query);
    mysql_close();
    if (!$result) {
       echo (mysql_error() . " unable to delete from dbMonths: " . $id);
       return false;
    }
    return true;
}

?>


testdbMonths.php

<?php
include_once(dirname(__FILE__).'/../domain/Month.php');
include_once(dirname(__FILE__).'/../database/dbMonths.php');


class testdbMonths extends UnitTestCase {
     function testdbMonthsModule() {


         
         //creates an empty dbMonths table
       //$this->assertTrue(create_dbMonths());
                   
         // create a month to add to the table
         $m1 = new Month("06-01-10", "One", "unpublished");
         
<?php
include_once(dirname(__FILE__).'/../domain/Month.php');
include_once(dirname(__FILE__).'/../database/dbMonths.php');


class testdbMonths extends UnitTestCase {
     function testdbMonthsModule() {


         
         //creates an empty dbMonths table
       //$this->assertTrue(create_dbMonths());
                   
         // create a month to add to the table
         $m1 = new Month("06-01-10", "One", "unpublished");
         
         
         // test the insert function
         $this->assertTrue(insert_dbMonths($m1));
         
         // test the retrieve function
         $this->assertEqual(retrieve_dbMonths($m1->get_id())->get_id(), "06-01-10");
         $this->assertEqual(retrieve_dbMonths($m1->get_id())->get_status(), "unpublished");
         $this->assertEqual(retrieve_dbMonths($m1->get_id())->get_group(), "One");
         $this->assertEqual(retrieve_dbMonths($m1->get_id())->get_end_of_month_timestamp(), mktime(0, 0, 0, 6, 30, 2010));
         
         
         // test the update function
         $m1->set_status("published");
         $this->assertTrue(update_dbMonths($m1));
         $this->assertEqual(retrieve_dbMonths($m1->get_id())->get_status(), "published");
         
         // tests the delete function
         $this->assertTrue(delete_dbMonths($m1->get_id()));
         echo("\ntestdbMonths complete\n");
     }
}


?>
         
         // test the insert function
         $this->assertTrue(insert_dbMonths($m1));
         
         // test the retrieve function
         $this->assertEqual(retrieve_dbMonths($m1->get_id())->get_id(), "06-01-10");
         $this->assertEqual(retrieve_dbMonths($m1->get_id())->get_status(), "unpublished");
         $this->assertEqual(retrieve_dbMonths($m1->get_id())->get_group(), "One");
         $this->assertEqual(retrieve_dbMonths($m1->get_id())->get_end_of_month_timestamp(), mktime(0, 0, 0, 6, 30, 2010));
         
         
         // test the update function
         $m1->set_status("published");
         $this->assertTrue(update_dbMonths($m1));
         $this->assertEqual(retrieve_dbMonths($m1->get_id())->get_status(), "published");
         
         // tests the delete function
         $this->assertTrue(delete_dbMonths($m1->get_id()));
         echo("\ntestdbMonths complete\n");
     }
}


?>

No comments:

Post a Comment