Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add the basics of python testing #82

Merged
merged 7 commits into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ workflows:
- rust/lint-test-build:
release: true
- build-and-test
- python-tests-e2e

jobs:
lint-test-build:
Expand Down Expand Up @@ -119,6 +120,27 @@ jobs:
- ~/.cargo
working_directory: <<parameters.working_directory>>

python-tests-e2e:
docker:
- image: cimg/python:3.8

steps:
- checkout
- python/install-packages:
pkg-manager: pip
app-dir: ~/project # If you're requirements.txt isn't in the root directory.
pip-dependency-file: requirements.txt # if you have a different name for your requirements file, maybe one that combines your runtime and test requirements.

- run:
name: Install docker compose
command: |
curl -L https://github.com/docker/compose/releases/download/1.25.3/docker-compose-`uname -s`-`uname -m` > ~/docker-compose
chmod +x ~/docker-compose
sudo mv ~/docker-compose /usr/local/bin/docker-compose
cd ~/project
docker-compose build
docker-compose up

build-and-test: # This is the name of the job, feel free to change it to better match what you're trying to do!
# These next lines defines a Docker executors: https://circleci.com/docs/2.0/executor-types/
# You can specify an image from Dockerhub or use one of the convenience images from CircleCI's Developer Hub
Expand Down Expand Up @@ -148,4 +170,3 @@ jobs:
python3 -m venv venv
source venv/bin/activate
maturin develop
pytest
21 changes: 21 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# syntax=docker/dockerfile:1
FROM python:buster
# RUN apt install gcc musl-dev linux-headers software-properties-common && add-apt-repository ppa:deadsnakes/ppa
# RUN apt update && apt install python3.8
# RUN apt update && apt install -y software-properties-common
# RUN add-apt-repository ppa:deadsnakes/ppa
# RUN apt -y install python3-9
RUN curl https://sh.rustup.rs -sSf | sh -s -- -y

# Add .cargo/bin to PATH
ENV PATH="/root/.cargo/bin:${PATH}"

# Check cargo is visible
RUN cargo --help
WORKDIR /code
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
EXPOSE 5000
COPY test_python .
CMD [ "python3", "base_routes.py" ]

14 changes: 14 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: "3.7"
services:
web:
build: .
image: localhost:5000/webimage
ports:
- "127.0.0.1:5000:5000"
test:
expose:
- 5000
depends_on:
- web
build: test_python/.

4 changes: 4 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
requests==2.26.0
pytest==6.2.5
maturin
robyn==0.6.1
11 changes: 11 additions & 0 deletions test_dir/build/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
Hello world how are you?
</body>
</html>
4 changes: 4 additions & 0 deletions test_python/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM python:buster
RUN pip3 install pytest requests
COPY . .
CMD [ "pytest", "test_routes.py" ]
20 changes: 11 additions & 9 deletions test_python/test.py → test_python/base_routes.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import sys
import os

robyn_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../robyn")
sys.path.insert(0, robyn_path)

# robyn_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../robyn")
# sys.path.insert(0, robyn_path)

from robyn import Robyn, static_file, jsonify
import asyncio
Expand All @@ -21,10 +20,13 @@ async def h(request):
return message

@app.get("/test")
async def test():
import os
path = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), "index.html"))
return static_file(path)
async def test(request):
return static_file("./index.html")

@app.get("/jsonify")
async def json_get(request):
return jsonify({"hello": "world"})


@app.post("/jsonify")
async def json(request):
Expand Down Expand Up @@ -63,4 +65,4 @@ def blocker():
if __name__ == "__main__":
app.add_header("server", "robyn")
app.add_directory(route="/test_dir",directory_path="./test_dir/build", index_file="index.html")
app.start(port=5000)
app.start(port=5000, url='0.0.0.0')
4 changes: 4 additions & 0 deletions test_python/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh

python3 ../base_routes.py
pytest test_routes
12 changes: 12 additions & 0 deletions test_python/test_routes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pytest
import requests

def test_jsonify():
r = requests.get("http://web:5000/jsonify")
assert r.json()=={"hello":"world"}
assert r.status_code==200

def test_html():
r = requests.get("http://web:5000/test")
assert "Hello world. How are you?" in r.text