Beispiel #1
0
def test_cache_miss():
    s_nocache = Session.connect('unit-testing', cache_size=10)
    t = TExtra(i=4)
    s_nocache.save(t)

    s = Session.connect('unit-testing', cache_size=10)
    s.add_to_session(t)
    t2 = s.query(TExtra).filter_by(mongo_id=t.mongo_id).one()
Beispiel #2
0
def test_cache_miss():
    s_nocache = Session.connect("unit-testing", cache_size=10)
    t = TExtra(i=4)
    s_nocache.insert(t)

    s = Session.connect("unit-testing", cache_size=10)
    s.add_to_session(t)
    t2 = s.query(TExtra).filter_by(mongo_id=t.mongo_id).one()
Beispiel #3
0
def test_dereference():
    class A(Document):
        x = IntField()

    s = Session.connect('unit-testing', cache_size=0)
    s.clear_collection(A)

    a = A(x=5)
    s.save(a)
    dbaref = DBRef(collection='A', id=a.mongo_id, database='unit-testing')
    s2 = Session.connect('unit-testing2', cache_size=0)
    assert s2.dereference(dbaref).x == 5
def test_dereference():
    class A(Document):
        x = IntField()

    s = Session.connect("unit-testing", cache_size=0)
    s.clear_collection(A)

    a = A(x=5)
    s.insert(a)
    dbaref = DBRef(collection="A", id=a.mongo_id, database="unit-testing")
    s2 = Session.connect("unit-testing2", cache_size=0)
    assert s2.dereference(dbaref).x == 5
Beispiel #5
0
def test_dereference_doc():
    class A(Document):
        x = IntField()
    
    s = Session.connect('unit-testing', cache_size=0)
    s.clear_collection(A)

    a = A(x=5)
    s.insert(a)
    dbaref = DBRef(collection='A', id=a.mongo_id, database='unit-testing')
    s2 = Session.connect('unit-testing2', cache_size=0)
    assert s2.dereference(a).x == 5
def test_tz_aware():
    import pytz
    from mongoalchemy.session import Session
    from mongoalchemy.document import Document

    # doc
    class DocTZ(Document):
        time = DateTimeField(use_tz=True)

    class DocNoTZ(Document):
        time = DateTimeField(use_tz=False)

    # timezone -- choose one where the author doesn't live
    eastern = pytz.timezone('Australia/Melbourne')
    utc = pytz.utc
    # session
    s = Session.connect('unit-testing', timezone=eastern)
    s.clear_collection(DocTZ)
    s.clear_collection(DocNoTZ)
    # time
    local = eastern.localize(datetime.now())
    local = local.replace(microsecond=0)
    doc = DocTZ(time=local)
    s.save(doc)

    doc = s.query(DocTZ).one()
    assert doc.time == local, (doc.time, local)

    # do the no timezone case for code coverage
    s.save(DocNoTZ(time=datetime(2012, 1, 1)))
    obj = s.query(DocNoTZ).one()
Beispiel #7
0
def admin(request):
    message = ''
    if 'form.submitted' in request.params:
        login = request.params['login']
        password = request.params['password']
        max_keys = request.params['max_keys']
        try:
            max_keys = int(max_keys)
        except:
            max_keys = -1
        with Session.connect(request.registry.settings['db_name']) as s:
            try:
                user = s.query(User).filter_by(email=login).one()
            except:
                user = User(
                    email=login,
                    password=password,
                    max_keys=max_keys,
                    groups=['admin'] if request.params.get('admin') else [],
                    scritti=[],
                    bloccati=[])
                s.insert(user)
                message = 'user created'

    return dict(message=message)
def test_geo():
    class Place(Document):
        config_collection_name = 'places4'
        loc = GeoField()
        val = IntField()
        index = Index().geo2d('loc', min=-100, max=100)
    s = Session.connect('unit-testing')
    s.clear_collection(Place)
    s.insert(Place(loc=(1,1), val=2))
    s.insert(Place(loc=(5,5), val=4))
    s.insert(Place(loc=(30,30 ), val=5))
    x = s.query(Place).filter(Place.loc.near(0, 1))
    assert x.first().val == 2, x.query

    xs = s.query(Place).filter(Place.loc.near(1, 1, max_distance=2)).all()
    assert len(xs) == 1, xs
    
    xs = s.query(Place).filter(Place.loc.near_sphere(1, 1, max_distance=50)).all()
    assert len(xs) == 3

    q = s.query(Place).filter(Place.loc.within_box([-2, -2], [2, 2]))
    assert len(q.all()) == 1, q.query

    q = s.query(Place).filter(Place.loc.within_radius(0, 0, 2))
    assert len(q.all()) == 1, q.query

    q = s.query(Place).filter(Place.loc.within_polygon(
        [[-2, 0], [2, 0], [0, 2], [0, -2]]
    ))
    assert len(q.all()) == 1, q.query

    q = s.query(Place).filter(Place.loc.within_radius_sphere(30, 30, 0.0001))
    assert len(q.all()) == 1, q.all()
Beispiel #9
0
    async def middleware(request):
        if request.path.startswith('/api/'):
            request.db_session = Session.connect(
                config.get("mongo_database_name"))

        response = await handler(request)
        return response
Beispiel #10
0
def test_update_list():
    s = Session.connect("unit-testing")
    s.clear_collection(TIntListDoc)

    tIntList = TIntListDoc(intlist=[1, 2])
    s.insert(tIntList)

    # pull out of db
    tFetched = s.query(TIntListDoc).one()

    assert sorted([1, 2]) == sorted(tFetched.intlist)

    # append to list, update
    l = tFetched.intlist
    l.append(3)
    s.update(tFetched)

    # pull out of db
    tFetched = s.query(TIntListDoc).one()

    assert sorted([1, 2, 3]) == sorted(tFetched.intlist)

    tFetched.intlist.remove(1)
    s.update(tFetched)

    tFetched = s.query(TIntListDoc).one()

    assert sorted([2, 3]) == sorted(tFetched.intlist)
Beispiel #11
0
def admin(request):
    message = ""
    if "form.submitted" in request.params:
        login = request.params["login"]
        password = request.params["password"]
        max_keys = request.params["max_keys"]
        try:
            max_keys = int(max_keys)
        except:
            max_keys = -1
        with Session.connect(request.registry.settings["db_name"]) as s:
            try:
                user = s.query(User).filter_by(email=login).one()
            except:
                user = User(
                    email=login,
                    password=password,
                    max_keys=max_keys,
                    groups=["admin"] if request.params.get("admin") else [],
                    scritti=[],
                    bloccati=[],
                )
                s.insert(user)
                message = "user created"

    return dict(message=message)
Beispiel #12
0
def test_update_ignore_extras():
    s = Session.connect("unit-testing")
    s.clear_collection(TExtra)
    # Create Object
    t = TExtra(i=1, j="test", k="test2")
    s.insert(t)
    # Retrieve Object
    t = s.query(TExtra).one()
    assert t.i == 1
    assert t.get_extra_fields()["j"] == "test"
    assert t.get_extra_fields()["k"] == "test2"
    # Update Object
    t.i = 5
    del t.get_extra_fields()["j"]  # delete an extra field
    t.get_extra_fields()["k"] = "changed"  # change an extra field
    t.get_extra_fields()["l"] = "added"  # new extra field
    s.update(t)

    # Retrieve Object
    t_new = s.query(TExtra).one()

    assert "j" not in t_new.get_extra_fields()
    assert t_new.get_extra_fields()["k"] == "changed"
    assert t_new.get_extra_fields()["l"] == "added"
    assert t_new.i == 5
