Test for BasicFileStorage.file_url()
[mediagoblin.git] / mediagoblin / tests / test_storage.py
index 94e16a8e0c8e3f5d4c6db9d396c966f460dd0d71..0db9df842a580a434f213ff38d134da2c0b237ea 100644 (file)
@@ -104,12 +104,79 @@ def test_basic_storage_get_unique_filepath():
 
 
 def test_basic_storage_get_file():
-    pass
+    tmpdir, this_storage = get_tmp_filestorage()
+
+    # Write a brand new file
+    filepath = ['dir1', 'dir2', 'ourfile.txt']
+
+    with this_storage.get_file(filepath, 'w') as our_file:
+        our_file.write('First file')
+    with this_storage.get_file(filepath, 'r') as our_file:
+        assert our_file.read() == 'First file'
+    assert os.path.exists(os.path.join(tmpdir, 'dir1/dir2/ourfile.txt'))
+    with file(os.path.join(tmpdir, 'dir1/dir2/ourfile.txt'), 'r') as our_file:
+        assert our_file.read() == 'First file'
+
+    # Write to the same path but try to get a unique file.
+    new_filepath = this_storage.get_unique_filepath(filepath)
+    assert not os.path.exists(os.path.join(tmpdir, *new_filepath))
+
+    with this_storage.get_file(new_filepath, 'w') as our_file:
+        our_file.write('Second file')
+    with this_storage.get_file(new_filepath, 'r') as our_file:
+        assert our_file.read() == 'Second file'
+    assert os.path.exists(os.path.join(tmpdir, *new_filepath))
+    with file(os.path.join(tmpdir, *new_filepath), 'r') as our_file:
+        assert our_file.read() == 'Second file'
+
+    # Read from an existing file
+    manually_written_file = os.makedirs(
+        os.path.join(tmpdir, 'testydir'))
+    with file(os.path.join(tmpdir, 'testydir/testyfile.txt'), 'w') as testyfile:
+        testyfile.write('testy file!  so testy.')
+
+    with this_storage.get_file(['testydir', 'testyfile.txt']) as testyfile:
+        assert testyfile.read() == 'testy file!  so testy.'
 
 
 def test_basic_storage_delete_file():
-    pass
+    tmpdir, this_storage = get_tmp_filestorage()
+
+    assert not os.path.exists(
+        os.path.join(tmpdir, 'dir1/dir2/ourfile.txt'))
+
+    filepath = ['dir1', 'dir2', 'ourfile.txt']
+    with this_storage.get_file(filepath, 'w') as our_file:
+        our_file.write('Testing this file')
+
+    assert os.path.exists(
+        os.path.join(tmpdir, 'dir1/dir2/ourfile.txt'))
+
+    this_storage.delete_file(filepath)
+    
+    assert not os.path.exists(
+        os.path.join(tmpdir, 'dir1/dir2/ourfile.txt'))
 
 
 def test_basic_storage_url_for_file():
-    pass
+    # Not supplying a base_url should actually just bork.
+    tmpdir, this_storage = get_tmp_filestorage()
+    assert_raises(
+        storage.NoWebServing,
+        this_storage.file_url,
+        ['dir1', 'dir2', 'filename.txt'])
+
+    # base_url without domain
+    tmpdir, this_storage = get_tmp_filestorage('/media/')
+    result = this_storage.file_url(
+        ['dir1', 'dir2', 'filename.txt'])
+    expected = '/media/dir1/dir2/filename.txt'
+    assert result == expected
+
+    # base_url with domain
+    tmpdir, this_storage = get_tmp_filestorage(
+        'http://media.example.org/ourmedia/')
+    result = this_storage.file_url(
+        ['dir1', 'dir2', 'filename.txt'])
+    expected = 'http://media.example.org/ourmedia/dir1/dir2/filename.txt'
+    assert result == expected