updated changelog + improved query_counter test
This commit is contained in:
		| @@ -24,6 +24,7 @@ Development | |||||||
| - The codebase is now formatted using ``black``. #2109 | - The codebase is now formatted using ``black``. #2109 | ||||||
| - In bulk write insert, the detailed error message would raise in exception. | - In bulk write insert, the detailed error message would raise in exception. | ||||||
| - Added ability to compare Q and Q operations #2204 | - Added ability to compare Q and Q operations #2204 | ||||||
|  | - Added ability to use a db alias on query_counter #2194 | ||||||
|  |  | ||||||
| Changes in 0.18.2 | Changes in 0.18.2 | ||||||
| ================= | ================= | ||||||
|   | |||||||
| @@ -171,7 +171,7 @@ class no_sub_classes(object): | |||||||
| class query_counter(object): | class query_counter(object): | ||||||
|     """Query_counter context manager to get the number of queries. |     """Query_counter context manager to get the number of queries. | ||||||
|     This works by updating the `profiling_level` of the database so that all queries get logged, |     This works by updating the `profiling_level` of the database so that all queries get logged, | ||||||
|     resetting the db.system.profile collection at the beginnig of the context and counting the new entries. |     resetting the db.system.profile collection at the beginning of the context and counting the new entries. | ||||||
|  |  | ||||||
|     This was designed for debugging purpose. In fact it is a global counter so queries issued by other threads/processes |     This was designed for debugging purpose. In fact it is a global counter so queries issued by other threads/processes | ||||||
|     can interfere with it |     can interfere with it | ||||||
|   | |||||||
| @@ -2825,44 +2825,6 @@ class TestInstance(MongoDBTestCase): | |||||||
|  |  | ||||||
|         assert "testdb-1" == B._meta.get("db_alias") |         assert "testdb-1" == B._meta.get("db_alias") | ||||||
|  |  | ||||||
|     def test_query_counter_alias(self): |  | ||||||
|         """query_counter works properly with db aliases?""" |  | ||||||
|         # Register a connection with db_alias testdb-1 |  | ||||||
|         register_connection("testdb-1", "mongoenginetest2") |  | ||||||
|  |  | ||||||
|         class A(Document): |  | ||||||
|             """Uses default db_alias |  | ||||||
|             """ |  | ||||||
|  |  | ||||||
|             name = StringField() |  | ||||||
|  |  | ||||||
|         class B(Document): |  | ||||||
|             """Uses testdb-1 db_alias |  | ||||||
|             """ |  | ||||||
|  |  | ||||||
|             name = StringField() |  | ||||||
|             meta = {"db_alias": "testdb-1"} |  | ||||||
|  |  | ||||||
|         with query_counter() as q: |  | ||||||
|             assert q == 0 |  | ||||||
|             a = A.objects.create(name="A") |  | ||||||
|             assert q == 1 |  | ||||||
|             a = A.objects.first() |  | ||||||
|             assert q == 2 |  | ||||||
|             a.name = "Test A" |  | ||||||
|             a.save() |  | ||||||
|             assert q == 3 |  | ||||||
|  |  | ||||||
|         with query_counter(alias="testdb-1") as q: |  | ||||||
|             assert q == 0 |  | ||||||
|             b = B.objects.create(name="B") |  | ||||||
|             assert q == 1 |  | ||||||
|             b = B.objects.first() |  | ||||||
|             assert q == 2 |  | ||||||
|             b.name = "Test B" |  | ||||||
|             b.save() |  | ||||||
|             assert q == 3 |  | ||||||
|  |  | ||||||
|     def test_db_ref_usage(self): |     def test_db_ref_usage(self): | ||||||
|         """DB Ref usage in dict_fields.""" |         """DB Ref usage in dict_fields.""" | ||||||
|  |  | ||||||
|   | |||||||
| @@ -282,6 +282,52 @@ class TestContextManagers: | |||||||
|             assert q < 1000 |             assert q < 1000 | ||||||
|             assert q <= int(q) |             assert q <= int(q) | ||||||
|  |  | ||||||
|  |     def test_query_counter_alias(self): | ||||||
|  |         """query_counter works properly with db aliases?""" | ||||||
|  |         # Register a connection with db_alias testdb-1 | ||||||
|  |         register_connection("testdb-1", "mongoenginetest2") | ||||||
|  |  | ||||||
|  |         class A(Document): | ||||||
|  |             """Uses default db_alias""" | ||||||
|  |  | ||||||
|  |             name = StringField() | ||||||
|  |  | ||||||
|  |         class B(Document): | ||||||
|  |             """Uses testdb-1 db_alias""" | ||||||
|  |  | ||||||
|  |             name = StringField() | ||||||
|  |             meta = {"db_alias": "testdb-1"} | ||||||
|  |  | ||||||
|  |         A.drop_collection() | ||||||
|  |         B.drop_collection() | ||||||
|  |  | ||||||
|  |         with query_counter() as q: | ||||||
|  |             assert q == 0 | ||||||
|  |             A.objects.create(name="A") | ||||||
|  |             assert q == 1 | ||||||
|  |             a = A.objects.first() | ||||||
|  |             assert q == 2 | ||||||
|  |             a.name = "Test A" | ||||||
|  |             a.save() | ||||||
|  |             assert q == 3 | ||||||
|  |             # querying the other db should'nt alter the counter | ||||||
|  |             B.objects().first() | ||||||
|  |             assert q == 3 | ||||||
|  |  | ||||||
|  |         with query_counter(alias="testdb-1") as q: | ||||||
|  |             assert q == 0 | ||||||
|  |             B.objects.create(name="B") | ||||||
|  |             assert q == 1 | ||||||
|  |             b = B.objects.first() | ||||||
|  |             assert q == 2 | ||||||
|  |             b.name = "Test B" | ||||||
|  |             b.save() | ||||||
|  |             assert b.name == "Test B" | ||||||
|  |             assert q == 3 | ||||||
|  |             # querying the other db should'nt alter the counter | ||||||
|  |             A.objects().first() | ||||||
|  |             assert q == 3 | ||||||
|  |  | ||||||
|     def test_query_counter_counts_getmore_queries(self): |     def test_query_counter_counts_getmore_queries(self): | ||||||
|         connect("mongoenginetest") |         connect("mongoenginetest") | ||||||
|         db = get_db() |         db = get_db() | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user