Development Exercises – L3

Created by Ramses Alexander Coraspe Valdez

Created on March 2, 2020

Right - Bicep

Right - Are the results right?

B - are all the boundary conditions correct?

I - can you check the inverse relationships?

C - can you cross-check results using other means?

E - can you force error conditions to happen?

P - are performance characteristics within bounds?

16. Given the math.ceil function

  • Define a set of unit test cases that exercise the function

17. Given the math.factorial function

  • Define a set of test cases that exercise the function

18. Given the math.pow function

  • Define a set of test cases that exercise the function

20. Implement a class that manages a directory that is saved in a text file. The data saved includes:

  • a. Name
  • b. Email
  • c. Age
  • d. Country of Origin

The class should have capabilities to:

  • Add new record
  • Delete a record
  • Look for a record by mail and age
  • List on screen all records
In [0]:
import math
import unittest
import csv
In [68]:
class directory:
    def __init__(self, n, e, a, c):
        self.Name= n
        self.Email = e
        self.Age = a
        self.Country= c

class ManageDirectory:
    def __init__(self, filename):
        self.myList = [];
        self.myFile= filename;

    def load_from_file(self):
        li = [];        
        with open(self.myFile) as f:
            reader = csv.reader(f)
            data = [r for r in reader]
            data.pop(0);
            for r in data:
                li.append(directory(r[0], r[1], r[2], r[3]));            
            self.myList = li;

    def saveToCSVFile(self):
        with open(self.myFile,'w',newline='') as csvfile:
            writer = csv.writer(csvfile)
            writer.writerow(['Name', 'Email', 'Age', 'Country'])
            for r in self.myList:                    
                writer.writerow([r.Name, r.Email, r.Age, r.Country])

    def add(self, e):
        if e is None:
            return False        
        try:
            self.myList.append(e);
            li.saveToCSVFile();            
            return True
        except:
            return False                
        

    def remove(self, i):
        if (i<=1):
            return False        
        try:
            self.myList.pop(i-1);
            li.saveToCSVFile();            
            return True
        except:
            return False         

    def lookBy_email_age(self, e, a):
        match=[];
        for r in self.myList:
            if((e in r.Email) and (a== r.Age)):
                match.append(r)
        return match;         

    def show_list(self):
        return self.myList;

li = ManageDirectory('test.csv');

##if the file  already contains  data you can use this method
####################### li.load_from_file(); #######################
# If there is no data in the file or it does not exist you can add records to the file in this way
li.add(directory("Ram1", "ram1@hotmail.com", "3", "Mexico"));
li.add(directory("Ram2", "ram2@hotmail.com", "2", "Mexico"));
li.add(directory("Ram3", "ram3@hotmail.com", "7", "Mexico"));
li.add(directory("Ram4", "ram4@hotmail.com", "5", "Mexico"));
li.add(directory("Ram", "ram@hotmail.com", "5", "Mexico"));
l= li.show_list()
for row in l:     
    print(row.Name, row.Email, row.Age, row.Country)

print("----------Removing record 1--------------------")    
li.remove(1)
l= li.show_list()
for row in l:     
    print(row.Name, row.Email, row.Age, row.Country)

print("----------Searching record by email and age--------------------")    
res= li.lookBy_email_age('ram4@hotmail.com', '5')
for row in res:     
    print(row.Name, row.Email, row.Age, row.Country)

print("----------Showing all the records--------------------")    
l= li.show_list()
for row in l:     
    print(row.Name, row.Email, row.Age, row.Country)
Ram1 ram1@hotmail.com 3 Mexico
Ram2 ram2@hotmail.com 2 Mexico
Ram3 ram3@hotmail.com 7 Mexico
Ram4 ram4@hotmail.com 5 Mexico
Ram ram@hotmail.com 5 Mexico
----------Removing record 1--------------------
Ram1 ram1@hotmail.com 3 Mexico
Ram2 ram2@hotmail.com 2 Mexico
Ram3 ram3@hotmail.com 7 Mexico
Ram4 ram4@hotmail.com 5 Mexico
Ram ram@hotmail.com 5 Mexico
----------Searching record by email and age--------------------
Ram4 ram4@hotmail.com 5 Mexico
----------Showing all the records--------------------
Ram1 ram1@hotmail.com 3 Mexico
Ram2 ram2@hotmail.com 2 Mexico
Ram3 ram3@hotmail.com 7 Mexico
Ram4 ram4@hotmail.com 5 Mexico
Ram ram@hotmail.com 5 Mexico
In [72]:
class My_tests(unittest.TestCase):

    li = ManageDirectory('test.csv');

    def test_ceil_positive(self):
        res= math.ceil(2.25);
        self.assertEqual(3, res);

    def test_ceil_negative(self):
        res= math.ceil(-2.25);
        self.assertEqual(-2, res);  

    def test_factorial_zero(self):
        res = math.factorial(0)
        self.assertEqual(1, res)    

    def test_factorial(self):
        res = math.factorial(5)
        self.assertEqual(120, res)

    def test_pow(self):
        res = math.pow(2,2)
        self.assertEqual(4, res)

    def test_adding_record(self):
        res= li.add(directory('testname','testemail@hotmail.com','20','Mex'));
        self.assertTrue(res);
    
    def test_adding_record2(self):
        res= li.add(directory('testname2','testemail2@hotmail.com','25','USA'));
        self.assertTrue(res);        

    def test_adding_record3(self):
        res= li.add(directory('testname3','testemail3@hotmail.com','22','Arg'));
        self.assertTrue(res);        

    def test_adding_record_fail(self):
        res= li.add(None);
        self.assertFalse(res);    

    def test_removing_record(self):
        res= li.remove(2)
        self.assertTrue(res);

    def test_removing_record_fail(self):
        res= li.remove(0)
        self.assertFalse(res);

    def test_searching_record(self):
        res= li.lookBy_email_age('testemail3@hotmail.com', '22')
        self.assertEqual(True, len(res)>0)        

    def test_searching_no_record(self):
        res= li.lookBy_email_age('testemail4@gmail.com', '22')
        self.assertEqual(False, len(res)>0)        

if __name__ == '__main__':    
    unittest.main(argv=['first-arg-is-ignored'], exit=False)
.............
----------------------------------------------------------------------
Ran 13 tests in 0.016s

OK