def test_geo():
    class Place(Document):
        config_collection_name = 'places4'
        loc = GeoField()
        val = IntField()
        index = Index().geo2d('loc', min=-100, max=100)
    s = Session.connect('unit-testing')
    s.clear_collection(Place)
    s.insert(Place(loc=(1,1), val=2))
    s.insert(Place(loc=(5,5), val=4))
    s.insert(Place(loc=(30,30 ), val=5))
    x = s.query(Place).filter(Place.loc.near(0, 1))
    assert x.first().val == 2, x.query

    xs = s.query(Place).filter(Place.loc.near(1, 1, max_distance=2)).all()
    assert len(xs) == 1, xs

    xs = s.query(Place).filter(Place.loc.near_sphere(1, 1, max_distance=50)).all()
    assert len(xs) == 3

    q = s.query(Place).filter(Place.loc.within_box([-2, -2], [2, 2]))
    assert len(q.all()) == 1, q.query

    q = s.query(Place).filter(Place.loc.within_radius(0, 0, 2))
    assert len(q.all()) == 1, q.query

    q = s.query(Place).filter(Place.loc.within_polygon(
        [[-2, 0], [2, 0], [0, 2], [0, -2]]
    ))
    assert len(q.all()) == 1, q.query

    q = s.query(Place).filter(Place.loc.within_radius_sphere(30, 30, 0.0001))
    assert len(q.all()) == 1, q.all()
Beispiel #14
0
def test_update_ignore_extras():
    s = Session.connect('unit-testing')
    s.clear_collection(TExtra)
    # Create Object
    t = TExtra(i=1, j='test', k='test2')
    s.save(t)
    # Retrieve Object
    t = s.query(TExtra).one()
    assert t.i == 1
    assert t.get_extra_fields()['j'] == 'test'
    assert t.get_extra_fields()['k'] == 'test2'
    # Update Object
    t.i = 5
    del t.get_extra_fields()['j']  # delete an extra field
    t.get_extra_fields()['k'] = 'changed'  # change an extra field
    t.get_extra_fields()['l'] = 'added'  # new extra field
    s.update(t)

    # Retrieve Object
    t_new = s.query(TExtra).one()

    assert 'j' not in t_new.get_extra_fields()
    assert t_new.get_extra_fields()['k'] == 'changed'
    assert t_new.get_extra_fields()['l'] == 'added'
    assert t_new.i == 5
Beispiel #15
0
def test_cache_max():
    # not a great test, but gets coverage
    s = Session.connect('unit-testing', cache_size=3)
    for i in range(0, 10):
        t = TExtra(i=4)
        s.save(t)
    assert len(s.cache) == 3
Beispiel #16
0
def test_update_ignore_extras():
    s = Session.connect('unit-testing')
    s.clear_collection(TExtra)
    # Create Object
    t = TExtra(i=1, j='test', k='test2')
    s.save(t)
    # Retrieve Object
    t = s.query(TExtra).one()
    assert t.i == 1
    assert t.get_extra_fields()['j'] == 'test'
    assert t.get_extra_fields()['k'] == 'test2'
    # Update Object
    t.i = 5
    del t.get_extra_fields()['j'] # delete an extra field
    t.get_extra_fields()['k'] = 'changed' # change an extra field
    t.get_extra_fields()['l'] = 'added' # new extra field
    s.update(t)

    # Retrieve Object
    t_new = s.query(TExtra).one()

    assert 'j' not in t_new.get_extra_fields()
    assert t_new.get_extra_fields()['k'] == 'changed'
    assert t_new.get_extra_fields()['l'] == 'added'
    assert t_new.i == 5
Beispiel #17
0
def test_update_list():
    s = Session.connect('unit-testing')
    s.clear_collection(TIntListDoc)

    tIntList = TIntListDoc(intlist=[1, 2])
    s.save(tIntList)

    # pull out of db
    tFetched = s.query(TIntListDoc).one()

    assert sorted([1, 2]) == sorted(tFetched.intlist)

    # append to list, update
    l = tFetched.intlist
    l.append(3)
    s.update(tFetched)

    # pull out of db
    tFetched = s.query(TIntListDoc).one()

    assert sorted([1, 2, 3]) == sorted(tFetched.intlist)

    tFetched.intlist.remove(1)
    s.update(tFetched)

    tFetched = s.query(TIntListDoc).one()

    assert sorted([2, 3]) == sorted(tFetched.intlist)
