Exemple #1
0
    def make_own_cc_info(self, config_uuid: 'UUIDLike') -> CcInfo:
        """自身のCircleCore Infoを作成する.

        Args:
            config_uuid (str): uuid, autoの場合は生成する

        Returns:
            circle_core.models.CcInfo: 自身のCircleCore Info
        """
        with MetaDataSession.begin():
            try:
                my_cc_info = CcInfo.query.filter_by(myself=True).one()
            except NoResultFound:
                logger.info('My CCInfo not found. Create new one')
                my_cc_info = CcInfo(display_name='My CircleCore',
                                    myself=True,
                                    work='')
                if config_uuid == 'auto':
                    my_cc_info.uuid = generate_uuid(model=CcInfo)
                else:
                    my_cc_info.uuid = config_uuid

                MetaDataSession.add(my_cc_info)

        return my_cc_info
Exemple #2
0
    def test_message_box(self, _input, expected, mock_circlecore):
        schema = Schema.create(display_name='Schema',
                               properties='x:int,y:float')
        module = Module.create(display_name='Module')

        box = MessageBox(uuid=generate_uuid(model=MessageBox),
                         schema_uuid=schema.uuid,
                         module_uuid=module.uuid,
                         **_input)

        with MetaDataSession.begin():
            MetaDataSession.add(schema)
            MetaDataSession.add(module)
            MetaDataSession.add(box)

        box = MessageBox.query.get(box.uuid)
        assert isinstance(box, MessageBox)
        assert box.display_name == expected['display_name']
        assert box.memo == expected['memo']

        assert isinstance(hash(box), int)
        assert isinstance(box.cc_uuid, uuid.UUID)

        jsonobj = box.to_json(with_schema=True, with_module=True)
        assert str(box.uuid) == jsonobj['uuid']
        assert box.display_name == jsonobj['displayName']
        assert box.memo == jsonobj['memo']
        assert str(box.module_uuid) == jsonobj['moduleUuid']
        assert str(box.schema_uuid) == jsonobj['schemaUuid']
        assert schema.display_name == jsonobj['schema']['displayName']
        assert module.display_name == jsonobj['module']['displayName']
Exemple #3
0
    def test_replication_link(self, _input, expected, mock_circlecore):
        schema = Schema.create(display_name='Schema',
                               properties='x:int,y:float')
        module = Module.create(display_name='Module')

        box = MessageBox(uuid=generate_uuid(model=MessageBox),
                         schema_uuid=schema.uuid,
                         module_uuid=module.uuid,
                         display_name='Box')

        with MetaDataSession.begin():
            MetaDataSession.add(schema)
            MetaDataSession.add(module)
            MetaDataSession.add(box)

        replication_link = ReplicationLink.create(message_box_uuids=[box.uuid],
                                                  **_input)

        with MetaDataSession.begin():
            MetaDataSession.add(replication_link)

        replication_link = ReplicationLink.query.get(replication_link.uuid)
        assert isinstance(replication_link, ReplicationLink)
        assert replication_link.display_name == expected['display_name']
        assert replication_link.memo == expected['memo']
Exemple #4
0
def _post_invitation():
    """Invitationを作成する.

    :return: 作成したInvitationの情報
    :rtype: Response
    """
    # maxInvites項目しか許可しない
    with MetaDataSession.begin():
        obj = Invitation(uuid=generate_uuid(model=Invitation),
                         max_invites=request.json['maxInvites'],
                         created_at=datetime.datetime.utcnow())
        MetaDataSession.add(obj)

    return respond_success(invitation=obj.to_json())
