Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
aitilabs authored Sep 2, 2023
1 parent 893b11d commit 4da7b5f
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM python:3.10-slim

ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./

RUN pip install --no-cache-dir -r requirements.txt

CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app
30 changes: 30 additions & 0 deletions cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
steps:
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/cli-project-genai/demo-flask-app:$COMMIT_SHA', '.']

- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/cli-project-genai/demo-flask-app:$COMMIT_SHA']

- name: 'gcr.io/cli-project-genai/demo-flask-app:$COMMIT_SHA'
entrypoint: 'bash'
args:
- '-c'
- |
python -m pytest
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: gcloud
args:
- 'run'
- 'deploy'
- 'py-bq-load'
- '--image'
- 'gcr.io/cli-project-genai/demo-flask-app:$COMMIT_SHA'
- '--region'
- 'us-central1'
- '--allow-unauthenticated'
images:
- 'gcr.io/cli-project-genai/demo-flask-app:$COMMIT_SHA'

options:
logging: CLOUD_LOGGING_ONLY
28 changes: 28 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from google.cloud import bigquery
from flask import Flask
from flask import request
import os

app = Flask(__name__)
client = bigquery.Client()

@app.route('/')
def main(big_query_client=client):
table_id = "cli-project-genai.test_schema.us_states"
job_config = bigquery.LoadJobConfig(
write_disposition=bigquery.WriteDisposition.WRITE_TRUNCATE,
source_format=bigquery.SourceFormat.CSV,
skip_leading_rows=1,
)
uri = "gs://rando-aitilabs/us-states.csv"
load_job = big_query_client.load_table_from_uri(
uri, table_id, job_config=job_config
)

load_job.result()

destination_table = big_query_client.get_table(table_id)
return {"data": destination_table.num_rows}

if __name__ == "__main__":
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 5052)))
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
google-cloud-bigquery==2.34.4
Flask==2.1.0
gunicorn==20.1.0
pandas
pyarrow
pytest
28 changes: 28 additions & 0 deletions test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import pytest
from unittest.mock import patch,MagicMock
from main import app, client as app_client

@pytest.fixture
def client():
with app.test_client() as client:
yield client

@patch.object(app_client,'load_table_from_uri')
@patch.object(app_client,'get_table')
def test_main_endpoint(mock_get_table, mock_load_table_from_uri, client):
mock_load_job = MagicMock()
mock_load_table_from_uri.return_value = mock_load_job

mock_table = MagicMock()
mock_table.num_rows = 50
mock_get_table.return_value = mock_table

response = client.get('/')
assert response.status_code == 200
data = response.get_json()
assert 'data' in data
assert data['data'] == 50

mock_load_table_from_uri.assert_called_once()
mock_load_job.result.assert_called_once()
mock_get_table.assert_called_once()

0 comments on commit 4da7b5f

Please sign in to comment.