def test_userfile_checksum(self):
        """Test get_checksum"""

        if not self.use_test_files:
            self.skipTest("No test files present")

        test_path = os.path.join(self.cwd, "test_assets", "star.png")
        api = mock.create_autospec(batchapps.api.BatchAppsApi)
        u_file = UserFile(api, {'name':'star.png'})
        u_file.path = test_path
        chsum = u_file.get_checksum()
        self.assertEqual(chsum, "")

        u_file._exists = True
        chsum = u_file.get_checksum()
        u_file._checksum = chsum
        self.assertEqual(len(chsum), 16)

        u_file.path = None
        chsum = u_file.get_checksum()
        self.assertEqual(chsum, "")

        u_file.path = "c:\\test"
        chsum = u_file.get_checksum()
        self.assertEqual(chsum, "")

        x_file = UserFile(api, {'name':'star.png'})
        x_file.path = os.path.join(self.cwd, "test_assets", "same.png")

        self.assertFalse(x_file == u_file)
        x_file._exists = True
        x_file._checksum = x_file.get_checksum()

        self.assertTrue(u_file == x_file)
    def test_userfile_len(self, mock_size):
        """Test __len__"""

        api = mock.create_autospec(batchapps.api.BatchAppsApi)
        mock_size.return_value = 4096
        u_file = UserFile(api, {})
        u_file.path = "c:\\test"
        self.assertEqual(len(u_file), 0)

        u_file._exists = True
        self.assertEqual(len(u_file), 4096)
    def test_userfile_create_submit_specifier(self, mock_path):
        """Test create_submit_specifier"""

        api = mock.create_autospec(batchapps.api.BatchAppsApi)
        u_file = UserFile(api, {})
        mock_path.return_value = "new_path"

        with self.assertRaises(FileMissingException):
            u_file.create_submit_specifier()
        u_file._exists = True
        spec = u_file.create_submit_specifier()
        self.assertEqual(spec, {'Name':'Unknown',
                                'Timestamp':''})
    def test_userfile_compare_lastmodified(self, mock_mod):
        """Test compare_lastmodified"""

        api = mock.create_autospec(batchapps.api.BatchAppsApi)
        mock_mod.return_value = "2014-08-04T03:53:30Z"
        u_file = UserFile(api, {'name':'star.png'})
        u_file._last_modified = "2014-08-04T03:53:30Z"

        x_file = UserFile(api, {'name':'same.png'})
        x_file._exists = True
        self.assertTrue(x_file.compare_lastmodified(u_file))

        mock_mod.return_value = ""
        self.assertFalse(x_file.compare_lastmodified(u_file))
    def test_userfile_last_modified(self, mock_mod):
        """Test get_last_modified"""

        mock_mod.return_value = 1407124410.692879
        api = mock.create_autospec(batchapps.api.BatchAppsApi)
        u_file = UserFile(api, {})
        u_file.path = "c:\\test"
        mod = u_file.get_last_modified()
        self.assertEqual(mod, "")

        u_file._exists = True
        mod = u_file.get_last_modified()
        mock_mod.assert_called_once_with("c:\\test")
        self.assertTrue(mod.startswith("2014-08-04T03:53:30"))
    def test_userfile_exists(self, mock_size, mock_isfile):
        """Test _verify_path"""

        api = mock.create_autospec(batchapps.api.BatchAppsApi)
        mock_isfile.return_value = False
        u_file = UserFile(api, {})
        u_file.path = "c:\\test"
        self.assertFalse(u_file._verify_path())

        mock_isfile.return_value = True
        self.assertTrue(u_file._verify_path())

        self.assertFalse(u_file)

        u_file._exists = True
        self.assertTrue(u_file)
    def test_userfile_download(self, mock_size, mock_is_uploaded):
        """Test download"""

        _callback = mock.Mock()
        mock_size.return_value = 0
        api = mock.create_autospec(batchapps.api.BatchAppsApi)
        ufile = UserFile(api, {})
        download_dir = "test"

        mock_is_uploaded.side_effect = RestCallException(None, "Boom", None)
        with self.assertRaises(RestCallException):
            ufile.download(download_dir)
        
        mock_is_uploaded.side_effect = None
        mock_is_uploaded.return_value = None
        ufile.download(download_dir)
        self.assertFalse(api.props_file.called)

        ufile._exists = True
        resp = mock.create_autospec(Response)
        resp.success = False
        resp.result = RestCallException(None, "Boom", None)
        api.props_file.return_value = resp
        mock_is_uploaded.return_value = ufile
        with self.assertRaises(RestCallException):
            ufile.download(download_dir)
            self.assertTrue(api.props_file.called)

        resp.success = True
        resp.result = 123
        api.props_file.return_value = resp
        r = mock.create_autospec(Response)
        r.success = False
        r.result = RestCallException(None, "Boom", None)
        api.get_file.return_value = r
        with self.assertRaises(RestCallException):
            ufile.download(download_dir)
        api.get_file.assert_called_with(ufile, resp.result, download_dir, callback=None, block=4096)
        
        r.success = True
        r.result = "test"
        ufile.download(download_dir, callback=_callback, block=1)
        api.get_file.assert_called_with(ufile, resp.result, download_dir, callback=_callback, block=1)