From cacb25fc5b0055c45cc1c54265029ae9233c87ab Mon Sep 17 00:00:00 2001 From: kehiy Date: Wed, 24 Jul 2024 01:43:04 +0330 Subject: [PATCH] ci(python): adding python linter ruff --- .github/workflows/linting.yml | 6 +- .github/workflows/releaser.yml | 4 +- .github/workflows/ruff-python-linter.yml | 10 ++ .github/workflows/testing.yml | 4 +- Makefile | 2 +- test/normal_test.py | 123 +++++++++++++++++------ test/utils.py | 4 +- 7 files changed, 111 insertions(+), 42 deletions(-) create mode 100644 .github/workflows/ruff-python-linter.yml diff --git a/.github/workflows/linting.yml b/.github/workflows/linting.yml index 810e246..a614e95 100644 --- a/.github/workflows/linting.yml +++ b/.github/workflows/linting.yml @@ -14,12 +14,12 @@ jobs: name: lint runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - uses: actions/setup-go@v4 + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 with: go-version: '1.21.1' cache: false - name: golangci-lint - uses: golangci/golangci-lint-action@v3 + uses: golangci/golangci-lint-action@v6 with: version: v1.54 \ No newline at end of file diff --git a/.github/workflows/releaser.yml b/.github/workflows/releaser.yml index 7f68a0b..ab6d547 100644 --- a/.github/workflows/releaser.yml +++ b/.github/workflows/releaser.yml @@ -27,7 +27,7 @@ jobs: sudo apt install zip - name: Install Go - uses: actions/setup-go@v3 + uses: actions/setup-go@v5 with: go-version: 1.21.1 @@ -59,7 +59,7 @@ jobs: needs: [build-cli] runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Create SHA256SUMS file run: | echo "${{ needs.build-cli.outputs.checksums }}" >> SHA256SUMS diff --git a/.github/workflows/ruff-python-linter.yml b/.github/workflows/ruff-python-linter.yml new file mode 100644 index 0000000..ecd87a8 --- /dev/null +++ b/.github/workflows/ruff-python-linter.yml @@ -0,0 +1,10 @@ +name: Python linter (Ruff) + +on: [ push, pull_request ] + +jobs: + ruff: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: chartboost/ruff-action@v1 diff --git a/.github/workflows/testing.yml b/.github/workflows/testing.yml index b183af3..046d134 100644 --- a/.github/workflows/testing.yml +++ b/.github/workflows/testing.yml @@ -18,10 +18,10 @@ jobs: steps: - name: Checkout code - uses: actions/checkout@v3 + uses: actions/checkout@v4 - name: Install Go - uses: actions/setup-go@v3 + uses: actions/setup-go@v5 with: go-version: '1.21.1' diff --git a/Makefile b/Makefile index 69078c9..bc5848f 100644 --- a/Makefile +++ b/Makefile @@ -23,4 +23,4 @@ fmt: go mod tidy check: - golangci-lint run --build-tags "${BUILD_TAG}" --timeout=20m0s \ No newline at end of file + golangci-lint run --build-tags "${BUILD_TAG}" --timeout=20m0s diff --git a/test/normal_test.py b/test/normal_test.py index 0cd719a..f9370de 100644 --- a/test/normal_test.py +++ b/test/normal_test.py @@ -1,7 +1,6 @@ import socket import time import utils -import random #! GET method is not tested here now. # TODO: writing test for method GET. @@ -11,29 +10,33 @@ st = time.time() -#? Global variables +# ? Global variables sub_set_names = [] set_names = [] elements_value = [] sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) -server_address = ('localhost', 7070) #! You can change port and host for testing. +server_address = ("localhost", 7070) #! You can change port and host for testing. sock.connect(server_address) + def test_connect_ok(): """ Connecting to database and creating a session. Testing: CON TQL command Error: null """ - query = "CON root super_secret_password" #! Considers you config is default. + query = "CON root super_secret_password" #! Considers you config is default. response = utils.make_query(query, sock) - assert response == "OK", f"\033[91mCan't connect to server with default info, received: {response}, expected: OK\033[0m" + assert ( + response == "OK" + ), f"\033[91mCan't connect to server with default info, received: {response}, expected: OK\033[0m" print(f"\033[32mReceived response: {response}, Connection test Passed.\033[32m") + def test_ping_ok(): """ Ping the database to check session health. @@ -42,10 +45,13 @@ def test_ping_ok(): """ response = utils.make_query("PING", sock) - assert response == "PONG", f"\033[91mCan't ping the database, received: {response}, expected: PONG\033[0m" + assert ( + response == "PONG" + ), f"\033[91mCan't ping the database, received: {response}, expected: PONG\033[0m" print(f"\033[32mReceived response: {response}, we are connected!\033[32m") + def test_new_set_ok(): """ Creating random sets. @@ -53,14 +59,19 @@ def test_new_set_ok(): Error: null """ for i in range(10): - set_names.append(utils.get_random_string_name(i+2)) + set_names.append(utils.get_random_string_name(i + 2)) query = f"SET {set_names[i]}" response = utils.make_query(query, sock) - assert response == "OK", f"\033[91mCan't create set: {set_names[i]}, received: {response}, expected: OK\033[0m" + assert ( + response == "OK" + ), f"\033[91mCan't create set: {set_names[i]}, received: {response}, expected: OK\033[0m" + + print( + f"\033[32mReceived response: {response}, {len(set_names)} sets created successfully.\nSets: {set_names}\033[32m" + ) - print(f"\033[32mReceived response: {response}, {len(set_names)} sets created successfully.\nSets: {set_names}\033[32m") def test_new_sub_set_ok(): """ @@ -70,14 +81,19 @@ def test_new_sub_set_ok(): """ for s in set_names: for i in range(7): - sub_set_names.append(utils.get_random_string_name(i+2)) + sub_set_names.append(utils.get_random_string_name(i + 2)) query = f"SSET {s} {sub_set_names[i]}" response = utils.make_query(query, sock) - assert response == "OK", f"\033[91mCan't create subset: {sub_set_names[i]} in set {s}, received: {response}, expected: OK\033[0m" + assert ( + response == "OK" + ), f"\033[91mCan't create subset: {sub_set_names[i]} in set {s}, received: {response}, expected: OK\033[0m" + + print( + f"\033[32mReceived response: {response}, {len(sub_set_names)} subsets created successfully.\nSubsets: {sub_set_names}\033[32m" + ) - print(f"\033[32mReceived response: {response}, {len(sub_set_names)} subsets created successfully.\nSubsets: {sub_set_names}\033[32m") def test_push_element_ok(): """ @@ -90,19 +106,24 @@ def test_push_element_ok(): for s in set_names: for i in range(7): for _ in range(1_000): - element_value = utils.get_random_string_name(i+8) + element_value = utils.get_random_string_name(i + 8) elements_value.append(element_value) element_time = int(time.mktime(time.gmtime())) query = f"PUSH {s} {sub_set_names[i]} {element_value} {element_time}" response = utils.make_query(query, sock) - assert response == "OK", f"\033[91mCan't push element with value of: {elements_value[i]} and time of: {element_time}, received: {response}, expected: OK\033[0m" + assert ( + response == "OK" + ), f"\033[91mCan't push element with value of: {elements_value[i]} and time of: {element_time}, received: {response}, expected: OK\033[0m" set_index += 7 - #? Change `elements_value[:10]` to get more or less elements. - print(f"\033[32mReceived response: {response}, {len(elements_value)} elements pushed successfully.\nElements: {elements_value[:10]}\033[32m") + # ? Change `elements_value[:10]` to get more or less elements. + print( + f"\033[32mReceived response: {response}, {len(elements_value)} elements pushed successfully.\nElements: {elements_value[:10]}\033[32m" + ) + def test_count_sets_ok(): """ @@ -112,11 +133,16 @@ def test_count_sets_ok(): """ response = utils.make_query("CNTS", sock) - assert response == str(len(set_names)), f"\033[91mCan't count sets, received: {response}, expected: {len(set_names)}\033[0m" + assert ( + response == str(len(set_names)) + ), f"\033[91mCan't count sets, received: {response}, expected: {len(set_names)}\033[0m" - print(f"\033[32mReceived response: {response}, sets number counted successfully.\033[32m") + print( + f"\033[32mReceived response: {response}, sets number counted successfully.\033[32m" + ) -def test_count_sub_sets_ok(): + +def test_count_sub_sets_ok(): """ Counting all subsets. Testing: CNTSS TQL command @@ -130,9 +156,14 @@ def test_count_sub_sets_ok(): sub_sets_count += int(response) - assert sub_sets_count == len(sub_set_names), f"\033[91mCan't count subsets, received: {sub_sets_count}, expected: {len(sub_set_names)}\033[0m" + assert ( + sub_sets_count == len(sub_set_names) + ), f"\033[91mCan't count subsets, received: {sub_sets_count}, expected: {len(sub_set_names)}\033[0m" + + print( + f"\033[32mReceived response: {sub_sets_count}, subsets counted successfully.\033[32m" + ) - print(f"\033[32mReceived response: {sub_sets_count}, subsets counted successfully.\033[32m") def test_count_elements_ok(): """ @@ -149,12 +180,17 @@ def test_count_elements_ok(): response = utils.make_query(query, sock) elements_count += int(response) - + set_index += 7 - assert elements_count == len(elements_value), f"\033[91mCan't count elements, received: {elements_count}, expected: {len(elements_value)}\033[0m" + assert ( + elements_count == len(elements_value) + ), f"\033[91mCan't count elements, received: {elements_count}, expected: {len(elements_value)}\033[0m" + + print( + f"\033[32mReceived response: {elements_count}, elements counted successfully.\033[32m" + ) - print(f"\033[32mReceived response: {elements_count}, elements counted successfully.\033[32m") def test_clean_sub_sets_elements_ok(): """ @@ -169,11 +205,16 @@ def test_clean_sub_sets_elements_ok(): query = f"CLNSS {s} {sub_set_names[i]}" response = utils.make_query(query, sock) - assert response == "OK", f"\033[91mCan't clean subset: {sub_set_names[i]} of set {s}, received: {response}, expected: OK\033[0m" + assert ( + response == "OK" + ), f"\033[91mCan't clean subset: {sub_set_names[i]} of set {s}, received: {response}, expected: OK\033[0m" set_index += 7 - print(f"\033[32mReceived response: {response}, subset elements cleaned successfully.\033[32m") + print( + f"\033[32mReceived response: {response}, subset elements cleaned successfully.\033[32m" + ) + def test_drop_sub_sets_ok(): """ @@ -188,11 +229,16 @@ def test_drop_sub_sets_ok(): query = f"DRPSS {s} {sub_set_names[i]}" response = utils.make_query(query, sock) - assert response == "OK", f"\033[91mCan't drop subset: {sub_set_names[i]} from set: {s}, received: {response}, expected: OK\033[0m" - + assert ( + response == "OK" + ), f"\033[91mCan't drop subset: {sub_set_names[i]} from set: {s}, received: {response}, expected: OK\033[0m" + set_index += 7 - print(f"\033[32mReceived response: {response}, subsets dropped successfully.\033[32m") + print( + f"\033[32mReceived response: {response}, subsets dropped successfully.\033[32m" + ) + def test_clean_sub_sets_ok(): """ @@ -204,10 +250,13 @@ def test_clean_sub_sets_ok(): query = f"CLNS {s}" response = utils.make_query(query, sock) - assert response == "OK", f"\033[91mCan't clean set: {s}, received: {response}, expected: OK\033[0m" + assert ( + response == "OK" + ), f"\033[91mCan't clean set: {s}, received: {response}, expected: OK\033[0m" print(f"\033[32mReceived response: {response}, sets cleaned successfully.\033[32m") + def test_drop_sets_ok(): """ Dropping all sets. @@ -218,10 +267,13 @@ def test_drop_sets_ok(): query = f"DRPS {s}" response = utils.make_query(query, sock) - assert response == "OK", f"\033[91mCan't drop set: {s}, received: {response}, expected: OK\033[0m" + assert ( + response == "OK" + ), f"\033[91mCan't drop set: {s}, received: {response}, expected: OK\033[0m" print(f"\033[32mReceived response: {response}, sets dropped successfully.\033[32m") + def test_clean_sets_ok(): """ Cleaning all sets. @@ -230,7 +282,9 @@ def test_clean_sets_ok(): """ response = utils.make_query("CLN", sock) - assert response == "OK", f"\033[91mCan't clean sets, received: {response}, expected: OK\033[0m" + assert ( + response == "OK" + ), f"\033[91mCan't clean sets, received: {response}, expected: OK\033[0m" print(f"\033[32mReceived response: {response},sets cleaned successfully.\033[32m") @@ -250,7 +304,10 @@ def main(): test_drop_sets_ok() test_clean_sets_ok() + if __name__ == "__main__": main() sock.close() - print('\033[34mAll tests successfully passed in:\033[34m', time.time() - st, 'seconds') + print( + "\033[34mAll tests successfully passed in:\033[34m", time.time() - st, "seconds" + ) diff --git a/test/utils.py b/test/utils.py index 46cf925..e38f1c1 100644 --- a/test/utils.py +++ b/test/utils.py @@ -2,8 +2,10 @@ import string import socket + def get_random_string_name(length: int) -> str: - return ''.join(random.choice(string.ascii_lowercase) for i in range(length)) + return "".join(random.choice(string.ascii_lowercase) for i in range(length)) + def make_query(query: str, sock: socket.socket) -> str: sock.sendall(query.encode())