Cleanup, unittests, GitHub action

This commit is contained in:
Farkhod Sadykov
2023-02-13 05:45:01 +01:00
committed by Farkhod Sadykov
parent 95561119de
commit 81ae0fab73
7 changed files with 76 additions and 48 deletions
-1
View File
@@ -1 +0,0 @@
config/
@@ -1,9 +1,9 @@
name: Lint
name: Run linters and unittests
on: [push]
jobs:
build:
lint_test:
runs-on: ubuntu-latest
strategy:
matrix:
@@ -19,10 +19,23 @@ jobs:
python -m pip install --upgrade pip
pip install pylint
pip install black
pip install requests_mock
pip install -r requirements.txt
- name: Analysing the code with pylint
run: |
pylint sgpt.py
pylint \
sgpt.py \
tests.py \
--disable=missing-function-docstring \
--disable=too-many-arguments \
--disable=missing-module-docstring \
--disable=import-error \
--disable=missing-class-docstring \
--disable=too-many-instance-attributes \
--max-line-length=120
- name: Analysing the code with black
run: |
black --target-version py310 -l 120 sgpt.py
black --target-version py310 -l 120 sgpt.py tests.py
- name: Run unittests
run: |
python -m unittest tests.py
-33
View File
@@ -1,33 +0,0 @@
name: Main
on:
push:
branches:
- main
workflow_run:
workflows: [ Lint ]
types:
- completed
jobs:
main:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pyinstaller
- name: Build
run: |
pyinstaller --onefile sgpt.py
- name: Release
uses: svenstaro/upload-release-action@v2
with:
repo_token: ${{ secrets.GITHUB_TOKEN }}
file: dist/sgpt
tag: ${{ github.ref }}
overwrite: true
-5
View File
@@ -1,5 +0,0 @@
*.egg-info
dist
config/api_key.txt
sgpt.spec
build
+19 -3
View File
@@ -21,16 +21,24 @@ if [ -f "$PWD/venv/bin/activate" ]; then
source $PWD/venv/bin/activate
fi
if black --check --target-version py310 -l 120 sgpt.py
if black --check --target-version py310 -l 120 sgpt.py tests.py
then
echo 'Black passed ✅'
else
echo 'Black failed ❌'
echo 'RUN: black --target-version py310 -l 120 sgpt.py'
echo 'RUN: black --target-version py310 -l 120 sgpt.py tests.py'
exit 1
fi
if pylint sgpt.py
if pylint sgpt.py \
tests.py \
--disable=missing-function-docstring \
--disable=too-many-arguments \
--disable=missing-module-docstring \
--disable=import-error \
--disable=missing-class-docstring \
--disable=too-many-instance-attributes \
--max-line-length=120
then
echo 'Pylint passed ✅'
else
@@ -38,4 +46,12 @@ else
exit 1
fi
if python -m unittest tests.py
then
echo 'Unittests passed ✅'
else
echo 'Unittests failed ❌'
exit 1
fi
echo 'Well done.'
-2
View File
@@ -10,7 +10,6 @@ commands directly from the interface.
API Key is stored locally for easy use in future runs.
"""
# pylint: disable=missing-function-docstring,too-many-arguments
import os
from time import sleep
@@ -65,7 +64,6 @@ def openai_request(prompt, model, max_tokens, api_key):
def typer_writer(text, code, shell, animate):
shell_or_code = shell or code
color = "magenta" if shell_or_code else None
# typer.colors.
if animate and not shell_or_code:
for char in text:
typer.secho(char, nl=False, fg=color, bold=shell_or_code)
+40
View File
@@ -0,0 +1,40 @@
import unittest
import requests_mock
import requests
import sgpt
class TestMain(unittest.TestCase):
def setUp(self):
self.prompt = "What is the capital of France?"
self.model = "text-davinci-003"
self.max_tokens = 2048
self.shell = False
self.execute = False
self.code = False
self.animation = True
self.spinner = True
self.api_key = "test-api-key"
self.response_text = "Paris"
@requests_mock.Mocker()
def test_openai_request(self, mock):
mock.post(sgpt.API_URL, json={"choices": [{"text": self.response_text}]}, status_code=200)
result = sgpt.openai_request(self.prompt, self.model, self.max_tokens, self.api_key, spinner=self.spinner)
self.assertEqual(result, self.response_text)
expected_json = {"prompt": self.prompt, "model": self.model, "max_tokens": self.max_tokens}
expected_headers = {"Content-Type": "application/json", "Authorization": f"Bearer {self.api_key}"}
request = mock.request_history[0]
self.assertEqual(request.json(), expected_json)
for key, value in expected_headers.items():
self.assertEqual(request.headers[key], value)
@requests_mock.Mocker()
def test_openai_request_fail(self, mock):
mock.post(sgpt.API_URL, status_code=400)
with self.assertRaises(requests.exceptions.HTTPError):
sgpt.openai_request(self.prompt, self.model, self.max_tokens, self.api_key, spinner=self.spinner)
if __name__ == "__main__":
unittest.main()