Example #1
0
    def test_addToCollection(self):
        db.create_collection('test.collection')
        data = {'_id': '1234567890', 'content': 'testcontent'}
        db.add_oneDictIntoCollection(data, 'test.collection')
        db.get_db().drop_collection('test.collection')

        db.add_oneDictIntoCollection(data, 'test.collection')
Example #2
0
File: base.py Project: YKJIN/CAEML
    def createObjectReferencesRecursive(self, data):
        if isinstance(data, dict):
            aDict = data
            new_dict = {}

            if "_id" in data:  # data is a DB Object: Store as data only as DBRef
                # check data for more potential db_refs and remove them before this data dict is stored.
                for k, v in data.items():
                    new_dict[k] = self.createObjectReferencesRecursive(v)
                assert new_dict['_id'] is not None, "if objects _id is set it is not allowed to be None"
                ref = DBRef(str(new_dict['caemlType'][0]), new_dict['_id'])
                res = dereference(ref)  # is referenced object already in the db?
                if res is None:
                    add_oneDictIntoCollection(new_dict, ref.collection)
                else:
                    replace_oneDictInCollection(new_dict, ref.collection)
                return ref

            else:  # check nested dicts items for potentials DBRefs
                for k, v in data.items():
                    new_dict[k] = self.createObjectReferencesRecursive(v)
                return new_dict


        elif isinstance(data, list):  # check nested list items for potentials DBRefs
            new_list = [self.createObjectReferencesRecursive(l) for l in data]
            return new_list

        else:  # data is not a dict
            return data
Example #3
0
    def test_oneDictByIdWithUnKnownCollection(self):
        data = {'_id': '1234567890', 'content': 'testcontent'}
        db.add_oneDictIntoCollection(data, 'test.collection')
        aDict = db.get_dictById('1234567890')
        self.assertIsNotNone(aDict)
        self.assertEqual(data['_id'], aDict['_id'])

        self.assertIsNone(
            db.get_dictById('no.tool.will.ever.have.an.id.like.this'))
Example #4
0
    def test_replace(self):
        data = {'_id': '1234567890', 'content': 'testcontent'}
        db.add_oneDictIntoCollection(data, 'test.collection')

        first_load = db.get_dictById(data['_id'])

        data = {'_id': '1234567890', 'content': 'newtestcontent'}

        db.replace_oneDictInCollection(data, 'test.collection')

        second_load = db.get_dictById(data['_id'])

        self.assertEqual(second_load['_id'], first_load['_id'])
        self.assertEqual(first_load['content'], 'testcontent')
        self.assertEqual(second_load['content'], 'newtestcontent')
Example #5
0
    def initCmdTool(self, path: str):
        create_collection('caeml.common.tools')
        try:
            with open(path, 'r') as f:
                data = yaml.load(f)
            if not 'dockerImageId' in data:
                data['baseCommand'] = \
                    os.path.abspath(os.path.join(os.path.dirname(path), data['baseCommand'].format(path)))
            elif 'scriptPath' in data:
                data['scriptPath'] = os.path.abspath(os.path.join(os.path.dirname(path), data['scriptPath']))
            for k, v in data['inputParameters'].items():
                v['caemlType'] = 'caeml.common.process.Parameter'
                if 'inputBinding' in v:
                    v['inputBinding']['caemlType'] = "caeml.common.process.InputBinding"
                if '[]' == v['parameterType'] or isinstance(v['parameterType'],
                                                            list):  # TODO: allow typed list (eg int[])
                    v['parameterType'] = 'list'
                if 'file' == v['parameterType']:
                    v['parameterType'] = 'caeml.files.file.File'
            for k, v in data['outputParameters'].items():
                v['caemlType'] = "caeml.common.process.Parameter"
                if 'outputBinding' in v:
                    v['outputBinding']['caemlType'] = "caeml.common.process.OutputBinding"
                if '[]' == v['parameterType'] or isinstance(v['parameterType'], list):
                    v['parameterType'] = 'list'
                if 'file' == v['parameterType']:
                    v['parameterType'] = 'caeml.files.file.File'

            if not 'name' in data:
                raise LookupError("YAML file needs to specify tool name for tool at path: {}".format(path))
        except Exception as e:
            raise Exception("Error from Tool: {}: {}".format(path, str(e)))

        loaded_data = get_dictByQuery({"name": data['name']}, 'caeml.common.tools')
        if loaded_data is None:
            data['_id'] = str(uuid.uuid4())
            add_oneDictIntoCollection(data, 'caeml.common.tools')
        else:
            data['_id'] = loaded_data['_id']
            replace_oneDictInCollection(data, 'caeml.common.tools')

        if data['name'] in self.inserted_names:
            logging.getLogger('init').warning(
                "Tool with name: {} already exists in database and has been overwritten".format(data['name']))
        self.inserted_names.append(data['name'])
        pass
Example #6
0
    def test_dereference(self):
        data = {'_id': '1234567890', 'content': 'testcontent'}
        ref = DBRef('test.collection', '1234567890')
        db.add_oneDictIntoCollection(data, 'test.collection')

        self.assertEqual(data, db.dereference(ref))