Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -------------------- tasks/unique_person.py ------------
- from datetime import datetime
- CURRENT_DATE = datetime(2018, 1, 1)
- class Person:
- def __init__(self, first_name, last_name, birth_date,
- job, working_years, salary, country, city, gender='unknown'):
- self.first_name = first_name
- self.last_name = last_name
- self.birth_date = datetime.strptime(birth_date, "%d.%m.%Y")
- self.job = job
- self.working_years = working_years
- self.salary = salary
- self.country = country
- self.city = city
- self.gender = gender
- def name(self) -> str:
- # return self.first_name + " " + self.last_name
- # return "%s %s" % (self.first_name, self.last_name)
- # return "{} {}".format(self.first_name, self.last_name)
- # return f"{self.first_name} {self.last_name}"
- # pattern = "{} {}"
- # return pattern.format(self.first_name, self.last_name)
- pattern = "{name} {last}"
- return pattern.format(last=self.last_name, name=self.first_name)
- def age(self) -> int:
- years_delta = CURRENT_DATE.year - self.birth_date.year
- if CURRENT_DATE.month < self.birth_date.month or \
- CURRENT_DATE.day < self.birth_date.day:
- years_delta -= 1
- return years_delta
- def money(self) -> str:
- # return "{:_}".format(self.salary * self.working_years * 12).replace("_", " ")
- return format(self.salary * self.working_years * 12, "_").replace("_", " ")
- def work(self) -> str:
- prefix = "Is"
- if self.gender == "male":
- prefix = "He is"
- elif self.gender == "female":
- prefix = "She is"
- return "{} a {}".format(prefix, self.job)
- def home(self) -> str:
- return "Lives in {city}, {country}".format(city=self.city, country=self.country)
- # ------------ tests/test_unique_person.py ------------------
- from datetime import datetime
- import pytest
- from tasks.unique_person import Person
- def test_person_date():
- p1 = Person("John", "Smith", "19.09.1979", "welder", 15, 3600,
- "Canada", "Vancouver", "male")
- assert p1.birth_date == datetime(1979, 9, 19)
- def test_person_name():
- p1 = Person("John", "Smith", "19.09.1979", "welder", 15, 3600,
- "Canada", "Vancouver", "male")
- assert p1.name() == "John Smith"
- @pytest.mark.parametrize(["birth_date", "age"], [
- ["01.01.2010", 8],
- ["02.01.2010", 7],
- ["31.12.2009", 8],
- ])
- def test_person_age(birth_date, age):
- p1 = Person("John", "Smith", birth_date, "welder", 15, 3600,
- "Canada", "Vancouver", "male")
- assert p1.age() == age
- def test_person_1():
- p1 = Person("John", "Smith", "19.09.1979", "welder", 15, 3600,
- "Canada", "Vancouver", "male")
- assert p1.name() == "John Smith", "Name"
- assert p1.age() == 38, "Age"
- assert p1.money() == "648 000", "Money"
- assert p1.work() == "He is a welder", "Job"
- def test_person_2():
- p2 = Person("Hanna Rose", "May", "05.12.1995", "designer", 2.2, 2150,
- "Austria", "Vienna")
- assert p2.work() == "Is a designer", "Job"
- assert p2.home() == "Lives in Vienna, Austria", "Home"
- def test_person_3():
- p1 = Person("Ann", "Smith", "19.09.1979", "welder", 15, 3600,
- "Canada", "Vancouver", "female")
- assert p1.name() == "Ann Smith", "Name"
- assert p1.age() == 38, "Age"
- assert p1.money() == "648 000", "Money"
- assert p1.work() == "She is a welder", "Job"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement