# TEST SCHEMAS comment_schema = Schema({ "commenter": {"type": name_schema, "required": True}, "email": {"type": basestring, "required": False}, "comment": {"type": basestring, "required": True}, "votes": {"type": int, "default": 0} }) blog_post_schema = Schema({ "author": {"type": name_schema, "required": True}, "content": {"type": Schema({ "title": {"type": basestring, "required": True}, "text": {"type": basestring, "required": True}, "page_views": {"type": int, "default": 1} }), "required": True}, "category": {"type": basestring, "validates":one_of("cooking", "politics")}, "comments": [comment_schema], "likes": {"type": int, "default": 0}, "creation_date": {"type": datetime, "default": stubnow}, "tags": [basestring], "misc": {"type": Mixed}, "linked_id": {"type": Mixed(int, basestring)}, "publication_id": {"type": ObjectId} }) def valid_doc(overrides=None): doc = { "author": { "first": "John", "last": "Humphreys"
def test_valid(self): self.validator = one_of('peas', 'carrots') self.assertIsNone(self.validator('peas')) self.assertIsNone(self.validator('carrots'))
from utils import costumes ROOMS_TABLE = 'rooms' KILLS_TABLE = 'kills' GNOME_TABLE = 'gnome' ROULETTE_TABLE = 'roulette' RATE_TABLE = 'rate' DOCTOR_TABLE = 'doctor' TABLES = [ ROOMS_TABLE, KILLS_TABLE, GNOME_TABLE, ROULETTE_TABLE, RATE_TABLE, DOCTOR_TABLE ] score_schema = Schema({ 'uid': {'type': str, 'required': True}, 'name': {'type': str, 'required': True}, 'score': {'type': int, 'required': True}, 'leaderboard': {'type': str, 'validates': one_of(*TABLES)}, 'death_reason': {'type': str } }) def get_model(db): Scores = create_model(score_schema, db['scores']) @Scores.class_method def get_leaderboard(cls, leaderboard, count=10): if leaderboard == 'death': res = list(cls.find({"leaderboard": 'rooms'})) keyf = Code("function(doc) {return{\"death_reason\": doc.death_reason}}") reducer = Code("function(curr, result) { result.count++; }") res = list(cls.get_collection().group(
def test_invalid_array(self): self.validator = one_of(['peas', 'carrots']) self.assertEqual( "'sweetcorn' is not in the list ('peas', 'carrots')", self.validator('sweetcorn'))
from mongothon import Document, Schema, NotFoundException, Array from mongothon.validators import one_of from mongothon.scopes import STANDARD_SCOPES from bson import ObjectId from copy import deepcopy from .fake import FakeCursor car_schema = Schema({ "make": {"type": basestring, "required": True}, "model": {"type": basestring, "required": True}, "trim": {"type": Schema({ "ac": {"type": bool, "default": True}, "doors": {"type": int, "required": True, "default": 4} }), "required": True}, "wheels": {"type": Array(Schema({ "position": {"type": basestring, "required": True, "validates": one_of('FR', 'FL', 'RR', 'RL')}, "tire": {"type": basestring}, "diameter": {"type": int} }))}, "options": {"type": Array(basestring)} }) doc = { "make": "Peugeot", "model": "406", "trim": { "ac": False, "doors": 5 }, "wheels": [
}, "text": { "type": basestring, "required": True }, "page_views": { "type": int, "default": 1 } }), "required": True }, "category": { "type": basestring, "validates": one_of("cooking", "politics") }, "comments": { "type": Array(comment_schema) }, "likes": { "type": int, "default": 0 }, "creation_date": { "type": datetime, "default": stubnow }, "tags": { "type": Array(basestring) },
def test_single_validation_function(self): Schema({'some_field': {'type':int, "validates":one_of(['a', 'b'])}}).verify()
"type": int, "required": True, "default": 4 } }), "required": True }, "wheels": { "type": Array( Schema({ "position": { "type": str, "required": True, "validates": one_of('FR', 'FL', 'RR', 'RL') }, "tire": { "type": str }, "diameter": { "type": int } })) }, "options": { "type": Array(str) } }) doc = {