Source

HORNET / tests / test_file.py

# -*- coding: utf-8 -*-
#
# Copyright 2009 Vanderbilt University
# 
# Licensed under the Apache License, Version 2.0 (the "License"); 
# you may not use this file except in compliance with the License. 
# You may obtain a copy of the License at 
# 
#     http://www.apache.org/licenses/LICENSE-2.0 
# 
# Unless required by applicable law or agreed to in writing, software 
# distributed under the License is distributed on an "AS IS" BASIS, 
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
# See the License for the specific language governing permissions and 
# limitations under the License. 

from __future__ import with_statement
from hornet.file import *
import hornet.file
import nose
import os.path
import tempfile

def setup_file():
    global filename 
    filename = tmp_file()
    
def teardown_file():
    global filename
    try: remove(filename)
    except: pass
    del filename
        
def get_dir_list():
    dir = tempfile.gettempdir()
    return [os.path.join(dir, f) for f in list_dir(dir)]

@nose.with_setup(setup_file, teardown_file)
def test_csv_read():
    input = "1,2,3,5\na,b,c,e"
    with open(filename, 'w') as f:
        f.write(input)
    
    data = list(read_csv(filename))
    expected = [['1', '2', '3', '5'],
                ['a', 'b', 'c', 'e']]
    assert data == expected

@nose.with_setup(setup_file, teardown_file)
def test_csv_read_empty():
    data = list(read_csv(filename))
    assert data == []

@nose.with_setup(setup_file, teardown_file)
def test_csv_write():
    input = [[1, 2],
             ['aa', 'b']]
    write_csv(filename, input)
    
    expected = "1,2\r\naa,b\r\n"
    with open(filename, 'r') as f:
        assert f.read() == expected

@nose.with_setup(setup_file, teardown_file)
def test_csv_write_iterator():
    input = [[1, 2],
             ['aa', 'b']]
    write_csv(filename, iter(input))
    
    expected = "1,2\r\naa,b\r\n"
    with open(filename, 'r') as f:
        assert f.read() == expected

@nose.with_setup(setup_file, teardown_file)
def test_csv_write_generator():
    input = [[1, 2],
             ['aa', 'b']]
    write_csv(filename, (x for x in input))
    
    expected = "1,2\r\naa,b\r\n"
    with open(filename, 'r') as f:
        assert f.read() == expected

@nose.with_setup(setup_file, teardown_file)
def test_csv_write_header():
    input = [[1, 2],
             ['aa', 'b']]
    write_csv(filename, input, header=['col1', 'col2'])
    
    expected = 'col1,col2\r\n1,2\r\naa,b\r\n'
    with open(filename, 'r') as f:
        assert f.read() == expected
            
@nose.with_setup(setup_file, teardown_file)
def test_pickling():
    obj = {'4': ['a', 2]}
    serialize(filename, obj)
    data = deserialize(filename)
    assert obj == data
    
@nose.with_setup(setup_file, teardown_file)
def test_remove():
    remove(filename)
    assert not filename in get_dir_list()

def test_remove_dir():
    location = tmp_dir()
    assert len(list_dir(location)) == 0
    remove(location)
    nose.tools.assert_raises(OSError, list_dir, location)

def test_remove_dir_with_contents():
    location = tmp_dir()
    fn = join(location, 'thing.tmp')
    with file(fn, 'w') as f:
        f.write('hello world')

    remove(location)
    nose.tools.assert_raises(OSError, list_dir, location)

def test_list_dir():
    path = tempfile.gettempdir()
    assert list_dir(path) == os.listdir(path) 
 
@nose.with_setup(setup_file, teardown_file)
def test_tmp_file():
    assert filename in get_dir_list()

def test_tmp_file_args():
    fn = tmp_file(prefix='bumblebee')
    assert 'bumblebee' in fn
    assert fn in get_dir_list()
    remove(fn)

def test_tmp_dir():
    location = tmp_dir()
    fn = join(location, 'thing.tmp')
    with file(fn, 'w') as f:
        f.write('hello world')
    assert 'thing.tmp' in list_dir(location)
    
    ## teardown
    remove(location)

def test_join():
    expected = os.path.join('a', 'b')
    assert join('a','b') == expected

def test_join_multiple():
    expected = os.path.join('a', 'b', 'c')
    assert join('a','b', 'c') == expected

def test_get_dir():
    assert get_dir(__file__) == os.path.dirname(os.path.abspath(__file__))