<commit_before>Locking tests
=============

Simple case
+++++++++++

>>> import os
>>> import StringIO
>>> import zeit.connector.connector
>>> import zeit.connector.resource
>>> connector = zeit.connector.connector.Connector(roots={
...       "default": os.environ['connector-url']})

>>> id = 'http://xml.zeit.de/testing/lt1'
>>> res = zeit.connector.resource.Resource(
...     id, 'lt1', 'text',
...     StringIO.StringIO('Pop goes the weasel!'),
...     contentType='text/plain')
>>> connector.add(res)
>>> connector.lock(res.id, 'frodo', until=None)
'opaquelocktoken:...'

>>> connector.lock(res.id, 'gandalf', until=None)
Traceback (most recent call last):
    ...
LockingError: http://xml.zeit.de/testing/lt1 is already locked.

>>> connector.unlock(res.id)
>>> del connector[res.id]


Edge case 1: We think a resource is locked but it isn't
+++++++++++++++++++++++++++++++++++++++++++++++++++++++

We unlock the resource under the hood and of course expect that we can add it:

>>> res.id = 'http://xml.zeit.de/testing/edge1'
>>> token = connector.lock(res.id, 'saruman', until=None)
>>> url = connector._id2loc(res.id)
>>> connector._conn().do_unlock(url, token)
>>> connector.add(res)
>>> connector.unlock(res.id)
>>> del connector[res.id]

Edge case 1.1: With existing resource:

>>> res.id = 'http://xml.zeit.de/testing/edge1.1'
>>> connector.add(res)
>>> token = connector.lock(res.id, 'saruman', until=None)
>>> url = connector._id2loc(res.id)
>>> connector._conn().do_unlock(url, token)
>>> connector.add(res)
>>> connector.unlock(res.id)
>>> del connector[res.id]


Edge case 2: We think a resource is not locked, but it is
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Lock the resoruce under the hood; we get a useful error:

>>> res.id = 'http://xml.zeit.de/testing/edge2'
>>> url = connector._id2loc(res.id)
>>> token = connector._conn().do_lock(url, owner='gimli', depth=0, timeout=20)
>>> connector.add(res)
Traceback (most recent call last):
    ...
LockedByOtherSystemError: (u'gimli', datetime.datetime(...))
>>> connector.unlock(res.id, token)

Edge case 2.2: With existing resource:

>>> res.id = 'http://xml.zeit.de/testing/edge2.2'
>>> connector.add(res)
>>> url = connector._id2loc(res.id)
>>> token = connector._conn().do_lock(url, owner='gimli', depth=0, timeout=20)
>>> connector.add(res)
Traceback (most recent call last):
    ...
LockedByOtherSystemError: (u'gimli', datetime.datetime(...))
>>> connector.unlock(res.id, token)
>>> del connector[res.id]

Edge case 3: Locked resource w/o locking user
+++++++++++++++++++++++++++++++++++++++++++++

When a resource is locked for instance with cadaver the connector was telling
the resource would not be locked. Verify the bug is fixed. We need the internal
API to create an anonymous lock. Create a resource first:

>>> res.id = 'http://xml.zeit.de/testing/edge3'
>>> connector.add(res)

Now lock:

>>> dav_url = connector._id2loc(connector._get_cannonical_id(res.id))
>>> token = connector._conn().do_lock(dav_url)

The connector tells us it is locked now:

>>> connector.locked(res.id)
(None, datetime.datetime(9998, 12, 31, 23, 59, 59, 999999, tzinfo=<UTC>),
 False)


Deleting the resource now will yield a `LockedByOtherSystemError`:

>>> del connector[res.id]
Traceback (most recent call last):
    ...
LockedByOtherSystemError: (None, datetime.datetime(9998, 12, 31, 23, 59, 59, 999999, tzinfo=<UTC>))

Unlock the resource and remove it:

>>> connector._conn().do_unlock(dav_url, token)
>>> del connector[res.id]<commit_msg>added another edge-case test<commit_after>Locking tests
=============

Simple case
+++++++++++

>>> import os
>>> import StringIO
>>> import zeit.connector.connector
>>> import zeit.connector.resource
>>> connector = zeit.connector.connector.Connector(roots={
...       "default": os.environ['connector-url']})

>>> id = 'http://xml.zeit.de/testing/lt1'
>>> res = zeit.connector.resource.Resource(
...     id, 'lt1', 'text',
...     StringIO.StringIO('Pop goes the weasel!'),
...     contentType='text/plain')
>>> connector.add(res)
>>> connector.lock(res.id, 'frodo', until=None)
'opaquelocktoken:...'