Beispiel #18
0
def login(request):
	basept = get_renderer('templates/base.pt').implementation()
	login_url = resource_url(request.context, request, 'login')
	referrer = request.url
	if referrer == login_url:
		referrer = request.application_url
	came_from = request.params.get('came_from', referrer)
	message = ''
	login = ''
	password = ''
	if 'form.submitted' in request.params:
		login = request.params['login']
		password = request.params['password']
		with Session.connect(request.registry.settings['db_name'] ) as s:
			try:
				user = s.query(User).filter_by(email=login).one()
			except:
				user = None
		if user is not None and user.password == password:
			headers = remember(request, login)
			return HTTPFound(location = came_from,
							 headers = headers)
		message = 'Failed login'
    
	return dict(
		message = message,
		url = request.application_url + '/login',
		came_from = came_from,
		login = login,
		password = password,
		base_pt = basept
		)
Beispiel #19
0
def test_cache_max():
    # not a great test, but gets coverage
    s = Session.connect("unit-testing", cache_size=3)
    for i in range(0, 10):
        t = TExtra(i=4)
        s.insert(t)
    assert len(s.cache) == 3
def test_tz_aware():
    import pytz
    from mongoalchemy.session import Session
    from mongoalchemy.document import Document
    # doc
    class DocTZ(Document):
        time = DateTimeField(use_tz=True)
    class DocNoTZ(Document):
        time = DateTimeField(use_tz=False)

    # timezone -- choose one where the author doesn't live
    eastern = pytz.timezone('Australia/Melbourne')
    utc = pytz.utc
    # session
    s = Session.connect('unit-testing', timezone=eastern)
    s.clear_collection(DocTZ)
    s.clear_collection(DocNoTZ)
    # time
    local = eastern.localize(datetime.now())
    local = local.replace(microsecond=0)
    doc = DocTZ(time=local)
    s.insert(doc)

    doc = s.query(DocTZ).one()
    assert doc.time == local, (doc.time, local)

    # do the no timezone case for code coverage
    s.insert(DocNoTZ(time=datetime(2012, 1, 1)))
    obj = s.query(DocNoTZ).one()
def test_regex():
    class Spell(Document):
        name = StringField()
    s = Session.connect('unit-testing')
    s.clear_collection(Spell)
    s.save(Spell(name='Wingardium Leviosa'))
    s.save(Spell(name='abracadabra'))
    s.save(Spell(name='ab.*ra.ca.da.*bra'))
    s.save(Spell(name='Alacazam'))

    # check ignore case True
    q = s.query(Spell).filter(Spell.name.startswith('wingardium', ignore_case=True))
    assert q.first().name == 'Wingardium Leviosa'

    # check ignore case False (default)
    xs = s.query(Spell).filter(Spell.name.startswith('wingardium')).all()
    assert len(xs) == 0

    # check regex-free startswith and endswith
    assert len(s.query(Spell).filter(Spell.name.startswith('ab.*ra.ca')).all()) == 1
    assert len(s.query(Spell).filter(Spell.name.endswith('da.*bra')).all()) == 1

    # check regex
    assert len(s.query(Spell).filter(Spell.name.regex(r'^[Aa]\w*[am]$')).all()) == 2

    # check regex with options
    assert len(s.query(Spell).filter(Spell.name.regex(r'^[a]\w*[am]$', options='i')).all()) == 2
def get_news_content(url):
    html = download(url)
    soup = BeautifulSoup(html, "html.parser")
    # print soup.prettify("utf-8")
    print soup.find(attrs={"class": "pg-headline"})
    title = soup.find(attrs={"class": "pg-headline"})
    print soup.find(attrs={"class": "metadata__byline__author"})
    author = soup.find(attrs={"class": "metadata__byline__author"})
    print soup.find(attrs={"class": "update-time"})
    update_time = soup.find(attrs={"class": "update-time"})
    contents = soup.find_all(attrs={"class": "zn-body__paragraph"})
    content = ""
    for i in contents:
        print i.text
        content += i.text

    #存入mongoDB
    session = Session.connect('runoob')
    #session.clear_collection(News)

    news = News(title=str(title),
                author=str(author),
                update_time=str(update_time),
                content=str(content))
    print news.title
    session.save(news)
    print '查询结果'

    result = session.query(News).skip(3).limit(2)
    for news in session.query(News).skip(3).limit(2):
        print news.title, news.update_time
