Example #1
0
    def post_sample_Prs(self):

        db = self.g['db']
        usr = self.g['usr']

        generic = controllers.Generic(self.g)

        cnts = myyaml.pyObj(self.tests_data_yaml_dir + 'cnts.yaml')
        prs_larry_stooge = cnts['larry_stooge']

        response = controllers.Generic(
            self.g).post(**{'docs': [prs_larry_stooge]})
        assert response['status'] == 200
        sample_doc = response['response']['docs'][0]['doc']
        return db['cnts'].find_one({'_id': sample_doc['_id']})
Example #2
0
    def test_put_listtype(self):
        '''Need doc here
            '''

        # lets create a some sample docs bypassing tmp process.
        sample_doc = self.post_sample_Prs()

        db = self.g['db']
        usr = self.g['usr']
        generic = controllers.Generic(self.g)

        test_field = 'emails'
        test_field_c = 'Email'
        test_values = sample_doc['emails']

        sample_doc_id = sample_doc['_id']

        # when we need to edit most docs, we lock the doc and clone a tmp doc to work on.
        # now let's pretent to initiate an update of this doc
        response = generic.post(**{'docs': [sample_doc]})

        # this is the temp doc, the original is locked
        doc_tmp = response['response']['docs'][0]['doc']

        test_value = [{
            "_c": "Email",
            "eId": 2,
            "address": "*****@*****.**",
            "typ": "home",
            "w": 0.0
        }]
        test_eId = 2
        test_elemOffset = 1
        data = {
            "_c": sample_doc['_c'],
            'where': {
                '_id': sample_doc['_id'],
                test_field + '.eId': test_eId
            },
            'patch': {
                test_field: test_value,
            },
            "eId": 2
        }

        # submit patch to tmp doc
        response = generic.put(**{'data': data})
        tmp_doc = response['response']['doc']

        # let's submit changes to original source doc.
        response = generic.post(**{'docs': [tmp_doc]})
        doc = response['response']['docs'][0]['doc']

        # original doc should now have updated value
        assert doc[test_field][test_elemOffset]['address'] == test_value[0][
            'address']
Example #3
0
    def test_nextid_id_empty_collection(self):
        '''Doc this
            '''
        db = self.g['db']
        generic = controllers.Generic(self.g)
        coll = db['cnts']

        response = nextId(coll)

        assert response['status'] == 200
        assert response['nextId'] == 1
Example #4
0
def put(class_name):
    from db import db
    from es import es
    generic = controllers.Generic(db, es)

    data = json.loads(request.data, object_hook=json_util.object_hook)

    args = dict(request.view_args.items() + request.args.items())
    args['data'] = data
    args['usrid'] = "50468de92558713d84b03fd7"

    response = generic.put(**args)

    return prep_response(response['response'], status=response['status'])
Example #5
0
    def test_post_listtype(self):
        '''Passing in doc with OID should clone the doc and save in tmp collection. Set isTmp = True.
            '''
        db = self.g['db']
        usr = self.g['usr']
        generic = controllers.Generic(self.g)

        # lets create a some sample docs bypassing tmp process.
        sample_doc = self.post_sample({
            '_c': 'Prs',
            'fNam': 'Larry',
            'lNam': 'Stooge'
        })

        # now let's pretent to initiate an update of this doc
        response = generic.post(**{'docs': [sample_doc]})

        test_field = 'emails'
        test_field_c = 'Email'
        test_values = [{
            'typ': 'work',
            '_c': test_field_c,
            'address': '*****@*****.**',
            'prim': '1'
        }, {
            'typ': 'home',
            '_c': test_field_c,
            'address': '*****@*****.**',
            'note': 'Deceased'
        }]
        for test_value in test_values:
            generic.post_attr(sample_doc, test_field, test_field_c, test_value)

        # let's get the tmp doc
        tmp_doc_id = sample_doc['_id']
        tmp_doc = db['cnts_tmp'].find_one({'_id': ObjectId(tmp_doc_id)})

        # should have the correct number of added attr elements
        assert len(tmp_doc[test_field]) == len(test_values)

        # let's submit changes to original source doc.
        response = generic.post(**{'docs': [tmp_doc]})
        doc = response['response']['docs'][0]['doc']

        # original doc should now have updated value
        assert doc[test_field][0]['address'] == test_values[0]['address']

        # verify that eIds was correctly added
        assert doc['eIds'][test_field] == 3
