Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@

# First create the device detection pipeline with the desired settings.

# The free Lite data file, found in the folder this runs in or a folder
# The data file named in the _51DEGREES_DD_PATH environment variable, or
# else the free Lite data file, found in the folder this runs in or a folder
# above it, or downloaded when it is missing.
data_file = ExampleUtils.find_file("51Degrees-LiteV4.1.hash")
resource_key = "!!YOUR_RESOURCE_KEY!!"
data_file = (ExampleUtils.get_data_file_path()
or ExampleUtils.find_file("51Degrees-LiteV4.1.hash"))
# The resource key for the Cloud line below, from the
# _51DEGREES_RESOURCE_KEY environment variable.
resource_key = ExampleUtils.get_resource_key() or "!!YOUR_RESOURCE_KEY!!"

# Uncommment the line below 'On-Premise' and comment the line below 'Cloud' to
# switch to an On-Premise installation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,41 @@ def test_runs_from_the_repository_root(self):
folder = os.getcwd()
os.chdir(ROOT)
try:
# The builder shares usage, so nothing is sent from a test.
with mock.patch("requests.post"), redirect_stdout(printed):
runpy.run_path(str(EXAMPLE), run_name="__main__")
# The free file is the one under test, so a data file named in
# the environment, as CI names the Enterprise file, is set
# aside. The builder shares usage, so nothing is sent from a
# test.
with mock.patch.dict(os.environ):
for name in (ExampleUtils.DD_PATH_ENV_VAR,
ExampleUtils.DATA_FILE_ENV_VAR):
os.environ.pop(name, None)
with mock.patch("requests.post"), redirect_stdout(printed):
runpy.run_path(str(EXAMPLE), run_name="__main__")
finally:
os.chdir(folder)
said = printed.getvalue()
self.assertIn(
"Who made the device? \n" + ExampleUtils.NEEDS_ENTERPRISE, said)
self.assertIn(CONTACT_US_TAG, said)

def test_reads_the_data_file_from_the_environment(self):
# A data file named in _51DEGREES_DD_PATH that is not there makes
# the example stop naming it, which shows the variable was read.
missing = str(
ROOT / "no-such-folder" / "named-by-the-environment.hash")
folder = os.getcwd()
os.chdir(ROOT)
try:
with mock.patch.dict(os.environ, {"_51DEGREES_DD_PATH": missing}):
with mock.patch("requests.post"):
with redirect_stdout(io.StringIO()):
with self.assertRaises(Exception) as caught:
runpy.run_path(
str(EXAMPLE), run_name="__main__")
finally:
os.chdir(folder)
self.assertIn(missing, str(caught.exception))


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,14 @@

# First create the IP intelligence pipeline with the desired settings.

# The free Lite data file, found in the folder this runs in or a folder
# The data file named in the _51DEGREES_IPI_PATH environment variable, or
# else the free Lite data file, found in the folder this runs in or a folder
# above it, or downloaded when it is missing.
data_file = ExampleUtils.find_file("51Degrees-LiteV41.ipi")
resource_key = "!!YOUR_RESOURCE_KEY!!"
data_file = (ExampleUtils.get_data_file_path()
or ExampleUtils.find_file("51Degrees-LiteV41.ipi"))
# The resource key for the Cloud line below, from the
# _51DEGREES_RESOURCE_KEY environment variable.
resource_key = ExampleUtils.get_resource_key() or "!!YOUR_RESOURCE_KEY!!"

# Uncommment the line below 'On-Premise' and comment the line below 'Cloud' to
# switch to an On-Premise installation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ def get_value(ip_data, property):
except Exception:
return None

@staticmethod
def get_data_file_path():
"""!
The data file named in the environment, or None.
"""

return ExampleUtils.__get_env_variable(
ExampleUtils.IPI_PATH_ENV_VAR, ExampleUtils.DATA_FILE_ENV_VAR)

@staticmethod
def get_resource_key():
"""!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import unittest
from contextlib import redirect_stdout
from pathlib import Path
from unittest import mock

from fiftyone_ipintelligence_examples.example_utils import ExampleUtils

Expand All @@ -54,8 +55,15 @@ def test_runs_from_the_repository_root(self):
folder = os.getcwd()
os.chdir(ROOT)
try:
with redirect_stdout(printed):
runpy.run_path(str(EXAMPLE), run_name="__main__")
# The free file is the one under test, so a data file named in
# the environment, as CI names the Enterprise file, is set
# aside.
with mock.patch.dict(os.environ):
for name in (ExampleUtils.IPI_PATH_ENV_VAR,
ExampleUtils.DATA_FILE_ENV_VAR):
os.environ.pop(name, None)
with redirect_stdout(printed):
runpy.run_path(str(EXAMPLE), run_name="__main__")
finally:
os.chdir(folder)
said = printed.getvalue()
Expand All @@ -65,6 +73,24 @@ def test_runs_from_the_repository_root(self):
said)
self.assertIn(CONTACT_US_TAG, said)

def test_reads_the_data_file_from_the_environment(self):
# A data file named in _51DEGREES_IPI_PATH that is not there makes
# the example stop naming it, which shows the variable was read.
missing = str(
ROOT / "no-such-folder" / "named-by-the-environment.ipi")
folder = os.getcwd()
os.chdir(ROOT)
try:
with mock.patch.dict(os.environ, {"_51DEGREES_IPI_PATH": missing}):
with mock.patch("requests.post"):
with redirect_stdout(io.StringIO()):
with self.assertRaises(Exception) as caught:
runpy.run_path(
str(EXAMPLE), run_name="__main__")
finally:
os.chdir(folder)
self.assertIn(missing, str(caught.exception))


if __name__ == "__main__":
unittest.main()
Loading