Skip to content

Commit

Permalink
Add a couple of basic unit/integration tests and some sample data.
Browse files Browse the repository at this point in the history
Closes #71 and #73
  • Loading branch information
scls authored and firecat53 committed Jan 17, 2015
1 parent 57aa202 commit cc23130
Show file tree
Hide file tree
Showing 8 changed files with 1,541 additions and 9 deletions.
18 changes: 18 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
language: python
python:
- "2.7"
- "3.2"
- "3.3"
- "3.4"

# command to install dependencies
install:
- "pip install ."

# command to run tests
script: python tests/test_tabview.py
branches:
only:
- master
notifications:
email: false
5 changes: 5 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ Usage:
import tabview as t
t.view(fn=<filename>)
Tests:
------

* ``python tests/test_tabview.py``

Keybindings:
---------------

Expand Down
1,262 changes: 1,262 additions & 0 deletions sample/data_ohlcv.csv

Large diffs are not rendered by default.

67 changes: 67 additions & 0 deletions sample/multiindex.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
lvl0,,,,a,a,b,b
lvl1,,,,bar,foo,bah,foo
,,,,,,,
A0,B0,C0,D0,1,0,3,2
A0,B0,C0,D1,5,4,7,6
A0,B0,C1,D0,9,8,11,10
A0,B0,C1,D1,13,12,15,14
A0,B0,C2,D0,17,16,19,18
A0,B0,C2,D1,21,20,23,22
A0,B0,C3,D0,25,24,27,26
A0,B0,C3,D1,29,28,31,30
A0,B1,C0,D0,33,32,35,34
A0,B1,C0,D1,37,36,39,38
A0,B1,C1,D0,41,40,43,42
A0,B1,C1,D1,45,44,47,46
A0,B1,C2,D0,49,48,51,50
A0,B1,C2,D1,53,52,55,54
A0,B1,C3,D0,57,56,59,58
A0,B1,C3,D1,61,60,63,62
A1,B0,C0,D0,65,64,67,66
A1,B0,C0,D1,69,68,71,70
A1,B0,C1,D0,73,72,75,74
A1,B0,C1,D1,77,76,79,78
A1,B0,C2,D0,81,80,83,82
A1,B0,C2,D1,85,84,87,86
A1,B0,C3,D0,89,88,91,90
A1,B0,C3,D1,93,92,95,94
A1,B1,C0,D0,97,96,99,98
A1,B1,C0,D1,101,100,103,102
A1,B1,C1,D0,105,104,107,106
A1,B1,C1,D1,109,108,111,110
A1,B1,C2,D0,113,112,115,114
A1,B1,C2,D1,117,116,119,118
A1,B1,C3,D0,121,120,123,122
A1,B1,C3,D1,125,124,127,126
A2,B0,C0,D0,129,128,131,130
A2,B0,C0,D1,133,132,135,134
A2,B0,C1,D0,137,136,139,138
A2,B0,C1,D1,141,140,143,142
A2,B0,C2,D0,145,144,147,146
A2,B0,C2,D1,149,148,151,150
A2,B0,C3,D0,153,152,155,154
A2,B0,C3,D1,157,156,159,158
A2,B1,C0,D0,161,160,163,162
A2,B1,C0,D1,165,164,167,166
A2,B1,C1,D0,169,168,171,170
A2,B1,C1,D1,173,172,175,174
A2,B1,C2,D0,177,176,179,178
A2,B1,C2,D1,181,180,183,182
A2,B1,C3,D0,185,184,187,186
A2,B1,C3,D1,189,188,191,190
A3,B0,C0,D0,193,192,195,194
A3,B0,C0,D1,197,196,199,198
A3,B0,C1,D0,201,200,203,202
A3,B0,C1,D1,205,204,207,206
A3,B0,C2,D0,209,208,211,210
A3,B0,C2,D1,213,212,215,214
A3,B0,C3,D0,217,216,219,218
A3,B0,C3,D1,221,220,223,222
A3,B1,C0,D0,225,224,227,226
A3,B1,C0,D1,229,228,231,230
A3,B1,C1,D0,233,232,235,234
A3,B1,C1,D1,237,236,239,238
A3,B1,C2,D0,241,240,243,242
A3,B1,C2,D1,245,244,247,246
A3,B1,C3,D0,249,248,251,250
A3,B1,C3,D1,253,252,255,254
4 changes: 4 additions & 0 deletions sample/test_latin-1.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"Company","Item Number","Description","Features","Piece","Dozen","Case","Style Code","Size Name","Size Category","Size Code","Color Name","Hex Code","Color Code","Weight","Domain","ProdDetail Image","ProdGallery Image","Retail Price","Style Number","GTIN Number","Max Inventory","Closeout","Mill Name","Pack Qty","Case Qty","Launch Date","Coming Soon","Front of Image Name","Back of Image Name","Side of Image Name"
"ALP","B34130003","Ladies' 7 oz. ComfortSoft� Cotton Piqu� Polo - WHITE - S","100% ComfortSoft&#174; cotton; Welt-knit collar; Tag-free neck label; High-stitch density for superior embellishment platform; Ash is 99% cotton, 1% polyester; Light Steel is 90% cotton, 10% polyester; Hemmed sleeves; Sideseamed for a feminine fit; Narrow, feminine, clean-finished placket with four dyed-to-match buttons; ","8.96","7.51","5.78","035","S","28","3","WHITE","FFFFFF","00",".5","http://www.alphabroder.com","/images/alp/prodDetail/035_00_p.jpg","/images/alp/prodGallery/035_00_g.jpg",17.92,"035","00766369145669",100,no,"Hanes",6,36,"1007",no,"/images/alp/prodDetail/035_00_p.jpg","/images/alp/backDetail/035_bk_00_p.jpg","/images/alp/sideDetail/035_sd_00_p.jpg"
"ALP","B34130004","Ladies' 7 oz. ComfortSoft� Cotton Piqu� Polo - WHITE - M","100% ComfortSoft&#174; cotton; Welt-knit collar; Tag-free neck label; High-stitch density for superior embellishment platform; Ash is 99% cotton, 1% polyester; Light Steel is 90% cotton, 10% polyester; Hemmed sleeves; Sideseamed for a feminine fit; Narrow, feminine, clean-finished placket with four dyed-to-match buttons; ","8.96","7.51","5.78","035","M","28","4","WHITE","FFFFFF","00",".53","http://www.alphabroder.com","/images/alp/prodDetail/035_00_p.jpg","/images/alp/prodGallery/035_00_g.jpg",17.92,"035","00766369145676",100,no,"Hanes",6,36,"1007",no,"/images/alp/prodDetail/035_00_p.jpg","/images/alp/backDetail/035_bk_00_p.jpg","/images/alp/sideDetail/035_sd_00_p.jpg"
"ALP","B34130005","Ladies' 7 oz. ComfortSoft� Cotton Piqu� Polo - WHITE - L","100% ComfortSoft&#174; cotton; Welt-knit collar; Tag-free neck label; High-stitch density for superior embellishment platform; Ash is 99% cotton, 1% polyester; Light Steel is 90% cotton, 10% polyester; Hemmed sleeves; Sideseamed for a feminine fit; Narrow, feminine, clean-finished placket with four dyed-to-match buttons; ","8.96","7.51","5.78","035","L","28","5","WHITE","FFFFFF","00",".58","http://www.alphabroder.com","/images/alp/prodDetail/035_00_p.jpg","/images/alp/prodGallery/035_00_g.jpg",17.92,"035","00766369145683",100,no,"Hanes",6,36,"1007",no,"/images/alp/prodDetail/035_00_p.jpg","/images/alp/backDetail/035_bk_00_p.jpg","/images/alp/sideDetail/035_sd_00_p.jpg"
53 changes: 53 additions & 0 deletions sample/unicode-example-utf8.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
Origin (English),Name (English),Origin (Native),Name (Native)
Australia,Nicole Kidman,Australia,Nicole Kidman
Austria,Johann Strauss,Österreich,Johann Strauß
Belgium (Flemish),Rene Magritte,België,René Magritte
Belgium (French),Rene Magritte,Belgique,René Magritte
Belgium (German),Rene Magritte,Belgien,René Magritte
Bhutan,Gonpo Dorji,འབྲུག་ཡུལ།,མགོན་པོ་རྡོ་རྗེ།
Canada,Celine Dion,Canada,Céline Dion
Canada - Nunavut (Inuktitut),Susan Aglukark,ᓄᓇᕗᒻᒥᐅᑦ,ᓱᓴᓐ ᐊᒡᓗᒃᑲᖅ
Democratic People's Rep. of Korea,LEE Sol-Hee,조선 민주주의 인민 공화국,이설희
Denmark,Soren Hauch-Fausboll,Danmark,Søren Hauch-Fausbøll
Denmark,Soren Kierkegaard,Danmark,Søren Kierkegård
Egypt,Abdel Halim Hafez,ﻣﺼﺮ,ﻋﺑﺪﺍﻠﺣﻟﻳﻢ ﺤﺎﻓﻅ
Egypt,Om Kolthoum,ﻣﺼﺮ,ﺃﻡ ﻛﻟﺛﻭﻡ
Eritrea,Berhane Zeray,ብርሃነ ዘርኣይ,ኤርትራ
Ethiopia,Haile Gebreselassie,ኃይሌ ገብረሥላሴ,ኢትዮጵያ
France,Gerard Depardieu,France,Gérard Depardieu
France,Jean Reno,France,Jean Réno
France,Camille Saint-Saens,France,Camille Saint-Saëns
France,Mylene Demongeot,France,Mylène Demongeot
France,Francois Truffaut,France,François Truffaut
France (Braille),Louis Braille,⠋⠗⠁⠝⠉⠑,⠇⠕⠥⠊⠎⠀<BR>⠃⠗⠁⠊⠇⠇⠑
Georgia,Eduard Shevardnadze,საქართველო,ედუარდ შევარდნაძე
Germany,Rudi Voeller,Deutschland,Rudi Völler
Germany,Walter Schultheiss,Deutschland,Walter Schultheiß
Greece,Giorgos Dalaras,Ελλάς,Γιώργος Νταλάρας
Iceland,Bjork Gudmundsdottir,Ísland,Björk Guðmundsdóttir
India (Hindi),Madhuri Dixit,भारत,माधुरी दिछित
Ireland,Sinead O'Connor,Éire,Sinéad O'Connor
Israel,Yehoram Gaon,ישראל,יהורם גאון
Italy,Fabrizio DeAndre,Italia,Fabrizio De André
Japan,KUBOTA Toshinobu,日本,久保田    利伸
Japan,HAYASHIBARA Megumi,日本,林原 めぐみ
Japan,Mori Ogai,日本,森鷗外
Japan,Tex Texin,日本,テクス テクサン
Norway,Tor Age Bringsvaerd,Noreg,Tor Åge Bringsværd
Pakistan (Urdu),Nusrat Fatah Ali Khan,پاکستان,نصرت فتح علی خان
People's Rep. of China,ZHANG Ziyi,中国,章子怡
People's Rep. of China,WONG Faye,中国,王菲
Poland,Lech Walesa,Polska,Lech Wałęsa
Puerto Rico,Olga Tanon,Puerto Rico,Olga Tañón
Rep. of China,Hsu Chi,臺灣,舒淇
Rep. of China,Ang Lee,臺灣,李安
Rep. of Korea,AHN Sung-Gi,한민국,안성기
Rep. of Korea,SHIM Eun-Ha,한민국,심은하
Russia,Mikhail Gorbachev,Россия,Михаил Горбачёв
Russia,Boris Grebenshchikov,Россия,Борис Гребенщиков
Slovenia,"Frane ""Jezek"" Milcinski",Slovenija,Frane Milčinski - Ježek
Syracuse (Sicily),Archimedes,Συρακούσα,Ἀρχιμήδης
Thailand,Thongchai McIntai,ประเทศไทย,ธงไชย แม็คอินไตย์
U.S.A.,Brad Pitt,U.S.A.,Brad Pitt
Yugoslavia (Cyrillic),Djordje Balasevic,Југославија,Ђорђе Балашевић
Yugoslavia (Latin),Djordje Balasevic,Jugoslavija,Đorđe Balašević
21 changes: 12 additions & 9 deletions tabview/tabview.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,10 @@ class Viewer:
def __init__(self, scr, data, column_width=20, column_gap=2,
trunc_char='…'):
self.scr = scr
self.data = [[str(j) for j in i] for i in data]
if sys.version_info.major < 3:
self.data = [[j for j in i] for i in data]
else:
self.data = [[str(j) for j in i] for i in data]
self.header_offset_orig = 3
self.header = self.data[0]
if len(self.data) > 1:
Expand All @@ -80,7 +83,7 @@ def __init__(self, scr, data, column_width=20, column_gap=2,
self.column_gap = column_gap

try:
trunc_char.encode(sys.stdout.encoding)
trunc_char.encode(sys.stdout.encoding or 'utf-8')
self.trunc_char = trunc_char
except (UnicodeDecodeError, UnicodeError):
self.trunc_char = '>'
Expand Down Expand Up @@ -667,11 +670,11 @@ def detect_encoding(data):
enc - system encoding
"""
enc_list = ['UTF-8', 'LATIN-1', 'iso8859-1', 'iso8859-2',
'UTF-16', 'CP720']
enc_list = ['utf-8', 'latin-1', 'iso8859-1', 'iso8859-2',
'utf-16', 'cp720']
code = locale.getpreferredencoding(False)
if code not in enc_list:
enc_list.insert(0, code)
if code.lower() not in enc_list:
enc_list.insert(0, code.lower())
for c in enc_list:
try:
for line in data:
Expand All @@ -682,10 +685,10 @@ def detect_encoding(data):
print("Encoding not detected. Please pass encoding value manually")


def main(stdscr, data):
def main(stdscr, *args, **kwargs):
curses.use_default_colors()
curses.curs_set(False)
Viewer(stdscr, data).run()
Viewer(stdscr, *args, **kwargs).run()


def view(data, enc=None):
Expand Down Expand Up @@ -715,7 +718,7 @@ def view(data, enc=None):
d = process_data(data, enc)
curses.wrapper(main, d)
except (QuitException, KeyboardInterrupt):
return
return 0
except ReloadException:
continue
finally:
Expand Down
120 changes: 120 additions & 0 deletions tests/test_tabview.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# -*- coding: utf-8 -*-
import curses
import unittest
import tabview.tabview as t

res1 = ["Yugoslavia (Latin)", "Djordje Balasevic", "Jugoslavija",
"Đorđe Balašević"]
res2 = ["ALP", "B34130005", "Ladies' 7 oz. ComfortSoft® Cotton "
"Piqué Polo - WHITE - L",
"100% ComfortSoft&#174; cotton; Welt-knit collar; "
"Tag-free neck label; High-stitch density for superior "
"embellishment platform; "
"Ash is 99% cotton, 1% polyester; Light Steel is "
"90% cotton, 10% polyester; Hemmed sleeves; "
"Sideseamed for a feminine fit; "
"Narrow, feminine, clean-finished placket with four "
"dyed-to-match buttons; ",
"8.96", "7.51", "5.78", "035", "L",
"28", "5", "WHITE", "FFFFFF", "00", ".58",
"http://www.alphabroder.com",
"/images/alp/prodDetail/035_00_p.jpg",
"/images/alp/prodGallery/035_00_g.jpg", 17.92, "035",
"00766369145683", "100", "no", "Hanes", "6",
"36", "1007", "no", "/images/alp/prodDetail/035_00_p.jpg",
"/images/alp/backDetail/035_bk_00_p.jpg",
"/images/alp/sideDetail/035_sd_00_p.jpg"]

data_1 = ('sample/unicode-example-utf8.txt', 'utf-8', res1)
data_2 = ('sample/test_latin-1.csv', 'latin-1', res2)


class TestTabviewUnits(unittest.TestCase):
"""Unit tests for tabview
"""
def setUp(self):
pass

def data(self, fn):
with open(fn, 'rb') as f:
return f.readlines()

def tabview_encoding(self, info):
"""Test that correct encoding is returned for encoded data.
"""
fn, enc, _ = info
d = self.data(fn)
r = t.detect_encoding(d)
self.assertEqual(r, enc)

def test_tabview_encoding_utf8(self):
"""Test that correct encoding is returned for utf-8 encoded file.
"""
self.tabview_encoding(data_1)

def test_tabview_encoding_latin1(self):
"""Test that correct encoding is returned for latin-1 encoded file.
"""
self.tabview_encoding(data_2)

def tabview_file(self, info):
"""Test that data processed from a unicode file matches the sample data
above
"""
fn, _, sample_data = info
code = 'utf-8' # Per top line of file
res = t.process_data(self.data(fn))
# Check that process_file returns a list of lists
self.assertEqual(type(res), list)
self.assertEqual(type(res[0]), list)
# Have to decode res1 and res2 from utf-8 so they can be compared to
# the results from the file, which are unicode (py2) or string (py3)
for j, i in enumerate(sample_data):
try:
i = i.decode(code)
except AttributeError:
i = str(i)
self.assertEqual(i, res[-1][j])

def test_tabview_file_unicode(self):
self.tabview_file(data_1)

def test_tabview_file_latin1(self):
self.tabview_file(data_2)


class TestTabviewIntegration(unittest.TestCase):
"""Integration tests for tabview. Run through the curses routines and some
of the non-interactive movements.
"""
def setUp(self):
pass

def main(self, stdscr, *args, **kwargs):
curses.use_default_colors()
curses.curs_set(False)
v = t.Viewer(stdscr, *args, **kwargs)
v.display()
for key in v.keys:
if key not in ('q', 'Q', 'r', '?', '/', '\n', 'a', 'A', 's', 'S'
'y', curses.KEY_F1, curses.KEY_ENTER):
v.keys[key]()

def data(self, fn):
with open(fn, 'rb') as f:
return f.readlines()

def test_tabview_unicode(self):
curses.wrapper(self.main, self.data(data_1[0]))

def test_tabview_latin1(self):
curses.wrapper(self.main, self.data(data_2[0]))

if __name__ == '__main__':
unittest.main()

0 comments on commit cc23130

Please sign in to comment.