Skip to content

Commit ff5cf00

Browse files
Merge pull request #66 from bridadan/fix_remove_all_mocks
Fix remove all mocks call
2 parents fa4f337 + c4c02de commit ff5cf00

File tree

4 files changed

+73
-12
lines changed

4 files changed

+73
-12
lines changed

packages/mbed-ls/test/mbedls_toolsbase.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class BasicTestCase(unittest.TestCase):
4343
"""
4444

4545
def setUp(self):
46-
self.base = DummyLsTools()
46+
self.base = DummyLsTools(force_mock=True)
4747

4848
def tearDown(self):
4949
pass

src/mbed_os_tools/detect/platform_database.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -508,13 +508,16 @@ def remove(self, id, permanent=False, device_type="daplink", verbose_data=False)
508508
logger.debug("Trying remove of %s", id)
509509
if id is "*" and device_type in self._dbs[self._prim_db]:
510510
self._dbs[self._prim_db][device_type] = {}
511-
for db in self._dbs.values():
512-
if device_type in db and id in db[device_type]:
513-
logger.debug("Removing id...")
514-
removed = db[device_type][id]
515-
del db[device_type][id]
516-
self._keys[device_type].remove(id)
517-
if permanent:
518-
self._update_db()
511+
if permanent:
512+
self._update_db()
513+
else:
514+
for db in self._dbs.values():
515+
if device_type in db and id in db[device_type]:
516+
logger.debug("Removing id...")
517+
removed = db[device_type][id]
518+
del db[device_type][id]
519+
self._keys[device_type].remove(id)
520+
if permanent:
521+
self._update_db()
519522

520-
return _modify_data_format(removed, verbose_data)
523+
return _modify_data_format(removed, verbose_data)

test/detect/mbedls_toolsbase.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class BasicTestCase(unittest.TestCase):
4141
"""
4242

4343
def setUp(self):
44-
self.base = DummyLsTools()
44+
self.base = DummyLsTools(force_mock=True)
4545

4646
def tearDown(self):
4747
pass
@@ -264,6 +264,7 @@ def test_mock_manufacture_ids_star(self):
264264
self.assertEqual(None, self.base.plat_db.get("0342"))
265265
self.assertEqual(None, self.base.plat_db.get("0343"))
266266

267+
267268
def test_update_device_from_fs_mid_unmount(self):
268269
dummy_mount = 'dummy_mount'
269270
device = {

test/detect/platform_database.py

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import logging
2020
import tempfile
2121
import json
22+
import shutil
2223
from mock import patch, MagicMock, DEFAULT
2324
from io import StringIO
2425

@@ -35,14 +36,16 @@ class EmptyPlatformDatabaseTests(unittest.TestCase):
3536
"""
3637

3738
def setUp(self):
38-
self.base_db_path = os.path.join(tempfile.mkdtemp(), 'base')
39+
self.tempd_dir = tempfile.mkdtemp()
40+
self.base_db_path = os.path.join(self.tempd_dir, 'base')
3941
self.base_db = open(self.base_db_path, 'w+b')
4042
self.base_db.write(b'{}')
4143
self.base_db.seek(0)
4244
self.pdb = PlatformDatabase([self.base_db_path])
4345

4446
def tearDown(self):
4547
self.base_db.close()
48+
shutil.rmtree(self.tempd_dir)
4649

4750
def test_broken_database_io(self):
4851
"""Verify that the platform database still works without a
@@ -123,6 +126,60 @@ def test_remove(self):
123126
self.assertEqual(self.pdb.remove('4753', permanent=False), 'Test_Platform')
124127
self.assertEqual(self.pdb.get('4753', None), None)
125128

129+
def test_remove_all(self):
130+
"""Test that multiple entries can be removed at once
131+
"""
132+
self.assertEqual(self.pdb.get('4753', None), None)
133+
self.assertEqual(self.pdb.get('4754', None), None)
134+
self.pdb.add('4753', 'Test_Platform1', permanent=False)
135+
self.pdb.add('4754', 'Test_Platform2', permanent=False)
136+
self.assertEqual(self.pdb.get('4753', None), 'Test_Platform1')
137+
self.assertEqual(self.pdb.get('4754', None), 'Test_Platform2')
138+
self.pdb.remove('*', permanent=False)
139+
self.assertEqual(self.pdb.get('4753', None), None)
140+
self.assertEqual(self.pdb.get('4754', None), None)
141+
142+
def test_remove_permanent(self):
143+
"""Test that once something is removed permanently it no longer shows up
144+
when queried
145+
"""
146+
self.assertEqual(self.pdb.get('4753', None), None)
147+
self.pdb.add('4753', 'Test_Platform', permanent=True)
148+
self.assertEqual(self.pdb.get('4753', None), 'Test_Platform')
149+
150+
# Recreate platform database to simulate rerunning mbedls
151+
self.pdb = PlatformDatabase([self.base_db_path])
152+
self.assertEqual(self.pdb.get('4753', None), 'Test_Platform')
153+
self.assertEqual(self.pdb.remove('4753', permanent=True), 'Test_Platform')
154+
self.assertEqual(self.pdb.get('4753', None), None)
155+
156+
# Recreate platform database to simulate rerunning mbedls
157+
self.pdb = PlatformDatabase([self.base_db_path])
158+
self.assertEqual(self.pdb.get('4753', None), None)
159+
160+
def test_remove_all_permanent(self):
161+
"""Test that multiple entries can be removed permanently at once
162+
"""
163+
self.assertEqual(self.pdb.get('4753', None), None)
164+
self.assertEqual(self.pdb.get('4754', None), None)
165+
self.pdb.add('4753', 'Test_Platform1', permanent=True)
166+
self.pdb.add('4754', 'Test_Platform2', permanent=True)
167+
self.assertEqual(self.pdb.get('4753', None), 'Test_Platform1')
168+
self.assertEqual(self.pdb.get('4754', None), 'Test_Platform2')
169+
170+
# Recreate platform database to simulate rerunning mbedls
171+
self.pdb = PlatformDatabase([self.base_db_path])
172+
self.assertEqual(self.pdb.get('4753', None), 'Test_Platform1')
173+
self.assertEqual(self.pdb.get('4754', None), 'Test_Platform2')
174+
self.pdb.remove('*', permanent=True)
175+
self.assertEqual(self.pdb.get('4753', None), None)
176+
self.assertEqual(self.pdb.get('4754', None), None)
177+
178+
# Recreate platform database to simulate rerunning mbedls
179+
self.pdb = PlatformDatabase([self.base_db_path])
180+
self.assertEqual(self.pdb.get('4753', None), None)
181+
self.assertEqual(self.pdb.get('4754', None), None)
182+
126183
def test_bogus_add(self):
127184
"""Test that add requires properly formatted platform ids
128185
"""

0 commit comments

Comments
 (0)