Repository: lexibank/pylexibank

Issue:
Add check that metadata['title'] follows recommended form?
Do we want to check if the title follows the [recommended form](https://github.com/lexibank/lexibank/blob/master/CONTRIBUTING.md#titles-for-lexibank-datasets): 

```
  "title": "CLDF dataset derived from AUTHOR's \"SHORTTITLE\" from YEAR."
```

(even superficially like `assert title.startswith("CLDF dataset")`)

Discussion:
SimonGreenhill: Hmm, wouldn't this be better implemented as a [check](https://github.com/lexibank/pylexibank/blob/master/src/pylexibank/commands/check_lexibank.py) rather than a test? 
LinguList: Nope, the rule is "Smith's", it is never "Smithes". In German, we have
some quirks, but not in English.

SimonGreenhill: Yes, they should raise warnings on buildbot and should be caught.
xrotwang: @SimonGreenhill but if we add this to something run by buildbot for **all** datasets, we will get false positives, e.g. there will be "born-CLDF" datasets at some point - or the CLDF will become the authoritative version, e.g. for WOLD. Then "CLDF dataset derived from ..." does not make sense anymore. So running this from a dataset's `test.py` seems appropriate.
codecov-io: # [Codecov](https://codecov.io/gh/lexibank/pylexibank/pull/214?src=pr&el=h1) Report
> Merging [#214](https://codecov.io/gh/lexibank/pylexibank/pull/214?src=pr&el=desc) into [master](https://codecov.io/gh/lexibank/pylexibank/commit/42ebfccd439b80fcad71f986a0759749f9db532a&el=desc) will **increase** coverage by `0.03%`.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/lexibank/pylexibank/pull/214/graphs/tree.svg?width=650&height=150&src=pr&token=v0oYsvytpU)](https://codecov.io/gh/lexibank/pylexibank/pull/214?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #214      +/-   ##
==========================================
+ Coverage   99.17%   99.20%   +0.03%     
==========================================
  Files          30       32       +2     
  Lines        1693     1884     +191     
==========================================
+ Hits         1679     1869     +190     
- Misses         14       15       +1     
```


| [Impacted Files](https://codecov.io/gh/lexibank/pylexibank/pull/214?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/pylexibank/\_\_init\_\_.py](https://codecov.io/gh/lexibank/pylexibank/pull/214/diff?src=pr&el=tree#diff-c3JjL3B5bGV4aWJhbmsvX19pbml0X18ucHk=) | `100.00% <100.00%> (ø)` | |
| [src/pylexibank/commands/check\_lexibank.py](https://codecov.io/gh/lexibank/pylexibank/pull/214/diff?src=pr&el=tree#diff-c3JjL3B5bGV4aWJhbmsvY29tbWFuZHMvY2hlY2tfbGV4aWJhbmsucHk=) | `83.78% <100.00%> (+14.21%)` | :arrow_up: |
| [src/pylexibank/commands/check\_profile.py](https://codecov.io/gh/lexibank/pylexibank/pull/214/diff?src=pr&el=tree#diff-c3JjL3B5bGV4aWJhbmsvY29tbWFuZHMvY2hlY2tfcHJvZmlsZS5weQ==) | `100.00% <100.00%> (ø)` | |
| [src/pylexibank/commands/format\_profile.py](https://codecov.io/gh/lexibank/pylexibank/pull/214/diff?src=pr&el=tree#diff-c3JjL3B5bGV4aWJhbmsvY29tbWFuZHMvZm9ybWF0X3Byb2ZpbGUucHk=) | `100.00% <100.00%> (ø)` | |
| [src/pylexibank/dataset.py](https://codecov.io/gh/lexibank/pylexibank/pull/214/diff?src=pr&el=tree#diff-c3JjL3B5bGV4aWJhbmsvZGF0YXNldC5weQ==) | `100.00% <100.00%> (ø)` | |
| [src/pylexibank/metadata.py](https://codecov.io/gh/lexibank/pylexibank/pull/214/diff?src=pr&el=tree#diff-c3JjL3B5bGV4aWJhbmsvbWV0YWRhdGEucHk=) | `100.00% <100.00%> (ø)` | |
| [src/pylexibank/models.py](https://codecov.io/gh/lexibank/pylexibank/pull/214/diff?src=pr&el=tree#diff-c3JjL3B5bGV4aWJhbmsvbW9kZWxzLnB5) | `100.00% <100.00%> (ø)` | |
| [src/pylexibank/profile.py](https://codecov.io/gh/lexibank/pylexibank/pull/214/diff?src=pr&el=tree#diff-c3JjL3B5bGV4aWJhbmsvcHJvZmlsZS5weQ==) | `100.00% <100.00%> (ø)` | |
| [src/pylexibank/providers/tob.py](https://codecov.io/gh/lexibank/pylexibank/pull/214/diff?src=pr&el=tree#diff-c3JjL3B5bGV4aWJhbmsvcHJvdmlkZXJzL3RvYi5weQ==) | `100.00% <100.00%> (ø)` | |
| [src/pylexibank/db.py](https://codecov.io/gh/lexibank/pylexibank/pull/214/diff?src=pr&el=tree#diff-c3JjL3B5bGV4aWJhbmsvZGIucHk=) | `99.21% <0.00%> (-0.79%)` | :arrow_down: |
| ... and [2 more](https://codecov.io/gh/lexibank/pylexibank/pull/214/diff?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/lexibank/pylexibank/pull/214?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/lexibank/pylexibank/pull/214?src=pr&el=footer). Last update [6d10128...fe96bf7](https://codecov.io/gh/lexibank/pylexibank/pull/214?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

xrotwang: tbh. I'm a bit reluctant due to the requirement to install pylexibank for tests. This won't be a problem when tests are run from buildbot, because pylexibank will already be present, but I wouldn't want to make the tests run from travis more brittle. So maybe the recommendation would be
- to do the import **in** the test function and exclude the test when run on travis with
```python
@pytest.mark.skipff("TRAVIS" in os.environ and os.environ["TRAVIS"] == "true", reason="Skipping this test on Travis CI.")
```

Patch:
diff --git a/setup.py b/setup.py
index 8873953..d59cb48 100755
--- a/setup.py
+++ b/setup.py
@@ -28,12 +28,12 @@ setup(
     platforms='any',
     python_requires='>=3.5',
     install_requires=[
+        'attrs>=19.2',
         'segments>=2.1.1',
         'cldfbench[excel]>=1.0',
         'csvw>=1.5.6',
         'clldutils>=2.8.0',
         'pycldf>=1.7.0',
-        'attrs>=18.1.0',
         'pyglottolog>=2.0',
         'pyconcepticon>=2.1.0',
         'pyclts>=2.0',
@@ -49,7 +49,7 @@ setup(
         'dev': ['flake8', 'wheel', 'twine'],
         'test': [
             'mock',
-            'pytest>=3.6',
+            'pytest>=5',
             'pytest-mock',
             'pytest-cov',
             'coverage>=4.2',
diff --git a/src/pylexibank/metadata.py b/src/pylexibank/metadata.py
index 1fdd3df..f79829c 100644
--- a/src/pylexibank/metadata.py
+++ b/src/pylexibank/metadata.py
@@ -1,12 +1,40 @@
+import re
 import pkg_resources
 
 import attr
 from cldfbench.metadata import Metadata
 
-__all__ = ['LexibankMetadata']
+__all__ = ['LexibankMetadata', 'check_standard_title']
 
 version = pkg_resources.get_distribution('pylexibank').version
 
+STANDARD_TITLE_PATTERN = re.compile(
+    r'CLDF dataset derived from\s+'
+    r'(?P<authors>[^"]+)'
+    r'"(?P<title>[^"]+)"\s+from\s+'
+    r'(?P<year>[0-9]{4})'
+)
+
+
+def check_standard_title(title):
+    """
+    Assert a title conforms to the standard format.
+
+    > CLDF dataset derived from AUTHOR's "TITLE" from YEAR
+
+    Usage: In a dataset's `test.py`:
+    ```python
+    import os, pytest
+
+    @pytest.mark.skipif("TRAVIS" in os.environ and os.environ["TRAVIS"] == "true")
+    def test_valid_title(cldf_dataset, cldf_logger):
+        from pylexibank import check_standard_title
+        check_standard_title(cldf_dataset.metadata_dict['dc:title'])
+    ```
+    """
+    match = STANDARD_TITLE_PATTERN.fullmatch(title)
+    assert match and match.group('authors').strip().endswith(("'s", "s'"))
+
 
 @attr.s
 class LexibankMetadata(Metadata):


Test patch:
diff --git a/tests/test_metadata.py b/tests/test_metadata.py
index 80ee591..97b390e 100644
--- a/tests/test_metadata.py
+++ b/tests/test_metadata.py
@@ -1,3 +1,5 @@
+import pytest
+
 from pylexibank.metadata import *
 
 
@@ -5,3 +7,24 @@ def test_conceptlist():
     md = LexibankMetadata(conceptlist='Swadesh-1964-100', related='abc')
     assert isinstance(md.conceptlist, list)
     assert 'concepticon.clld.org' in md.markdown()
+
+
+STANDARD_TITLE = """CLDF dataset derived from A et al.'s "Paper" from 1934"""
+
+
+@pytest.mark.parametrize('title', [
+    "",
+    STANDARD_TITLE.replace('CLDF', 'cldf'),
+    STANDARD_TITLE.replace("et al.'s", 'et al.’s'),
+    STANDARD_TITLE.replace('"', "'"),
+    STANDARD_TITLE.replace('1934', '34'),
+])
+def test_invalid_standard_title(title):
+    with pytest.raises(AssertionError):
+        check_standard_title(title)
+
+
+def test_valid_standard_title():
+    assert check_standard_title(STANDARD_TITLE) is None
+    assert check_standard_title(STANDARD_TITLE.replace(" et al.'s", "brams'")) is None
+