diff --git a/device-detection/fiftyone_devicedetection/examples/gettingstarted.py b/device-detection/fiftyone_devicedetection/examples/gettingstarted.py index 05e0e6ea..b17ecaa5 100644 --- a/device-detection/fiftyone_devicedetection/examples/gettingstarted.py +++ b/device-detection/fiftyone_devicedetection/examples/gettingstarted.py @@ -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 diff --git a/device-detection/fiftyone_devicedetection_examples/tests/test_package_gettingstarted_from_root.py b/device-detection/fiftyone_devicedetection_examples/tests/test_package_gettingstarted_from_root.py index 6caac8cd..1449018c 100644 --- a/device-detection/fiftyone_devicedetection_examples/tests/test_package_gettingstarted_from_root.py +++ b/device-detection/fiftyone_devicedetection_examples/tests/test_package_gettingstarted_from_root.py @@ -55,9 +55,16 @@ 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() @@ -65,6 +72,24 @@ def test_runs_from_the_repository_root(self): "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() diff --git a/ip-intelligence/fiftyone_ipintelligence/examples/gettingstarted.py b/ip-intelligence/fiftyone_ipintelligence/examples/gettingstarted.py index 7679d565..b9082597 100644 --- a/ip-intelligence/fiftyone_ipintelligence/examples/gettingstarted.py +++ b/ip-intelligence/fiftyone_ipintelligence/examples/gettingstarted.py @@ -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 diff --git a/ip-intelligence/fiftyone_ipintelligence_examples/src/fiftyone_ipintelligence_examples/example_utils.py b/ip-intelligence/fiftyone_ipintelligence_examples/src/fiftyone_ipintelligence_examples/example_utils.py index fb201446..a5c7c29d 100644 --- a/ip-intelligence/fiftyone_ipintelligence_examples/src/fiftyone_ipintelligence_examples/example_utils.py +++ b/ip-intelligence/fiftyone_ipintelligence_examples/src/fiftyone_ipintelligence_examples/example_utils.py @@ -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(): """! diff --git a/ip-intelligence/fiftyone_ipintelligence_examples/tests/test_package_gettingstarted_from_root.py b/ip-intelligence/fiftyone_ipintelligence_examples/tests/test_package_gettingstarted_from_root.py index 0b5c113e..454bfd06 100644 --- a/ip-intelligence/fiftyone_ipintelligence_examples/tests/test_package_gettingstarted_from_root.py +++ b/ip-intelligence/fiftyone_ipintelligence_examples/tests/test_package_gettingstarted_from_root.py @@ -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 @@ -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() @@ -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()