'Test automatic ordering of export items by mtime if msince is given'

def testExportTreeWithMSince(self):

    assertEqual = self.assertEqual
    auth = current.auth
    auth.override = True
    xmlstr = '\n<s3xml>\n                <resource    name="hms_hospital"    uuid="ORDERTESTHOSPITAL1">\n                                <data    field="name">OrderTestHospital1</data>\n                </resource>\n                <resource    name="hms_hospital"    uuid="ORDERTESTHOSPITAL2">\n                                <data    field="name">OrderTestHospital2</data>\n                </resource>\n</s3xml>'
    try:
        xmltree = etree.ElementTree(etree.fromstring(xmlstr))
        resource = current.s3db.resource('hms_hospital')
        resource.import_xml(xmltree)
        resource = current.s3db.resource(resource, uid=['ORDERTESTHOSPITAL1', 'ORDERTESTHOSPITAL2'])
        resource.load(limit=2)
        assertEqual(len(resource), 2)
        first = resource._rows[0]['uuid']
        last = resource._rows[1]['uuid']
        now = datetime.datetime.utcnow()
        ts = (now - datetime.timedelta(seconds=5))
        resource._rows[0].update_record(created_on=ts, modified_on=ts)
        resource._rows[1].update_record(created_on=now, modified_on=now)
        msince = msince = (datetime.datetime.utcnow() - datetime.timedelta(days=1))
        tree = resource.export_tree(start=0, limit=1, dereference=False)
        root = tree.getroot()
        assertEqual(len(root), 1)
        child = root[0]
        uuid = child.get('uuid', None)
        assertEqual(uuid, first)
        tree = resource.export_tree(start=0, limit=1, msince=msince, dereference=False)
        root = tree.getroot()
        assertEqual(len(root), 1)
        child = root[0]
        uuid = child.get('uuid', None)
        assertEqual(uuid, last)
    finally:
        current.db.rollback()
        auth.override = False
