Skip to content

Commit

Permalink
Configuring github actions to run on live api (#128)
Browse files Browse the repository at this point in the history
pytest github actions runs on live APIs #101
  • Loading branch information
jhnwllr committed Mar 4, 2024
1 parent 1180c24 commit af56dab
Show file tree
Hide file tree
Showing 54 changed files with 26,928 additions and 59,466 deletions.
54 changes: 54 additions & 0 deletions .github/workflows/python_live_tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
name: Python live API tests

on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ['3.11']
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GBIF_USER: ${{ secrets.GBIF_USER }}
GBIF_PWD: ${{ secrets.GBIF_PWD }}
GBIF_EMAIL: ${{ secrets.GBIF_EMAIL }}
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Install system dependencies
run: |
sudo add-apt-repository ppa:ubuntugis/ppa
sudo apt-get update
sudo apt-get install -y libgeos-c1v5 libgeos-dev
- name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Delete vcr cassettes so that it runs live tests
run: |
rm -rf test/vcr_cassettes/*
if [ "$(ls -A test/vcr_cassettes)" ]; then
echo "Directory is not empty"
exit 1
fi
- name: Tests
run: pytest

- name: Check if vcr cassettes were created today
run: |
for file in test/vcr_cassettes/*; do
file_date=$(stat -c %y "$file" | cut -d ' ' -f 1)
echo "File $file was created on $file_date"
current_date=$(date +%Y-%m-%d)
if [[ "$file_date" != "$current_date" ]]; then
echo "File $file was not created today"
exit 1
fi
done
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
2 changes: 1 addition & 1 deletion pygbif/caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def caching(
requests_cache.install_cache(
cache_name=CACHE_NAME, backend=backend, expire_after=expire_after
)
requests_cache.delete(expired=True)
requests_cache.delete(expired=True)()

This comment has been minimized.

Copy link
@airnst

airnst Mar 6, 2024

This is not working because there is nothing to call. Why did you add the parentheses?

This comment has been minimized.

Copy link
@jhnwllr

jhnwllr Mar 6, 2024

Author Contributor

@airnst Sorry this floated in from another earlier PR. We will fix it thanks!

This comment has been minimized.

Copy link
@airnst

airnst Mar 6, 2024

Thank you for fixing it so quickly!


cache_settings = {
"cache": cache,
Expand Down
16 changes: 8 additions & 8 deletions test/test-occurrences-count.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
from pygbif import occurrences

brecord_res = [
"LITERATURE",
"OBSERVATION",
"LIVING_SPECIMEN",
"UNKNOWN",
"MATERIAL_SAMPLE",
"PRESERVED_SPECIMEN",
"MACHINE_OBSERVATION",
"HUMAN_OBSERVATION",
"FOSSIL_SPECIMEN",
"PRESERVED_SPECIMEN",
"MATERIAL_SAMPLE",
"OBSERVATION",
"MACHINE_OBSERVATION",
"OCCURRENCE",
"FOSSIL_SPECIMEN",
"MATERIAL_CITATION",
"LIVING_SPECIMEN"
]

year_res = [
Expand Down
1 change: 0 additions & 1 deletion test/test-occurrences-download_cancel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from pygbif import occurrences as occ
import vcr


@vcr.use_cassette(
"test/vcr_cassettes/test_download_cancel.yaml", filter_headers=["authorization"]
)
Expand Down
12 changes: 6 additions & 6 deletions test/test-occurrences-get.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@
@vcr.use_cassette("test/vcr_cassettes/test_get.yaml")
def test_get():
"occurrences.get - basic test"
res = occurrences.get(key=1248387548)
res = occurrences.get(key=1986620884)
assert "dict" == res.__class__.__name__
assert len(res) > 30
assert 1248387548 == res["key"]
assert 1986620884 == res["key"]


@vcr.use_cassette("test/vcr_cassettes/test_get_verbatim.yaml")
def test_get_verbatim():
"occurrences.get_verbatim - basic test"
res = occurrences.get_verbatim(key=1248387548)
res = occurrences.get_verbatim(key=1986620884)
assert "dict" == res.__class__.__name__
assert len(res) > 20
assert 1248387548 == res["key"]
assert 1986620884 == res["key"]


@vcr.use_cassette("test/vcr_cassettes/test_get_fragment.yaml")
def test_get_fragment():
"occurrences.get_fragment - basic test"
res = occurrences.get_fragment(key=1052909293)
res = occurrences.get_fragment(key=1986620884)
assert "dict" == res.__class__.__name__
assert "humanobservation" == res["basisOfRecord"]
assert "HumanObservation" == res["basisOfRecord"]
2 changes: 1 addition & 1 deletion test/test-registry-dataset_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def test_dataset_metrics():
"registry.dataset_metrics - basic test"
res = registry.dataset_metrics(uuid="3f8a1297-3259-4700-91fc-acc4170b27ce")
assert dict == res.__class__
assert 19 == len(res)
assert 18 == len(res)


@vcr.use_cassette("test/vcr_cassettes/test_dataset_metrics_multiple_uuids.yaml")
Expand Down
4 changes: 2 additions & 2 deletions test/test-registry-networks.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def test_networks():
res = registry.networks()
assert dict == res.__class__
assert 2 == len(res)
assert 4 == len(res["data"])
assert 11 == len(res["data"])
assert ["data", "meta"] == sorted(res.keys())


Expand All @@ -18,7 +18,7 @@ def test_networks_limit():
"registry.networks - limit param "
res = registry.networks(limit=5)
assert dict == res.__class__
assert 4 == len(res["data"])
assert 5 == len(res["data"])


@vcr.use_cassette("test/vcr_cassettes/test_networks_uuid.yaml")
Expand Down
2 changes: 1 addition & 1 deletion test/test-species-name_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def test_name_lookup():
assert dict == res.__class__
assert 6 == len(res)
assert 100 == len(res["results"])
assert "Mammalia" == res["results"][0]["canonicalName"]
assert "Mammaliaformes" == res["results"][0]["canonicalName"]


@vcr.use_cassette("test/vcr_cassettes/test_name_lookup_paging.yaml")
Expand Down
61 changes: 43 additions & 18 deletions test/vcr_cassettes/test_count.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,51 @@ interactions:
- request:
body: null
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Connection: [keep-alive]
user-agent: ['python-requests/2.22.0,pygbif/0.3.9']
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
user-agent:
- python-requests/2.31.0,pygbif/0.6.3
method: GET
uri: https://api.gbif.org/v1/occurrence/count?taxonKey=3329049
response:
body: {string: '443'}
body:
string: '874'
headers:
Accept-Ranges: [bytes]
Access-Control-Allow-Methods: ['HEAD, GET, POST, DELETE, PUT, OPTIONS']
Access-Control-Allow-Origin: ['*']
Age: ['648']
Cache-Control: ['public, max-age=600']
Connection: [keep-alive]
Content-Length: ['3']
Content-Type: [application/json]
Date: ['Wed, 20 Nov 2019 17:47:00 GMT']
Server: [Jetty(9.3.z-SNAPSHOT)]
Via: [1.1 varnish (Varnish/5.2)]
X-Varnish: [1003619963 1001194130]
status: {code: 200, message: OK}
Accept-Ranges:
- bytes
Age:
- '0'
Cache-Control:
- public, max-age=600
Connection:
- keep-alive
Content-Length:
- '3'
Content-Type:
- application/json
Date:
- Fri, 01 Mar 2024 08:32:14 GMT
Expires:
- '0'
Pragma:
- no-cache
Vary:
- Origin, Access-Control-Request-Method, Access-Control-Request-Headers
Via:
- 1.1 varnish (Varnish/6.0)
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Varnish:
- '726239410'
X-XSS-Protection:
- 1; mode=block
status:
code: 200
message: OK
version: 1
61 changes: 43 additions & 18 deletions test/vcr_cassettes/test_count_basisofrecord.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,51 @@ interactions:
- request:
body: null
headers:
Accept: ['*/*']
Accept-Encoding: ['gzip, deflate']
Connection: [keep-alive]
user-agent: ['python-requests/2.22.0,pygbif/0.3.9']
Accept:
- '*/*'
Accept-Encoding:
- gzip, deflate
Connection:
- keep-alive
user-agent:
- python-requests/2.31.0,pygbif/0.6.3
method: GET
uri: https://api.gbif.org/v1/occurrence/counts/basisOfRecord
response:
body: {string: '{"HUMAN_OBSERVATION":1105239040,"PRESERVED_SPECIMEN":166101035,"MATERIAL_SAMPLE":25326157,"OBSERVATION":19633469,"UNKNOWN":17292300,"MACHINE_OBSERVATION":11917524,"FOSSIL_SPECIMEN":8961546,"LIVING_SPECIMEN":1629229,"LITERATURE":219727}'}
body:
string: '{"HUMAN_OBSERVATION":2300698840,"PRESERVED_SPECIMEN":237830598,"MATERIAL_SAMPLE":55644452,"OBSERVATION":20774268,"MACHINE_OBSERVATION":20542140,"OCCURRENCE":19297806,"FOSSIL_SPECIMEN":10246202,"MATERIAL_CITATION":2664586,"LIVING_SPECIMEN":2000680}'
headers:
Accept-Ranges: [bytes]
Access-Control-Allow-Methods: ['HEAD, GET, POST, DELETE, PUT, OPTIONS']
Access-Control-Allow-Origin: ['*']
Age: ['0']
Cache-Control: ['public, max-age=600']
Connection: [keep-alive]
Content-Length: ['235']
Content-Type: [application/json]
Date: ['Wed, 20 Nov 2019 17:57:49 GMT']
Server: [Jetty(9.3.z-SNAPSHOT)]
Via: [1.1 varnish (Varnish/5.2)]
X-Varnish: ['1006275396']
status: {code: 200, message: OK}
Accept-Ranges:
- bytes
Age:
- '657'
Cache-Control:
- public, max-age=600
Connection:
- keep-alive
Content-Length:
- '247'
Content-Type:
- application/json
Date:
- Fri, 01 Mar 2024 08:21:17 GMT
Expires:
- '0'
Pragma:
- no-cache
Vary:
- Origin, Access-Control-Request-Method, Access-Control-Request-Headers
Via:
- 1.1 varnish (Varnish/6.0)
X-Content-Type-Options:
- nosniff
X-Frame-Options:
- DENY
X-Varnish:
- 704940086 710148283
X-XSS-Protection:
- 1; mode=block
status:
code: 200
message: OK
version: 1
Loading

0 comments on commit af56dab

Please sign in to comment.