Example #6
0
    def test_post_submit(self):
        '''Passing in doc with OI and isTmp is set will move/clone the tmp doc into the base/primary doc collection and remove the tmp doc.
            '''
        db = self.g['db']
        usr = self.g['usr']
        generic = controllers.Generic(self.g)

        # lets create a sample doc bypassing tmp process.
        doc = self.post_sample({
            '_c': 'Prs',
            'fNam': 'Larry',
            'lNam': 'King',
            'suffix': 'Sr'
        })

        # now let's pretent to initiate an update of this doc
        response = generic.post(**{'docs': [doc]})

        # let's get the tmp doc
        src_doc_id = doc['_id']
        tmp_doc = db['cnts_tmp'].find_one({'_id': ObjectId(src_doc_id)})

        # UI will handle making changes to specific fields.
        # Let's make a sample change to the tmp doc.
        # NOTE: THIS IS A NO-NO.
        tmp_doc = db['cnts_tmp'].find_and_modify(
            query={'_id': tmp_doc['_id']},
            update={"$set": {
                'fNam2': 'Wayne'
            }},
            new=True)

        # Once any updates are made to tmp/clone copy, update original/source doc. Then delete the temp doc.
        response = generic.post(**{'docs': [tmp_doc]})

        assert response['status'] == 200

        # let's get the sample source doc that should now have been updated from the tmp copy
        src_doc = db['cnts'].find_one({'_id': ObjectId(src_doc_id)})

        # should not be locked
        assert not src_doc['locked']

        # should have the sample change
        assert src_doc['fNam2'] == 'Wayne'

        # tmp doc should have been removed
        assert not db['cnts_tmp'].find_one({'_id': ObjectId(src_doc_id)})
Example #7
0
    def test_nextid_insert_new_record(self):
        '''Doc this
            '''
        db = self.g['db']
        generic = controllers.Generic(self.g)

        # lets create a some sample docs bypassing tmp process.
        sample_doc = self.post_sample({
            '_c': 'Prs',
            'fNam': 'Larry',
            'lNam': 'Stooge'
        })
        sample_doc = self.post_sample({
            '_c': 'Prs',
            'fNam': 'Moe',
            'lNam': 'Stooge'
        })

        assert sample_doc['dId'] == 2
Example #8
0
def post(class_name):
    from db import db
    from es import es
    generic = controllers.Generic(db, es)

    #data = loads(request.data)
    data = request.data

    docs_to_post = json.loads(data, object_hook=json_util.object_hook)

    # if a dict, then stuff it into a list
    if type(docs_to_post) == dict: docs_to_post = [docs_to_post]

    args = dict(request.view_args.items() + request.args.items())
    args['docs'] = docs_to_post

    response = generic.post(**args)

    return prep_response(response['response'], status=response['status'])
Example #9
0
    def test_post(self):
        print
        print "Post one doc"
        print "^^^^^^^^^^^^"

        db = self.db
        generic = controllers.Generic(db)

        # here is the basic function call being tested
        fn = "controllers.generic.get(db, **args)"

        # POST ONE ################################
        host = self.host
        sample_doc = {
            "fNam": "johnathan",
            "lNam": "doe",
            "mOn": isodate.parse_datetime("2012-09-27T21:43:33.927Z"),
            "oBy": ObjectId("50468de92558713d84b03fd0"),
            "rBy": ObjectId("50468de92558713d84b03fd7"),
            "gen": 'm',
            "emails": [{
                "email": "*****@*****.**"
            }]
        }

        args = {}
        args['class_name'] = self.class_name
        args['docs'] = [sample_doc]

        response = generic.post(**args)

        assert response['status'] == 200
        data = response['response']
        got_docs = data['docs']

        assert data['total_inserted'] == 1

        doc = data['docs'][0]['doc']
        id = data['docs'][0]['id']
        printIndentedString("INSERTED OBJECT_ID: " + id, 0)
        assert doc['fNam'] == sample_doc['fNam']
Example #10
0
    def test_post_update(self):
        '''Passing in doc with OID should clone the doc and save in tmp collection. Set isTmp = True.
            '''
        db = self.g['db']
        usr = self.g['usr']
        generic = controllers.Generic(self.g)

        # lets create a sample doc bypassing tmp process.
        doc = self.post_sample({
            '_c': 'Prs',
            'fNam': 'Larry',
            'lNam': 'King',
            'suffix': 'Sr'
        })

        # now let's pretent to initiate an update of this doc
        args = {}
        sample_doc = doc
        args['docs'] = [sample_doc]

        response = generic.post(**args)

        # let's get the sample source doc that should now have a cloned copy inserted into the tmp collection
        src_doc_id = doc['_id']
        src_doc = db['cnts'].find_one({'_id': ObjectId(src_doc_id)})

        # should be locked
        assert src_doc['locked']

        assert response['status'] == 200
        data = response['response']

        assert data['total_inserted'] == 1
        tmp_doc = data['docs'][0]['doc']

        # _id's should match
        assert src_doc['_id'] == tmp_doc['_id']

        # should be flagged as tmp
        assert tmp_doc['isTmp']
