# Path: src/petmail/eventual.py
# def flushEventualQueue(value=None):
#     """This returns a Deferred which fires when the eventual-send queue is
#     finally empty. This is useful to wait upon as the last step of a Trial
#     test method. If an argument is provided, the Deferred will fire with it,
#     making this useful at the end of a unit test:
# 
#         d = some_stuff_that_uses_eventual_sends()
#         d.addBoth(flushEventualQueue)
#         return d
#     """
#     d = _theSimpleQueue.flush()
#     d.addBoth(lambda _: value)
#     return d
# 
# Path: src/petmail/database.py
# def get_db(dbfile, stderr=sys.stderr):
#     """Open or create the given db file. The parent directory must exist.
#     Returns the db connection object, or raises DBError.
#     """
# 
#     must_create = not os.path.exists(dbfile)
#     try:
#         db = sqlite3.connect(dbfile)
#     except (EnvironmentError, sqlite3.OperationalError), e:
#         raise DBError("Unable to create/open db file %s: %s" % (dbfile, e))
#     db.row_factory = sqlite3.Row
# 
#     VERSION = 1
#     if must_create:
#         schema = get_schema(VERSION)
#         db.executescript(schema)
#         db.execute("INSERT INTO version (version) VALUES (?)", (VERSION,))
#         db.commit()
# 
#     try:
#         version = db.execute("SELECT version FROM version").fetchone()[0]
#     except sqlite3.DatabaseError, e:
#         # this indicates that the file is not a compatible database format.
#         # Perhaps it was created with an old version, or it might be junk.
#         raise DBError("db file is unusable: %s" % e)
# 
#     if version != VERSION:
#         raise DBError("Unable to handle db version %s" % version)
# 
#     return db
# 
# def make_observable_db(dbfile, stderr=sys.stderr):
#     return ObservableDatabase(get_db(dbfile, stderr))
# 
# class DBError(Exception):
#     pass


import os.path
from twisted.trial import unittest
from common import BasedirMixin
from ..eventual import flushEventualQueue
from ..database import get_db, make_observable_db, DBError
                        "mailboxes")
        self.failUnlessEqual(mid, 1)
        # and this one doesn't even have an 'id' column
        db.insert("INSERT INTO services (name) VALUES (?)", ("name",))
        # but this one is observed
        imid = db.insert("INSERT INTO inbound_messages"
                         " (cid, seqnum, payload_json)"
                         " VALUES (?,?,?)", (4, 9, "payload"),
                         "inbound_messages", {"tag1": "v1"})
        self.failUnlessEqual(n, [])
        db.commit()
        self.failUnlessEqual(n, [])
        d = flushEventualQueue()
        def _then(_):
            self.failUnlessEqual(len(n), 1)
            n0 = n.pop(0)
            self.failUnlessEqual(n0.table, "inbound_messages")
            self.failUnlessEqual(n0.action, "insert")
            self.failUnlessEqual(n0.id, imid)
            self.failUnlessEqual(n0.new_value["seqnum"], 9)
            self.failUnlessEqual(n0.tags, {"tag1": "v1"})

            db.update("UPDATE inbound_messages SET seqnum=10"
                      " WHERE id=?", (imid,),
                      "inbound_messages", imid, {"tag2": "v2"})
            db.delete("DELETE FROM inbound_messages WHERE id=?", (imid,),
                      "inbound_messages", imid, {"tag3": "v3"})
            self.failUnlessEqual(len(n), 0)
            db.commit()
            self.failUnlessEqual(len(n), 0)

# Path: src/petmail/eventual.py
# def flushEventualQueue(value=None):
#     """This returns a Deferred which fires when the eventual-send queue is
#     finally empty. This is useful to wait upon as the last step of a Trial
#     test method. If an argument is provided, the Deferred will fire with it,
#     making this useful at the end of a unit test:
# 
#         d = some_stuff_that_uses_eventual_sends()
#         d.addBoth(flushEventualQueue)
#         return d
#     """
#     d = _theSimpleQueue.flush()
#     d.addBoth(lambda _: value)
#     return d
# 
# Path: src/petmail/database.py
# def get_db(dbfile, stderr=sys.stderr):
#     """Open or create the given db file. The parent directory must exist.
#     Returns the db connection object, or raises DBError.
#     """
# 
#     must_create = not os.path.exists(dbfile)
#     try:
#         db = sqlite3.connect(dbfile)
#     except (EnvironmentError, sqlite3.OperationalError), e:
#         raise DBError("Unable to create/open db file %s: %s" % (dbfile, e))
#     db.row_factory = sqlite3.Row
# 
#     VERSION = 1
#     if must_create:
#         schema = get_schema(VERSION)
#         db.executescript(schema)
#         db.execute("INSERT INTO version (version) VALUES (?)", (VERSION,))
#         db.commit()
# 
#     try:
#         version = db.execute("SELECT version FROM version").fetchone()[0]
#     except sqlite3.DatabaseError, e:
#         # this indicates that the file is not a compatible database format.
#         # Perhaps it was created with an old version, or it might be junk.
#         raise DBError("db file is unusable: %s" % e)
# 
#     if version != VERSION:
#         raise DBError("Unable to handle db version %s" % version)
# 
#     return db
# 
# def make_observable_db(dbfile, stderr=sys.stderr):
#     return ObservableDatabase(get_db(dbfile, stderr))
# 
# class DBError(Exception):
#     pass


# Path: src/petmail/test/test_database.py
import os.path
from twisted.trial import unittest
from common import BasedirMixin
from ..eventual import flushEventualQueue
from ..database import get_db, make_observable_db, DBError

                        "mailboxes")
        self.failUnlessEqual(mid, 1)
        # and this one doesn't even have an 'id' column
        db.insert("INSERT INTO services (name) VALUES (?)", ("name",))
        # but this one is observed
        imid = db.insert("INSERT INTO inbound_messages"
                         " (cid, seqnum, payload_json)"
                         " VALUES (?,?,?)", (4, 9, "payload"),
                         "inbound_messages", {"tag1": "v1"})
        self.failUnlessEqual(n, [])
        db.commit()
        self.failUnlessEqual(n, [])
        d = flushEventualQueue()
        def _then(_):
            self.failUnlessEqual(len(n), 1)
            n0 = n.pop(0)
            self.failUnlessEqual(n0.table, "inbound_messages")
            self.failUnlessEqual(n0.action, "insert")
            self.failUnlessEqual(n0.id, imid)
            self.failUnlessEqual(n0.new_value["seqnum"], 9)
            self.failUnlessEqual(n0.tags, {"tag1": "v1"})

            db.update("UPDATE inbound_messages SET seqnum=10"
                      " WHERE id=?", (imid,),
                      "inbound_messages", imid, {"tag2": "v2"})
            db.delete("DELETE FROM inbound_messages WHERE id=?", (imid,),
                      "inbound_messages", imid, {"tag3": "v3"})
            self.failUnlessEqual(len(n), 0)
            db.commit()
            self.failUnlessEqual(len(n), 0)

            return flushEventualQueue()