diff --git a/load_tests/.gitignore b/tests/.gitignore similarity index 100% rename from load_tests/.gitignore rename to tests/.gitignore diff --git a/load_tests/api.py b/tests/api.py similarity index 70% rename from load_tests/api.py rename to tests/api.py index d41fc4c..c945007 100644 --- a/load_tests/api.py +++ b/tests/api.py @@ -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 \ No newline at end of file diff --git a/tests/integration/test_user.py b/tests/integration/test_user.py new file mode 100644 index 0000000..9c69bc6 --- /dev/null +++ b/tests/integration/test_user.py @@ -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) \ No newline at end of file diff --git a/load_tests/makefile b/tests/makefile similarity index 83% rename from load_tests/makefile rename to tests/makefile index efc9b49..b349abd 100644 --- a/load_tests/makefile +++ b/tests/makefile @@ -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 diff --git a/load_tests/tests/dummy.py b/tests/performance/dummy.py similarity index 65% rename from load_tests/tests/dummy.py rename to tests/performance/dummy.py index bf84587..8d5f08d 100644 --- a/load_tests/tests/dummy.py +++ b/tests/performance/dummy.py @@ -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) \ No newline at end of file + \ No newline at end of file diff --git a/load_tests/tests/health.py b/tests/performance/health.py similarity index 65% rename from load_tests/tests/health.py rename to tests/performance/health.py index df5ddc5..8c05304 100644 --- a/load_tests/tests/health.py +++ b/tests/performance/health.py @@ -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) \ No newline at end of file + self.api.health_get() \ No newline at end of file diff --git a/load_tests/loads/low_load.py b/tests/performance/low_load.py similarity index 83% rename from load_tests/loads/low_load.py rename to tests/performance/low_load.py index c84d101..722a030 100644 --- a/load_tests/loads/low_load.py +++ b/tests/performance/low_load.py @@ -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() diff --git a/load_tests/tests/shortlink.py b/tests/performance/shortlink.py similarity index 61% rename from load_tests/tests/shortlink.py rename to tests/performance/shortlink.py index 2f9072c..8fb0e46 100644 --- a/load_tests/tests/shortlink.py +++ b/tests/performance/shortlink.py @@ -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) \ No newline at end of file + self.api.shortlink_create("https://example.com") \ No newline at end of file diff --git a/load_tests/tests/user.py b/tests/performance/user.py similarity index 66% rename from load_tests/tests/user.py rename to tests/performance/user.py index d9f3881..e67b067 100644 --- a/load_tests/tests/user.py +++ b/tests/performance/user.py @@ -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() \ No newline at end of file + self.api.user_login(self.user) \ No newline at end of file diff --git a/load_tests/requirements.txt b/tests/requirements.txt similarity index 84% rename from load_tests/requirements.txt rename to tests/requirements.txt index 166fda7..ea6536c 100644 --- a/load_tests/requirements.txt +++ b/tests/requirements.txt @@ -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