Ejemplo n.º 1
0
def test_nest_txn():
    with db.txn() as session:
        session.add(model.MessageThread(okc_id=2, initiator_id=1, respondent_id=1))
        with db.txn() as nested:
            nested.add(model.MessageThread(okc_id=1, initiator_id=1, respondent_id=1))
            assert nested is not session
            session.add(model.MessageThread(okc_id=3, initiator_id=1, respondent_id=1))
Ejemplo n.º 2
0
def test_nest_txn():
    with db.txn() as session:
        session.add(
            model.MessageThread(okc_id=2, initiator_id=1, respondent_id=1))
        with db.txn() as nested:
            nested.add(
                model.MessageThread(okc_id=1, initiator_id=1, respondent_id=1))
            assert nested is not session
            session.add(
                model.MessageThread(okc_id=3, initiator_id=1, respondent_id=1))
Ejemplo n.º 3
0
    def update_mailbox(self, mailbox_name='inbox'):
        """Update the mailbox associated with the given mailbox name.
        """
        with txn() as session:
            last_updated_name = '{0}_last_updated'.format(mailbox_name)
            okcupyd_user = session.query(model.OKCupydUser).join(model.User).filter(
                model.User.okc_id == self._user.profile.id
            ).with_for_update().one()
            log.info(simplejson.dumps({
                '{0}_last_updated'.format(mailbox_name): helpers.datetime_to_string(
                    getattr(okcupyd_user, last_updated_name)
                )
            }))

            res = self._sync_mailbox_until(
                getattr(self._user, mailbox_name)(),
                getattr(okcupyd_user, last_updated_name)
            )
            if not res:
                return None, None

            last_updated, threads, new_messages = res
            if last_updated:
                setattr(okcupyd_user, last_updated_name, last_updated)
            return threads, new_messages
Ejemplo n.º 4
0
def test_thread_adapter_create_and_update(T):
    initiator = 'first_initiator'
    respondent = 'respondent_one'

    message_thread = T.build_mock.thread(initiator=initiator,
                                         respondent=respondent)

    thread_model, _ = ThreadAdapter(message_thread).get_thread()
    T.ensure.thread_model_resembles_okcupyd_thread(
        thread_model, message_thread
    )

    # Ensure that the operation is idempotent
    other_thread_model, _ = ThreadAdapter(message_thread).get_thread()
    T.ensure.thread_model_resembles_okcupyd_thread(
        thread_model, message_thread
    )
    assert other_thread_model.id == thread_model.id

    # Add messages and ensure that they are picked up.
    message_thread.messages.append(
        T.build_mock.message(sender=message_thread.initiator.username,
                             recipient=message_thread.respondent.username,
                             content='other')
    )

    ThreadAdapter(message_thread).get_thread()
    with txn() as session:
        loaded_thread_model = model.MessageThread.find_no_txn(session,
                                                              thread_model.id)
        assert loaded_thread_model.id == thread_model.id
        T.ensure.thread_model_resembles_okcupyd_thread(
            loaded_thread_model, message_thread
        )
        for i, message in enumerate(loaded_thread_model.messages):
            assert message.thread_index == i

    assert other_thread_model.id == thread_model.id

    second_thread = T.build_mock.thread(initiator=initiator,
                                        respondent='new_respondent')

    second_thread_model, _ = ThreadAdapter(second_thread).get_thread()
    assert second_thread_model.initiator.id == thread_model.initiator.id
    assert second_thread_model.respondent != thread_model.initiator

    T.ensure.thread_model_resembles_okcupyd_thread(second_thread_model,
                                                   second_thread)
Ejemplo n.º 5
0
 def get_thread(self):
     with txn() as session:
         thread_model = self._get_thread(session)
         return thread_model, self._add_messages(thread_model)
Ejemplo n.º 6
0
 def add_messages(self):
     with txn() as session:
         thread_model = model.MessageThread.find_no_txn(session,
                                                        self.thread.id,
                                                        id_key='okc_id')
         return self._add_messages(thread_model)
Ejemplo n.º 7
0
def session(ctx):
    with db.txn() as session:
        session = session
        IPython.embed()
