From 0e65d76594d5b5507f5fe83af6a251e54bcf75a8 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 8 May 2023 17:29:48 -0400 Subject: [PATCH] mock id for _block.store testing --- asdf/_tests/_block/test_key.py | 16 +++----------- asdf/_tests/_block/test_store.py | 38 ++++++++++++++++++-------------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/asdf/_tests/_block/test_key.py b/asdf/_tests/_block/test_key.py index 2c9934252..a284816c6 100644 --- a/asdf/_tests/_block/test_key.py +++ b/asdf/_tests/_block/test_key.py @@ -37,20 +37,10 @@ def test_is_valid(): assert not bk.is_valid() -def test_memory_reuse(): +def test_same_class(): f = Foo() bk = Key(f) - fid = id(f) del f - objs = [] - for _ in range(100): - f = Foo() - objs.append(f) - if fid == id(f): - break - else: - raise AssertionError("Failed to find reused memory address") - - assert fid == id(f) + f2 = Foo() assert not bk.is_valid() - assert not bk.matches(f) + assert not bk.matches(f2) diff --git a/asdf/_tests/_block/test_store.py b/asdf/_tests/_block/test_store.py index 29a6f83d5..05ede0faf 100644 --- a/asdf/_tests/_block/test_store.py +++ b/asdf/_tests/_block/test_store.py @@ -1,3 +1,5 @@ +from unittest.mock import patch + import pytest from asdf._block.key import Key @@ -99,13 +101,15 @@ def test_get_memory_reused(): s.assign_object(f, v) fid = id(f) del f - for _ in range(1000): - f = Foo() - if id(f) == fid: - break - else: - raise AssertionError("Failed to trigger memory reuse") - assert s.lookup_by_object(f) is None + f2 = Foo() + + def mock_id(obj): + if obj is f2: + return fid + return id(obj) + + with patch("asdf._block.store.id", mock_id): + assert s.lookup_by_object(f2) is None def test_set_memory_reused(): @@ -115,15 +119,17 @@ def test_set_memory_reused(): s.assign_object(f, v) fid = id(f) del f - for _ in range(1000): - f = Foo() - if id(f) == fid: - break - else: - raise AssertionError("Failed to trigger memory reuse") - nv = 26 - s.assign_object(f, nv) - assert s.lookup_by_object(f) is nv + f2 = Foo() + + def mock_id(obj): + if obj is f2: + return fid + return id(obj) + + with patch("asdf._block.store.id", mock_id): + nv = 26 + s.assign_object(f2, nv) + assert s.lookup_by_object(f2) is nv def test_cleanup():