def test_regex():
    class Spell(Document):
        name = StringField()

    s = Session.connect('unit-testing')
    s.clear_collection(Spell)
    s.save(Spell(name='Wingardium Leviosa'))
    s.save(Spell(name='abracadabra'))
    s.save(Spell(name='ab.*ra.ca.da.*bra'))
    s.save(Spell(name='Alacazam'))

    # check ignore case True
    q = s.query(Spell).filter(
        Spell.name.startswith('wingardium', ignore_case=True))
    assert q.first().name == 'Wingardium Leviosa'

    # check ignore case False (default)
    xs = s.query(Spell).filter(Spell.name.startswith('wingardium')).all()
    assert len(xs) == 0

    # check regex-free startswith and endswith
    assert len(
        s.query(Spell).filter(Spell.name.startswith('ab.*ra.ca')).all()) == 1
    assert len(s.query(Spell).filter(
        Spell.name.endswith('da.*bra')).all()) == 1

    # check regex
    assert len(
        s.query(Spell).filter(Spell.name.regex(r'^[Aa]\w*[am]$')).all()) == 2

    # check regex with options
    assert len(
        s.query(Spell).filter(Spell.name.regex(r'^[a]\w*[am]$',
                                               options='i')).all()) == 2
Beispiel #24
0
def login(request):
    basept = get_renderer('templates/base.pt').implementation()
    login_url = resource_url(request.context, request, 'login')
    referrer = request.url
    if referrer == login_url:
        referrer = request.application_url
    came_from = request.params.get('came_from', referrer)
    message = ''
    login = ''
    password = ''
    if 'form.submitted' in request.params:
        login = request.params['login']
        password = request.params['password']
        with Session.connect(request.registry.settings['db_name']) as s:
            try:
                user = s.query(User).filter_by(email=login).one()
            except:
                user = None
        if user is not None and user.password == password:
            headers = remember(request, login)
            return HTTPFound(location=came_from, headers=headers)
        message = 'Failed login'

    return dict(message=message,
                url=request.application_url + '/login',
                came_from=came_from,
                login=login,
                password=password,
                base_pt=basept)
Beispiel #25
0
def test_cache3():
    s = Session.connect('unit-testing', cache_size=10)
    t = TExtra(i=4)
    s.save(t)
    s.save(t)
    t2 = s.query(TExtra).filter_by(mongo_id=t.mongo_id)[0]
    assert id(t) == id(t2)
    assert id(s.refresh(t)) != t2
def test_bad_dereference():
    class A(Document):
        x = IntField()

    s = Session.connect("unit-testing", cache_size=0)
    s.clear_collection(A)
    dbaref = DBRef(collection="A", id=ObjectId(), database="unit-testing")
    s.dereference(dbaref)
Beispiel #27
0
def groupfinder(userid, request):
    with Session.connect(request.registry.settings['db_name']) as s:
        user = s.query(User).filter_by(email=userid).one()
    try:
        r = user.groups
    except AttributeError:
        r = []
    return r
Beispiel #28
0
def test_bad_dereference():
    class A(Document):
        x = IntField()

    s = Session.connect('unit-testing', cache_size=0)
    s.clear_collection(A)
    dbaref = DBRef(collection='A', id=ObjectId(), database='unit-testing')
    s.dereference(dbaref)
Beispiel #29
0
    async def middleware(request):
        if request.path.startswith('/api/'):
            request.db_session = Session.connect(
                config.get("mongo_database_name")
            )

        response = await handler(request)
        return response
Beispiel #30
0
def groupfinder(userid, request):
	with Session.connect(request.registry.settings['db_name'] ) as s:
		user = s.query(User).filter_by(email=userid).one()
	try:
		r = user.groups
	except AttributeError:
		r = []
	return r
