Skip to content

Commit

Permalink
name_for_model() function, refs #1
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Mar 24, 2023
1 parent e4988e5 commit f408606
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
15 changes: 14 additions & 1 deletion datasette_chatgpt_plugin/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
from datasette import hookimpl
import hashlib
from urllib.parse import urlparse


def name_for_model(url):
name = urlparse(url).hostname.replace(".", "_").replace("-", "_")
md5_hash = hashlib.md5(url.encode("utf-8")).hexdigest()[:6]
output = f"datasette_{name}_{md5_hash}"

if len(output) > 50:
excess_length = len(output) - 50
output = f"datasette_{name[:-excess_length]}_{md5_hash}"

return output
17 changes: 17 additions & 0 deletions tests/test_chatgpt_plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datasette.app import Datasette
from datasette_chatgpt_plugin import name_for_model
import pytest


Expand All @@ -9,3 +10,19 @@ async def test_plugin_is_installed():
assert response.status_code == 200
installed_plugins = {p["name"] for p in response.json()}
assert "datasette-chatgpt-plugin" in installed_plugins


@pytest.mark.parametrize(
"input,expected",
(
("https://example.com", "datasette_example_com_c984d0"),
(
"https://congress-legislators.datasettes.com/",
"datasette_congress_legislators_datasettes_c_91d42f",
),
),
)
def test_name_for_model(input, expected):
output = name_for_model(input)
assert len(output) <= 50
assert output == expected

0 comments on commit f408606

Please sign in to comment.