add integration tests

This commit is contained in:
Sergey Chubaryan 2025-02-07 17:12:59 +03:00
parent 64bb402ad1
commit 1126ff1321
10 changed files with 47 additions and 53 deletions

View File

@ -9,6 +9,7 @@ class Auth():
self.token = token
class User():
id: string
email: string
name: string
password: string
@ -21,18 +22,16 @@ class User():
class BackendApi():
http: FastHttpUser
def __init__(self, http: FastHttpUser):
self.http = http
def __init__(self, httpClient):
self.httpClient = httpClient
def user_create(self) -> User:
email = ''.join(random.choices(string.ascii_lowercase + string.digits, k=10)) + '@test.test'
name = ''.join(random.choices(string.ascii_letters, k=10))
password = 'Abcdef1!!1'
response = self.http.client.post(
"/user/create",
response = self.httpClient.post(
"/v1/user/create",
json={
"email": email,
"password": password,
@ -45,8 +44,8 @@ class BackendApi():
return User(email, password, name)
def user_login(self, user: User) -> Auth:
response = self.http.client.post(
"/user/login",
response = self.httpClient.post(
"/v1/user/login",
json={
"email": user.email,
"password": user.password,
@ -63,22 +62,11 @@ class BackendApi():
def dummy_get(self, auth: Auth):
headers = {"X-Auth": auth.token}
response = self.http.client.get("/dummy", headers=headers)
response = self.httpClient.get("/v1/dummy", headers=headers)
if response.status_code != 200:
raise AssertionError('something wrong')
def health_get(self):
response = self.http.client.get("/health")
response = self.httpClient.get("/health")
if response.status_code != 200:
raise AssertionError('something wrong')
def shortlink_create(self, url: string) -> string:
response = self.http.client.post("/s/new?url=" + url)
if response.status_code != 200:
raise AssertionError('can not login user')
link = response.json()['link']
if link == '':
raise AssertionError('empty user token')
return link

View File

@ -0,0 +1,12 @@
from api import BackendApi
import requests
class TestUser:
def test_create_user(self):
api = BackendApi(requests)
api.user_create()
def test_login_user(self):
api = BackendApi(requests)
user = api.user_create()
api.user_login(user)

View File

@ -16,6 +16,6 @@ requirements:
pip freeze > requirements.txt
run-web:
locust -f tests,loads --class-picker --host http://localhost:8080 --processes 16
locust -f performance --class-picker --host http://localhost:8080 --processes 16

View File

@ -3,15 +3,13 @@ from locust import FastHttpUser, task
from api import BackendApi, Auth
class DummyGet(FastHttpUser):
api: BackendApi
auth: Auth
def on_start(self):
self.api = BackendApi(self.client)
user = self.api.user_create()
self.auth = self.api.user_login(user)
@task
def dummy_test(self):
self.api.dummy_get(self.auth)
def on_start(self):
self.api = BackendApi(self)
user = self.api.user_create()
self.auth = self.api.user_login(user)

View File

@ -3,11 +3,9 @@ from locust import FastHttpUser, task
from api import BackendApi
class HealthGet(FastHttpUser):
api: BackendApi
def on_start(self):
self.api = BackendApi(self.client)
@task
def user_create_test(self):
self.api.health_get()
def on_start(self):
self.api = BackendApi(self)
self.api.health_get()

View File

@ -1,9 +1,9 @@
from locust import LoadTestShape
class LowLoad(LoadTestShape):
time_limit = 600
spawn_rate = 5
max_users = 100
time_limit = 60
spawn_rate = 2
max_users = 10
def tick(self) -> (tuple[float, int] | None):
user_count = self.spawn_rate * self.get_run_time()

View File

@ -3,11 +3,9 @@ from locust import FastHttpUser, task
from api import BackendApi
class ShortlinkCreate(FastHttpUser):
api: BackendApi
def on_start(self):
self.api = BackendApi(self.client)
@task
def user_create_test(self):
self.api.shortlink_create("https://ya.ru")
def on_start(self):
self.api = BackendApi(self)
self.api.shortlink_create("https://example.com")

View File

@ -3,23 +3,18 @@ from locust import FastHttpUser, task
from api import BackendApi, User
class UserCreate(FastHttpUser):
api: BackendApi
def on_start(self):
self.api = BackendApi(self.client)
@task
def user_create_test(self):
self.api.user_create()
def on_start(self):
self.api = BackendApi(self)
class UserLogin(FastHttpUser):
api: BackendApi
user: User
def on_start(self):
self.api = BackendApi(self)
self.user = self.api.user_create()
@task
def user_create_test(self):
self.api.user_login(self.user)
def on_start(self):
self.api = BackendApi(self)
self.user = self.api.user_create()
self.api.user_login(self.user)

View File

@ -4,6 +4,7 @@ certifi==2024.7.4
charset-normalizer==3.3.2
click==8.1.7
ConfigArgParse==1.7
exceptiongroup==1.2.2
Flask==3.0.3
Flask-Cors==4.0.1
Flask-Login==0.6.3
@ -11,12 +12,16 @@ gevent==24.2.1
geventhttpclient==2.3.1
greenlet==3.0.3
idna==3.7
iniconfig==2.0.0
itsdangerous==2.2.0
Jinja2==3.1.4
locust==2.31.3
MarkupSafe==2.1.5
msgpack==1.0.8
packaging==24.2
pluggy==1.5.0
psutil==6.0.0
pytest==8.3.4
pyzmq==26.1.0
requests==2.32.3
tomli==2.0.1