Skip to content

Commit

Permalink
Test environment (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
12rambau authored Dec 13, 2023
2 parents c251074 + 8a67e78 commit 43020b7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/unit.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
- main
pull_request:

env:
EARTHENGINE_TOKEN: ${{ secrets.EARTHENGINE_TOKEN }}

jobs:
lint:
runs-on: ubuntu-latest
Expand Down
41 changes: 41 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@
"""

# -- Path setup ----------------------------------------------------------------
import os
import re
from datetime import datetime
from pathlib import Path

import ee
import httplib2

# -- Project information -------------------------------------------------------
project = "ipygee"
Expand Down Expand Up @@ -64,3 +70,38 @@

# -- Options for intersphinx output --------------------------------------------
intersphinx_mapping = {}

# -- Script to authenticate to Earthengine using a token -----------------------
def gee_configure() -> None:
"""Initialize earth engine according to the environment.
It will use the creddential file if the EARTHENGINE_TOKEN env variable exist.
Otherwise it use the simple Initialize command (asking the user to register if necessary).
"""
# only do the initialization if the credential are missing
if not ee.data._credentials:

# if the credentials token is asved in the environment use it
if "EARTHENGINE_TOKEN" in os.environ:

# get the token from environment variable
ee_token = os.environ["EARTHENGINE_TOKEN"]

# as long as RDT quote the token, we need to remove the quotes before writing
# the string to the file
pattern = r"^'[^']*'$"
if re.match(pattern, ee_token) is not None:
ee_token = ee_token[1:-1]

# write the token to the appropriate folder
credential_folder_path = Path.home() / ".config" / "earthengine"
credential_folder_path.mkdir(parents=True, exist_ok=True)
credential_file_path = credential_folder_path / "credentials"
credential_file_path.write_text(ee_token)

# if the user is in local development the authentication should
# already be available
ee.Initialize(http_transport=httplib2.Http())


gee_configure()
11 changes: 8 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ classifiers = [
]
requires-python = ">=3.8"
dependencies = [
"deprecated>=1.2.14"
"deprecated>=1.2.14",
"earthengine-api",
]

[[project.authors]]
Expand All @@ -43,14 +44,18 @@ test = [
"pytest",
"pytest-sugar",
"pytest-cov",
"pytest-deadfixtures"
"pytest-deadfixtures",
"httplib2",
"pytest-gee",
]
doc = [
"sphinx>=6.2.1",
"pydata-sphinx-theme",
"sphinx-copybutton",
"sphinx-design",
"sphinx-autoapi"
"sphinx-autoapi",
"jupyter-sphinx",
"httplib2",
]

[tool.hatch.build.targets.wheel]
Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
"""Pytest session configuration."""

import pytest_gee


def pytest_configure():
"""Configure test environment."""
pytest_gee.init_ee_from_token()
6 changes: 6 additions & 0 deletions tests/test_ipygee.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
"""Test the ipygee package."""
import ee

import ipygee


def test_hello_world():
"""Hello world test."""
assert ipygee.Hello().hello_world() == "hello world !"


def test_gee_connection():
"""Test the geeconnection is working."""
assert ee.Number(1).getInfo() == 1

0 comments on commit 43020b7

Please sign in to comment.