Exemplo n.º 1
0
def load_random_users(number: int):
    repository = PersonRepository.PersonRepository()
    if number < 0:
        print('Argument must be greater than 0')
        return
    repository.create_data(number)
    print('Converted data from .json to .db format.\nCreated '
          'tables with data about ' + str(number) + ' persons')
    repository.close_connection()
class TestDBO:
    repository = PersonRepository.PersonRepository('data_test.db')
    dbo = DBOperationsService.DBOperationsService(repository)

    def test_should_return_most_secure_passwords_list(self):
        passwords = self.dbo.most_secure_passwords()
        assert passwords == ['minemine', 'animated', 'asdfghjkl']

    def test_should_return_correct_percentage_of_men(self):
        percent_men = self.dbo.percentage_men()
        assert percent_men == 50

    def test_should_return_correct_percentage_of_women(self):
        percent_women = self.dbo.percentage_women()
        assert percent_women == 50

    def test_should_return_correct_age_of_men(self):
        aa = self.dbo.average_age_male()
        assert aa == 52.4

    def test_should_return_correct_age_of_women(self):
        aa = self.dbo.average_age_female()
        assert aa == 59.2

    def test_should_return_correct_age_of_generally(self):
        aa = self.dbo.average_age_all()
        assert aa == 55.8

    def test_should_return_five_most_common_cities(self):
        cities = self.dbo.most_common_cities(5)
        all_cities = [('Bray', 1), ('Maynooth', 1), ('Notre Dame de Lourdes', 1), ('Salla', 1),
                      ('Churwalden', 1), ('Grenoble', 1), ('Caven', 1), ('Hagen', 1), ('Scottsdale', 1), ('Lumsden', 1)]
        assert all(x in all_cities for x in cities)

    def test_should_return_five_most_common_passwords(self):
        passwords = self.dbo.most_common_passwords(5)
        all_passwords = [('bigben', 1), ('minemine', 1), ('animated', 1), ('hall', 1),
                      ('yyyyyy', 1), ('body', 1), ('loving', 1), ('asdfghjkl', 1), ('cookie1', 1), ('julio', 1)]
        assert all(x in all_passwords for x in passwords)

    def test_should_return_users_born_between_1945_12_31_and_1955_12_31(self):
        users = self.dbo.users_born_between_dates('1945-12-31', '1955-12-31')

        assert users == [('orangewolf556', '1950-05-06T08:57:22.462Z'),
                         ('orangeelephant208', '1952-02-06T15:36:51.801Z'),
                         ('heavylion807', '1953-09-27T22:45:44.522Z')]

    def test_users_born_between_dates_should_raise_exception_due_to_invalid_month(self):
        with pytest.raises(Exception):
            self.dbo.users_born_between_dates('1945-13-31', '1955-12-31')

    def test_should_return_most_secure_passwords(self):
        passwords = self.dbo.most_secure_passwords()
        assert passwords == ['minemine', 'animated', 'asdfghjkl']
Exemplo n.º 3
0
def most_secure_pass():
    if not os.path.isfile('./data.db'):
        print(
            "Error: data.db file doesn't exist. Use --load-random-users [number] to create database."
        )
        return
    repository = PersonRepository.PersonRepository()
    service = DBOperationsService.DBOperationsService(repository)
    print('\nThe most secure passwords:\n')
    for password in service.most_secure_passwords():
        print(password)
    repository.close_connection()
Exemplo n.º 4
0
def gender_percentage(gender: str):
    if not os.path.isfile('./data.db'):
        print(
            "Error: data.db file doesn't exist. Use --load-random-users [number] to create database."
        )
        return
    repository = PersonRepository.PersonRepository()
    service = DBOperationsService.DBOperationsService(repository)
    if gender == 'male':
        print('Percentage of men: ' + str(round(service.percentage_men(), 1)) +
              '%')
    elif gender == 'female':
        print('Percentage of women: ' +
              str(round(service.percentage_women(), 1)) + '%')
    repository.close_connection()
Exemplo n.º 5
0
def users_born_between_dates(d1: str, d2: str):
    if not os.path.isfile('./data.db'):
        print(
            "Error: data.db file doesn't exist. Use --load-random-users [number] to create database."
        )
        return
    repository = PersonRepository.PersonRepository()
    service = DBOperationsService.DBOperationsService(repository)
    try:
        users = service.users_born_between_dates(d1, d2)
    except:
        print("Error: Invalid date format")
        return
    print('\nUsers born between ' + d1 + ' and ' + d2 + ':\n')
    print('USERNAME{:>16} DATE OF BIRTH'.format('|'))
    print('-' * 38)
    for username, dob in users:
        print('{:23}| {:^13}'.format(username, dob[:10]))
    repository.close_connection()
Exemplo n.º 6
0
def most_common_pass(limit: int):
    if not os.path.isfile('./data.db'):
        print(
            "Error: data.db file doesn't exist. Use --load-random-users [number] to create database."
        )
        return
    repository = PersonRepository.PersonRepository()
    service = DBOperationsService.DBOperationsService(repository)
    try:
        passwords = service.most_common_passwords(limit)
    except:
        print("Error: argument must be greater than 0.")
        return
    print('\n' + str(limit) + ' most popular passwords: \n')
    print('PASSWORD{:>12} OCCURRENCES'.format('|'))
    print('-' * 32)
    for password, occurrence in passwords:
        print('{:19}| {:^11}'.format(password, str(occurrence)))
    repository.close_connection()