Beispiel #31
0
def test_cache():
    s = Session.connect("unit-testing", cache_size=10)
    t = TExtra(i=4)
    s.insert(t)
    s.insert(t)
    t2 = s.query(TExtra).filter_by(mongo_id=t.mongo_id).one()
    assert id(t) == id(t2)
    assert id(s.refresh(t)) != t2
Beispiel #32
0
def test_cache3():
    s = Session.connect('unit-testing', cache_size=10)
    t = TExtra(i=4)
    s.save(t)
    s.save(t)
    t2 = s.query(TExtra).filter_by(mongo_id=t.mongo_id)[0]
    assert id(t) == id(t2)
    assert id(s.refresh(t)) != t2
Beispiel #33
0
def test_cache2():
    s = Session.connect('unit-testing', cache_size=10)
    t = TExtra(i=4)
    s.insert(t)
    s.insert(t)
    for t2 in s.query(TExtra).filter_by(mongo_id=t.mongo_id):
        assert id(t) == id(t2)
        assert id(s.refresh(t)) != t2
        break
Beispiel #34
0
def test_clone():
    s = Session.connect("unit-testing", cache_size=10)
    t = TExtra(i=4)
    s.insert(t)

    t2 = s.clone(t)
    s.insert(t2)

    assert t2.mongo_id != t.mongo_id
Beispiel #35
0
 def post(self):
     data = parser.parse_args()
     account = Account.query.filter(Account.username == data['username'])
     temp_stocks = [data['stock']]
     if hasattr(account.first(), 'stocks'):
         temp_stocks.extend(account.first().stocks)
     with Session.connect('mongoalchemy') as s:
         update = account.set(Account.stocks, temp_stocks)
         update.execute()
Beispiel #36
0
def test_safe_with_error():
    s = Session.connect("unit-testing")
    s.clear_collection(TUnique)
    s.insert(TUnique(i=1))
    try:
        s.insert(TUnique(i=1), safe=True)
        assert False, "No error raised on safe insert for second unique item"
    except DuplicateKeyError:
        assert len(s.queue) == 0
Beispiel #37
0
def test_safe_with_error():
    s = Session.connect('unit-testing')
    s.clear_collection(TUnique)
    s.save(TUnique(i=1))
    try:
        s.save(TUnique(i=1), safe=True)
        assert False, 'No error raised on safe insert for second unique item'
    except DuplicateKeyError:
        assert len(s.queue) == 0
Beispiel #38
0
def test_cache2():
    s = Session.connect('unit-testing', cache_size=10)
    t = TExtra(i=4)
    s.insert(t)
    s.insert(t)
    for t2 in s.query(TExtra).filter_by(mongo_id=t.mongo_id):
        assert id(t) == id(t2)
        assert id(s.refresh(t)) != t2
        break
Beispiel #39
0
def test_clone():
    s = Session.connect('unit-testing', cache_size=10)
    t = TExtra(i=4)
    s.save(t)

    t2 = s.clone(t)
    s.save(t2)

    assert t2.mongo_id != t.mongo_id
Beispiel #40
0
def health_check():
    try:
        #session = Session.connect('library', host=app.config['MONGO_URI'])
        session = Session.connect(app.config['MONGOALCHEMY_DATABASE'])
    except Exception as e:
        return Response(str(e.args[0]), status=HTTPStatus.INTERNAL_SERVER_ERROR.value)
    data = json.dumps({'msg': 'Still alive'})
    resp = Response(data, status=HTTPStatus.OK.value, mimetype='application/json')
    return resp
def test_geo_haystack():
    class Place(Document):
        config_collection_name = 'places'
        loc = GeoField()
        val = IntField()
        index = Index().geo_haystack('loc', bucket_size=100).descending('val')
    s = Session.connect('unit-testing')
    s.clear_collection(Place)
    s.insert(Place(loc=(1,1), val=2))
    s.insert(Place(loc=(5,5), val=4))
