def test_deserialize(self):
        self.doc.add_resource(
            Resource(path="/example", content="example"),
            False
        )
        self.doc.freeze()
        serial = self.doc.serialize()

        newdoc = Document(self.doc.name)
        newdoc.deserialize(serial)

        self.assertEqual(list(newdoc.resources.keys()), ['/example'])
        self.assertIsInstance(newdoc.resources['/example'], Resource)

        initial_resources = newdoc._initial.resources
        self.assertEqual(list(initial_resources.keys()), ['/example'])
        self.assertIsInstance(initial_resources['/example'], Resource)
Example #2
0
    def do_dinit(self, args):
        '''
        Initialize DEJE interactivity.

        This command must be used before any of the other d*
        commands. It reads from a few of the values in variable
        storage as initialization parameters:

        * idcache - EJTP identity cache
        * identity - location of EJTP identity in cache
        * docname - Name of the document for network sync
        * docserialized - serialized version of document

        The dinit command can be run more than once, but it's
        a bit of a reset, and may cause data loss in the
        stateful parts of the protocol. But it's also the only
        way to update the parameters used by the DEJE code -
        for example, any changes to the 'idcache' variable after
        initialization will have no effect.
        '''
        try:
            params = self.get_vars(
                'idcache',
                'identity',
                'docname',
                'docserialized'
            )
        except KeyError as e:
            self.fail('Need to set variable %r' % e.args[0])
        
        cache = IdentityCache()
        try:
            cache.deserialize(params['idcache'])
        except:
            self.fail('Could not deserialize data in idcache')

        try:
            ident = cache.find_by_location(params['identity'])
        except KeyError:
            loc_string = strict(params['identity']).export()
            self.fail('No identity in cache for ' + loc_string)

        owner = Owner(ident)
        owner.identities = cache
        owner.client.rcv_callback = self.on_ejtp

        self.write_json = owner.client.write_json
        owner.client.write_json = self.write_json_wrapped

        if type(params['docname']) != str:
            json_str = strict(params['docname']).export()
            self.fail('Not a valid docname: ' + json_str)

        doc = Document(params['docname'], owner=owner)

        try:
            doc.deserialize(params['docserialized'])
        except Exception as e:
            self.fail('Failed to deserialize data:\n%r' % e)

        doc.signals['enact-event'].connect(self.on_event)
        doc.debug = self.debug

        # Wait until everything that could fail has gone right
        self.interface.owner = owner
        self.interface.document = doc
        self.output('DEJE initialized')
 def setUp(self):
     self.doc = Document("testing")
class TestDocumentSimple(unittest.TestCase):

    def setUp(self):
        self.doc = Document("testing")

    def test_serialize(self):
        expected_original = {
            'handler' : '/handler.lua',
            'hash' : None,
            'resources': {},
        }

        serial = self.doc.serialize()
        self.assertEqual(
            sorted(serial.keys()),
            ['events', 'original']
        )
        self.assertEqual(serial['original'], expected_original)
        self.assertEqual(serial['events'],   [])

        self.doc.add_resource(
            Resource(path="/example", content="example"),
            False
        )
        self.assertEqual(self.doc.serialize()['original'], expected_original)
        self.doc.freeze()
        self.assertEqual(
            self.doc.serialize()['original'],
            {
                'handler': '/handler.lua',
                'hash': 'current',
                'resources': {
                    '/example': {
                        'comment': '',
                        'content': 'example',
                        'path':    '/example',
                        'type':    'application/x-octet-stream'
                   }
                }
            }
        )

    def test_deserialize(self):
        self.doc.add_resource(
            Resource(path="/example", content="example"),
            False
        )
        self.doc.freeze()
        serial = self.doc.serialize()

        newdoc = Document(self.doc.name)
        newdoc.deserialize(serial)

        self.assertEqual(list(newdoc.resources.keys()), ['/example'])
        self.assertIsInstance(newdoc.resources['/example'], Resource)

        initial_resources = newdoc._initial.resources
        self.assertEqual(list(initial_resources.keys()), ['/example'])
        self.assertIsInstance(initial_resources['/example'], Resource)

    def test_saving(self):
        self.doc.add_resource(
            Resource(path="/example", content="example"),
            False
        )
        self.doc.freeze()

        save_to(self.doc, "example.dje")
        newdoc = load_from("example.dje")
        self.assertEqual(newdoc.serialize(), self.doc.serialize())