class TestBox(TestCase): """Tests for the Box class.""" def setUp(self): """Execute steps before each tests.""" self.box = Box({}, 'storehouse_test', 'box') def test__str__(self): """Test __str__ method.""" box_str = '%s.%s' % (self.box.namespace, self.box.box_id) self.assertEqual(str(self.box), box_str) def test_name_property(self): """Test name property.""" self.assertEqual(self.box.name, 'box') def test_from_json(self): """Test from_json method.""" data = '{"data": "a", "namespace": "b", "name": "c"}' from_json_box = self.box.from_json(data) self.assertEqual(from_json_box.data, 'a') self.assertEqual(from_json_box.namespace, 'b') self.assertEqual(from_json_box.name, 'c') def test_to_dict(self): """Test to_dict method.""" expected_data = { "data": self.box.data, "namespace": self.box.namespace, "owner": self.box.owner, "created_at": self.box.created_at, "id": self.box.box_id, "name": self.box.name } dict_data = self.box.to_dict() self.assertEqual(dict_data, expected_data) def test_to_json(self): """Test to_json method.""" data = { "data": self.box.data, "namespace": self.box.namespace, "owner": self.box.owner, "created_at": self.box.created_at, "id": self.box.box_id, "name": self.box.name } expected_data = json.dumps(data, indent=4) json_data = self.box.to_json() self.assertEqual(json_data, expected_data) def test_metadata_from_box(self): """Test metadata_from_box method.""" expected_metadata = {"box_id": self.box.box_id, "name": self.box.name, "owner": self.box.owner, "created_at": self.box.created_at} metadata = metadata_from_box(self.box) self.assertEqual(metadata, expected_metadata)
def test_add_metadata_to_cache(self): """Test add_metadata_to_cache method.""" box = Box('any', 'namespace', '123') self.napp.add_metadata_to_cache(box) box_metadata = self.napp.metadata_cache['namespace'][0] self.assertEqual(box_metadata['box_id'], box.box_id) self.assertEqual(box_metadata['owner'], box.owner) self.assertEqual(box_metadata['created_at'], box.created_at)
def test_retrieve_failure_case(self, mock_path): """Test retrieve method to failure case.""" path = MagicMock() destination = MagicMock() destination.is_file.return_value = False mock_path.return_value = path path.joinpath.return_value = destination box = Box('any', 'namespace', box_id='123') retrieve = self.file_system.retrieve(box.namespace, box.box_id) self.assertFalse(retrieve)
def test_create_cache(self): """Test create_cache method.""" box = Box('any', 'namespace', '123') self.napp.backend.list_namespaces.return_value = ['namespace'] self.napp.backend.list.return_value = ['123'] self.napp.backend.retrieve.return_value = box self.napp.create_cache() box_metadata = self.napp.metadata_cache['namespace'][0] self.assertEqual(box_metadata['box_id'], box.box_id) self.assertEqual(box_metadata['owner'], box.owner) self.assertEqual(box_metadata['created_at'], box.created_at)
def test_update(self, *args): """Test update method.""" (mock_write, mock_path) = args destination = MagicMock() mock_path.return_value = destination box = Box('any', 'namespace', box_id='123') self.file_system.update(box.namespace, box) mock_path.assert_called_with(self.file_system.destination_path, 'namespace') destination.joinpath.assert_called_with('123') mock_write.assert_called_with(destination.joinpath.return_value, box)
def test_create(self, *args): """Test create method.""" (mock_write_to_file, mock_create_dirs, mock_path) = args destination = MagicMock() mock_path.return_value = destination box = Box('any', 'namespace', box_id='123') self.file_system.create(box) mock_path.assert_called_with(self.file_system.destination_path, 'namespace') mock_create_dirs.assert_called_with(destination) mock_write_to_file.assert_called_with(destination.joinpath('123'), box)
def test_delete(self, *args): """Test delete method.""" (mock_delete_file, mock_path) = args path = MagicMock() destination = MagicMock() mock_path.return_value = path path.joinpath.return_value = destination box = Box('any', 'namespace', box_id='123') self.file_system.delete(box.namespace, box.box_id) mock_path.assert_called_with(self.file_system.destination_path, 'namespace') path.joinpath.assert_called_with('123') mock_delete_file.assert_called_with(destination)
def test_backup_without_box_id(self, *args): """Test backup method without box_id parameter.""" (_, mock_list, mock_retrieve) = args mock_list.return_value = ['456'] retrieve = MagicMock() retrieve.to_json.return_value = {} mock_retrieve.return_value = retrieve box = Box('any', 'namespace', box_id='123') boxes_dict_1 = self.file_system.backup(box.namespace, box.box_id) boxes_dict_2 = self.file_system.backup(box.namespace) self.assertEqual(boxes_dict_1, {'123': {}}) self.assertEqual(boxes_dict_2, {'456': {}})
def test_retrieve_success_case(self, *args): """Test retrieve method to success case.""" (mock_load_from_file, mock_path) = args path = MagicMock() destination = MagicMock() destination.is_file.return_value = True mock_path.return_value = path path.joinpath.return_value = destination box = Box('any', 'namespace', box_id='123') retrieve = self.file_system.retrieve(box.namespace, box.box_id) mock_path.assert_called_with(self.file_system.destination_path, 'namespace') mock_load_from_file.assert_called_with(destination) self.assertEqual(retrieve, 'data')
def setUp(self): """Execute steps before each tests.""" self.box = Box({}, 'storehouse_test', 'box')