예제 #1
0
 def read(self, filename):
     """
     Return dictionary with key => value pairs
     :param filename is the file where the values exist
     """
     data = dict()
     empno = 0
     keys = []
     a_row = 0
     try:
         workbook = load_workbook(filename)
         first_sheet = workbook.sheetnames[0]
         worksheet = workbook[first_sheet]
         for row in worksheet.iter_rows():
             record = dict()
             row_num = 0
             for cell in row:
                 a_row = cell.row
                 if 1 == a_row:
                     keys.append(cell.value)
                 else:
                     valid = cell.value
                     if isinstance(cell.value, datetime):
                         valid = Validator.xlsx_date(cell.value)
                     record[keys[row_num]] = valid
                 row_num += 1
             if a_row > 1:
                 data[empno] = record
             empno += 1
         # print(data)
         result = Validator.save_dict(data)
         return result
     except PermissionError:
         print("Sorry, you don't have enough permissions to access this file")
예제 #2
0
    def read(self, filename):
        """
        Return dictionary with key => value pairs
        :param filename is the file where the values exist
        >>> read("Saves/data.txt")
        """
        file = open(filename, 'r')
        data = dict()
        empno = 0
        try:

            for line in file:  # FIRST LOOP
                rows = line.split(";")
                dictionary = dict()
                for row in rows:
                    if len(row.split("=")) == 2:
                        key = row.split("=")[0]
                        value = row.split("=")[1]
                        value = value.rstrip('\n')
                        dictionary[key] = value
                        data[empno] = dictionary
                    else:
                        print("File error")
                        raise ValueError
                empno += 1
            result = Validator.save_dict(data)
            return result

        except Exception as e:
            print(e)
 def test_xlxs_date(self):
     """"
     Tests static return function, test coverage purposes
     """
     expected = "13-12-1994"
     test_date = datetime(1994, 12, 13)
     result = Validator.xlsx_date(test_date)
     self.assertEquals(expected, result)
예제 #4
0
 def read(self, filename):
     """
     Return dictionary with key => value pairs
     :param filename is the file where the values exist
     >>> read("Saves/data.csv")
     """
     try:
         data = dict()
         empno = 0
         with open(filename) as f:
             reader = CSVDictReader(f)
             for row in reader:
                 record = dict()
                 for key in row:
                     record[key] = row.get(key)
                 data[empno] = record
                 empno += 1
         result = Validator.save_dict(data)
         return result
     except TypeError:  # pragma: no cover
         print("Error!!")  # pragma: no cover
     except Exception as e:  # pragma: no cover
         print(e)  # pragma: no cover
예제 #5
0
 def read(self, filename):
     """
     Return dictionary with key => value pairs
     :param filename is the file where the values exist
     >>> a=FileTypeCSV()
     >>> FileTypeCSV.read(a,"Saves/data.csv")
     """
     try:
         data = dict()
         empno = 0
         with open(filename) as f:
             reader = CSVDictReader(f)
             for row in reader:
                 record = dict()
                 for key in row:
                     record[key] = row.get(key)
                 data[empno] = record
                 empno += 1
             # print(data)
         # James' changes (13/03)
         result = Validator.save_dict(data)
         return result
     except TypeError:
         print("Error!!")
예제 #6
0
 def __init__(self):
     self.validator = Validator()
 def setUp(self):
     self.validator = Validator()
