Skip to content

Commit

Permalink
Add logging and pytest marker for slow and azure tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jianjie Liu (MAIDAP) committed Jan 26, 2021
1 parent b6d1af7 commit 1cbe5ad
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 6 deletions.
8 changes: 8 additions & 0 deletions tests/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
COMPUTER_VISION_ENDPOINT = "https://enki-vision.cognitiveservices.azure.com/"
SEARCH_SERVICE_NAME = "ocr-ner-pipeline"
SKILLSET_NAME = "testocrskillset"
INDEX_NAME = "testocrindex"
INDEXER_NAME = "testocrindexer"
DATASOURCE_NAME = "syntheticimages"
DATASOURCE_CONTAINER_NAME = "testocrimages"
BLOB_NAME = "syntheticimages"
19 changes: 19 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import logging
import os

import pytest
from dotenv import load_dotenv

from tests.required_env import RequiredEnvVar

ENV_FILEPATH = "tests/.env"


@pytest.fixture(scope="session")
def load_azure_resources():
# Loading the non-secrets
load_dotenv(ENV_FILEPATH)
logging.info(f"Loading .env from {ENV_FILEPATH}")
logging.debug("Printing environment vars: ")
for env in RequiredEnvVar:
logging.debug(f"\t{env.value}: {os.environ.get(env.value)}")
2 changes: 2 additions & 0 deletions tests/e2e/test_anchor_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from genalog.text import alignment, anchor, preprocess


@pytest.mark.slow
@pytest.mark.parametrize(
"gt_file, ocr_file",
zip(
Expand Down Expand Up @@ -34,6 +35,7 @@ def test_align_w_anchor_and_align(gt_file, ocr_file):
)


@pytest.mark.slow
@pytest.mark.parametrize(
"gt_file, ocr_file",
zip(
Expand Down
2 changes: 2 additions & 0 deletions tests/e2e/test_conll_format_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from genalog.text import conll_format


@pytest.mark.slow
@pytest.mark.parametrize(
"required_args", [(["tests/e2e/data/synthetic_dataset", "test_version"])]
)
Expand All @@ -27,6 +28,7 @@ def test_conll_format(required_args, optional_args):
basepath = "tests/e2e/data/conll_formatter/"


@pytest.mark.slow
@pytest.mark.parametrize(
"clean_label_filename, ocr_text_filename",
zip(
Expand Down
11 changes: 9 additions & 2 deletions tests/e2e/test_ocr_e2e.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import json
import logging

import pytest
from dotenv import load_dotenv

from genalog.ocr.blob_client import GrokBlobClient
from genalog.ocr.grok import Grok

load_dotenv("tests/ocr/.env")

@pytest.fixture(scope="module", autouse=True)
def load_azure_config(load_azure_resources):
# Loading the non-secrets
# Assume the secrets are set in the environment variable prior
pass


@pytest.mark.azure
class TestBlobClient:
@pytest.mark.parametrize("use_async", [True, False])
def test_upload_images(self, use_async):
Expand Down Expand Up @@ -43,6 +49,7 @@ def test_upload_images(self, use_async):
), f"folder {dst_folder} was not deleted"


@pytest.mark.azure
class TestGROKe2e:
@pytest.mark.parametrize("use_async", [False, True])
def test_grok_e2e(self, tmpdir, use_async):
Expand Down
24 changes: 24 additions & 0 deletions tests/required_env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from enum import Enum
from itertools import chain


class RequiredSecrets(Enum):
BLOB_KEY = 'BLOB_KEY'
SEARCH_SERVICE_KEY = 'SEARCH_SERVICE_KEY'
COGNITIVE_SERVICE_KEY = 'COGNITIVE_SERVICE_KEY'


class RequiredConstants(Enum):
COMPUTER_VISION_ENDPOINT = 'COMPUTER_VISION_ENDPOINT'
SEARCH_SERVICE_NAME = 'SEARCH_SERVICE_NAME'
SKILLSET_NAME = 'SKILLSET_NAME'
INDEX_NAME = "INDEX_NAME"
INDEXER_NAME = "INDEXER_NAME"
DATASOURCE_NAME = "DATASOURCE_NAME"
DATASOURCE_CONTAINER_NAME = "DATASOURCE_CONTAINER_NAME"
BLOB_NAME = "BLOB_NAME"


RequiredEnvVar = Enum("RequiredEnvVar", [
(i.name, i.value) for i in chain(RequiredSecrets, RequiredConstants)
])
8 changes: 6 additions & 2 deletions tests/unit/ocr/test_ocr.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import json
import os

import pytest
import requests
from dotenv import load_dotenv

from genalog.ocr.rest_client import GrokRestClient


load_dotenv("tests/ocr/.env")
@pytest.fixture(scope="module", autouse=True)
def set_azure_dummy_secrets(load_azure_resources):
os.environ['BLOB_KEY'] = "<YOUR BLOB KEY>"
os.environ['SEARCH_SERVICE_KEY'] = "<YOUR SEARCH SERVICE KEY>"
os.environ['COGNITIVE_SERVICE_KEY'] = "<YOUR COGNITIVE SERVICE KEY>"


@pytest.fixture(autouse=True)
Expand Down
15 changes: 13 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[tox]
envlist = flake8, py


[testenv]
passenv =
# For e2e testing the OCR components
Expand All @@ -9,26 +10,36 @@ passenv =
COGNITIVE_SERVICE_KEY
COMPUTER_VISION_SUBSCRIPTION_KEY
SEARCH_SERVICE_KEY

# Reading additional dependencies to run the test
# https://tox.readthedocs.io/en/latest/example/basic.html#depending-on-requirements-txt-or-defining-constraints
deps = -rrequirements-dev.txt
commands =
pytest


[testenv:flake8]
deps = flake8
skip_install = True
commands = flake8 .


# Configurations for running pytest
[pytest]
junit_family=xunit2
log_cli = False
log_format = %(asctime)s %(levelname)s %(message)s
junit_family = xunit2
# This enable custom marker as decorator "@pytest.mark.slow"
markers =
# These two markers allow to us to run faster subset of the test:
# EX: pytest -m "not slow and not azure"
slow: marks tests as slow (deselect with '-m "not slow"')
azure: marks as integration tests that require azure resource
testpaths =
tests
addopts =
-rsx --cov=genalog --cov-report=html --cov-report=term-missing --cov-report=xml --junitxml=junit/test-results.xml


[flake8]
# Configs for flake8-import-order, see https://pypi.org/project/flake8-import-order/ for more info.
import-order-style=edited
Expand Down

0 comments on commit 1cbe5ad

Please sign in to comment.