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 @@ -26,20 +26,21 @@

# You need to create a resource key at https://configure.51degrees.com?utm_source=code&utm_medium=example&utm_campaign=device-detection-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-failuretomatch.py&utm_term=top
# and paste it into the code, replacing !!YOUR_RESOURCE_KEY!! below.
# Alternatively, add a resource_key environment variable
import os
if "resource_key" in os.environ:
resource_key = os.environ["resource_key"]
else:
resource_key = "!!YOUR_RESOURCE_KEY!!"
# Alternatively, set the _51DEGREES_RESOURCE_KEY environment variable. The
# key is read through ExampleUtils, as every other example reads it, so
# this one answers to the same variable names as the rest and a reader who
# has set one of them does not have to work out why only this example says
# it has no key.
from fiftyone_devicedetection_examples.example_utils import ExampleUtils

resource_key = ExampleUtils.get_resource_key() or "!!YOUR_RESOURCE_KEY!!"

if resource_key == "!!YOUR_RESOURCE_KEY!!":
print("""
You need to create a resource key at
https://configure.51degrees.com?utm_source=code&utm_medium=example&utm_campaign=device-detection-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-failuretomatch.py&utm_term=resource-key-required and paste it into the code,
'replacing !!YOUR_RESOURCE_KEY!!
To include the properties used in this example, go to https://configure.51degrees.com/bxXqZhLT?utm_source=code&utm_medium=example&utm_campaign=device-detection-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-failuretomatch.py&utm_term=resource-key-required
""")
print(ExampleUtils.get_missing_resource_key_message())
# The address stays on one line, because the campaign lint reads a
# line at a time and a split address looks to it like a missing tag.
print(" To include the properties used in this example, go to "
"https://configure.51degrees.com/bxXqZhLT?utm_source=code&utm_medium=example&utm_campaign=device-detection-python&utm_content=fiftyone_devicedetection_examples-src-fiftyone_devicedetection_examples-cloud-failuretomatch.py&utm_term=resource-key-required")
else:

pipeline = DeviceDetectionCloudPipelineBuilder({
Expand Down
28 changes: 28 additions & 0 deletions fiftyone_devicedetection_examples/tests/test_cloudexamples.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@

import inspect
import json5
import os
from pathlib import Path
import subprocess
import sys
import unittest
from fiftyone_devicedetection_examples.cloud.nativemodellookup_console import NativeModelLookupConsole
from fiftyone_devicedetection_examples.cloud.taclookup_console import TacLookupConsole
Expand Down Expand Up @@ -149,3 +152,28 @@ def test_cloud_configurator_console(self):

self.assertIn("device.ismobile: ", output)
self.assertNotEqual("device.ismobile:", output.strip())

def test_cloud_failuretomatch(self):
"""The failure to match example is a script rather than a class,
so it is run as one. It is run with the current resource key
variable only, because it read the older name alone and told
anyone using the current one that no key was set."""

example = os.path.join(
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
"src", "fiftyone_devicedetection_examples", "cloud",
"failuretomatch.py")
environment = dict(os.environ)
environment.pop(ExampleUtils.LEGACY_RESOURCE_KEY_ENV_VAR, None)
environment[ExampleUtils.RESOURCE_KEY_ENV_VAR] = self.resource_key

finished = subprocess.run(
[sys.executable, example], capture_output=True, text=True,
env=environment, timeout=120)

output = finished.stdout + finished.stderr
self.assertEqual(0, finished.returncode, output)
self.assertNotIn("No resource key found", output)
self.assertIn("a mobile device?", output)
for marker in FAULT_MARKERS:
self.assertNotIn(marker, output)
Loading