Skip to content

Commit

Permalink
PEP8 and test suite
Browse files Browse the repository at this point in the history
  • Loading branch information
JulienPeloton committed Nov 25, 2024
1 parent 4d119fa commit d8b5f1d
Show file tree
Hide file tree
Showing 9 changed files with 360 additions and 16 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: PEP8

on:
push:
branches:
- master
pull_request:

jobs:
build:

runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.11]

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install ruff
- name: Lint
run: |
ruff check --statistics *.py
ruff check --statistics apps/
ruff check --ignore D205 tests/
- name: Format
run: |
ruff format --check *.py
ruff format --check apps/
ruff format --check tests/
28 changes: 28 additions & 0 deletions .github/workflows/run_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Sentinel

on:
push:
branches:
- main
pull_request:

jobs:
test-suite:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.11]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Install Python dependencies
run: |
pip install --upgrade pip setuptools wheel
pip install -r requirements.txt
cd ..
- name: Run test suites
run: |
./run_tests.sh --url ${{ secrets.APIURLDEV }}
4 changes: 3 additions & 1 deletion apps/routes/objects/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

bp = Blueprint("objects", __name__)


# Enable CORS for this blueprint
@bp.after_request
def after_request(response):
Expand Down Expand Up @@ -53,7 +54,7 @@ def after_request(response):
{
"name": "columns",
"required": False,
"description": f"Comma-separated data columns to transfer. Default is all columns.",
"description": "Comma-separated data columns to transfer. Default is all columns.",
},
{
"name": "output-format",
Expand All @@ -62,6 +63,7 @@ def after_request(response):
},
]


@bp.route("/api/v1/objects", methods=["GET"])
def return_object_arguments():
"""Obtain information about retrieving object data"""
Expand Down
21 changes: 10 additions & 11 deletions apps/routes/objects/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import pandas as pd
from numpy import array as nparray
from apps.utils.utils import download_cutout
from apps.utils.client import connect_to_hbase_table
from apps.utils.decoding import format_hbase_output
from apps.utils.decoding import format_hbase_output, hbase_to_dict


def extract_object_data(payload: dict) -> pd.DataFrame:
Expand Down Expand Up @@ -101,9 +102,9 @@ def extract_object_data(payload: dict) -> pd.DataFrame:
else:
colname = "b:cutout{}_stampData".format(cutout_kind)
pdf[colname] = pdf[["i:objectId", "i:candid"]].apply(
lambda x: pd.Series(
[download_cutout(x.iloc[0], x.iloc[1], cutout_kind)]
),
lambda x: pd.Series([
download_cutout(x.iloc[0], x.iloc[1], cutout_kind)
]),
axis=1,
)

Expand Down Expand Up @@ -145,12 +146,10 @@ def extract_object_data(payload: dict) -> pd.DataFrame:

if "i:jd" in pdfUP.columns:
# workaround -- see https://github.com/astrolabsoftware/fink-science-portal/issues/216
mask = np.array(
[
False if float(i) in pdf["i:jd"].to_numpy() else True
for i in pdfUP["i:jd"].to_numpy()
]
)
mask = nparray([
False if float(i) in pdf["i:jd"].to_numpy() else True
for i in pdfUP["i:jd"].to_numpy()
])
pdfUP = pdfUP[mask]

# Hacky way to avoid converting concatenated column to float
Expand All @@ -171,4 +170,4 @@ def extract_object_data(payload: dict) -> pd.DataFrame:

client.close()

return pdf
return pdf
9 changes: 5 additions & 4 deletions apps/routes/template/api.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Copyright 2024 AstroLab Software
# Author: Julien Peloton
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -20,6 +20,7 @@

bp = Blueprint("template", __name__)


# Enable CORS for this blueprint
@bp.after_request
def after_request(response):
Expand All @@ -42,6 +43,7 @@ def after_request(response):
},
]


@bp.route("/api/v1/template", methods=["GET"])
def return_template_arguments():
"""Obtain information about retrieving object data"""
Expand All @@ -51,6 +53,7 @@ def return_template_arguments():
else:
return jsonify({"args": ARGS})


@bp.route("/api/v1/template", methods=["POST"])
def return_template(payload=None):
"""Retrieve object data"""
Expand All @@ -70,5 +73,3 @@ def return_template(payload=None):

output_format = payload.get("output-format", "json")
return send_tabular_data(out, output_format)


1 change: 1 addition & 0 deletions apps/routes/template/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
# limitations under the License.
import pandas as pd


def my_function(payload):
return pd.DataFrame({payload["arg1"]: [1, 2, 3]})
3 changes: 3 additions & 0 deletions apps/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
"""Various utilities"""

import io
import json
import yaml
Expand All @@ -23,6 +24,7 @@
from astropy.table import Table
from astropy.io import votable


def extract_configuration(filename):
"""Extract user defined configuration
Expand All @@ -43,6 +45,7 @@ def extract_configuration(filename):
config["APIURL"] = "http://" + config["HOST"] + ":" + str(config["PORT"])
return config


def download_cutout(objectId, candid, kind):
""" """
config = extract_configuration("config.yml")
Expand Down
50 changes: 50 additions & 0 deletions run_tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/bash
# Copyright 2024 AstroLab Software
# Author: Julien Peloton
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e
message_help="""
Run the test suite of the modules\n\n
Usage:\n
\t./run_tests.sh [--url]\n\n
--url is the Science Portal URL you would like to test against.
"""
# Grab the command line arguments
while [ "$#" -gt 0 ]; do
case "$1" in
--url)
URL="$2"
shift 2
;;
-h)
echo -e $message_help
exit
;;
esac
done

if [[ -f $URL ]]; then
echo "You need to specify an URL" $URL
exit
fi

# Run the test suite on the utilities
cd tests
for filename in ./*.py
do
echo $filename
# Run test suite
python $filename $URL
done
Loading

0 comments on commit d8b5f1d

Please sign in to comment.