Beispiel #42
0
def test_update():
    s = Session.connect('unit-testing')
    s.clear_collection(T)
    t = T(i=6)
    s.save(t)
    assert s.query(T).one().i == 6

    t.i = 7
    s.update(t)
    assert s.query(T).one().i == 7
Beispiel #43
0
def back(context, request):
    user = _get_user(request)
    keyword = request.GET.get("keyword")
    if keyword in user.scritti:
        user.scritti.remove(keyword)
    if keyword in user.bloccati:
        user.bloccati.remove(keyword)
    with Session.connect(request.registry.settings["db_name"]) as s:
        s.insert(user)
    return dict()
def test_geo_haystack():
    class Place(Document):
        config_collection_name = 'places'
        loc = GeoField()
        val = IntField()
        index = Index().geo_haystack('loc', bucket_size=100).descending('val')
    s = Session.connect('unit-testing')
    s.clear_collection(Place)
    s.insert(Place(loc=(1,1), val=2))
    s.insert(Place(loc=(5,5), val=4))
Beispiel #45
0
def pop(context, request):
    with Session.connect(request.registry.settings['db_name']) as s:
        s.clear_collection(User)
        u = User(email='test1',
                 password='******',
                 groups=['admin'],
                 scritti=[],
                 bloccati=[])
        s.insert(u)
        return dict()
Beispiel #46
0
def back(context, request):
    user = _get_user(request)
    keyword = request.GET.get('keyword')
    if keyword in user.scritti:
        user.scritti.remove(keyword)
    if keyword in user.bloccati:
        user.bloccati.remove(keyword)
    with Session.connect(request.registry.settings['db_name']) as s:
        s.insert(user)
    return dict()
Beispiel #47
0
def test_update():
    s = Session.connect("unit-testing")
    s.clear_collection(T)
    t = T(i=6)
    s.insert(t)
    assert s.query(T).one().i == 6

    t.i = 7
    s.update(t)
    assert s.query(T).one().i == 7
Beispiel #48
0
def test_auto_ensure_indexes():
    s = Session.connect('unit-testing', auto_ensure=True)
    s.db.drop_collection(TUnique.get_collection_name())

    s.save(TUnique(i=1))

    indexes = s.get_indexes(TUnique)

    assert len(indexes) == 2
    assert "_id_" in indexes
    assert "i_1" in indexes
Beispiel #49
0
def test_auto_ensure_indexes():
    s = Session.connect("unit-testing", auto_ensure=True)
    s.db.drop_collection(TUnique.get_collection_name())

    s.insert(TUnique(i=1))

    indexes = s.get_indexes(TUnique)

    assert len(indexes) == 2
    assert "_id_" in indexes
    assert "i_1" in indexes
Beispiel #50
0
 def post(self):
     data = parser.parse_args()
     account = Account.query.filter(Account.username == data['username'])
     if not hasattr(account.first(), 'stocks'):
         print("empty")
     else:
         temp_stocks = account.first().stocks
         if data['stock'] in temp_stocks: temp_stocks.remove(data['stock'])
         with Session.connect('mongoalchemy') as s:
             update = account.set(Account.stocks, temp_stocks)
             update.execute()
Beispiel #51
0
def health_check():
    try:
        #session = Session.connect('library', host=app.config['MONGO_URI'])
        session = Session.connect(app.config['MONGOALCHEMY_DATABASE'])
    except Exception as e:
        return Response(str(e.args[0]),
                        status=HTTPStatus.INTERNAL_SERVER_ERROR.value)
    data = json.dumps({'msg': 'Still alive'})
    resp = Response(data,
                    status=HTTPStatus.OK.value,
                    mimetype='application/json')
    return resp
Beispiel #52
0
def test_update_change_ops():
    s = Session.connect("unit-testing")
    s.clear_collection(T)
    t = T(i=6, l=[8])
    s.insert(t)
    assert s.query(T).one().i == 6

    t.i = 7
    t.l = [8]
    s.update(t, update_ops={T.l: "$pullAll"}, i="$inc")
    t = s.query(T).one()
    assert t.i == 13, t.i
    assert t.l == [], t.l