class TestValidator(TestCase):

    def setUp(self):
        self.validator = Validator()

    def tearDown(self):
        del self.validator

    def test_empid_invalid(self):
        """
        Tests validating data
        """
        empid = "A12323123123"
        expected = False

        result = self.validator.check_all(empid, new_key='empid')
        self.assertEqual(expected, result)

    def test_empid_valid(self):
        """
        Tests validating data
        """
        empid = "Q666"
        expected = "Q666"

        result = self.validator.check_all(empid, new_key='empid')
        self.assertEqual(expected, result)

    def test_gender_true_female_letter(self):
        """
        Tests validating data
        """
        gender = "F"
        expected = "F"
        # fix this!!
        result = self.validator.check_all(gender, new_key='gender')
        self.assertEqual(expected, result)

    def test_gender_true_female_string(self):
        """
        Tests validating data
        """
        gender = "Female"
        expected = "F"

        result = self.validator.check_all(gender, new_key='gender')
        self.assertEqual(expected, result)

    def test_gender_true_male_string(self):
        """
        Tests validating data
        """
        gender = "male"
        expected = "M"

        result = self.validator.check_all(gender, new_key='gender')
        self.assertEqual(expected, result)

    def test_gender_false(self):
        """
        Tests validating data
        """
        gender = "transgender"
        expected = False

        result = self.validator.check_all(gender, new_key='gender')
        self.assertEqual(expected, result)

    def test_age_true(self):
        """
        Tests validating data
        """
        age = "23"
        expected = "23"

        result = self.validator.check_all(age, new_key='age')
        self.assertEqual(expected, result)

    def test_age_false(self):
        """
        Tests validating data
        """
        age = "213"
        expected = False

        result = self.validator.check_all(age, new_key='age')
        self.assertEqual(expected, result)

    def test_sales_true(self):
        """
        Tests validating data
        """
        sales = "123"
        expected = "123"

        result = self.validator.check_all(sales, new_key='sales')
        self.assertEqual(expected, result)

    def test_sales_false(self):
        """
        Tests validating data
        """
        sales = "1233"
        expected = False

        result = self.validator.check_all(sales, new_key='sales')
        self.assertEqual(expected, result)

    def test_BMI_true(self):
        """
        Tests validating data
        """
        BMI = "Normal"
        expected = "Normal"

        result = self.validator.check_all(BMI, new_key='bmi')
        self.assertEqual(expected, result)

    def test_BMI_lowercase(self):
        """
        Tests validating data
        """
        BMI = "overweight"
        expected = "overweight"

        result = self.validator.check_all(BMI, new_key='bmi')
        self.assertEqual(expected, result)

    def test_BMI_false(self):
        """
        Tests validating data
        """
        BMI = "Fatty"
        expected = False

        result = self.validator.check_all(BMI, new_key='bmi')
        self.assertEqual(expected, result)

    def test_salary_true(self):
        """
        Tests validating data
        """
        salary = "456"
        expected = "456"

        result = self.validator.check_all(salary, new_key='salary')
        self.assertEqual(expected, result)

    def test_salary_false(self):
        """
        Tests validating data
        """
        salary = "456million"
        expected = False

        result = self.validator.check_all(salary, new_key='salary')
        self.assertEqual(expected, result)

    def test_birthday_true(self):
        """
        Tests validating data
        """
        birthday = "13/12/1994"
        expected = "13/12/1994"

        result = self.validator.check_all(birthday, new_key='birthday')
        self.assertEqual(expected, result)

    def test_birthday_false_delim_fix(self):
        """
        Tests validating data
        """
        birthday = "13-12-1994"
        result = self.validator.check_all(birthday, new_key='Birthday')
        expected = "13/12/1994"
        self.assertEqual(expected, result)

    def test_xlxs_date(self):
        """"
        Tests static return function, test coverage purposes
        """
        expected = "13-12-1994"
        test_date = datetime(1994, 12, 13)
        result = Validator.xlsx_date(test_date)
        self.assertEquals(expected, result)

    def test_birthday_day_length(self):
        """
        Tests validating data
        """
        birthday = "1/12/1994"
        expected = "1/12/1994"

        result = self.validator.check_all(birthday, new_key='birthday')
        self.assertEqual(expected, result)

    def test_birthday_month_length(self):
        """
        Tests validating data
        """
        birthday = "13/1/1994"
        expected = "13/1/1994"

        result = self.validator.check_all(birthday, new_key='birthday')
        self.assertEqual(expected, result)

    def test_birthday_both_length(self):
        """
        Tests validating data
        """
        birthday = "1/1/1994"
        expected = "1/1/1994"

        result = self.validator.check_all(birthday, new_key='birthday')
        self.assertEqual(expected, result)

    def test_save_dict_valid(self):
        """
        Test static checker method
        """
        data2 = {0: {'ID': 'Q123', 'Gender': 'F', 'Age': '21', 'Sales': '001', 'BMI': 'Normal', 'Salary': '12',
                     'Birthday': '01/01/1996'}}
        expected2 = {0: {'Empid': 'Q123', 'Gender': 'F', 'Age': '21', 'Sales': '001', 'Bmi': 'normal', 'Salary': '12', 'Birthday': '01/01/1996'}}
        result2 = self.validator.save_dict(data2)
        self.assertEqual(result2, expected2)
        print(result2)

    def test_z_save_dict_invalid(self):
        """
        Test static checker method
        """
        data1 = {0: {'ID': 'A123', 'Gender': 'F', 'Age': '212', 'Sales': '101', 'BMI': 'Normal', 'Salary': '12',
                     'Birthday': '01/01/1996'},
                 1: {'ID': 'Q999', 'Gender': 'F', 'Age': '21', 'Sales': '001', 'BMI': 'Normal', 'Salary': '12',
                     'Birthday': '01/01/1996'}
                 }
        expected1 = {1: {'Empid': 'Q999', 'Gender': 'F', 'Age': '21', 'Sales': '001', 'Bmi': 'normal', 'Salary': '12',
                         'Birthday': '01/01/1996'}
                     }
        result1 = self.validator.save_dict(data1)
        self.assertEqual(result1, expected1)

    def test_check_checker_invalid_id(self):
        """
        Test static checker method
        """
        data = {0: {'ID': 'A123', 'Gender': 'F', 'Age': '201', 'Sales': '101', 'BMI': 'Normal', 'Salary': '12',
                    'Birthday': '01/01/1996'},
                1: {'ID': 'A123', 'Gender': 'F', 'Age': '21', 'Sales': '001', 'BMI': 'Normal', 'Salary': '12',
                    'Birthday': '01/01/1996'}
                }

        expected = {1: {'Empid': 'A123', 'Gender': 'F', 'Age': '21', 'Sales': '001', 'Bmi': 'normal', 'Salary': '12',
                        'Birthday': '01/01/1996'}
                    }
        result = self.validator.save_dict(data)
        self.assertEqual(result, expected)

    def test_check_invalid_key(self):
        """
        Tests validating data
        """
        data = {'ASDSADASD': 'A123', 'Gender': 'F', 'Age': '21', 'Sales': '001', 'BMI': 'Normal', 'Salary': '12',
                        'Birthday': '01/01/1996'}
        result = self.validator.checker(data)
        expected = False
        self.assertEqual(expected, result)