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
43 changes: 33 additions & 10 deletions notebooks/code_samples/custom_tests/implement_custom_tests.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -567,28 +567,51 @@
"outputs": [],
"source": [
"import requests\n",
"import random\n",
"\n",
"\n",
"@vm.test(\"my_custom_tests.ExternalAPI\")\n",
"def external_api():\n",
" \"\"\"This test calls an external API to get the current BTC price. It then creates\n",
" \"\"\"This test calls an external API to get a list of fake users. It then creates\n",
" a table with the relevant data so it can be displayed in the documentation.\n",
"\n",
" The purpose of this test is to demonstrate how to call an external API and use the\n",
" data in a test. A test like this could even be setup to run in a scheduled\n",
" pipeline to keep your documentation in-sync with an external data source.\n",
" \"\"\"\n",
" url = \"https://api.coindesk.com/v1/bpi/currentprice.json\"\n",
" url = \"https://jsonplaceholder.typicode.com/users\"\n",
" response = requests.get(url)\n",
" data = response.json()\n",
"\n",
" # extract the time and the current BTC price in USD\n",
" return [\n",
" {\n",
" \"Time\": data[\"time\"][\"updated\"],\n",
" \"Price (USD)\": data[\"bpi\"][\"USD\"][\"rate\"],\n",
" }\n",
" ]\n",
" return {\n",
" \"Model Owners/Stakeholders\": [\n",
" {\n",
" \"Name\": user[\"name\"],\n",
" \"Role\": random.choice([\"Owner\", \"Stakeholder\"]),\n",
" \"Email\": user[\"email\"],\n",
" \"Phone\": user[\"phone\"],\n",
" \"Slack Handle\": f\"@{user['name'].lower().replace(' ', '.')}\",\n",
" }\n",
" for user in data[:3]\n",
" ],\n",
" \"Model Developers\": [\n",
" {\n",
" \"Name\": user[\"name\"],\n",
" \"Role\": \"Developer\",\n",
" \"Email\": user[\"email\"],\n",
" }\n",
" for user in data[3:7]\n",
" ],\n",
" \"Model Validators\": [\n",
" {\n",
" \"Name\": user[\"name\"],\n",
" \"Role\": \"Validator\",\n",
" \"Email\": user[\"email\"],\n",
" }\n",
" for user in data[7:]\n",
" ],\n",
" }\n",
"\n",
"\n",
"result = run_test(\"my_custom_tests.ExternalAPI\")\n",
Expand All @@ -601,7 +624,7 @@
"source": [
"Again, you can add this to your documentation to see how it looks:\n",
"\n",
"![screenshot showing BTC price metric](../../images/btc-price-custom-metric.png)"
"![screenshot showing BTC price metric](../../images/external-data-custom-test.png)"
]
},
{
Expand Down Expand Up @@ -944,7 +967,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,31 +352,54 @@
"outputs": [],
"source": [
"import requests\n",
"import random\n",
"\n",
"\n",
"@test(\"my_custom_tests.ExternalAPI\")\n",
"def external_api():\n",
" \"\"\"This test calls an external API to get the current BTC price. It then creates\n",
" \"\"\"This test calls an external API to get a list of fake users. It then creates\n",
" a table with the relevant data so it can be displayed in the documentation.\n",
"\n",
" The purpose of this test is to demonstrate how to call an external API and use the\n",
" data in a test. A test like this could even be setup to run in a scheduled\n",
" pipeline to keep your documentation in-sync with an external data source.\n",
" \"\"\"\n",
" url = \"https://api.coindesk.com/v1/bpi/currentprice.json\"\n",
" url = \"https://jsonplaceholder.typicode.com/users\"\n",
" response = requests.get(url)\n",
" data = response.json()\n",
"\n",
" # extract the time and the current BTC price in USD\n",
" return [\n",
" {\n",
" \"Time\": data[\"time\"][\"updated\"],\n",
" \"Price (USD)\": data[\"bpi\"][\"USD\"][\"rate\"],\n",
" }\n",
" ]\n",
"\n",
"\n",
"external_api.save(tests_folder, imports=[\"import requests\"])"
" return {\n",
" \"Model Owners/Stakeholders\": [\n",
" {\n",
" \"Name\": user[\"name\"],\n",
" \"Role\": random.choice([\"Owner\", \"Stakeholder\"]),\n",
" \"Email\": user[\"email\"],\n",
" \"Phone\": user[\"phone\"],\n",
" \"Slack Handle\": f\"@{user['name'].lower().replace(' ', '.')}\",\n",
" }\n",
" for user in data[:3]\n",
" ],\n",
" \"Model Developers\": [\n",
" {\n",
" \"Name\": user[\"name\"],\n",
" \"Role\": \"Developer\",\n",
" \"Email\": user[\"email\"],\n",
" }\n",
" for user in data[3:7]\n",
" ],\n",
" \"Model Validators\": [\n",
" {\n",
" \"Name\": user[\"name\"],\n",
" \"Role\": \"Validator\",\n",
" \"Email\": user[\"email\"],\n",
" }\n",
" for user in data[7:]\n",
" ],\n",
" }\n",
"\n",
"\n",
"external_api.save(tests_folder, imports=[\"import requests\", \"import random\"])"
]
},
{
Expand Down Expand Up @@ -904,7 +927,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.9"
"version": "3.11.5"
}
},
"nbformat": 4,
Expand Down
Binary file removed notebooks/images/btc-price-custom-metric.png
Binary file not shown.
Binary file added notebooks/images/external-data-custom-test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading