-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
101 additions
and
0 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
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 |
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,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 |
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,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))) |
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,6 @@ | ||
google-cloud-bigquery==2.34.4 | ||
Flask==2.1.0 | ||
gunicorn==20.1.0 | ||
pandas | ||
pyarrow | ||
pytest |
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,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() |