Exemple #5
0
    def test_module(self, _input, expected, mock_circlecore):
        module = Module.create()

        schema = Schema.create(display_name='Schema', properties='x:int,y:float')
        box = MessageBox(
            uuid=generate_uuid(model=MessageBox), schema_uuid=schema.uuid, module_uuid=module.uuid, display_name='Box'
        )

        _input['messageBoxes'] = [dict(schema=schema.uuid, displayName='Box2', memo='memo')]
        module.update_from_json(_input, with_boxes=True)

        with MetaDataSession.begin():
            MetaDataSession.add(schema)
            MetaDataSession.add(module)
            MetaDataSession.add(box)

        module = Module.query.get(module.uuid)
        assert isinstance(module, Module)
        assert module.display_name == expected['displayName']

        assert isinstance(module.tags, list)
        assert len(module.tags) == len(expected['tags'])
        for tag, exp_tag in zip(module.tags, expected['tags']):
            assert isinstance(tag, str)
            assert tag == exp_tag

        assert isinstance(module.attributes, ModuleAttributes)
        assert len(module.attributes) == len(expected['attributes'])
        for attr, exp_attr in zip(module.attributes, expected['attributes']):
            assert isinstance(attr, ModuleAttribute)
            assert attr.name == exp_attr[0]
            assert attr.value == exp_attr[1]

        assert module.memo == expected['memo']

        assert isinstance(hash(module), int)

        jsonobj = module.to_json(with_boxes=True, with_schema=True, with_cc_info=True)
        assert str(module.uuid) == jsonobj['uuid']
        assert str(module.cc_uuid) == jsonobj['ccUuid']
        assert module.display_name == jsonobj['displayName']
        for tag, json_tag in zip(module.tags, jsonobj['tags']):
            assert tag == json_tag
        for attr, json_attr in zip(module.attributes, jsonobj['attributes']):
            assert attr.name == json_attr['name']
            assert attr.value == json_attr['value']
        assert module.memo == jsonobj['memo']
        assert len(jsonobj['messageBoxes']) == 2
        assert jsonobj['ccInfo']
        assert jsonobj['isReplication'] is False
Exemple #6
0
async def test_store_blob(mysql, mock_circlecore):
    envdir = mock_circlecore[1]

    with MetaDataSession.begin():
        ccinfo = CcInfo.query.filter_by(myself=True).one()

        module = Module.create()
        schema = Schema.create(display_name='Schema',
                               properties='x:int,y:float,data:blob')
        box = MessageBox(uuid=generate_uuid(model=MessageBox),
                         schema=schema,
                         module=module,
                         display_name='Box')

        MetaDataSession.add(schema)
        MetaDataSession.add(module)
        MetaDataSession.add(box)

    database = Database(mysql.url, time_db_dir=envdir, log_dir=envdir)
    connection = database._engine.connect()

    with database._engine.begin() as connection:
        database.store_message(
            box,
            ModuleMessage(
                box.uuid,
                '1545895047.000',
                0,
                {
                    'x':
                    0,
                    'y':
                    3.14,
                    'data':
                    BlobMetadata(ccinfo.uuid, 'text/plain',
                                 'deadbeafdeadbeafdeadbeafdeadbeaf')
                },
            ),
            connection=connection)

    with database._engine.begin() as connection:
        table = database.find_table_for_message_box(box)
        rows = connection.execute(select([table.c.data])).fetchall()
        print(rows)
        assert len(rows) == 1
        expected = '{{"$data": "deadbeafdeadbeafdeadbeafdeadbeaf", "$source": "{source}", "$type": "text/plain"}}' \
            .format(
                source=str(ccinfo.uuid)
            )
        assert rows[0][0] == expected
Exemple #7
0
    def test_invitation(self, _input, expected, mock_circlecore):
        invitation = Invitation(uuid=generate_uuid(model=Invitation), **_input)

        with MetaDataSession.begin():
            MetaDataSession.add(invitation)

        invitation = Invitation.query.get(invitation.uuid)
        assert isinstance(invitation, Invitation)
        assert invitation.max_invites == expected['max_invites']
        assert invitation.current_invites == expected['current_invites']

        for i in range(_input['max_invites']):
            assert invitation.can_invite() is True
            invitation.inc_invites()
        else:
            assert invitation.can_invite() is False
