본문 바로가기

Python345

@pytest.fixture 로 test 데이터 세팅하기 pytest.fixture 는 왜 사용할까? 테스트 코드를 작성하다보면 ‘클라이언트’나 ‘토큰’, ‘객체’ 등이 필요할 수 있습니다. 보통은 setup 이나 teardown 등으로 데이터를 세팅할 수 있지만 pytest에는 fixture 데코레이터를 통해 필요한 데이터를 세팅하고 어떤 테스트 함수든지 재활용할 수 있습니다. 테스트 코드 예시 아래는 로그인 api를 테스트 하는 함수입니다. # test_login.py def test_login_api(client, create_user): data = { "email": "test@test.com", "password": "password", } r = client.post("/api/v1/login", json=data) r_message = r.jso.. 2022. 4. 14.
Century From Year Introduction The first century spans from the year 1 up to and including the year 100, the second century - from the year 101 up to and including the year 200, etc. Task Given a year, return the century it is in. Examples 1705 --> 18 1900 --> 19 1601 --> 17 2000 --> 20 Solution: 1. Divide 'year' by 100. 2. Returns the decimal point rounded up. import math def century(year): return math.ceil(year.. 2022. 4. 13.
로그인시 Access Token, Refresh Token 보내주기 액세스 토큰을 사용하는 이유 서버가 액세스 토큰을 클라이언트에게 주면 클라이언트는 매 요청시 액세스 토큰을 서버로 보내주어 로그인 상태을 알려줍니다. 이러한 방식은 HTTP의 무상태 특성을 보완하기 위한 한 가지 방법이지만 액세스 토큰을 주는 방식은 전달 과정에서 탈취 당할 우려가 있어 보안에 문제가 있습니다. 이를 해결하기 위해 토큰에 만료기간을 주어, 만약 탈취를 당하더라도 시간이 지나면 토큰을 사용할 수 없게 만들 수 있습니다. 하지만 이는 로그인 상태가 주기적으로 풀린다는 뜻이고 사용자에게 큰 불편을 줄 것입니다. 그래서 사람들은 리프레시 토큰을 생각해내었습니다. 리프레시 토큰으로 액세스 토큰 재발급 리프레시 토큰은 액세스 토큰이 만료되었을 경우 이를 확인하고 다시 액세스 토큰을 발급하는 방법입니.. 2022. 4. 13.
Isograms Description: An isogram is a word that has no repeating letters, consecutive or non-consecutive. Implement a function that determines whether a string that contains only letters is an isogram. Assume the empty string is an isogram. Ignore letter case. Example: (Input --> Output) "Dermatoglyphics" --> true "aba" --> false "moOse" --> false (ignore letter case) Solution: 1. Change all 'string' to .. 2022. 4. 12.
SQLAlchemy, ‘PasswordType’으로 손쉽게 암호화하자 PasswordType 이란? sqlalchemy를 이용해 model 클래스에서 PasswordType을 적용할 수 있습니다. PasswordType을 적용한 model은 사용자가 입력한 패스워드를 자동으로 암호하하여 데이터베이스에 저장합니다. 모델 컬럼 타입을 지정한 것만으로 개발자가 따로 암호화 로직을 구현하지 않아도 되는 것입니다. 패스워드 암호화 저장 PasswordType을 사용하기 위해서는 먼저 SQLAlchemy-Utils를 설치해야합니다. pip install sqlalchemy-utils 설치가 완료되었다면 model파일에 import 해줍니다. # models.py from sqlalchemy_utils import PasswordType import 후 아래에 User model을 작.. 2022. 4. 12.
Student's Final Grade Description: Create a function finalGrade, which calculates the final grade of a student depending on two parameters: a grade for the exam and a number of completed projects. This function should take two arguments: exam - grade for exam (from 0 to 100); projects - number of completed projects (from 0 and above); This function should return a number (final grade). There are four types of final g.. 2022. 4. 11.