~therve/storm/twisted-integration

« back to all changes in this revision

Viewing changes to tests/store/base.py

Merged set-with-func [r=niemeyer,therve] [f=328603].

Added support for using Expressions with ResultSet.set(). Also fixed a
bug in reseting columns to AutoReload when a cached expression causes
a CompilerError.

Show diffs side-by-side

added added

removed removed

Lines of Context:
30
30
from storm.properties import PropertyPublisherMeta, Decimal
31
31
from storm.variables import PickleVariable
32
32
from storm.expr import (
33
 
    Asc, Desc, Select, Func, LeftJoin, SQL, Count, Sum, Avg, And, Or, Eq)
 
33
    Asc, Desc, Select, Func, LeftJoin, SQL, Count, Sum, Avg, And, Or, Eq,
 
34
    Lower)
34
35
from storm.variables import Variable, UnicodeVariable, IntVariable
35
36
from storm.info import get_obj_info, ClassAlias
36
37
from storm.exceptions import *
2320
2321
        foo = self.store.get(Foo, 20)
2321
2322
        self.assertEquals(foo.title, "Title 40")
2322
2323
 
 
2324
    def test_find_set_with_func_expr(self):
 
2325
        self.store.find(Foo, title=u"Title 20").set(title=Lower(u"Title 40"))
 
2326
        foo = self.store.get(Foo, 20)
 
2327
        self.assertEquals(foo.title, "title 40")
 
2328
 
 
2329
    def test_find_set_equality_with_func_expr(self):
 
2330
        self.store.find(Foo, title=u"Title 20").set(
 
2331
            Foo.title == Lower(u"Title 40"))
 
2332
        foo = self.store.get(Foo, 20)
 
2333
        self.assertEquals(foo.title, "title 40")
 
2334
 
2323
2335
    def test_find_set_column(self):
2324
2336
        self.store.find(Bar, title=u"Title 200").set(foo_id=Bar.id)
2325
2337
        bar = self.store.get(Bar, 200)
2370
2382
    def test_find_set_on_cached_unsupported_python_expr(self):
2371
2383
        foo1 = self.store.get(Foo, 20)
2372
2384
        foo2 = self.store.get(Foo, 30)
2373
 
        self.store.find(Foo, Foo.id == Select(SQL("20"))).set(title=u"Title 40")
 
2385
        self.store.find(
 
2386
            Foo, Foo.id == Select(SQL("20"))).set(title=u"Title 40")
2374
2387
        self.assertEquals(foo1.title, "Title 40")
2375
2388
        self.assertEquals(foo2.title, "Title 10")
2376
2389
 
2378
2391
        result = self.store.find(Foo, title=u"Title 20")
2379
2392
        self.assertRaises(FeatureError, result.set, Foo.title > u"Title 40")
2380
2393
 
2381
 
    def test_find_set_expr_unsupported_2(self):
2382
 
        result = self.store.find(Foo, title=u"Title 20")
2383
 
        self.assertRaises(FeatureError, result.set, Foo.title == Func("func"))
 
2394
    def test_find_set_expr_unsupported_without_column(self):
 
2395
        result = self.store.find(Foo, title=u"Title 20")
 
2396
        self.assertRaises(FeatureError, result.set,
 
2397
                          Eq(object(), IntVariable(1)))
 
2398
 
 
2399
    def test_find_set_expr_unsupported_without_expr_or_variable(self):
 
2400
        result = self.store.find(Foo, title=u"Title 20")
 
2401
        self.assertRaises(FeatureError, result.set, Eq(Foo.id, object()))
2384
2402
 
2385
2403
    def test_find_set_expr_unsupported_autoreloads(self):
2386
2404
        bar1 = self.store.get(Bar, 200)
2395
2413
        self.assertEquals(bar1.title, "Title 400")
2396
2414
        self.assertEquals(bar2.title, "Title 100")
2397
2415
 
 
2416
    def test_find_set_expr_unsupported_mixed_autoreloads(self):
 
2417
        # For an expression that does not compile (eg:
 
2418
        # ResultSet.cached() raises a CompileError), while setting
 
2419
        # cached entries' columns to AutoReload, if objects of
 
2420
        # different types could be found in the cache then a KeyError
 
2421
        # would happen if some object did not have a matching
 
2422
        # column. See Bug #328603 for more info.
 
2423
        foo1 = self.store.get(Foo, 20)
 
2424
        bar1 = self.store.get(Bar, 200)
 
2425
        self.store.find(Bar, id=Select(SQL("200"))).set(title=u"Title 400")
 
2426
        foo1_vars = get_obj_info(foo1).variables
 
2427
        bar1_vars = get_obj_info(bar1).variables
 
2428
        self.assertNotEquals(foo1_vars[Foo.title].get_lazy(), AutoReload)
 
2429
        self.assertEquals(bar1_vars[Bar.title].get_lazy(), AutoReload)
 
2430
        self.assertEquals(bar1_vars[Bar.foo_id].get_lazy(), None)
 
2431
        self.assertEquals(foo1.title, "Title 20")
 
2432
        self.assertEquals(bar1.title, "Title 400")
 
2433
 
 
2434
    def test_find_set_autoreloads_with_func_expr(self):
 
2435
        # In the process of fixing this bug, we've temporarily
 
2436
        # introduced another bug: the expression would be called
 
2437
        # twice. We've used an expression that increments the value by
 
2438
        # one here to see if that case is triggered. In the buggy
 
2439
        # bugfix, the value would end up being incremented by two due
 
2440
        # to misfiring two updates.
 
2441
        foo1 = self.store.get(FooValue, 1)
 
2442
        self.assertEquals(foo1.value1, 2)
 
2443
        self.store.find(FooValue, id=1).set(value1=SQL("value1 + 1"))
 
2444
        foo1_vars = get_obj_info(foo1).variables
 
2445
        self.assertEquals(foo1_vars[FooValue.value1].get_lazy(), AutoReload)
 
2446
        self.assertEquals(foo1.value1, 3)
 
2447
 
 
2448
    def test_find_set_equality_autoreloads_with_func_expr(self):
 
2449
        foo1 = self.store.get(FooValue, 1)
 
2450
        self.assertEquals(foo1.value1, 2)
 
2451
        self.store.find(FooValue, id=1).set(
 
2452
            FooValue.value1 == SQL("value1 + 1"))
 
2453
        foo1_vars = get_obj_info(foo1).variables
 
2454
        self.assertEquals(foo1_vars[FooValue.value1].get_lazy(), AutoReload)
 
2455
        self.assertEquals(foo1.value1, 3)
 
2456
 
2398
2457
    def test_wb_find_set_checkpoints(self):
2399
2458
        bar = self.store.get(Bar, 200)
2400
2459
        self.store.find(Bar, id=200).set(title=u"Title 400")