Skip to content

Commit

Permalink
refactor and upgrade tests
Browse files Browse the repository at this point in the history
Some prepwork for py3. Tossing these together:
- assertItemsEqual doesn't do what it says on the box. It only said "hey, these lists have the same number of items". Not content or anything. Changing that to SetEqual, which is much closer to what is wanted- ensuring that lists have the same content without caring about their order.
- use assertRaises instead of try/catch
- update some mocks, especially mock_open.
- include the refactored test_get_recovery_data sent to the maintenance branch in OctoPrint#2806.
  • Loading branch information
tedder committed Sep 2, 2018
1 parent 44fcd4e commit a5f8759
Showing 1 changed file with 23 additions and 34 deletions.
57 changes: 23 additions & 34 deletions tests/filemanager/test_filemanager.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,18 @@ def test_full_extension_tree(self):
self.assertTrue("machinecode" in full)
self.assertTrue("gcode" in full["machinecode"])
self.assertTrue(isinstance(full["machinecode"]["gcode"], octoprint.filemanager.ContentTypeMapping))
self.assertItemsEqual(["gcode", "gco", "g"], full["machinecode"]["gcode"].extensions)
self.assertSetEqual(set(["gcode", "gco", "g"]), set(full["machinecode"]["gcode"].extensions))
self.assertTrue("foo" in full["machinecode"])
self.assertTrue(isinstance(full["machinecode"]["foo"], list))
self.assertItemsEqual(["f", "foo"], full["machinecode"]["foo"])
self.assertSetEqual(set(["f", "foo"]), set(full["machinecode"]["foo"]))

self.assertTrue("model" in full)
self.assertTrue("stl" in full["model"])
self.assertTrue(isinstance(full["model"]["stl"], octoprint.filemanager.ContentTypeMapping))
self.assertItemsEqual(["stl"], full["model"]["stl"].extensions)
self.assertSetEqual(set(["stl"]), set(full["model"]["stl"].extensions))
self.assertTrue("amf" in full["model"])
self.assertTrue(isinstance(full["model"]["amf"], list))
self.assertItemsEqual(["amf"], full["model"]["amf"])
self.assertSetEqual(set(["amf"]), set(full["model"]["amf"]))

def test_get_mimetype(self):
self.assertEqual(octoprint.filemanager.get_mime_type("foo.stl"), "application/sla")
Expand Down Expand Up @@ -235,22 +235,18 @@ def test_add_folder(self):
def test_add_folder_not_ignoring_existing(self):
self.local_storage.add_folder.side_effect = RuntimeError("already there")

try:
with self.assertRaises(RuntimeError, msg="already there"):
self.file_manager.add_folder(octoprint.filemanager.FileDestinations.LOCAL, "test_folder", ignore_existing=False)
self.fail("Expected an exception to occur!")
except RuntimeError as e:
self.assertEqual("already there", e.message)
self.local_storage.add_folder.assert_called_once_with("test_folder", ignore_existing=False, display=None)

def test_add_folder_display(self):
self.local_storage.add_folder.side_effect = RuntimeError("already there")

try:
with self.assertRaises(RuntimeError, msg="already there"):
self.file_manager.add_folder(octoprint.filemanager.FileDestinations.LOCAL, "test_folder",
display=u"täst_folder")
self.fail("Expected an exception to occur!")
except RuntimeError as e:
self.assertEqual("already there", e.message)
self.local_storage.add_folder.assert_called_once_with("test_folder",
ignore_existing=True,
display=u"täst_folder")
Expand Down Expand Up @@ -287,41 +283,37 @@ def test_save_recovery_data(self, mock_time, mock_yaml_safe_dump, mock_atomic_wr
pos = 1234
recovery_file = os.path.join("/path/to/a/base_folder", "print_recovery_data.yaml")

mock_atomic_write.return_value = mock.MagicMock(spec=file)
mock_atomic_write_handle = mock_atomic_write.return_value.__enter__.return_value
mock_time.return_value = now
self.local_storage.path_in_storage.return_value = path

self.file_manager.save_recovery_data(octoprint.filemanager.FileDestinations.LOCAL, path, pos)
with mock.patch("builtins.open", mock.mock_open(), create=True) as m:
self.file_manager.save_recovery_data(octoprint.filemanager.FileDestinations.LOCAL, path, pos)
mock_atomic_write.assert_called_with(recovery_file, max_permissions=0o666)

expected = dict(origin=octoprint.filemanager.FileDestinations.LOCAL,
path=path,
pos=pos,
date=now)


mock_atomic_write.assert_called_with(recovery_file)
mock_yaml_safe_dump.assert_called_with(expected, stream=mock_atomic_write_handle, default_flow_style=False, indent=" ", allow_unicode=True)

@mock.patch("octoprint.util.atomic_write", create=True)
@mock.patch("yaml.safe_dump", create=True)
@mock.patch("time.time")
def test_save_recovery_data(self, mock_time, mock_yaml_safe_dump, mock_atomic_write):
def test_save_recovery_data_with_error(self, mock_time, mock_yaml_safe_dump, mock_atomic_write):
import os

now = 123456789
path = "some_file.gco"
pos = 1234
recovery_file = os.path.join("/path/to/a/base_folder", "print_recovery_data.yaml")

mock_atomic_write.return_value = mock.MagicMock(spec=file)
mock_atomic_write_handle = mock_atomic_write.return_value.__enter__.return_value
mock_time.return_value = now
self.local_storage.path_in_storage.return_value = path

mock_yaml_safe_dump.side_effect = RuntimeError

self.file_manager.save_recovery_data(octoprint.filemanager.FileDestinations.LOCAL, path, pos)
with mock.patch("builtins.open", mock.mock_open(), create=True) as m:
self.file_manager.save_recovery_data(octoprint.filemanager.FileDestinations.LOCAL, path, pos)

@mock.patch("os.path.isfile")
@mock.patch("os.remove")
Expand Down Expand Up @@ -352,29 +344,26 @@ def test_delete_recovery_data_error(self, mock_remove, mock_isfile):

self.file_manager.delete_recovery_data()

@mock.patch("os.path.isfile")
@mock.patch("yaml.safe_load")
def test_get_recovery_data(self, mock_yaml_safe_load, mock_isfile):
@mock.patch("os.path.isfile", return_value=True)
def test_get_recovery_data(self, mock_isfile):
import os
import yaml
recovery_file = os.path.join("/path/to/a/base_folder", "print_recovery_data.yaml")

mock_isfile.return_value = True

data = dict(path="some_path.gco",
origin="local",
pos=1234,
date=123456789)
mock_yaml_safe_load.return_value = data

with mock.patch("__builtin__.open", mock.mock_open(read_data=data), create=True) as m:
result = self.file_manager.get_recovery_data()

self.assertDictEqual(data, result)
text_data = yaml.dump(data)

m.assert_called_with(recovery_file)
with mock.patch("__builtin__.open", mock.mock_open(read_data=text_data)) as m:
# moved safe_load to here so we could mock up the return value properly
with mock.patch("yaml.safe_load", return_value=data) as n:
result = self.file_manager.get_recovery_data()

mock_handle = m()
mock_yaml_safe_load.assert_called_with(mock_handle)
self.assertDictEqual(data, result)
n.assert_called_with(m())
mock_isfile.assert_called_with(recovery_file)

@mock.patch("os.path.isfile")
def test_get_recovery_data_no_file(self, mock_isfile):
Expand Down

0 comments on commit a5f8759

Please sign in to comment.