def test_configure(): # a) keyword arguments assert not hasattr(_Options, 'foo') configure(foo='bar') assert hasattr(_Options, 'foo') del _Options.foo # b) module assert not hasattr(_Options, 'foo') module = ModuleType('config') module.MONGODB_FOO = 'bar' module.NON_MONGO_ATTR = 'bar' configure(foo='bar') assert not hasattr(_Options, 'NON_MONGO_ATTR') assert not hasattr(_Options, 'MONGODB_FOO') assert hasattr(_Options, 'foo') del _Options.foo # c) non-module (fails silently) try: configure(42) configure(None) configure('foobar') except Exception: pytest.fail('configure() should fail silently on invalid input.')
def test_nometa(): configure(database='test') try: class SomeModel(Model): pass except Exception: pytest.fail('A model with no Meta is perfectly fine :)') del _Options.database
def test_optoins_configure(): # Options have no defaults yet -- configure() was never called. with pytest.raises(AttributeError): _Options.foo configure(foo='bar') try: assert _Options.foo == 'bar' except AttributeError: pytest.fail('Options.foo should\'ve been set.') del _Options.foo
#!/usr/bin/env python #coding=utf-8 # 实现了MVC中的模型层 __authors__ = ['"zeroq" <*****@*****.**>'] from minimongo import Model, Index, configure configure(host='127.0.0.1', port=27018, database = "test") class BasketBall(Model): class Meta: collection = "BasketBall" indices = ( Index("username"), )
#!/usr/bin/env python # -*- coding: utf-8 -*- from minimongo import Model, Index, configure import json # Mongo db configure(host="localhost", port=27017) db_name = "tweets" class User(Model): class Meta: # Here, we specify the database and collection names. # A connection to your DB is automatically created. database = db_name collection = "weibousers" # Now, we programatically declare what indices we want. # The arguments to the Index constructor are identical to # the args to pymongo"s ensure_index function. # TODO : add indexes indices = (Index("uid"), Index("province")) def __init__(self): pass
#!/usr/bin/env python # -*- coding: utf-8 -*- import sys import os import argparse sys.path.append('minimongo') # load mongodb credentials import minimongo import mongocredentials minimongo.configure(module = mongocredentials) import osm import osm.compiler import osm.factory import osm.sink import re import unittest def mongo_legal_key_escape(s): '''Make sure a string is a legal mongo key name, substitute unsafe characters''' l = [] for c in s: if c == '%': l.append('%%') elif c == '~': l.append('~~') elif c == '^':
#!/usr/bin/env python # -*- coding: utf-8 -*- from minimongo import Model, Index, configure import json # mongo db configure(host="localhost", port=27017) db_name="tweets" class Tweet(Model): class Meta: # Here, we specify the database and collection names. # A connection to your DB is automatically created. database = db_name collection = "tweets" # Now, we programatically declare what indices we want. # The arguments to the Index constructor are identical to # the args to pymongo"s ensure_index function. # TODO : add indexes indices = ( # Index("mid"), Index("mid"), Index("hashtags"), Index("urls"), Index("mentions") ) def __init__(self):
from minimongo import configure, Model from minimongo.model import ObjectId import utils import time """ To Remove any Object from the Database Simply call object.remove() """ configure(database="bms") class User(Model): @staticmethod def get_user_from_id(guid): return User.collection.find_one({'guid':guid}) @staticmethod def update_user(update_dict): guid = update_dict["guid"] user = User.get_user_from_id(guid) if user: pass else: user = User(random = "axpr") for key in update_dict: #if key == "guid": # continue user[key] = update_dict[key] return user.save() class Meta: database = "bms" collection = "user"
def mongo_config(config,prefix='MONGODB_'): attrs = config.__dict__.iteritems() attrs = ((attr.replace(prefix, '').lower(), value) for attr, value in attrs if attr.startswith(prefix)) minimongo.configure(**dict(attrs))
# config if "TOPOGRAM_MONGO_HOST" in os.environ: host=os.environ.get('TOPOGRAM_MONGO_HOST') else: host="localhost" if "TOPOGRAM_MONGO_PORT" in os.environ: port=os.environ.get('TOPOGRAM_MONGO_PORT') else: port=27017 if "TOPOGRAM_MONGO_DB" in os.environ: db_name=os.environ.get('TOPOGRAM_MONGO_DB') else: db_name="weiboscope" # Mongo db configure(host=host, port=port) class User(Model): class Meta: # Here, we specify the database and collection names. # A connection to your DB is automatically created. database = db_name collection = "users" # Now, we programatically declare what indices we want. # The arguments to the Index constructor are identical to # the args to pymongo"s ensure_index function. # TODO : add indexes indices = ( Index("uid"),
import config_options from minimongo.model import CASCADE from minimongo import Model, Index, configure configure(config_options) class Author(Model): class Meta: database = 'minimongo' collection = 'author' indices = ( Index("first_name", unique=True), ) class Post(Model): class Meta: database = 'minimongo' collection = 'post' references = ( ("author", Author, CASCADE), ) # field_name : class_name : type p = Post.collection.find_one({"time":{"$exists":"true"}}) print p #posts = Post.collection.find({"content":"Second post"}) #for post in posts: # print post #auth1 = Author.collection.find_one({"last_name":{"$regex":"laso"}}) #ref = auth1.dbref() #Post.collection.update({"content":"superior"}, {"$set":{"author":ref}}) #c = Post.collection.find({"author":auth1.dbref()}).count() #p = Post(content="Superior!", author=auth1.dbref())