Beispiel #53
0
def test_update_change_ops():
    s = Session.connect('unit-testing')
    s.clear_collection(T)
    t = T(i=6, l=[8])
    s.save(t)
    assert s.query(T).one().i == 6

    t.i = 7
    t.l = [8]
    s.update(t, update_ops={T.l: '$pullAll'}, i='$inc')
    t = s.query(T).one()
    assert t.i == 13, t.i
    assert t.l == [], t.l
 def __init__(self, name, db_name):
     self.name = name
     self.db_name = db_name
     # TODO get the mongoalchemy session
     # from server/utils:get_mongoalchemy_session
     self.session = Session.connect(
         db_name,
         tz_aware=True,
         timezone=tz.gettz('UTC')
     )
     # TODO get the mongo client from server/utils:get_mongo_client
     self.mongo_client = MongoClient()
     self.db = self.mongo_client[self.db_name]
Beispiel #55
0
def test_update_change_ops():
    s = Session.connect('unit-testing')
    s.clear_collection(T)
    t = T(i=6, l=[8])
    s.save(t)
    assert s.query(T).one().i == 6

    t.i = 7
    t.l = [8]
    s.update(t, update_ops={T.l:'$pullAll'}, i='$inc')
    t = s.query(T).one()
    assert t.i == 13, t.i
    assert t.l == [], t.l
Beispiel #56
0
 def __init__(self, db, uri, create_ok=False, inc_manipulators=True):
     
     self.malchemy = Session.connect(db, host=uri, timezone=pytz.utc)
     self.create_ok = create_ok
     self.db = self.malchemy.db
     self.docs = self.db[DOCS_COLLECTION]
     self.docs.ensure_index('_digest')
     self.metrics_data = self.db[METRICS_DATA_COLLECTION]
     self.metrics_data.ensure_index('_digest')
     self.pymongo = self.db.connection
     if inc_manipulators:
         # NOTE: order is important here
         self.add_manipulator(DigestInjector([DOCS_COLLECTION, METRICS_DATA_COLLECTION]))
         self.add_manipulator(DatetimeInjector([DOCS_COLLECTION, METRICS_DATA_COLLECTION]))
         self.add_manipulator(DereferenceManipulator(MONGO_DOCS_DEREF_FIELDS))
Beispiel #57
0
 def __init__(self, db, uri, safe=False, create_ok=False, inc_manipulators=True):
     
     self.malchemy = Session.connect(db, host=uri, timezone=pytz.utc)
     self.create_ok = create_ok
     self.db = self.malchemy.db
     self.docs = self.db[DOCS_COLLECTION]
     self.docs.ensure_index('_digest')
     self.pymongo = self.db.connection
     if safe:
         self.pymongo.db.write_concern = {'w': 1, 'j': True}
     if inc_manipulators:
         # NOTE: order is important here
         self.add_manipulator(DigestInjector(DOCS_COLLECTION))
         self.add_manipulator(DatetimeInjector(DOCS_COLLECTION))
         self.add_manipulator(DereferenceManipulator(MONGO_DOCS_DEREF_FIELDS))
Beispiel #58
0
def test_transactions3():
    class Doc(Document):
        i = IntField()
    s = Session.connect('unit-testing')
    s.clear_collection(Doc)
    assert s.query(Doc).count() == 0
    with s:
        s.add(Doc(i=4))
        try:

            with s:
                s.add(Doc(i=2))
                raise Exception()
        except:
            assert s.query(Doc).count() == 0, s.query(Doc).count()
    assert s.query(Doc).count() == 1, s.query(Doc).count()
Beispiel #59
0
def test_update_push():
    s = Session.connect('unit-testing')
    s.clear_collection(T)
    # Create object
    t = T(i=6, l=[3])
    s.save(t)
    t = s.query(T).one()
    assert t.i == 6 and t.l == [3]

    t = s.query(T).fields(T.i).one()
    t.i = 7
    t.l = [4]
    s.update(t, id_expression=T.i == 6)

    t = s.query(T).one()
    assert s.query(T).one().i == 7 and t.l == [3, 4]