Example #11
0
    def test_post_attr(self):
        '''Post a new item to a listType attribute/field
            '''
        db = self.g['db']
        usr = self.g['usr']
        generic = controllers.Generic(self.g)

        # lets create a some sample docs bypassing tmp process.
        sample_doc = self.post_sample({
            '_c': 'Prs',
            'fNam': 'Larry',
            'lNam': 'Stooge'
        })

        test_field = 'emails'
        test_field_c = 'Email'
        test_values = [{
            'typ': 'work',
            '_c': test_field_c,
            'address': '*****@*****.**',
            'prim': '1'
        }, {
            'typ': 'home',
            '_c': test_field_c,
            'address': '*****@*****.**',
            'note': 'Deceased'
        }]
        for test_value in test_values:
            generic.post_attr(sample_doc,
                              test_field,
                              test_field_c,
                              test_value,
                              useTmpDoc=False)

        doc = db['cnts'].find_one({'_id': sample_doc['_id']})

        # should now have correct count
        assert len(doc[test_field]) == len(test_values)
Example #12
0
def get(class_name, id=None):
    from db import db
    generic = controllers.Generic(db)

    parsed_args = {}
    for k, v in dict(request.args.items()).iteritems():
        parsed_args[k] = json.loads(v, object_hook=mongo_json_object_hook)

    args = dict(request.view_args.items() + parsed_args.items())
    #args = dict(request.view_args.items() + request.args.items())

    #if 'where' in args:
    #args['where'] = dumps(json.loads(args['where'], object_hook=mongo_json_object_hook))

    resp = generic.get(**args)

    links = []
    host = 'http://' + request.environ['HTTP_HOST']
    links.append("<link rel='parent' title='%(name)s' href='%(modelURI)s' />" %
                 {
                     'name': class_name,
                     'modelURI': host + class_name
                 })
    links.append("<link rel='model' title='%(name)s' href='%(modelURI)s' />" %
                 {
                     'name': class_name,
                     'modelURI': host + class_name
                 })

    # need to add a link to each doc along with etag and updated i
    # links.append("<link rel='model' title='%(name)s' href='%(modelURI)s' />" %
    #         {'name':class_name, 'modelURI': host + class_name})

    resp['response']['links'] = links

    return prep_response(resp['response'], status=resp['status'])
Example #13
0
 def post_sample(self, doc):
     response = controllers.Generic(self.g).post(**{'docs': [doc]})
     assert response['status'] == 200
     return response['response']['docs'][0]['doc']
Example #14
0
    def test_put(self):
        '''Need doc here
            '''
        db = self.g['db']
        usr = self.g['usr']
        generic = controllers.Generic(self.g)

        # lets create a some sample docs bypassing tmp process.
        sample_docs = [
            self.post_sample({
                '_c': 'Prs',
                'fNam': 'Larry',
                'lNam': 'Stooge'
            }),
            self.post_sample({
                '_c': 'Prs',
                'fNam': 'Moe',
                'lNam': 'Stooge'
            }),
            self.post_sample({
                '_c': 'Prs',
                'fNam': 'Curley',
                'lNam': 'Stooge'
            }),
        ]

        # grab a random doc from sample docs
        sample_doc_offset = randint(0, len(sample_docs) - 1)
        sample_doc = sample_docs[sample_doc_offset]
        sample_doc_id = sample_doc['_id']

        # when we need to edit most docs, we lock the doc and clone a tmp doc to work on.
        # now let's pretent to initiate an update of this doc
        response = generic.post(**{'docs': [sample_doc]})

        # this is the temp doc, the original is locked
        doc_tmp = response['response']['docs'][0]['doc']

        test_field = 'fNam'
        test_value = 'longname'
        data = {
            "_c": sample_doc['_c'],
            'where': {
                '_id': sample_doc['_id']
            },
            'patch': {
                test_field: test_value,
                "rBy": ObjectId(usr['OID'])
            }
        }

        response = generic.put(**{'data': data})
        doc = response['response']['doc']

        # did it properly update the value?
        assert doc[test_field] == test_value

        # let's submit changes to original source doc.
        response = generic.post(**{'docs': [doc]})
        doc = response['response']['docs'][0]['doc']

        # original doc should now have updated value
        assert doc[test_field] == test_value