#!/www/python/bin/python

"""$URL: svn+ssh://svn.mems-exchange.org/repos/trunk/dulcinea/lib/test/test_history.py $
$Id: test_history.py 23447 2004-02-06 17:36:45Z dbinger $

Test for dulcinea.history.
"""

import re, time
from mx.DateTime import DateTime
from sancho.unittest import TestScenario, parse_args, run_scenarios
from dulcinea.user import DulcineaUser
from dulcinea.history import History, Event, Historical

tested_modules = [ "dulcinea.history" ]

class HistoryTest (TestScenario):

    def setup(self):
        self.user1 = DulcineaUser("joe")
        self.user2 = DulcineaUser("jane")

    def shutdown(self):
        del self.user1, self.user2

    def check_historical(self):
        self.test_stmt("Historical().get_history()")

    def check_event(self):
        user = DulcineaUser("jblow")
        message = "created new foobar ding-dong"
        event1 = Event(user, "create", message)
        self.test_val("event1.get_user()", user)
        self.test_val("event1.get_event_code()", 'create')
        self.test_val("event1.get_message()", message)

        self.test_val("str(event1)", "%s create" % event1.timestamp)

        event2 = Event(user, "modify", "changed blah blah on foobar")
        self.test_true("event2.timestamp >= event1.timestamp")
        time.sleep(0.01)
        event3 = Event(user, "delete", "jblow deleted foobar")
        self.test_true("event3.timestamp > event2.timestamp")
        self.test_true("event3.timestamp is event3.get_timestamp()")


    def check_cmp(self):
        user = DulcineaUser("jblow")
        t1 = DateTime(1999, 12, 1, 15, 34, 2)
        t2 = DateTime(1999, 12, 1, 15, 34, 2.01)
        t3 = DateTime(2001, 5, 19, 8, 30)

        event1 = Event(user, "create", "created new thing", t1)
        event2 = Event(user, "create", "created new thing", t1)
        event3 = Event(user, "create", "created new things", t1)
        event4 = Event(user, "create", "created new things", t2)
        event5 = Event(user, "create", "created new thing", t2)
        event6 = Event(user, "edit", "edited a thing", t1-1)

        self.test_true("event1 == event1")
        self.test_true("event1 == event2")
        self.test_true("event1 != event3")   # because of message
        self.test_true("event1 < event3")
        self.test_true("event3 > event1")
        self.test_true("event2 < event4")    # because of timestamp
        self.test_true("event4 >= event2")
        self.test_true("event6 < event1")
        self.test_true("event5 > event6")
        event4.timestamp = None
        self.test_true("event2 > event4")    # because of timestamp
        self.test_true("event4 < event2")    # because of timestamp

    def check_history(self):
        history = History()
        self.test_val("len(history)", 0)
        self.test_val("str(history)", "history (0 events)")
        self.test_stmt("history.add_event(self.user1, 'create', "
                                         "'joe created blah')")
        self.test_stmt("history.add_event(None, 'modify', "
                                         "'jane modified blah')")
        self.test_val("len(history)", 2)
        self.test_true("history[0].timestamp <= history[1].timestamp")
        self.test_val("history[0].user", self.user1)
        self.test_val("history[1].user", None)

        ts = DateTime(0)
        self.test_stmt("history.add_event(self.user2, 'create',"
                                         "'god created the universe',"
                                         "timestamp=ts)")
        self.test_val("len(history)", 3)
        self.test_true("history[0].timestamp < "
                       "history[1].timestamp <= "
                       "history[2].timestamp")
        self.test_val("history[0].message", "god created the universe")

        ts = DateTime(1500)
        self.test_stmt("history.add_event(None, 'create', "
                                         "'modern age begins', "
                                         "timestamp=ts)")
        self.test_val("len(history)", 4)
        self.test_val("history[0].message", "god created the universe")
        self.test_val("history[1].message", "modern age begins")


    def check_variations(self):
        history = History()
        # Works with no user object
        self.test_stmt("history.add_event(None, 'modify', 'tweaked it')")
        self.test_val("history[-1].user", None)

        # Replacing existing event
        self.test_stmt("history.add_unique_event(None, 'modify', 'tweaked it more')")
        self.test_val("history[-1].message", 'tweaked it more')

        self.test_stmt("history.add_unique_event(None, 'create', 'creation')")
        self.test_val("history[-2].message", 'tweaked it more')

    def check_find(self):
        history = History()
        self.test_val("history.find_event()", None)
        self.test_val("history.find_event(latest=True)", None)

        history.add_event(self.user1, "create", "joe created it")
        self.test_val("history.find_event().message",
                      "joe created it")
        self.test_val("history.find_event(latest=True).message",
                      "joe created it")
        self.test_val("history.find_event(user=self.user1).message",
                      "joe created it")
        self.test_val("history.find_event(user=self.user2)", None)
        self.test_val("history.find_event(event_code='create').message",
                      "joe created it")
        self.test_val("history.find_event(message='no created it')", None)

        self.test_val("history.find_event(event_code='access')", None)
        self.test_val("history.find_event(event_codes=['access'])", None)

        history.add_event(self.user2, "access", "jane accessed it")
        history.add_event(self.user1, "modify", "joe modified it")
        history.add_event(self.user2, "modify", "jane modified it")
        history.add_event(self.user2, "delete", "jane deleted it")

        self.test_val("history.find_event().message",
                      "joe created it")
        self.test_val("history.find_event(latest=True).message",
                      "jane deleted it")
        self.test_val("history.find_event(user=self.user1,latest=True)"
                      ".message",
                      "joe modified it")
        self.test_val("history.find_event(event_code='modify').message",
                      "joe modified it")
        self.test_val(
            "history.find_event(event_code='modify',latest=True).message",
            "jane modified it")
        self.test_val("len(history.find_events_after(0))", 5)
        self.test_val("history.find_last_date('foo')", None)


if __name__ == "__main__":
    (scenarios, options) = parse_args()
    run_scenarios (scenarios, options)
