Skip to content

Commit

Permalink
More fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
Shyue Ping Ong committed Sep 5, 2023
1 parent 7fbaca3 commit 0754f93
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 163 deletions.
2 changes: 1 addition & 1 deletion tests/test_design_patterns.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_cached_class(self):
a2 = A(2)

assert id(a1a) == id(a1b)
self.assertNotEqual(id(a1a), id(a2))
assert id(a1a) != id(a2)

# def test_pickle(self):
# a = A(2)
Expand Down
Binary file modified tests/test_files/3000_lines.txt.gz
Binary file not shown.
152 changes: 76 additions & 76 deletions tests/test_functools.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ def foo(self):
return 1

f = Foo()
self.assertEqual(f.foo, 1)
self.assertEqual(len(called), 1)
assert f.foo == 1
assert len(called) == 1

def test_evaluate_once(self):
# lazy_property attributes should be evaluated only once.
Expand All @@ -111,10 +111,10 @@ def foo(self):
return 1

f = Foo()
self.assertEqual(f.foo, 1)
self.assertEqual(f.foo, 1)
self.assertEqual(f.foo, 1)
self.assertEqual(len(called), 1)
assert f.foo == 1
assert f.foo == 1
assert f.foo == 1
assert len(called) == 1

def test_private_attribute(self):
# It should be possible to create private, name-mangled
Expand All @@ -131,10 +131,10 @@ def get_foo(self):
return self.__foo

f = Foo()
self.assertEqual(f.get_foo(), 1)
self.assertEqual(f.get_foo(), 1)
self.assertEqual(f.get_foo(), 1)
self.assertEqual(len(called), 1)
assert f.get_foo() == 1
assert f.get_foo() == 1
assert f.get_foo() == 1
assert len(called) == 1

def test_reserved_attribute(self):
# It should be possible to create reserved lazy_property attributes.
Expand All @@ -147,10 +147,10 @@ def __foo__(self):
return 1

f = Foo()
self.assertEqual(f.__foo__, 1)
self.assertEqual(f.__foo__, 1)
self.assertEqual(f.__foo__, 1)
self.assertEqual(len(called), 1)
assert f.__foo__ == 1
assert f.__foo__ == 1
assert f.__foo__ == 1
assert len(called) == 1

def test_result_shadows_descriptor(self):
# The result of the function call should be stored in
Expand All @@ -167,20 +167,20 @@ def foo(self):
assert isinstance(Foo.foo, lazy_property)
assert f.foo is f.foo
assert f.foo is f.__dict__["foo"] # !
self.assertEqual(len(called), 1)
assert len(called) == 1

self.assertEqual(f.foo, 1)
self.assertEqual(f.foo, 1)
self.assertEqual(len(called), 1)
assert f.foo == 1
assert f.foo == 1
assert len(called) == 1

lazy_property.invalidate(f, "foo")

self.assertEqual(f.foo, 1)
self.assertEqual(len(called), 2)
assert f.foo == 1
assert len(called) == 2

self.assertEqual(f.foo, 1)
self.assertEqual(f.foo, 1)
self.assertEqual(len(called), 2)
assert f.foo == 1
assert f.foo == 1
assert len(called) == 2

def test_readonly_object(self):
# The descriptor should raise an AttributeError when lazy_property is
Expand All @@ -196,7 +196,7 @@ def foo(self):
return 1

f = Foo()
self.assertEqual(len(called), 0)
assert len(called) == 0

self.assertException(
AttributeError,
Expand All @@ -207,7 +207,7 @@ def foo(self):
)

# The function was not called
self.assertEqual(len(called), 0)
assert len(called) == 0

def test_introspection(self):
# The lazy_property decorator should support basic introspection.
Expand All @@ -220,12 +220,12 @@ def foo(self):
def bar(self):
"""bar func doc"""

self.assertEqual(Foo.foo.__name__, "foo")
self.assertEqual(Foo.foo.__doc__, "foo func doc")
assert Foo.foo.__name__ == "foo"
assert Foo.foo.__doc__ == "foo func doc"
assert "test_functools" in Foo.foo.__module__

self.assertEqual(Foo.bar.__name__, "bar")
self.assertEqual(Foo.bar.__doc__, "bar func doc")
assert Foo.bar.__name__ == "bar"
assert Foo.bar.__doc__ == "bar func doc"
assert "test_functools" in Foo.bar.__module__


Expand All @@ -241,13 +241,13 @@ def foo(self):
return 1

f = Foo()
self.assertEqual(f.foo, 1)
self.assertEqual(len(called), 1)
assert f.foo == 1
assert len(called) == 1

lazy_property.invalidate(f, "foo")

self.assertEqual(f.foo, 1)
self.assertEqual(len(called), 2)
assert f.foo == 1
assert len(called) == 2

def test_invalidate_attribute_twice(self):
# It should be possible to invalidate a lazy_property attribute
Expand All @@ -261,14 +261,14 @@ def foo(self):
return 1

f = Foo()
self.assertEqual(f.foo, 1)
self.assertEqual(len(called), 1)
assert f.foo == 1
assert len(called) == 1

lazy_property.invalidate(f, "foo")
lazy_property.invalidate(f, "foo") # Nothing happens

self.assertEqual(f.foo, 1)
self.assertEqual(len(called), 2)
assert f.foo == 1
assert len(called) == 2

def test_invalidate_uncalled_attribute(self):
# It should be possible to invalidate an empty attribute
Expand All @@ -282,7 +282,7 @@ def foo(self):
return 1