Exemple #8
0
async def test_datareceiver_store_blob(mock_circlecore, mysql, monkeypatch):
    metadata_db_engine, tmp_dir = mock_circlecore

    writer_mock = MagicMock()
    make_writer_mock = Mock(name='make_writer', return_value=writer_mock)

    async def dummy_store(*args, **kwargs):
        return DEFAULT

    writer_mock.store.side_effect = dummy_store

    monkeypatch.setattr(Database, 'make_writer', make_writer_mock)

    # make test schema/mbox
    with MetaDataSession.begin():
        schema = Schema.create(display_name='Schema', properties='x:int,y:float,data:blob')
        module = Module.create(display_name='Module')
        mbox = MessageBox(uuid=generate_uuid(model=MessageBox), schema_uuid=schema.uuid, module_uuid=module.uuid)

        MetaDataSession.add(schema)
        MetaDataSession.add(module)
        MetaDataSession.add(mbox)

    core_mock = MagicMock()
    worker = DataReceiverWorker(
        core_mock,
        'worker_key',
        db_url=mysql.url,
        time_db_dir=tmp_dir,
        log_dir=tmp_dir,
        cycle_time=10,
        cycle_count=10,
    )

    datahash = (
        '2b7e36b16f8a849ef312f9ef5ff9b3f4281a8681d0657150899f1113a0eecfdb'
        'b4491da763159055b55e122e85281415b11897d268e124f9ef2b40457a63a465'
    )
    blobobj = StoredBlob(None, 'text/plain', datahash)
    await worker.receive_new_message(mbox.uuid, {'x': 1, 'y': 2.0, 'data': blobobj})

    publish_mock = core_mock.hub.publish
    publish_mock.assert_called_once()
    message = publish_mock.call_args[0][1]
    assert message.payload['data'].content_type == 'text/plain'
    assert message.payload['data'].datahash == datahash
Exemple #9
0
    def test_schema(self, _input, expected, mock_circlecore):
        schema = Schema.create()
        schema.update_from_json(_input)

        module = Module.create(display_name='Module')
        box = MessageBox(uuid=generate_uuid(model=MessageBox),
                         schema_uuid=schema.uuid,
                         module_uuid=module.uuid,
                         display_name='Box')

        with MetaDataSession.begin():
            MetaDataSession.add(schema)
            MetaDataSession.add(module)
            MetaDataSession.add(box)

        schema = Schema.query.get(schema.uuid)
        assert isinstance(schema, Schema)
        assert schema.display_name == expected['displayName']
        assert schema.memo == expected['memo']

        assert isinstance(schema.properties, SchemaProperties)
        assert len(schema.properties) == len(expected['properties'])
        for prop, exp_prop in zip(schema.properties, expected['properties']):
            assert isinstance(prop, SchemaProperty)
            assert prop.name == exp_prop[0]
            assert prop.type == exp_prop[1]

        assert isinstance(hash(schema), int)

        jsonobj = schema.to_json(with_modules=True)
        assert str(schema.uuid) == jsonobj['uuid']
        assert str(schema.cc_uuid) == jsonobj['ccUuid']
        assert schema.display_name == jsonobj['displayName']
        for prop, json_prop in zip(schema.properties, jsonobj['properties']):
            assert prop.name == json_prop['name']
            assert prop.type == json_prop['type']
        assert schema.memo == jsonobj['memo']

        assert len(jsonobj['modules']) == 1
        assert module.display_name == jsonobj['modules'][0]['displayName']
Exemple #10
0
    def test_update_from_json(self, _input, expected, mock_circlecore):
        schema = Schema.create(display_name='Schema',
                               properties='x:int,y:float')
        module = Module.create(display_name='Module')
        box = MessageBox(uuid=generate_uuid(model=MessageBox),
                         schema_uuid=schema.uuid,
                         module_uuid=module.uuid,
                         display_name='MessageBoxOldName')
        with MetaDataSession.begin():
            MetaDataSession.add(schema)
            MetaDataSession.add(module)
            MetaDataSession.add(box)

        box = MessageBox.query.get(box.uuid)
        box.update_from_json(_input)
        with MetaDataSession.begin():
            MetaDataSession.add(box)

        box = MessageBox.query.get(box.uuid)
        assert isinstance(box, MessageBox)
        assert box.display_name == expected['display_name']
        assert box.memo == expected['memo']
