-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from magniff/feature/toggle
Feature/toggle
- Loading branch information
Showing
6 changed files
with
267 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import perf | ||
|
||
|
||
from watch.builtins import InstanceOf, Container, Mapping | ||
from watch import WatchMe | ||
|
||
|
||
class MyClass(WatchMe): | ||
value = Container(InstanceOf(str) >> InstanceOf(int)) | ||
|
||
|
||
class MyClassNoWatch: | ||
pass | ||
|
||
|
||
controlled_instance = MyClass() | ||
simple_instance = MyClassNoWatch() | ||
|
||
|
||
value = [{'a': 1, 'b': 2, 'c': 3, 'd': 4}] | ||
|
||
|
||
def bench_set(): | ||
controlled_instance.value = value | ||
|
||
|
||
def pivot_bench_set(): | ||
simple_instance.value = value | ||
|
||
|
||
runner = perf.Runner() | ||
|
||
|
||
# Check with validation enabled | ||
WatchMe.is_active = True | ||
runner.bench_func("set: validation enabled", bench_set) | ||
|
||
# Check with validation disabled | ||
WatchMe.is_active = False | ||
runner.bench_func("set: validation disabled", bench_set) | ||
|
||
# Check with a plain type with no watch at all | ||
runner.bench_func("set: no watch at all", pivot_bench_set) | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import py.test | ||
import watch | ||
|
||
|
||
def test_toggle_scenario_for_types(): | ||
|
||
class A(watch.WatchMe): | ||
foo = watch.builtins.InstanceOf(int) | ||
|
||
class B(watch.WatchMe): | ||
foo = watch.builtins.InstanceOf(int) | ||
|
||
a = A() | ||
b = B() | ||
|
||
# default scenario for a | ||
with py.test.raises(AttributeError): | ||
a.foo = "hello" | ||
|
||
# default scenario for b | ||
with py.test.raises(AttributeError): | ||
b.foo = "hello" | ||
|
||
A.is_active = False | ||
a.foo = "hello" | ||
assert a.foo == "hello" | ||
|
||
# disabling watch for A has nothing to do with B | ||
with py.test.raises(AttributeError): | ||
b.foo = "hello" | ||
|
||
# now watch is disabled globaly | ||
watch.WatchMe.is_active = False | ||
|
||
a.foo = "hello" | ||
assert a.foo == "hello" | ||
|
||
b.foo = "hello" | ||
assert b.foo == "hello" | ||
|
||
watch.WatchMe.is_active = True | ||
|
||
|
||
def test_toggle_scenario_for_instances(): | ||
|
||
class A(watch.WatchMe): | ||
foo = watch.builtins.InstanceOf(int) | ||
|
||
a = A() | ||
b = A() | ||
|
||
a.is_active = False | ||
a.foo = "hello" | ||
assert a.foo == "hello" | ||
|
||
# default scenario for b | ||
with py.test.raises(AttributeError): | ||
b.foo = "hello" | ||
|
||
watch.WatchMe.is_active = True | ||
|
Oops, something went wrong.