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_is_uploaded(self, mock_mod, mock_query, mock_ufile):
        """Test is_uploaded"""

        mock_mod.return_value = True
        result = mock.create_autospec(UserFile)
        result.name = "1"
        mock_ufile.return_value = result
        api = mock.create_autospec(batchapps.api.BatchAppsApi)

        ufile = UserFile(api, {'name':'1'})

        resp = mock.create_autospec(Response)
        resp.success = False
        resp.result = RestCallException(None, "Boom", None)
        api.query_files.return_value = resp

        with self.assertRaises(RestCallException):
            ufile.is_uploaded()

        resp.success = True
        resp.result = ['1', '2', '3']
        self.assertIsInstance(ufile.is_uploaded(), UserFile)
        self.assertTrue(api.query_files.called)
        self.assertTrue(mock_query.called)
        self.assertEqual(mock_ufile.call_count, 3)
        mock_ufile.assert_called_with(mock.ANY, '3')

        result.name = "4"
        self.assertIsNone(ufile.is_uploaded())
    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_create(self, mock_sum, mock_mod, mock_verify, mock_path):
        """Test UserFile object"""

        api = mock.create_autospec(batchapps.api.BatchAppsApi)
        mock_sum.return_value = "check_sum"
        mock_verify.return_value = False
        mock_mod.return_value = "2014-06-04T03:48:40.909998Z"

        with self.assertRaises(TypeError):
            UserFile(None, None)
        with self.assertRaises(TypeError):
            UserFile(api, None)
        with self.assertRaises(TypeError):
            UserFile(api, 42)

        u_file = UserFile(api, {'name':'test1'})
        x_file = UserFile(api, {'name':'test2'})
        self.assertFalse(mock_sum.called)
        self.assertFalse(mock_verify.called)
        self.assertFalse(mock_mod.called)
        self.assertEqual(str(u_file), "test1")
        self.assertEqual(sorted([x_file, u_file]), [u_file, x_file])

        u_file = UserFile(api, "test")
        mock_path.basename.assert_called_with("test")
        mock_path.normpath.assert_called_with("test")
        self.assertTrue(mock_sum.called)
        self.assertTrue(mock_verify.called)
        self.assertTrue(mock_mod.called)
    def test_userfile_get_windows_path(self):
        """Test _get_windows_path"""

        api = mock.create_autospec(batchapps.api.BatchAppsApi)
        u_file = UserFile(api, {})
        u_file.path = "c:\\test"
        w_path = u_file._get_windows_path()
        self.assertEqual(w_path, u_file.path)

        u_file.path = "/user/test"
        w_path = u_file._get_windows_path()
        self.assertEqual(w_path, "\\user\\test")
    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_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_upload(self, mock_isup):
        """Test upload"""

        _callback = mock.Mock()
        api = mock.create_autospec(batchapps.api.BatchAppsApi)
        resp = mock.create_autospec(Response)
        resp.success = False
        resp.result = RestCallException(None, "Boom", None)

        mock_isup.return_value = mock.create_autospec(UserFile)
        api.send_file.return_value = resp

        ufile = UserFile(api, {})
        self.assertIsNone(ufile.upload())
        self.assertEqual(ufile.upload(force=True), resp)
        api.send_file.assert_called_once_with(ufile, callback=None, block=4096)

        mock_isup.return_value = None
        self.assertEqual(ufile.upload(), resp)
        self.assertEqual(ufile.upload(force=True, callback=_callback, block=1), resp)
        api.send_file.assert_called_with(ufile, callback=_callback, block=1)
    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)
    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)