|
2 | 2 |
|
3 | 3 | from ravendb.documents.session.misc import SessionOptions
|
4 | 4 | from ravendb.documents.session.query import QueryStatistics
|
5 |
| -from ravendb.infrastructure.orders import Product |
| 5 | +from ravendb.infrastructure.orders import Product, Supplier |
6 | 6 | from ravendb.tests.test_base import TestBase
|
7 | 7 |
|
8 | 8 |
|
9 | 9 | class TestRavenDB11217(TestBase):
|
10 | 10 | def setUp(self):
|
11 | 11 | super(TestRavenDB11217, self).setUp()
|
12 | 12 |
|
13 |
| - def test_session_wide_no_caching_should_work(self): |
| 13 | + def test_session_wide_no_tracking_should_work(self): |
14 | 14 | with self.store.open_session() as session:
|
15 |
| - stats: Optional[QueryStatistics] = None |
| 15 | + supplier = Supplier() |
| 16 | + supplier.name = "Supplier1" |
16 | 17 |
|
17 |
| - def __stats_callback(statistics: QueryStatistics): |
18 |
| - nonlocal stats |
19 |
| - stats = statistics |
| 18 | + session.store(supplier) |
20 | 19 |
|
21 |
| - list(session.query(object_type=Product).statistics(__stats_callback).where_equals("name", "HR")) |
22 |
| - self.assertGreaterEqual(stats.duration_in_ms, 0) |
23 |
| - list(session.query(object_type=Product).statistics(__stats_callback).where_equals("name", "HR")) |
24 |
| - self.assertEqual(-1, stats.duration_in_ms) |
| 20 | + product = Product() |
| 21 | + product.name = "Product1" |
| 22 | + product.supplier = supplier.Id |
25 | 23 |
|
26 |
| - no_cache_options = SessionOptions() |
27 |
| - no_cache_options.no_caching = True |
| 24 | + session.store(product) |
| 25 | + session.save_changes() |
28 | 26 |
|
29 |
| - with self.store.open_session(session_options=no_cache_options) as session: |
30 |
| - list(session.query(object_type=Product).statistics(__stats_callback).where_equals("name", "HR")) |
31 |
| - self.assertGreaterEqual(stats.duration_in_ms, 0) |
32 |
| - list(session.query(object_type=Product).statistics(__stats_callback).where_equals("name", "HR")) |
33 |
| - self.assertGreaterEqual(stats.duration_in_ms, 0) |
| 27 | + no_tracking_options = SessionOptions() |
| 28 | + no_tracking_options.no_tracking = True |
| 29 | + |
| 30 | + with self.store.open_session(session_options=no_tracking_options) as session: |
| 31 | + supplier = Supplier() |
| 32 | + supplier.name = "Supplier2" |
| 33 | + |
| 34 | + with self.assertRaises(RuntimeError): |
| 35 | + session.store(supplier) |
| 36 | + |
| 37 | + with self.store.open_session(session_options=no_tracking_options) as session: |
| 38 | + self.assertEqual(0, session.advanced.number_of_requests) |
| 39 | + |
| 40 | + product1 = session.load("products/1-A", Product, lambda b: b.include_documents("supplier")) |
| 41 | + |
| 42 | + self.assertEqual(1, session.advanced.number_of_requests) |
| 43 | + |
| 44 | + self.assertIsNotNone(product1) |
| 45 | + self.assertEqual("Product1", product1.name) |
| 46 | + self.assertFalse(session.advanced.is_loaded(product1.Id)) |
| 47 | + self.assertFalse(session.advanced.is_loaded(product1.supplier)) |
| 48 | + |
| 49 | + supplier = session.load(product1.supplier, Supplier) |
| 50 | + self.assertIsNotNone(supplier) |
| 51 | + self.assertEqual("Supplier1", supplier.name) |
| 52 | + self.assertEqual(2, session.advanced.number_of_requests) |
| 53 | + self.assertFalse(session.advanced.is_loaded(supplier.Id)) |
| 54 | + |
| 55 | + product2 = session.load("products/1-A", Product, lambda b: b.include_documents(("supplier"))) |
| 56 | + self.assertNotEqual(product2, product1) |
| 57 | + |
| 58 | + with self.store.open_session(session_options=no_tracking_options) as session: |
| 59 | + self.assertEqual(0, session.advanced.number_of_requests) |
| 60 | + |
| 61 | + product1 = session.advanced.load_starting_with("products/", Product)[0] |
| 62 | + |
| 63 | + self.assertEqual(1, session.advanced.number_of_requests) |
| 64 | + |
| 65 | + self.assertIsNotNone(product1) |
| 66 | + self.assertEqual("Product1", product1.name) |
| 67 | + self.assertFalse(session.advanced.is_loaded(product1.Id)) |
| 68 | + self.assertFalse(session.advanced.is_loaded(product1.supplier)) |
| 69 | + |
| 70 | + supplier = session.advanced.load_starting_with(product1.supplier, Supplier)[0] |
| 71 | + |
| 72 | + self.assertIsNotNone(supplier) |
| 73 | + self.assertEqual("Supplier1", supplier.name) |
| 74 | + self.assertEqual(2, session.advanced.number_of_requests) |
| 75 | + self.assertFalse(session.advanced.is_loaded(supplier.Id)) |
| 76 | + |
| 77 | + product2 = session.advanced.load_starting_with("products/", Product)[0] |
| 78 | + self.assertNotEqual(product2, product1) |
| 79 | + |
| 80 | + with self.store.open_session(session_options=no_tracking_options) as session: |
| 81 | + self.assertEqual(0, session.advanced.number_of_requests) |
| 82 | + products = list(session.query(object_type=Product).include("supplier")) |
| 83 | + |
| 84 | + self.assertEqual(1, session.advanced.number_of_requests) |
| 85 | + self.assertEqual(1, len(products)) |
| 86 | + |
| 87 | + product1 = products[0] |
| 88 | + self.assertIsNotNone(product1) |
| 89 | + self.assertFalse(session.advanced.is_loaded(product1.Id)) |
| 90 | + self.assertFalse(session.advanced.is_loaded(product1.supplier)) |
| 91 | + |
| 92 | + supplier = session.load(product1.supplier, Supplier) |
| 93 | + self.assertIsNotNone(supplier) |
| 94 | + self.assertEqual("Supplier1", supplier.name) |
| 95 | + self.assertEqual(2, session.advanced.number_of_requests) |
| 96 | + self.assertFalse(session.advanced.is_loaded(supplier.Id)) |
| 97 | + |
| 98 | + products = list(session.query(object_type=Product).include("supplier")) |
| 99 | + self.assertEqual(1, len(products)) |
| 100 | + |
| 101 | + product2 = products[0] |
| 102 | + self.assertNotEqual(product2, product1) |
| 103 | + |
| 104 | + with self.store.open_session() as session: |
| 105 | + session.counters_for("products/1-A").increment("c1") |
| 106 | + session.save_changes() |
| 107 | + |
| 108 | + with self.store.open_session(session_options=no_tracking_options) as session: |
| 109 | + product1 = session.load("products/1-A", Product) |
| 110 | + counters = session.counters_for(product1.Id) |
| 111 | + |
| 112 | + counters.get("c1") |
| 113 | + |
| 114 | + self.assertEqual(2, session.advanced.number_of_requests) |
| 115 | + |
| 116 | + counters.get("c1") |
| 117 | + |
| 118 | + self.assertEqual(3, session.advanced.number_of_requests) |
| 119 | + |
| 120 | + val1 = counters.get_all() |
| 121 | + |
| 122 | + self.assertEqual(4, session.advanced.number_of_requests) |
| 123 | + |
| 124 | + val2 = counters.get_all() |
| 125 | + |
| 126 | + self.assertEqual(5, session.advanced.number_of_requests) |
| 127 | + |
| 128 | + self.assertNotEqual(id(val2), id(val1)) |
| 129 | + |
| 130 | + |
| 131 | +def test_session_wide_no_caching_should_work(self): |
| 132 | + with self.store.open_session() as session: |
| 133 | + stats: Optional[QueryStatistics] = None |
| 134 | + |
| 135 | + def __stats_callback(statistics: QueryStatistics): |
| 136 | + nonlocal stats |
| 137 | + stats = statistics |
| 138 | + |
| 139 | + list(session.query(object_type=Product).statistics(__stats_callback).where_equals("name", "HR")) |
| 140 | + self.assertGreaterEqual(stats.duration_in_ms, 0) |
| 141 | + list(session.query(object_type=Product).statistics(__stats_callback).where_equals("name", "HR")) |
| 142 | + self.assertEqual(-1, stats.duration_in_ms) |
| 143 | + |
| 144 | + no_cache_options = SessionOptions() |
| 145 | + no_cache_options.no_caching = True |
| 146 | + |
| 147 | + with self.store.open_session(session_options=no_cache_options) as session: |
| 148 | + list(session.query(object_type=Product).statistics(__stats_callback).where_equals("name", "HR")) |
| 149 | + self.assertGreaterEqual(stats.duration_in_ms, 0) |
| 150 | + list(session.query(object_type=Product).statistics(__stats_callback).where_equals("name", "HR")) |
| 151 | + self.assertGreaterEqual(stats.duration_in_ms, 0) |
0 commit comments