def test_get_persons_born_between(self): database = Database(':memory:') session = database.get_session() dob_1 = DayOfBirth(date=datetime(year=1963, month=7, day=15)) dob_2 = DayOfBirth(date=datetime(year=1975, month=1, day=3)) dob_3 = DayOfBirth(date=datetime(year=2020, month=3, day=20)) person_1 = Person(email='to early') person_1.day_of_birth = [dob_1] person_2 = Person(email='should be returned') person_2.day_of_birth = [dob_2] person_3 = Person(email='to late') person_3.day_of_birth = [dob_3] min_date = datetime(year=1970, month=1, day=1) max_date = datetime(year=1999, month=12, day=30) session.add_all([person_1, person_2, person_3]) session.commit() data_fetcher = DataFetcher(database) values, columns = data_fetcher.get_persons_born_between( min_date=min_date, max_date=max_date) assert len(values) == 1 assert values[0][0].email == 'should be returned'
def __show_persons_born_between(self, min_date: str, max_date: str): """Fetch and display persons born between passed dates.""" min_date = self.__str_to_date(min_date) max_date = self.__str_to_date(max_date) data_fetcher = DataFetcher(self.__database) information, columns_names = data_fetcher.get_persons_born_between( min_date, max_date) pretty_table = PrettyTablePrinter(columns_names) pretty_table.show(information)
def test_get_gender_percentage_female(self): database = Database(':memory:') session = database.get_session() person_1 = Person(gender='female') person_2 = Person(gender='female') person_3 = Person(gender='female') session.bulk_save_objects([person_1, person_2, person_3]) session.commit() data_fetcher = DataFetcher(database) values, columns = data_fetcher.get_gender_percentage() expected = [('female', 100)] assert set(values) == set(expected)
def test_get_strongest_password(self, passwords, expected): database = Database(':memory:') session = database.get_session() logins = [] for password in passwords: logins.append(Login(password=password)) session.add_all(logins) session.commit() data_fetcher = DataFetcher(database) values, columns = data_fetcher.get_strongest_password() assert set(values) == set([(expected, )])
def test_get_most_popular_passwords_fetch_one(self): database = Database(':memory:') session = database.get_session() logins = [] logins.append(Login(password='******')) logins.append(Login(password='******')) logins.append(Login(password='******')) session.add_all(logins) session.commit() data_fetcher = DataFetcher(database) values, columns = data_fetcher.get_most_popular_passwords(1) expected = [('tricky', 2)] assert set(values) == set(expected)
def test_get_most_popular_cities_fetch_many_city_when_there_is_one(self): database = Database(':memory:') session = database.get_session() locations = [] locations.append(Location(city='Sejny')) locations.append(Location(city='Sejny')) locations.append(Location(city='Sejny')) session.add_all(locations) session.commit() data_fetcher = DataFetcher(database) values, columns = data_fetcher.get_most_popular_cities(4) expected = [('Sejny', 3)] assert set(values) == set(expected)
def test_get_average_age_botch(self, gender, expected): database = Database(':memory:') session = database.get_session() person_1 = Person(gender='female') dbo_1 = DayOfBirth(age=10) person_1.day_of_birth = [dbo_1] person_2 = Person(gender='male') dbo_2 = DayOfBirth(age=20) person_2.day_of_birth = [dbo_2] person_3 = Person(gender='male') dbo_3 = DayOfBirth(age=39) person_3.day_of_birth = [dbo_3] session.add_all([person_1, person_2, person_3]) session.commit() data_fetcher = DataFetcher(database) values, columns = data_fetcher.get_average_age(gender) assert set(values) == set(expected)
def __init__(self, options: argparse.Namespace, database: Database): self.options = options self.__database = database self.__data_fetcher = DataFetcher(database)
class Manager: """Class handling actions that should be done based on passed arguments to script.""" def __init__(self, options: argparse.Namespace, database: Database): self.options = options self.__database = database self.__data_fetcher = DataFetcher(database) def run(self): """Invokes the appropriate action based on passed arguments""" if self.options.data_path: self.__load_data() elif self.options.load_random: self.__load_random_persons_data(self.options.load_random) elif self.options.get_gender_percentage: self.__show_gender_percentage() elif self.options.get_age_gender: self.__show_average_age(self.options.get_age_gender) elif self.options.get_cities_popularity: self.__show_most_popular_cities(self.options.get_cities_popularity) elif self.options.get_password_popularity: self.__show_most_popular_passwords( self.options.get_password_popularity) elif self.options.get_strongest_password: self.__show_strongest_password() elif self.options.get_born_between: self.__show_persons_born_between(*self.options.get_born_between) else: print("Unknown argument or no argument passed.") def __load_data(self): """Loads data from file to database. Path to file is passed by options field.""" data = self.__load_results() data_deserializer = DataDeserializer() deserialized_data = data_deserializer.deserialize_many(data) self.__upload_persons_data(deserialized_data) print("Data uploaded.") def __upload_persons_data(self, deserialized_data: List[PersonData]): database_loader = DatabaseLoader(self.__database) database_loader.load_many_persons_data(deserialized_data) def __load_results(self): """Loads data from file. Path to file is passed by options field.""" data_path = self.options.data_path with open(data_path, encoding="utf8") as file: data = json.loads(file.read()) return data['results'] def __load_random_persons_data(self, how_many: int): persons = create_random_person_data(how_many) self.__upload_persons_data(persons) print("Data uploaded.") def __show_gender_percentage(self): """Fetch and display data about gender and prints it.""" information, columns_names = self.__data_fetcher.get_gender_percentage( ) pretty_table = PrettyTablePrinter(columns_names) pretty_table.show(information) def __show_average_age(self, gender): """Fetch and display data about age and prints it""" information, columns_names = self.__data_fetcher.get_average_age( gender) pretty_table = PrettyTablePrinter(columns_names) pretty_table.show(information) def __show_most_popular_cities(self, how_much): """Fetch and display data about n most popular cities and prints it""" information, columns_names = self.__data_fetcher.get_most_popular_cities( how_much) pretty_table = PrettyTablePrinter(columns_names) pretty_table.show(information) def __show_most_popular_passwords(self, how_much): """Fetch and display data about n most popular passwords and prints it""" information, columns_names = self.__data_fetcher.get_most_popular_passwords( how_much) pretty_table = PrettyTablePrinter(columns_names) pretty_table.show(information) def __show_strongest_password(self): """Fetch and display data about n most popular passwords and prints it""" information, columns_names = self.__data_fetcher.get_strongest_password( ) pretty_table = PrettyTablePrinter(columns_names) pretty_table.show(information) def __show_persons_born_between(self, min_date: str, max_date: str): """Fetch and display persons born between passed dates.""" min_date = self.__str_to_date(min_date) max_date = self.__str_to_date(max_date) data_fetcher = DataFetcher(self.__database) information, columns_names = data_fetcher.get_persons_born_between( min_date, max_date) pretty_table = PrettyTablePrinter(columns_names) pretty_table.show(information) def __str_to_date(self, date: str) -> datetime: """Converts string to datetime. Date should be in from of YYYY-MM-DD.""" return datetime.datetime.strptime(date, '%Y-%m-%d')