-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from Cdayz/fix/issue-#101
Allow passing variables to dbQuery and dbResponse
- Loading branch information
Showing
12 changed files
with
241 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
.PHONY: setup | ||
setup: | ||
@docker-compose -f docker-compose.yaml up --build -d | ||
@curl http://localhost:5000/info/10 | ||
|
||
.PHONY: teardown | ||
teardown: | ||
@docker-compose -f docker-compose.yaml down -v --remove-orphans | ||
|
||
.PHONY: test | ||
test: setup | ||
gonkey -db_dsn "postgresql://testing_user:testing_password@localhost:5432/testing_db?sslmode=disable" -debug -host http://localhost:5000 -tests ./cases/ | ||
make teardown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
- name: Get random number | ||
method: GET | ||
path: /randint/ | ||
response: | ||
200: '{ "num": {"generated": "$matchRegexp(\\d)" } }' | ||
variables_to_set: | ||
200: | ||
info_id: num.generated | ||
|
||
- name: Get info with database | ||
method: GET | ||
path: "/info/{{ $info_id }}" | ||
variables_to_set: | ||
200: | ||
golang_id: query_result.0.0 | ||
response: | ||
200: '{"result_id": "{{ $info_id }}", "query_result": [[ {{ $golang_id }}, "golang"], [2, "gonkey"]]}' | ||
dbQuery: > | ||
SELECT id, name FROM testing WHERE id={{ $golang_id }} | ||
dbResponse: | ||
- '{"id": {{ $golang_id }}, "name": "golang"}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
version: '3' | ||
|
||
services: | ||
postgres: | ||
image: postgres:10.3 | ||
command: postgres -c 'max_connections=100' | ||
volumes: | ||
- postgres-db:/var/lib/postgresql/data | ||
environment: | ||
- POSTGRES_HOST=postgres | ||
- POSTGRES_PORT=5432 | ||
- POSTGRES_DB=testing_db | ||
- POSTGRES_USER=testing_user | ||
- POSTGRES_PASSWORD=testing_password | ||
ports: | ||
- 5432:5432 | ||
healthcheck: | ||
test: "pg_isready -U postgres" | ||
|
||
svc: | ||
build: | ||
context: . | ||
dockerfile: server.dockerfile | ||
command: python /app/server.py | ||
ports: | ||
- 5000:5000 | ||
environment: | ||
- APP_POSTGRES_HOST=postgres | ||
- APP_POSTGRES_PORT=5432 | ||
- APP_POSTGRES_USER=testing_user | ||
- APP_POSTGRES_PASS=testing_password | ||
- APP_POSTGRES_DB=testing_db | ||
depends_on: | ||
postgres: | ||
condition: service_healthy | ||
|
||
volumes: | ||
postgres-db: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
FROM python:3.7.9 | ||
|
||
RUN pip install -U psycopg2-binary --no-cache-dir | ||
COPY server.py /app/server.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import http.server | ||
import random | ||
import json | ||
import os | ||
import socketserver | ||
from http import HTTPStatus | ||
|
||
import psycopg2 | ||
|
||
|
||
class Handler(http.server.SimpleHTTPRequestHandler): | ||
|
||
def get_response(self) -> dict: | ||
if self.path.startswith('/info/'): | ||
response = self.get_info() | ||
elif self.path.startswith('/randint/'): | ||
response = self.get_rand_num() | ||
else: | ||
response = {'non-existing': True} | ||
|
||
return response | ||
|
||
def get_info(self) -> dict: | ||
info_id = self.path.split('/')[-1] | ||
return { | ||
'result_id': info_id, | ||
'query_result': storage.get_sql_result('SELECT id, name FROM testing LIMIT 2'), | ||
} | ||
|
||
def get_rand_num(self) -> dict: | ||
return {'num': {'generated': str(random.randint(0, 100))}} | ||
|
||
def do_GET(self): | ||
# заголовки ответа | ||
self.send_response(HTTPStatus.OK) | ||
self.send_header("Content-type", "application/json") | ||
self.end_headers() | ||
self.wfile.write(json.dumps(self.get_response()).encode()) | ||
|
||
|
||
class PostgresStorage: | ||
def __init__(self): | ||
params = { | ||
"host": os.environ['APP_POSTGRES_HOST'], | ||
"port": os.environ['APP_POSTGRES_PORT'], | ||
"user": os.environ['APP_POSTGRES_USER'], | ||
"password": os.environ['APP_POSTGRES_PASS'], | ||
"database": os.environ['APP_POSTGRES_DB'], | ||
} | ||
self.conn = psycopg2.connect(**params) | ||
psycopg2.extensions.register_type(psycopg2.extensions.UNICODE, self.conn) | ||
self.conn.set_isolation_level(psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) | ||
self.cursor = self.conn.cursor() | ||
|
||
def apply_migrations(self): | ||
self.cursor.execute(""" | ||
CREATE TABLE IF NOT EXISTS testing (id SERIAL PRIMARY KEY, name VARCHAR(200) NOT NULL); | ||
""") | ||
self.conn.commit() | ||
self.cursor.executemany( | ||
"INSERT INTO testing (name) VALUES (%(name)s);", | ||
[{'name': 'golang'}, {'name': 'gonkey'}, {'name': 'testing'}], | ||
) | ||
self.conn.commit() | ||
|
||
def get_sql_result(self, sql_str): | ||
self.cursor.execute(sql_str) | ||
query_data = list(self.cursor.fetchall()) | ||
self.conn.commit() | ||
return query_data | ||
|
||
|
||
storage = PostgresStorage() | ||
storage.apply_migrations() | ||
|
||
if __name__ == '__main__': | ||
service = socketserver.TCPServer(('', 5000), Handler) | ||
service.serve_forever() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters