def test_limit_add_record(): collection = PatientCollection(CSV_PATH) limit = collection.limit(len(GOOD_PARAMS) + 10) for _ in range(len(GOOD_PARAMS)): next(limit) new_patient = Patient("Митрофан", "Космодемьянский", "1999-10-15", "79030000000", PASSPORT_TYPE, "4510 000444") new_patient.save() last_patient = next(limit) for field in PATIENT_FIELDS: assert getattr(new_patient, field) == getattr( last_patient, field), f"Wrong attr {field} for changed limit"
def prepare(): with open(CSV_PATH, 'w', encoding='utf-8') as f: f.write('') for params in GOOD_PARAMS: Patient(*params).save() yield os.remove(CSV_PATH)
def test_collection_iteration(): collection = PatientCollection(CSV_PATH) for i, patient in enumerate(collection): true_patient = Patient(*GOOD_PARAMS[i]) for field in PATIENT_FIELDS: assert getattr(patient, field) == getattr( true_patient, field), f"Wrong attr {field} for {GOOD_PARAMS[i]}"
def test_creation_all_good_params(): patient = Patient("Кондрат", "Коловрат", "1978-01-31", PHONE_FORMAT, PASSPORT_TYPE, PASSPORT_FORMAT) assert patient.first_name == "Кондрат", "Wrong attribute first_name" assert patient.last_name == "Коловрат", "Wrong attribute last_name" assert patient.birth_date == "1978-01-31" or patient.birth_date == datetime(1978, 1, 31), \ "Wrong attribute birth_date" assert patient.phone == PHONE_FORMAT, "Wrong attribute phone" assert patient.document_type == PASSPORT_TYPE, "Wrong attribute document_type" assert patient.document_id == PASSPORT_FORMAT, "Wrong attribute document_id" patient = Patient("Кондрат", "Коловрат", "1978-01-31", PHONE_FORMAT, INTERNATIONAL_PASSPORT_TYPE, INTERNATIONAL_PASSPORT_FORMAT) assert patient.document_type == INTERNATIONAL_PASSPORT_TYPE, "Wrong attribute document_type" assert patient.document_id == INTERNATIONAL_PASSPORT_FORMAT, "Wrong attribute document_id" patient = Patient("Кондрат", "Коловрат", "1978-01-31", PHONE_FORMAT, DRIVER_LICENSE_TYPE, DRIVER_LICENSE_FORMAT) assert patient.document_type == DRIVER_LICENSE_TYPE, "Wrong attribute document_type" assert patient.document_id == DRIVER_LICENSE_FORMAT, "Wrong attribute document_id"
def test_limit_usual(): collection = PatientCollection(CSV_PATH) try: len(collection.limit(8)) assert False, "Iterator should not have __len__ method" except (TypeError, AttributeError): assert True for i, patient in enumerate(collection.limit(8)): true_patient = Patient(*GOOD_PARAMS[i]) for field in PATIENT_FIELDS: assert getattr(patient, field) == getattr( true_patient, field), f"Wrong attr {field} for {GOOD_PARAMS[i]} in limit"
def test_creation_acceptable_driver_license(driver_license): patient = Patient("Кондрат", "Коловрат", "1978-01-31", PHONE_FORMAT, DRIVER_LICENSE_TYPE, driver_license) assert patient.document_id == DRIVER_LICENSE_FORMAT, "Wrong attribute document_id"
def test_creation_acceptable_international_passport(passport): patient = Patient("Кондрат", "Коловрат", "1978-01-31", PHONE_FORMAT, INTERNATIONAL_PASSPORT_TYPE, passport) assert patient.document_id == INTERNATIONAL_PASSPORT_FORMAT, "Wrong attribute document_id"
def test_creation_acceptable_phone(default, new): patient = Patient("Кондрат", "Коловрат", "1978-01-31", PHONE_FORMAT.replace(default, new), PASSPORT_TYPE, PASSPORT_FORMAT) assert patient.phone == PHONE_FORMAT, "Wrong attribute phone"
def test_save(): patient = Patient(*GOOD_PARAMS) patient.save()
def test_create_method_good_params(): patient = Patient.create(*GOOD_PARAMS) for param, field in zip(GOOD_PARAMS, PATIENT_FIELDS): assert getattr(patient, field) in (param, datetime(1978, 1, 31)), f"Wrong attribute {field}"
def test_creation_wrong_params(i): try: Patient(*GOOD_PARAMS[:i], WRONG_PARAMS[i], *GOOD_PARAMS[i + 1:]) assert False, f"ValueError for {PATIENT_FIELDS[i]} not invoked" except ValueError: assert True
def test_creation_wrong_type_params(i): try: Patient(*GOOD_PARAMS[:i], 1.8, *GOOD_PARAMS[i + 1:]) assert False, f"TypeError for {PATIENT_FIELDS[i]} not invoked" except TypeError: assert True
except ValueError: assert True # метод create @check_log_size("error") @check_log_size("good", increased=True) def test_create_method_good_params(): patient = Patient.create(*GOOD_PARAMS) for param, field in zip(GOOD_PARAMS, PATIENT_FIELDS): assert getattr(patient, field) in (param, datetime(1978, 1, 31)), f"Wrong attribute {field}" # обновление параметров @pytest.mark.parametrize("patient,field,param", zip( [Patient(*OTHER_GOOD_PARAMS)] * len(PATIENT_FIELDS[:2]), PATIENT_FIELDS[:2], GOOD_PARAMS[:2] )) @check_log_size("error", increased=True) @check_log_size("good") def test_names_assignment(patient, field, param): try: setattr(patient, field, param) assert False, f"Attribute error should be invoked for {field} changing" except AttributeError: assert True @pytest.mark.parametrize("patient,field,param", zip( [Patient(*OTHER_GOOD_PARAMS)] * len(PATIENT_FIELDS[2:]),