Ejemplo n.º 8
0
def test_mailbox_sync_creates_message_rows(T, mailbox_sync, mock_user):
    T.factory.okcupyd_user(mock_user)
    initiator_one = 'first_initiator'
    respondent_one = 'respondent_one'
    respondent_two = 'respondent_two'
    initiator_two = 'other'
    inbox_items = [
        T.build_mock.thread(initiator=initiator_one,
                            respondent=respondent_one,
                            datetime=datetime.datetime(year=2014,
                                                       day=2,
                                                       month=5)),
        T.build_mock.thread(initiator=initiator_one,
                            respondent=respondent_two,
                            datetime=datetime.datetime(year=2014,
                                                       day=2,
                                                       month=5)),
        T.build_mock.thread(initiator=initiator_two,
                            respondent=respondent_two,
                            datetime=datetime.datetime(year=2014,
                                                       day=2,
                                                       month=5),
                            message_count=1)
    ]
    set_mailbox(mock_user.inbox, inbox_items)

    id_to_mock_thread = {t.id: t for t in mock_user.inbox}

    mailbox_sync.all()

    with txn() as session:
        message_threads = session.query(model.MessageThread).all()
        assert len(message_threads) == len(id_to_mock_thread)
        for message_thread in message_threads:
            T.ensure.thread_model_resembles_okcupyd_thread(
                message_thread, id_to_mock_thread[message_thread.id])

    # Sync again and make sure that nothing has been updated.
    mailbox_sync.all()
    with txn() as session:
        message_threads = session.query(model.MessageThread).all()
        assert len(message_threads) == len(id_to_mock_thread)
        for message_thread in message_threads:
            T.ensure.thread_model_resembles_okcupyd_thread(
                message_thread, id_to_mock_thread[message_thread.okc_id])

    # Add messages to existing threads
    mock_user.inbox[0].messages.append(
        T.build_mock.message(sender=respondent_one,
                             recipient=initiator_one,
                             content='final'))

    mock_user.inbox[2].messages.append(
        T.build_mock.message(sender=initiator_two,
                             recipient=respondent_two,
                             content='last'))

    # Sync and make sure that only the new messages appear.
    mailbox_sync.all()
    with txn() as session:
        message_threads = session.query(model.MessageThread).all()
        assert len(message_threads) == len(id_to_mock_thread)
        for message_thread in message_threads:
            T.ensure.thread_model_resembles_okcupyd_thread(
                message_thread, id_to_mock_thread[message_thread.okc_id])

    # Add a new message thread and make sure that only it appears.
    set_mailbox(mock_user.inbox, [
        T.build_mock.thread(initiator=initiator_one, respondent=initiator_two)
    ] + mock_user.inbox.items)

    id_to_mock_thread = {t.id: t for t in mock_user.inbox.items}
    mailbox_sync.all()

    with txn() as session:
        message_threads = session.query(model.MessageThread).all()
        for message_thread in message_threads:
            T.ensure.thread_model_resembles_okcupyd_thread(
                message_thread, id_to_mock_thread[message_thread.okc_id])
Ejemplo n.º 9
0
def session():
    with db.txn() as session:
        IPython.embed()
Ejemplo n.º 10
0
def session(ctx):
    with db.txn() as session:
        session = session
        IPython.embed()
Ejemplo n.º 11
0
 def get_thread(self):
     with txn() as session:
         thread_model = self._get_thread(session)
         return thread_model, self._add_messages(thread_model)
Ejemplo n.º 12
0
 def add_messages(self):
     with txn() as session:
         thread_model = model.MessageThread.find_no_txn(session,
                                                        self.thread.id,
                                                        id_key='okc_id')
         return self._add_messages(thread_model)
Ejemplo n.º 13
0
def test_mailbox_sync_creates_message_rows(T, mailbox_sync, mock_user):
    T.factory.okcupyd_user(mock_user)
    initiator_one = 'first_initiator'
    respondent_one = 'respondent_one'
    respondent_two = 'respondent_two'
    initiator_two = 'other'
    inbox_items = [
        T.build_mock.thread(initiator=initiator_one, respondent=respondent_one,
                            datetime=datetime.datetime(year=2014, day=2, month=5)),
        T.build_mock.thread(initiator=initiator_one, respondent=respondent_two,
                            datetime=datetime.datetime(year=2014, day=2, month=5)),
        T.build_mock.thread(initiator=initiator_two, respondent=respondent_two,
                            datetime=datetime.datetime(year=2014, day=2, month=5),
                            message_count=1)
    ]
    set_mailbox(mock_user.inbox, inbox_items)

    id_to_mock_thread = {t.id: t for t in mock_user.inbox}

    mailbox_sync.all()

    with txn() as session:
        message_threads = session.query(model.MessageThread).all()
        assert len(message_threads) == len(id_to_mock_thread)
        for message_thread in message_threads:
            T.ensure.thread_model_resembles_okcupyd_thread(
                message_thread,
                id_to_mock_thread[message_thread.id]
            )

    # Sync again and make sure that nothing has been updated.
    mailbox_sync.all()
    with txn() as session:
        message_threads = session.query(model.MessageThread).all()
        assert len(message_threads) == len(id_to_mock_thread)
        for message_thread in message_threads:
            T.ensure.thread_model_resembles_okcupyd_thread(
                message_thread,
                id_to_mock_thread[message_thread.okc_id]
            )

    # Add messages to existing threads
    mock_user.inbox[0].messages.append(T.build_mock.message(
        sender=respondent_one, recipient=initiator_one, content='final'
    ))

    mock_user.inbox[2].messages.append(T.build_mock.message(
        sender=initiator_two, recipient=respondent_two, content='last'
    ))

    # Sync and make sure that only the new messages appear.
    mailbox_sync.all()
    with txn() as session:
        message_threads = session.query(model.MessageThread).all()
        assert len(message_threads) == len(id_to_mock_thread)
        for message_thread in message_threads:
            T.ensure.thread_model_resembles_okcupyd_thread(
                message_thread,
                id_to_mock_thread[message_thread.okc_id]
            )

    # Add a new message thread and make sure that only it appears.
    set_mailbox(mock_user.inbox, [
        T.build_mock.thread(initiator=initiator_one,
                            respondent=initiator_two)
    ] + mock_user.inbox.items)

    id_to_mock_thread = {t.id: t for t in mock_user.inbox.items}
    mailbox_sync.all()

    with txn() as session:
        message_threads = session.query(model.MessageThread).all()
        for message_thread in message_threads:
            T.ensure.thread_model_resembles_okcupyd_thread(
                message_thread,
                id_to_mock_thread[message_thread.okc_id]
            )
Ejemplo n.º 14
0
Archivo: db.py Proyecto: kfred/okcupyd
def session():
    with db.txn() as session:
        IPython.embed()