Skip to content

Commit

Permalink
Add github action CI for space2stats api
Browse files Browse the repository at this point in the history
  • Loading branch information
zacdezgeo committed Jul 18, 2024
1 parent b447a85 commit 4f0e079
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 32 deletions.
35 changes: 35 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Run Tests

on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-latest

env:
DB_HOST: localhost
DB_PORT: 5432
DB_NAME: mydatabase
DB_USER: myuser
DB_PASSWORD: mypassword
DB_TABLE_NAME: space2stats

steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.11

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r space2stats_api/requirements.txt
- name: Set PYTHONPATH
run: echo "PYTHONPATH=$(pwd)/space2stats_api" >> $GITHUB_ENV

- name: Run tests
run: pytest space2stats_api/tests
28 changes: 12 additions & 16 deletions notebooks/visualize_nyc_sample.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,18 @@
"cells": [
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Collecting geojson_pydantic\n",
" Downloading geojson_pydantic-1.1.0-py3-none-any.whl.metadata (4.1 kB)\n",
"Requirement already satisfied: geojson_pydantic in /Users/zacdez/Library/Caches/pypoetry/virtualenvs/notebooks-sNYx7QfP-py3.12/lib/python3.12/site-packages (1.1.0)\n",
"Requirement already satisfied: pydantic~=2.0 in /Users/zacdez/Library/Caches/pypoetry/virtualenvs/notebooks-sNYx7QfP-py3.12/lib/python3.12/site-packages (from geojson_pydantic) (2.7.0)\n",
"Requirement already satisfied: annotated-types>=0.4.0 in /Users/zacdez/Library/Caches/pypoetry/virtualenvs/notebooks-sNYx7QfP-py3.12/lib/python3.12/site-packages (from pydantic~=2.0->geojson_pydantic) (0.6.0)\n",
"Requirement already satisfied: pydantic-core==2.18.1 in /Users/zacdez/Library/Caches/pypoetry/virtualenvs/notebooks-sNYx7QfP-py3.12/lib/python3.12/site-packages (from pydantic~=2.0->geojson_pydantic) (2.18.1)\n",
"Requirement already satisfied: typing-extensions>=4.6.1 in /Users/zacdez/Library/Caches/pypoetry/virtualenvs/notebooks-sNYx7QfP-py3.12/lib/python3.12/site-packages (from pydantic~=2.0->geojson_pydantic) (4.9.0)\n",
"Downloading geojson_pydantic-1.1.0-py3-none-any.whl (8.6 kB)\n",
"Installing collected packages: geojson_pydantic\n",
"Successfully installed geojson_pydantic-1.1.0\n",
"\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m24.0\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m24.1.2\u001b[0m\n",
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
Expand All @@ -31,7 +27,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -44,7 +40,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -55,7 +51,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand All @@ -77,7 +73,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -109,7 +105,7 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -160,7 +156,7 @@
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": 8,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -529,7 +525,7 @@
"[10 rows x 39 columns]"
]
},
"execution_count": 44,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -540,7 +536,7 @@
},
{
"cell_type": "code",
"execution_count": 46,
"execution_count": 9,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -570,15 +566,15 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "05f3c0e7e24b4e7fbbf7868addaf6046",
"model_id": "b80af907501741a5a53bed80677e7eb4",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Map(basemap_style=<CartoBasemap.DarkMatter: 'https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json'…"
]
},
"execution_count": 46,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
Expand Down
5 changes: 2 additions & 3 deletions space2stats_api/app/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pydantic import BaseSettings
from pydantic_settings import BaseSettings, SettingsConfigDict


class Settings(BaseSettings):
Expand All @@ -9,8 +9,7 @@ class Settings(BaseSettings):
DB_PASSWORD: str
DB_TABLE_NAME: str

class Config:
env_file = "../db.env"
model_config = SettingsConfigDict(env_file="../db.env")


settings = Settings()
5 changes: 1 addition & 4 deletions space2stats_api/app/utils/db_utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import psycopg as pg

from ..settings import settings


DB_HOST = settings.DB_HOST
DB_PORT = settings.DB_PORT
DB_NAME = settings.DB_NAME
DB_USER = settings.DB_USER
DB_PASSWORD = settings.DB_PASSWORD
DB_TABLE_NAME = settings.DB_TABLE_NAME
DB_TABLE_NAME = settings.DB_TABLE_NAME or "space2stats"


def get_summaries(fields, h3_ids):
Expand Down Expand Up @@ -44,7 +42,6 @@ def get_available_fields():
FROM information_schema.columns
WHERE table_name = '{DB_TABLE_NAME}'
"""

try:
conn = pg.connect(
host=DB_HOST,
Expand Down
3 changes: 2 additions & 1 deletion space2stats_api/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ pytest
psycopg[binary]
httpx
geojson-pydantic
shapely
shapely
pydantic-settings>=2.0.0
12 changes: 4 additions & 8 deletions space2stats_api/tests/test_db_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import pytest
import unittest
from unittest.mock import patch, Mock
from app.utils.db_utils import get_summaries, get_available_fields

Expand All @@ -21,12 +21,10 @@ def test_get_summaries(mock_connect):
mock_cursor.execute.assert_called_once_with(
f"""
SELECT hex_id, {', '.join(fields)}
FROM space2stats_nyc_sample
FROM space2stats
WHERE hex_id IN ('hex_1')
"""
)
mock_cursor.close.assert_called_once()
mock_conn.close.assert_called_once()

assert rows == [("hex_1", 100, 200)]
assert colnames == ["hex_id", "field1", "field2"]
Expand All @@ -48,14 +46,12 @@ def test_get_available_fields(mock_connect):
f"""
SELECT column_name
FROM information_schema.columns
WHERE table_name = 'space2stats_nyc_sample'
WHERE table_name = 'space2stats'
"""
)
mock_cursor.close.assert_called_once()
mock_conn.close.assert_called_once()

assert columns == ["field1", "field2", "field3"]


if __name__ == "__main__":
pytest.main()
unittest.main()

0 comments on commit 4f0e079

Please sign in to comment.