Skip to content

Commit

Permalink
toolinfo: Allow direct input of keywords as array of strings
Browse files Browse the repository at this point in the history
We want folks submitting toolinfo records via the API directly to be
able to provide keywords as an array of strings rather than the legacy
comma separated string from the Hay's Directory toolinfo standard.

Change-Id: I26327f224ee758f018f3a077d87b2a7b713c6e3f
  • Loading branch information
bd808 committed Jan 26, 2021
1 parent e0603cd commit 97bfdf1
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion toolhub/apps/toolinfo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def _normalize_record(self, record):
fixed.append(value)
record[field] = fixed

if "keywords" in record:
if "keywords" in record and isinstance(record["keywords"], str):
record["keywords"] = list(
filter(
None, (s.strip() for s in record["keywords"].split(","))
Expand Down
61 changes: 61 additions & 0 deletions toolhub/apps/toolinfo/test/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,67 @@ def test_from_toolinfo(self):
self.assertFalse(updated)
self.assertTool(obj, self.toolinfo)

def test_legacy_toolforge_name_fix(self):
"""Names starting with 'toolforge.' are slugified."""
fixture = self.toolinfo.copy()
fixture["name"] = "toolforge.some-tool"
obj, created, updated = models.Tool.objects.from_toolinfo(
fixture, self.user, models.Tool.ORIGIN_CRAWLER
)

self.assertTrue(created)
self.assertFalse(updated)
self.assertTool(
obj, {**self.toolinfo, **{"name": "toolforge-some-tool"}}
)
self.assertEqual(obj.name, "toolforge-some-tool")

def test_url_multilingual_fixups(self):
"""url_multilingual fields should be normalized."""
fixture = self.toolinfo.copy()
obj, created, updated = models.Tool.objects.from_toolinfo(
fixture, self.user, models.Tool.ORIGIN_CRAWLER
)

self.assertTrue(created)
self.assertFalse(updated)
self.assertTool(
obj,
{
**self.toolinfo,
**{
"developer_docs_url": {
"language": "en",
"url": "https://toolhub.wikimedia.org/static/docs/index.html",
}
},
},
)

def test_keywords_string_to_array(self):
"""Keywords as a string will convert to an array."""
fixture = self.toolinfo.copy()
fixture["keywords"] = "a, b"
obj, created, updated = models.Tool.objects.from_toolinfo(
fixture, self.user, models.Tool.ORIGIN_CRAWLER
)

self.assertTrue(created)
self.assertFalse(updated)
self.assertTool(obj, {**self.toolinfo, **{"keywords": ["a", "b"]}})

def test_allow_keywords_as_array(self):
"""Keywords can be an array in the input."""
fixture = self.toolinfo.copy()
fixture["keywords"] = ["a", "b"]
obj, created, updated = models.Tool.objects.from_toolinfo(
fixture, self.user, models.Tool.ORIGIN_CRAWLER
)

self.assertTrue(created)
self.assertFalse(updated)
self.assertTool(obj, {**self.toolinfo, **{"keywords": ["a", "b"]}})

def test_from_toolinfo_origin_change(self):
"""Expect a validation error when changing a Tool's origin."""
models.Tool.objects.from_toolinfo(
Expand Down

0 comments on commit 97bfdf1

Please sign in to comment.