Exemple #11
0
def mock_circlecore_context():
    with tempfile.TemporaryDirectory('cc_') as tmp_dir:
        assert os.path.exists(tmp_dir)
        assert not os.path.exists(os.path.join(tmp_dir, 'metadata.sqlite3'))
        metadata_db_engine = sqlalchemy.create_engine(
            'sqlite:///' + os.path.join(tmp_dir, 'metadata.sqlite3'),
            poolclass=NullPool)
        MetaDataSession.configure(bind=metadata_db_engine)
        # empty
        MetaDataBase.metadata.create_all(metadata_db_engine)

        # make my CcInfo
        with MetaDataSession.begin():
            my_cc_info = CcInfo(display_name='My CircleCore',
                                myself=True,
                                work='')
            my_cc_info.uuid = generate_uuid(model=CcInfo)
            MetaDataSession.add(my_cc_info)

        yield metadata_db_engine, tmp_dir

        MetaDataBase.metadata.drop_all(metadata_db_engine)
        MetaDataSession.remove()
async def test_replicate_blob(mysql, mock_circlecore):
    """BLOBをReplicateできるかテスト

    このテストは見ている範囲が広すぎるにしてはみたいところがみれてないのでは"""
    mock_cc = MagicMock()
    envdir = mock_circlecore[1]

    database = Database(mysql.url, time_db_dir=envdir, log_dir=envdir)

    with MetaDataSession.begin():
        master_uuid = generate_uuid(model=CcInfo)

        # ccinfo = CcInfo.query.filter_by(myself=True).one()
        master_cc_info = CcInfo(uuid=master_uuid,
                                display_name='test master',
                                myself=False,
                                work='')
        replication_master = ReplicationMaster(endpoint_url='',
                                               info=master_cc_info)
        module = Module(uuid=generate_uuid(model=Module),
                        cc_info=master_cc_info)
        schema = Schema.create(cc_uuid=master_cc_info.uuid,
                               display_name='Schema',
                               properties='x:int,y:float,data:blob')
        box = MessageBox(uuid=generate_uuid(model=MessageBox),
                         schema=schema,
                         module=module,
                         display_name='Box')

        MetaDataSession.add(master_cc_info)
        MetaDataSession.add(replication_master)
        MetaDataSession.add(module)
        MetaDataSession.add(schema)
        MetaDataSession.add(box)

    async def dummy_store(*args):
        return DEFAULT

    slave_dirver = SlaveDriverWorker(mock_cc, '', False)
    slave_dirver.initialize()
    assert len(slave_dirver.replicators) == 1
    assert replication_master.id in slave_dirver.replicators

    replicator = slave_dirver.replicators[replication_master.id]
    replicator.state = ReplicationState.SYNCING
    replicator.target_boxes = {box.uuid: box}
    replicator.ws = MagicMock()

    writer = QueuedDBWriter(database, envdir)
    replicator.writer = writer

    datahash = (
        'bf3408132f944568dd02827441f8c69b1f3e5a36bd7693a7aeeffd490037b56d'
        '9ad9406892ecd70cb9c0d31a746e7e790e731ae491dc0176a49369a2a4044581')
    replicator.ws.read_message = Mock(side_effect=make_dummy_read_message('''\
{{
    "command": "new_message",
    "message": {{
        "boxId": "{box_id}",
        "timestamp": "1546006083.213117",
        "counter": 0,
        "payload": {{
            "x": 32768,
            "y": 3.14,
            "data": {{"$data": "{datahash}", "$source": "{source}", "$type": "text/plain"}}
        }}
    }}
}}
'''.format(box_id=str(box.uuid), datahash=datahash,
           source=master_cc_info.uuid)))
    await replicator.wait_command((MasterCommand.NEW_MESSAGE, ))

    replicator.ws.read_message.assert_called_once()

    with database._engine.begin() as connection:
        table = database.find_table_for_message_box(box)
        rows = connection.execute(select([table.c.data])).fetchall()

        assert len(rows) == 1
        expected = '{{"$data": "{datahash}", "$source": "{source}", "$type": "text/plain"}}' \
            .format(
                datahash=datahash,
                source=str(master_cc_info.uuid)
            )
        assert rows[0][0] == expected