()
| 20 | |
| 21 | |
| 22 | def test_basic(): |
| 23 | class Book(Document): |
| 24 | name = StringField() |
| 25 | pages = IntField() |
| 26 | tags = ListField(StringField()) |
| 27 | is_published = BooleanField() |
| 28 | author_email = EmailField() |
| 29 | |
| 30 | Book.drop_collection() |
| 31 | |
| 32 | def init_book(): |
| 33 | return Book( |
| 34 | name="Always be closing", |
| 35 | pages=100, |
| 36 | tags=["self-help", "sales"], |
| 37 | is_published=True, |
| 38 | author_email="alec@example.com", |
| 39 | ) |
| 40 | |
| 41 | print("Doc initialization: %.3fus" % (timeit(init_book, 1000) * 10**6)) |
| 42 | |
| 43 | b = init_book() |
| 44 | print("Doc getattr: %.3fus" % (timeit(lambda: b.name, 10000) * 10**6)) |
| 45 | |
| 46 | print( |
| 47 | "Doc setattr: %.3fus" |
| 48 | % (timeit(lambda: setattr(b, "name", "New name"), 10000) * 10**6) # noqa B010 |
| 49 | ) |
| 50 | |
| 51 | print("Doc to mongo: %.3fus" % (timeit(b.to_mongo, 1000) * 10**6)) |
| 52 | |
| 53 | print("Doc validation: %.3fus" % (timeit(b.validate, 1000) * 10**6)) |
| 54 | |
| 55 | def save_book(): |
| 56 | b._mark_as_changed("name") |
| 57 | b._mark_as_changed("tags") |
| 58 | b.save() |
| 59 | |
| 60 | print("Save to database: %.3fus" % (timeit(save_book, 100) * 10**6)) |
| 61 | |
| 62 | son = b.to_mongo() |
| 63 | print("Load from SON: %.3fus" % (timeit(lambda: Book._from_son(son), 1000) * 10**6)) |
| 64 | |
| 65 | print("Load from database: %.3fus" % (timeit(lambda: Book.objects[0], 100) * 10**6)) |
| 66 | |
| 67 | def create_and_delete_book(): |
| 68 | b = init_book() |
| 69 | b.save() |
| 70 | b.delete() |
| 71 | |
| 72 | print( |
| 73 | "Init + save to database + delete: %.3fms" |
| 74 | % (timeit(create_and_delete_book, 10) * 10**3) |
| 75 | ) |
| 76 | |
| 77 | |
| 78 | def test_big_doc(): |
no test coverage detected