def test_get_path(self): self.store.save('test.txt', test_files_path / 'small.txt') self.assertEqual( FieldFile('test.txt', self.store).path, self.store.path('test.txt')) self.assertRaises(NoFileNameError, lambda: FieldFile(None, self.store).path)
def test_save_with_name(self): self.store.save('aaa.txt', test_files_path / 'small.txt') ff = FieldFile('aaa.txt', self.store) # Make it load the file x = ff.file ff.save(open(test_files_path / 'adam.png', 'rb'), 'text.txt') self.assertIn('text.txt', ff.file.name)
def test_close(self): self.store.save('aaa.txt', test_files_path / 'small.txt') ff = FieldFile('aaa.txt', self.store) # Make it load the file x = ff.file ff.close() # Make it load the file again, just to check it can still be reopened ff.file.read()
def test_delete_with_file(self): self.store.save('aaa.txt', test_files_path / 'small.txt') ff = FieldFile('aaa.txt', self.store) # Make it load the file x = ff.file ff.delete() self.assertFalse(ff.has_file_handle) self.assertFalse(os.path.exists(self.store.path('aaa.txt')))
def test_save_with_name_with_fn_gen(self): self.store.save('aaa.txt', test_files_path / 'small.txt') ff = FieldFile('aaa.txt', self.store, file_name_generator=lambda x: 'generated.foo') # Make it load the file x = ff.file ff.save(open(test_files_path / 'adam.png', 'rb'), 'text.txt') self.assertIn('generated.foo', ff.file.name) self.assertIn('generated.foo', ff.name)
def test_save_without_name(self): self.store.save('aaa.txt', test_files_path / 'small.txt') ff = FieldFile('aaa.txt', self.store) # Make it load the file x = ff.file # Overwriting, so the new file stays in palce and this one gets '_1' appended ff.save(open(test_files_path / 'adam.png', 'rb')) self.assertIn('aaa_1.txt', ff.file.name)
def test_file_del(self): self.store.save('test.txt', test_files_path / 'small.txt') ff = FieldFile('test.txt', self.store) self.assertIsInstance(ff.file, file) del ff.file # Cached file is now gone self.assertFalse(hasattr(ff, '_file')) # Getting it via the property recreates it self.assertIsInstance(ff.file, file) # And check it now exists self.assertTrue(hasattr(ff, '_file'))
def test_delete_no_file(self): FieldFile(None, self.store).delete()
def test_file_set(self): ff = FieldFile('test.txt', self.store) ff.file = open(test_files_path / 'small.txt', 'rb') # Setting the file doesn't really do anything self.assertEqual(ff.name, 'test.txt')
def test_eq(self): self.assertEqual(FieldFile('my-file.jpg', self.store), FieldFile('my-file.jpg', self.store)) self.assertNotEqual(FieldFile('my-file.txt', self.store), FieldFile('my-file.jpg', self.store)) self.assertNotEqual(FieldFile('foo-bar.jpg', self.store), FieldFile('my-file.jpg', self.store)) self.assertNotEqual(FieldFile('my-file.JPG', self.store), FieldFile('my-file.jpg', self.store)) self.assertEqual(FieldFile('my-file.jpg', self.store), 'my-file.jpg') self.assertNotEqual(FieldFile('my-file.txt', self.store), 'my-file.jpg') self.assertNotEqual(FieldFile('foo-bar.jpg', self.store), 'my-file.jpg') self.assertNotEqual(FieldFile('my-file.JPG', self.store), 'my-file.jpg') self.assertEqual(FieldFile(None, self.store), None)
def test_file_get(self): self.store.save('test.txt', test_files_path / 'small.txt') self.assertIsInstance(FieldFile('test.txt', self.store).file, file) self.assertRaises(IOError, lambda: FieldFile('test-XXX.txt', self.store).file)
def fn(): FieldFile('test.txt', self.store).url = 'x'
def fn(): ff = FieldFile('test.txt', self.store) del ff.path
def fn(): FieldFile('test.txt', self.store).path = 'x'
def fn(): FieldFile('test.txt', self.store).size = 123