>>> connector.lock(res.id, 'gandalf', until=None)
Traceback (most recent call last):
    ...
LockingError: http://xml.zeit.de/testing/lt1 is already locked.

>>> connector.unlock(res.id)
>>> del connector[res.id]


Edge case 1: We think a resource is locked but it isn't
+++++++++++++++++++++++++++++++++++++++++++++++++++++++

We unlock the resource under the hood and of course expect that we can add it:

>>> res.id = 'http://xml.zeit.de/testing/edge1'
>>> token = connector.lock(res.id, '<PASSWORD>uman', until=None)
>>> url = connector._id2loc(res.id)
>>> connector._conn().do_unlock(url, token)
>>> connector.add(res)
>>> connector.unlock(res.id)
>>> del connector[res.id]

Edge case 1.1: With existing resource:

>>> res.id = 'http://xml.zeit.de/testing/edge1.1'
>>> connector.add(res)
>>> token = connector.lock(res.id, 'saruman', until=None)
>>> url = connector._id2loc(res.id)
>>> connector._conn().do_unlock(url, token)
>>> connector.add(res)
>>> connector.unlock(res.id)
>>> del connector[res.id]


Edge case 2: We think a resource is not locked, but it is
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Lock the resoruce under the hood; we get a useful error:

>>> res.id = 'http://xml.zeit.de/testing/edge2'
>>> url = connector._id2loc(res.id)
>>> token = connector._conn().do_lock(url, owner='gimli', depth=0, timeout=20)
>>> connector.add(res)
Traceback (most recent call last):
    ...
LockedByOtherSystemError: (u'gimli', datetime.datetime(...))
>>> connector.unlock(res.id, token)

Edge case 2.2: With existing resource:

>>> res.id = 'http://xml.zeit.de/testing/edge2.2'
>>> connector.add(res)
>>> url = connector._id2loc(res.id)
>>> token = connector._conn().do_lock(url, owner='gimli', depth=0, timeout=20)
>>> connector.add(res)
Traceback (most recent call last):
    ...
LockedByOtherSystemError: (u'gimli', datetime.datetime(...))
>>> connector.unlock(res.id, token)
>>> del connector[res.id]

Edge case 3: Locked resource w/o locking user
+++++++++++++++++++++++++++++++++++++++++++++

When a resource is locked for instance with cadaver the connector was telling
the resource would not be locked. Verify the bug is fixed. We need the internal
API to create an anonymous lock. Create a resource first:

>>> res.id = 'http://xml.zeit.de/testing/edge3'
>>> connector.add(res)

Now lock:

>>> dav_url = connector._id2loc(connector._get_cannonical_id(res.id))
>>> token = connector._conn().do_lock(dav_url)

The connector tells us it is locked now:

>>> connector.locked(res.id)
(None, datetime.datetime(9998, 12, 31, 23, 59, 59, 999999, tzinfo=<UTC>),
 False)


Deleting the resource now will yield a `LockedByOtherSystemError`:

>>> del connector[res.id]
Traceback (most recent call last):
    ...
LockedByOtherSystemError: (None, datetime.datetime(9998, 12, 31, 23, 59, 59, 999999, tzinfo=<UTC>))

Unlock the resource and remove it:

>>> connector._conn().do_unlock(dav_url, token)
>>> del connector[res.id]


Edge case 4: We think the resource is locked but it is by somebody else
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

>>> res.id = 'http://xml.zeit.de/testing/edge4'
>>> token = connector.lock(res.id, 'saruman', until=None)
>>> url = connector._id2loc(res.id)
>>> connector._conn().do_unlock(url, token)
>>> new_token = connector._conn().do_lock(
...     url, owner='gimli', depth=0, timeout=20)
>>> connector.add(res)
Traceback (most recent call last):
    ...
LockedByOtherSystemError: (u'gimli', datetime.datetime(...))
>>> connector.unlock(res.id)
>>> connector.add(res)
>>> del connector[res.id]
{'fraction_non_alphanumeric': 0.2222222222222222, 'fraction_numerical': 0.018431983385254414, 'mean_word_length': 4.908742331288344, 'pattern_counts': {'":': 2, '<': 8, '<?xml version=': 0, '>': 317, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 15}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '100879', 'n_tokens_mistral': 2721, 'n_tokens_neox': 2492, 'n_words': 634}