f = Foo()
self.assertEqual(len(called), 0)
assert len(called) == 0
lazy_property.invalidate(f, "foo") # Nothing happens

def test_invalidate_private_attribute(self):
Expand All @@ -299,13 +299,13 @@ def get_foo(self):
return self.__foo

f = Foo()
self.assertEqual(f.get_foo(), 1)
self.assertEqual(len(called), 1)
assert f.get_foo() == 1
assert len(called) == 1

lazy_property.invalidate(f, "__foo")

self.assertEqual(f.get_foo(), 1)
self.assertEqual(len(called), 2)
assert f.get_foo() == 1
assert len(called) == 2

def test_invalidate_mangled_attribute(self):
# It should be possible to invalidate a private lazy_property attribute
Expand All @@ -322,13 +322,13 @@ def get_foo(self):
return self.__foo

f = Foo()
self.assertEqual(f.get_foo(), 1)
self.assertEqual(len(called), 1)
assert f.get_foo() == 1
assert len(called) == 1

lazy_property.invalidate(f, "_Foo__foo")

self.assertEqual(f.get_foo(), 1)
self.assertEqual(len(called), 2)
assert f.get_foo() == 1
assert len(called) == 2

def test_invalidate_reserved_attribute(self):
# It should be possible to invalidate a reserved lazy_property attribute.
Expand All @@ -341,13 +341,13 @@ def __foo__(self):
return 1

f = Foo()
self.assertEqual(f.__foo__, 1)
self.assertEqual(len(called), 1)
assert f.__foo__ == 1
assert len(called) == 1

lazy_property.invalidate(f, "__foo__")

self.assertEqual(f.__foo__, 1)
self.assertEqual(len(called), 2)
assert f.__foo__ == 1
assert len(called) == 2

def test_invalidate_nonlazy_attribute(self):
# Invalidating an attribute that is not lazy_property should
Expand Down Expand Up @@ -447,13 +447,13 @@ def bar(self):
return 1

b = Bar()
self.assertEqual(b.bar, 1)
self.assertEqual(len(called), 1)
assert b.bar == 1
assert len(called) == 1

cached.invalidate(b, "bar")

self.assertEqual(b.bar, 1)
self.assertEqual(len(called), 2)
assert b.bar == 1
assert len(called) == 2

def test_invalidate_attribute_twice(self):
# It should be possible to invalidate a cached attribute
Expand All @@ -467,14 +467,14 @@ def bar(self):
return 1

b = Bar()
self.assertEqual(b.bar, 1)
self.assertEqual(len(called), 1)
assert b.bar == 1
assert len(called) == 1

cached.invalidate(b, "bar")
cached.invalidate(b, "bar") # Nothing happens

self.assertEqual(b.bar, 1)
self.assertEqual(len(called), 2)
assert b.bar == 1
assert len(called) == 2

def test_invalidate_uncalled_attribute(self):
# It should be possible to invalidate an empty attribute
Expand All @@ -488,7 +488,7 @@ def bar(self):
return 1

b = Bar()
self.assertEqual(len(called), 0)
assert len(called) == 0
cached.invalidate(b, "bar") # Nothing happens

def test_invalidate_private_attribute(self):
Expand All @@ -505,13 +505,13 @@ def get_bar(self):
return self.__bar

b = Bar()
self.assertEqual(b.get_bar(), 1)
self.assertEqual(len(called), 1)
assert b.get_bar() == 1
assert len(called) == 1

cached.invalidate(b, "__bar")

self.assertEqual(b.get_bar(), 1)
self.assertEqual(len(called), 2)
assert b.get_bar() == 1
assert len(called) == 2

def test_invalidate_mangled_attribute(self):
# It should be possible to invalidate a private cached attribute
Expand All @@ -528,13 +528,13 @@ def get_bar(self):
return self.__bar

b = Bar()
self.assertEqual(b.get_bar(), 1)
self.assertEqual(len(called), 1)
assert b.get_bar() == 1
assert len(called) == 1

cached.invalidate(b, "_Bar__bar")

self.assertEqual(b.get_bar(), 1)
self.assertEqual(len(called), 2)
assert b.get_bar() == 1
assert len(called) == 2

def test_invalidate_reserved_attribute(self):
# It should be possible to invalidate a reserved cached attribute.
Expand All @@ -547,13 +547,13 @@ def __bar__(self):
return 1

b = Bar()
self.assertEqual(b.__bar__, 1)
self.assertEqual(len(called), 1)
assert b.__bar__ == 1
assert len(called) == 1

cached.invalidate(b, "__bar__")

self.assertEqual(b.__bar__, 1)
self.assertEqual(len(called), 2)
assert b.__bar__ == 1
assert len(called) == 2

def test_invalidate_uncached_attribute(self):
# Invalidating an attribute that is not cached should
Expand Down Expand Up @@ -665,13 +665,13 @@ def bar(self):
return 1

b = Bar()
self.assertEqual(b.bar, 1)
self.assertEqual(len(called), 1)
assert b.bar == 1
assert len(called) == 1

lazy_property.invalidate(b, "bar")

self.assertEqual(b.bar, 1)
self.assertEqual(len(called), 2)
assert b.bar == 1
assert len(called) == 2


class AssertExceptionTests(TestCase):
Expand Down Expand Up @@ -759,7 +759,7 @@ def test_with(self):
time.sleep(2)
assert False, "Did not timeout!"
except TimeoutError as ex:
self.assertEqual(ex.message, "timeout!")
assert ex.message == "timeout!"


class ProfMainTest(unittest.TestCase):
Expand Down
Loading

0 comments on commit 0754f93

Please sign in to comment.