Skip to content

Commit

Permalink
Restore python tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mbklein committed Oct 17, 2023
1 parent 54decee commit 2320c3d
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 10 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,8 @@ jobs:
path: ".tfvars"
- uses: actions/setup-python@v2
with:
python-version: '3.9'
python-version: '3.10'
- uses: aws-actions/setup-sam@v1
- name: sam fix https://github.com/aws/aws-sam-cli/issues/4527
run: $(dirname $(readlink $(which sam)))/pip install --force-reinstall "cryptography==38.0.4"
- uses: aws-actions/configure-aws-credentials@master
with:
role-to-assume: arn:aws:iam::${{ secrets.AwsAccount }}:role/github-actions-role
Expand Down
7 changes: 4 additions & 3 deletions .github/workflows/test-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ on:
push:
paths:
- ".github/workflows/test-python.yml"
- "python/**"
- "chat/**"
workflow_dispatch:
defaults:
run:
working-directory: ./python
working-directory: ./chat
jobs:
test:
runs-on: ubuntu-latest
Expand All @@ -19,8 +19,9 @@ jobs:
- uses: actions/setup-python@v4
with:
python-version: '3.9'
cache-dependency-path: python/requirements.txt
cache-dependency-path: chat/src/requirements.txt
- run: pip install -r requirements.txt
working-directory: ./chat/src
- name: Check code style
run: ruff check .
- name: Run tests
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ deps-python:
cover-python:
cd chat/src && coverage run --include='src/**/*' -m unittest -v && coverage report
style-python:
cd chat/src && ruff check .
cd chat && ruff check .
test-python:
cd chat/src && python -m unittest -v
cd chat && python -m unittest -v
build: .aws-sam/build.toml
link: build
cd chat/src && for src in *.py **/*.py; do for target in $$(find ../../.aws-sam/build -maxdepth 1 -type d); do if [[ -f $$target/$$src ]]; then ln -f $$src $$target/$$src; fi; done; done
Expand Down
7 changes: 6 additions & 1 deletion chat/src/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Runtime Dependencies
langchain~=0.0.208
nbformat~=5.9.0
openai~=0.27.8
Expand All @@ -6,4 +7,8 @@ pyjwt~=2.6.0
python-dotenv~=1.0.0
tiktoken~=0.4.0
weaviate-client~=3.19.2
wheel~=0.40.0
wheel~=0.40.0

# Dev/Test Dependencies
ruff~=0.1.0
coverage~=7.3.2
Empty file added chat/test/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions chat/test/fixtures/apitoken.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
TEST_SECRET = "TEST_SECRET"
TEST_TOKEN_NAME = "dcTestToken"
TEST_TOKEN = ('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjQ4NDM1ODY2MDYxNjUs'
'ImlhdCI6MTY4Nzg5MTM2OSwiZW50aXRsZW1lbnRzIjpbXSwiaXNMb2dnZWRJbiI6d'
'HJ1ZSwic3ViIjoidGVzdFVzZXIifQ.vIZag1pHE1YyrxsKKlakXX_44ckAvkg7xWOoA_w4x58')
56 changes: 56 additions & 0 deletions chat/test/fixtures/events.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
from copy import deepcopy
from test.fixtures.apitoken import TEST_TOKEN_NAME, TEST_TOKEN

POST_EVENT = {
"version": "2.0",
"routeKey": "$default",
"rawPath": "/chat",
"cookies": [
"cookie_1=cookie_value_1",
"cookie_2=cookie_value_2",
],
"headers": {
"Authorization": f"Bearer {TEST_TOKEN}",
"origin": "https://example.edu"
},
"queryStringParameters": {
"param1": "value1",
"param2": "value2",
},
"requestContext": {
"accountId": "123456789012",
"apiId": "api-id",
"domainName": "id.execute-api.us-east-1.amazonaws.com",
"domainPrefix": "id",
"http": {
"method": "POST",
"path": "/chat",
"protocol": "HTTP/1.1",
"sourceIp": "192.168.0.1/32",
"userAgent": "agent"
},
"requestId": "id",
"routeKey": "$default",
"stage": "$default",
"time": "12/Mar/2020:19:03:58 +0000",
"timeEpoch": 1583348638390
},
"body": "UE9TVGVkIENvbnRlbnQ=",
"pathParameters": {},
"isBase64Encoded": True,
"stageVariables": {}
}

PLAIN_BODY_EVENT = deepcopy(POST_EVENT)
PLAIN_BODY_EVENT["isBase64Encoded"] = False
PLAIN_BODY_EVENT["body"] = "POSTed Content"

NO_BODY_EVENT = deepcopy(POST_EVENT)
NO_BODY_EVENT["isBase64Encoded"] = False
NO_BODY_EVENT["body"] = ""

NO_TOKEN_EVENT = deepcopy(POST_EVENT)
del NO_TOKEN_EVENT["headers"]["Authorization"]

COOKIE_TOKEN_EVENT = deepcopy(NO_TOKEN_EVENT)
COOKIE_TOKEN_EVENT["cookies"].append(f"{TEST_TOKEN_NAME}={TEST_TOKEN}")
Empty file added chat/test/handlers/__init__.py
Empty file.
Empty file added chat/test/helpers/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions chat/test/helpers/test_apitoken.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os
from src.helpers.apitoken import ApiToken
from test.fixtures.apitoken import TEST_SECRET, TEST_TOKEN
from unittest import mock, TestCase

@mock.patch.dict(
os.environ,
{
"API_TOKEN_SECRET": TEST_SECRET
}
)
class TestFunction(TestCase):
def test_empty_token(self):
subject = ApiToken()
self.assertFalse(subject.is_logged_in())

def test_valid_token(self):
subject = ApiToken(TEST_TOKEN)
self.assertTrue(subject.is_logged_in())

def test_invalid_token(self):
subject = ApiToken("INVALID_TOKEN")
self.assertFalse(subject.is_logged_in())

2 changes: 1 addition & 1 deletion node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"scripts": {
"lint": "eslint src/**/*.js test/**/*.js",
"preinstall": "cd src && npm i && cd ../lambdas && npm i && cd ../",
"preinstall": "cd src && npm i && cd - && cd ../lambdas && npm i && cd -",
"prettier": "prettier -c src test",
"prettier:fix": "prettier -cw src test",
"test": "mocha",
Expand Down

0 comments on commit 2320c3d

Please sign in to comment.