from __future__ import absolute_import

from mock import Mock

from sentry.utils.cursors import build_cursor, Cursor


def build_mock(**attrs):
    obj = Mock()
    for key, value in attrs.items():
        setattr(obj, key, value)
    obj.__repr__ = lambda x: repr(attrs)
    return obj


def test_build_cursor():
    event1 = build_mock(id=1.1, message='one')
    event2 = build_mock(id=1.1, message='two')
    event3 = build_mock(id=2.1, message='three')

    results = [event1, event2, event3]

    cursor = build_cursor(results, key='id', limit=1)
    assert isinstance(cursor.next, Cursor)
    assert cursor.next
    assert isinstance(cursor.prev, Cursor)
    assert not cursor.prev
    assert list(cursor) == [event1]

    cursor = build_cursor(results, key='id', limit=1, cursor=cursor.next)
    assert isinstance(cursor.next, Cursor)
    assert cursor.next
    assert isinstance(cursor.prev, Cursor)
    assert cursor.prev
    assert list(cursor) == [event2]

    cursor = build_cursor(results, key='id', limit=1, cursor=cursor.next)
    assert isinstance(cursor.next, Cursor)
    assert cursor.next
    assert isinstance(cursor.prev, Cursor)
    assert cursor.prev
    assert list(cursor) == [event3]

@@ -33,16 +33,29 @@
 import%0A%0A
+import math%0A%0A
 from moc
@@ -504,16 +504,108 @@
 vent3%5D%0A%0A
+    cursor_kwargs = %7B%0A        'key': lambda x: math.floor(x.id),%0A        'limit': 1,%0A    %7D%0A%0A
     curs
@@ -631,33 +631,31 @@
 esults, 
-key='id', limit=1
+**cursor_kwargs
 )%0A    as
@@ -854,35 +854,20 @@
 (results
-, key='id', limit=1
+%5B1:%5D
 , cursor
@@ -870,32 +870,49 @@
 rsor=cursor.next
+, **cursor_kwargs
 )%0A    assert isi
@@ -1103,27 +1103,12 @@
 ults
-, key='id', limit=1
+%5B2:%5D
 , cu
@@ -1123,16 +1123,33 @@
 sor.next
+, **cursor_kwargs
 )%0A    as
@@ -1177,32 +1177,32 @@
 r.next, Cursor)%0A
-
     assert curso
@@ -1188,32 +1188,36 @@
 sor)%0A    assert 
+not 
 cursor.next%0A    

Correct cursor test