Skip to content

Commit

Permalink
Spaced test requests. Adjusted tests for changes in classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
miballe committed Nov 7, 2024
1 parent 45794c5 commit bbd383a
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 141 deletions.
2 changes: 2 additions & 0 deletions src/factiva/analytics/common/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,5 @@
ACTION_CONSOLE_INDICATOR[REP_ACTION] = ':'
ACTION_CONSOLE_INDICATOR[DEL_ACTION] = '&'
ACTION_CONSOLE_INDICATOR[ERR_ACTION] = '!'

TEST_REQUEST_SPACING_SECONDS = 3
33 changes: 15 additions & 18 deletions src/factiva/analytics/snapshots/time_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class SnapshotTimeSeriesQuery(SnapshotBaseQuery):
date_field : str
Schema date-time field used to calculate the time-series dataset
group_dimension : str
List of fields to break-down aggregates per time period unit
Field name to break-down aggregates per time period unit
top : str
Max entries per group_dimension per time period unit
"""
Expand All @@ -100,7 +100,7 @@ def __init__(self,
exclude_lists: dict = None,
frequency: str = const.API_MONTH_PERIOD,
date_field:str = const.API_PUBLICATION_DATETIME_FIELD,
group_dimension: str = 'source_code',
group_dimension: str = None,
top: int = 10):
"""
Class constructor
Expand Down Expand Up @@ -133,7 +133,7 @@ def __init__(self,
any of the three values: ``publication_datetime`` (default), ``modification_datetime``,
and ``ingestion_datetime``.
group_dimension : str, optional (default: 'source_code')
Field that will be used to break-down subtotals for each period. Allowed values are
Field name that will be used to break-down subtotals for each period. Allowed values are one of the following:
``['source_code', 'subject_codes',
'region_codes', 'industry_codes', 'company_codes', 'person_codes', 'company_codes_about',
'company_codes_relevance', 'company_codes_cusip', 'company_codes_isin',
Expand All @@ -157,10 +157,13 @@ def __init__(self,
tools.validate_field_options(date_field, const.API_DATETIME_FIELDS)
self.date_field = date_field

if group_dimension in const.API_GROUP_DIMENSIONS_FIELDS:
self.group_dimension = group_dimension
if group_dimension:
if group_dimension in const.API_GROUP_DIMENSIONS_FIELDS:
self.group_dimension = group_dimension
else:
raise ValueError('Group dimension is not valid')
else:
raise ValueError('Group dimension is not valid')
self.group_dimension = None

tools.validate_type(top, int, "Unexpected value for top")
if top >= 0:
Expand Down Expand Up @@ -190,17 +193,11 @@ def get_payload(self) -> dict:
tools.validate_field_options(self.date_field, const.API_DATETIME_FIELDS)
payload["query"].update({"date_field": self.date_field})

# if(self.group_dimension):
# if(len(self.group_dimension)<=4):
# for option in self.group_dimension:
# tools.validate_field_options(option, const.API_GROUP_DIMENSIONS_FIELDS)
# else:
# raise ValueError("The maximiun group_dimensions size is 4")
# else:
# self.group_dimension = []

payload["query"].update(
{"group_dimensions": [self.group_dimension]})
if(self.group_dimension):
payload["query"].update(
{"group_dimension": [self.group_dimension]})
else:
self.group_dimension = []

payload["query"].update({"top": self.top})

Expand All @@ -216,7 +213,7 @@ def __str__(self, detailed=True, prefix=' ├─', root_prefix=''):
ret_val = ret_val.replace('└─ex', '├─ex')
ret_val += f"\n{prefix}frequency: {tools.print_property(self.frequency)}"
ret_val += f"\n{prefix}date_field: {tools.print_property(self.date_field)}"
ret_val += f"\n{prefix}group_dimensions: {tools.print_property(self.group_dimension)}"
ret_val += f"\n{prefix}group_dimension: {tools.print_property(self.group_dimension)}"
ret_val += f"\n{prefix[0:-2]}└─top: {tools.print_property(self.top)}"
return ret_val

Expand Down
16 changes: 13 additions & 3 deletions test/article_retrieval/test_articleretrieval.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
Tests for the ArticleRetrieval module
"""
import pytest
import time
from factiva.analytics import OAuthUser, ArticleRetrieval, UIArticle
from factiva.analytics.common import config
from factiva.analytics.common import config, const

GITHUB_CI = config.load_environment_value('CI', False)
FACTIVA_CLIENTID = config.load_environment_value("FACTIVA_CLIENTID")
FACTIVA_USERNAME = config.load_environment_value("FACTIVA_USERNAME")
FACTIVA_PASSWORD = config.load_environment_value("FACTIVA_PASSWORD")
Expand All @@ -28,19 +30,27 @@ def _assert_uiarticle_values(uiarticle: UIArticle):

def test_article_retrieval_env_user():
""""
Creates the object using the ENV variable and request the usage details to the API service
Creates the object using the ENV variable and request the article content to the API service
"""
if GITHUB_CI:
pytest.skip("Not to be tested in GitHub Actions")
time.sleep(const.TEST_REQUEST_SPACING_SECONDS)
ar = ArticleRetrieval()
article = ar.retrieve_single_article(ARTICLE_ID)
_assert_uiarticle_values(article)


def test_article_retrieval_params_user():
"""
Creates the object using the passed params and request the article content to the API service
"""
if GITHUB_CI:
pytest.skip("Not to be tested in GitHub Actions")
time.sleep(const.TEST_REQUEST_SPACING_SECONDS)
o = OAuthUser(client_id=FACTIVA_CLIENTID,
username=FACTIVA_USERNAME,
password=FACTIVA_PASSWORD)
ar = ArticleRetrieval(oauth_user=o)
article = ar.retrieve_single_article(ARTICLE_ID)
_assert_uiarticle_values(article)


62 changes: 7 additions & 55 deletions test/auth/test_accountinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,14 @@
Tests for the AccountInfo module
"""
import pytest
import time
from factiva.analytics import AccountInfo
from factiva.analytics.common import config
from factiva.analytics.common import config, const

GITHUB_CI = config.load_environment_value('CI', False)
FACTIVA_USERKEY = config.load_environment_value("FACTIVA_USERKEY")
DUMMY_KEY = 'abcd1234abcd1234abcd1234abcd1234'

# API Response sample with the most complete set of attributes
# {
# "data": {
# "id": "abcd1234abcd1234abcd1234abcd1234",
# "attributes": {
# "cnt_curr_ext": 1,
# "current_downloaded_amount": 427567508,
# "max_allowed_concurrent_extracts": 10,
# "max_allowed_document_extracts": 2500000,
# "max_allowed_extracts": 5,
# "name": "Company Corp",
# "products": "DNA",
# "tot_document_extracts": 1595383,
# "tot_extracts": 4,
# "tot_subscriptions": 0,
# "tot_topics": 0,
# "licensed_company_ids": [
# 4,
# 3,
# 1,
# 5
# ],
# "enabled_company_identifiers": [
# {
# "id": 4,
# "name": "isin"
# },
# {
# "id": 3,
# "name": "cusip"
# },
# {
# "id": 1,
# "name": "sedol"
# },
# {
# "id": 5,
# "name": "ticker_exchange"
# }
# ]
# },
# "type": "account_with_contract_limits"
# }
# }


def _test_userkey_types(usr):
""""
Expand All @@ -65,7 +21,7 @@ def _test_userkey_types(usr):
assert isinstance(usr.user_key.cloud_token, dict)
assert isinstance(usr.account_name, str)
assert isinstance(usr.active_product, str)
assert isinstance(usr.max_allowed_concurrent_extractions, int)
# assert isinstance(usr.max_allowed_concurrent_extractions, int)
assert isinstance(usr.max_allowed_extracted_documents, int)
assert isinstance(usr.max_allowed_extractions, int)
assert isinstance(usr.remaining_documents, int)
Expand All @@ -90,7 +46,7 @@ def _test_userkey_values(usr):
assert usr.user_key.key == FACTIVA_USERKEY
assert len(usr.account_name) >= 0
assert len(usr.active_product) >= 0
assert usr.max_allowed_concurrent_extractions >= 0
# assert usr.max_allowed_concurrent_extractions >= 0
assert usr.max_allowed_extracted_documents >= 0
assert usr.max_allowed_extractions >= 0
# assert usr.total_downloaded_bytes >= 0
Expand All @@ -105,6 +61,7 @@ def test_invalid_key():
Creates an object from the provided string and request the usage details to the API service
The key is invalid and this should validate how the error is processed
"""
time.sleep(const.TEST_REQUEST_SPACING_SECONDS)
with pytest.raises(ValueError, match=r'Factiva User-Key does not exist or inactive.'):
AccountInfo(DUMMY_KEY)

Expand All @@ -120,8 +77,7 @@ def test_userkey_with_stats():
""""
Creates the object using the ENV variable and request the usage details to the API service
"""
if GITHUB_CI:
pytest.skip("Not to be tested in GitHub Actions")
time.sleep(const.TEST_REQUEST_SPACING_SECONDS)
usr = AccountInfo()
_test_userkey_types(usr)
_test_userkey_values(usr)
Expand All @@ -131,11 +87,7 @@ def test_user_with_parameter_and_stats():
"""
API Key is passed as a string
"""
if GITHUB_CI:
pytest.skip("Not to be tested in GitHub Actions")
time.sleep(const.TEST_REQUEST_SPACING_SECONDS)
usr = AccountInfo(FACTIVA_USERKEY)
_test_userkey_types(usr)
_test_userkey_values(usr)



6 changes: 3 additions & 3 deletions test/auth/test_oauthuser.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
Tests for the UserKey module
"""
import pytest
import time
from factiva.analytics import OAuthUser
from factiva.analytics.common import config
from factiva.analytics.common import config, const

GITHUB_CI = config.load_environment_value('CI', False)
FACTIVA_CLIENTID = config.load_environment_value("FACTIVA_CLIENTID")
Expand Down Expand Up @@ -51,8 +52,7 @@ def test_wrong_credentials():
Creates an object from the provided string
The key is invalid and this should validate how the error is processed
"""
if GITHUB_CI:
pytest.skip("Not to be tested in GitHub Actions")
time.sleep(const.TEST_REQUEST_SPACING_SECONDS)
with pytest.raises(PermissionError, match=r'Invalid user credentials'):
o = OAuthUser(client_id='client_id_value',
username='username_value',
Expand Down
12 changes: 5 additions & 7 deletions test/auth/test_userkey.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
Tests for the UserKey module
"""
import pytest
import time
from factiva.analytics import UserKey
from factiva.analytics.common import config
from factiva.analytics.common import config, const

GITHUB_CI = config.load_environment_value('CI', False)
FACTIVA_USERKEY = config.load_environment_value("FACTIVA_USERKEY")
Expand All @@ -14,8 +15,7 @@ def test_userkey_from_env():
"""
Creates an empty object from the ENV variable with a value only for the key property
"""
if GITHUB_CI:
pytest.skip("Not to be tested in GitHub Actions")
time.sleep(const.TEST_REQUEST_SPACING_SECONDS)
usr = UserKey()
assert usr.key == FACTIVA_USERKEY
assert isinstance(usr.cloud_token, dict)
Expand All @@ -25,8 +25,7 @@ def test_user_with_parameter_and_stats():
"""
API Key is passed as a string and stats=True
"""
if GITHUB_CI:
pytest.skip("Not to be tested in GitHub Actions")
time.sleep(const.TEST_REQUEST_SPACING_SECONDS)
usr = UserKey(FACTIVA_USERKEY)
assert usr.key == FACTIVA_USERKEY
assert isinstance(usr.cloud_token, dict)
Expand All @@ -37,8 +36,7 @@ def test_invalid_key():
Creates an object from the provided string and request the usage details to the API service
The key is invalid and this should validate how the error is processed
"""
if GITHUB_CI:
pytest.skip("Not to be tested in GitHub Actions")
time.sleep(const.TEST_REQUEST_SPACING_SECONDS)
with pytest.raises(ValueError, match=r'Factiva User-Key does not exist or inactive.'):
UserKey(DUMMY_KEY)

Expand Down
17 changes: 7 additions & 10 deletions test/snapshots/test_explain.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import pytest
import time
from factiva.analytics.common import config, const
from factiva.analytics import SnapshotExplain, UserKey, SnapshotExplainQuery
import pandas as pd
Expand All @@ -13,8 +14,7 @@
# Test operations before any API request

def test_create_from_envuser():
if GITHUB_CI:
pytest.skip("Not to be tested in GitHub Actions")
time.sleep(const.TEST_REQUEST_SPACING_SECONDS)
se = SnapshotExplain()
assert isinstance(se, SnapshotExplain)
assert se.user_key.key == ENVIRONMENT_USER_KEY
Expand All @@ -25,8 +25,7 @@ def test_create_from_envuser():
}

def test_create_from_user_param():
if GITHUB_CI:
pytest.skip("Not to be tested in GitHub Actions")
time.sleep(const.TEST_REQUEST_SPACING_SECONDS)
se = SnapshotExplain(user_key=VALID_USER_KEY)
assert isinstance(se, SnapshotExplain)
assert se.user_key.key == VALID_USER_KEY
Expand All @@ -37,8 +36,7 @@ def test_create_from_user_param():
}

def test_create_from_userkey():
if GITHUB_CI:
pytest.skip("Not to be tested in GitHub Actions")
time.sleep(const.TEST_REQUEST_SPACING_SECONDS)
u = UserKey()
assert isinstance(u, UserKey)
se = SnapshotExplain(user_key=u)
Expand All @@ -51,8 +49,7 @@ def test_create_from_userkey():
}

def test_create_envuser_where():
if GITHUB_CI:
pytest.skip("Not to be tested in GitHub Actions")
time.sleep(const.TEST_REQUEST_SPACING_SECONDS)
se = SnapshotExplain(query=VALID_WHERE_STATEMENT)
assert isinstance(se, SnapshotExplain)
assert se.user_key.key == ENVIRONMENT_USER_KEY
Expand All @@ -63,8 +60,7 @@ def test_create_envuser_where():
}

def test_create_envuser_envwhere():
if GITHUB_CI:
pytest.skip("Not to be tested in GitHub Actions")
time.sleep(const.TEST_REQUEST_SPACING_SECONDS)
seq = SnapshotExplainQuery()
assert isinstance(seq, SnapshotExplainQuery)
se = SnapshotExplain(query=seq)
Expand All @@ -90,6 +86,7 @@ def test_failed_where_and_jobid():
def test_job_envwhere_samples():
if GITHUB_CI:
pytest.skip("Not to be tested in GitHub Actions")
time.sleep(const.TEST_REQUEST_SPACING_SECONDS)
se = SnapshotExplain()
assert isinstance(se, SnapshotExplain)
assert se.process_job()
Expand Down
Loading

0 comments on commit bbd383a

Please sign in to comment.