'Test transferring files to and from the device'

def test_file_transfer(self):

    self.check_setup()
    name = u'zzz-test-folder'
    folder = self.dev.create_folder(self.storage, name)
    self.cleanup.append(folder)
    self.assertTrue(folder.is_folder)
    self.assertEqual(folder.parent_id, self.storage.object_id)
    size = (1024 ** 2)
    raw = io.BytesIO(('a' * size))
    raw.seek(0)
    name = u'test-file.txt'
    pc = ProgressCallback()
    f = self.dev.put_file(folder, name, raw, size, callback=pc)
    self.cleanup.append(f)
    self.assertEqual(f.name, name)
    self.assertEqual(f.size, size)
    self.assertEqual(f.parent_id, folder.object_id)
    self.assertEqual(f.storage_id, folder.storage_id)
    self.assertTrue(pc.end_called, msg=u'Progress    callback    not    called    with    equal    values    (put_file)')
    self.assertTrue((pc.count > 1), msg=u'Progress    callback    only    called    once    (put_file)')
    raw2 = io.BytesIO()
    pc = ProgressCallback()
    self.dev.get_mtp_file(f, raw2, callback=pc)
    self.assertEqual(raw.getvalue(), raw2.getvalue())
    self.assertTrue(pc.end_called, msg=u'Progress    callback    not    called    with    equal    values    (get_file)')
    self.assertTrue((pc.count > 1), msg=u'Progress    callback    only    called    once    (get_file)')
    raw = io.BytesIO('abcd')
    raw.seek(0)
    size = 4
    f = self.dev.put_file(folder, name, raw, size)
    self.cleanup.append(f)
    self.assertEqual(f.name, name)
    self.assertEqual(f.size, size)
    self.assertEqual(f.parent_id, folder.object_id)
    self.assertEqual(f.storage_id, folder.storage_id)
    raw.seek(0)
    with self.assertRaises(ValueError):
        self.dev.put_file(folder, name, raw, size, replace=False)
    raw.seek(0)
    name = u'zzz-test-file.txt'
    f = self.dev.put_file(self.storage, name, raw, size)
    self.cleanup.append(f)
    self.assertEqual(f.name, name)
    self.assertEqual(f.size, size)
    self.assertEqual(f.parent_id, self.storage.object_id)
    self.assertEqual(f.storage_id, self.storage.storage_id)
    raw2 = io.BytesIO()
    self.dev.get_mtp_file(f, raw2)
    self.assertEqual(raw.getvalue(), raw2.getvalue())
