def test_convertOK(tmpdir): print "TMP DIR ", tmpdir inDataHeader = ['boolean','number', 'decimal', 'date','datetime', 'string'] typedefs = {c: {'type':c} for c in inDataHeader} #map(lambda c: {'type':c}, inDataHeader) inDataGeneratorMap = { 'boolean': lambda: random.choice(['true', 'false']), 'number': lambda: str(random.randrange(0,1000)), 'decimal': lambda: str(random.random()), 'date': lambda: strTimeProp("2015-1-1","1960-1-1","%Y-%m-%d", random.random()), 'datetime': lambda: strTimeProp("2015-1-1 00:00:00","1960-1-1 00:00:00", "%Y-%m-%d %H:%M:%S", random.random()), 'string': lambda: 'test string' } inFilePath = tmpdir.mkdir("in").join("intable.csv") inFilePath = str(inFilePath.realpath()) outDir = tmpdir.mkdir("out") outFilePath = outDir.join("intable.csv.tde") outFilePath = str(outFilePath.realpath()) inFileData = [] for rowIdx in range(0, testCsvLinesNumber): row = [] for column in inDataHeader: row.append(inDataGeneratorMap[column]()) inFileData.append(row) with open(inFilePath, 'w') as inFile: csvFile = csv.writer(inFile, delimiter=',') csvFile.writerows([inDataHeader]) csvFile.writerows(inFileData) src.convert2tde(inFilePath, outFilePath, typedefs) outSize = os.path.getsize(outFilePath) assert outSize > 0
def test_emptyoutput(tmpdir): header = ["pokus"] data = [] inFilePath, outFilePath = createcsvfile('pokus.csv', tmpdir, header, data) print inFilePath, outFilePath src.convert2tde(inFilePath, outFilePath, {}) assert file_exists(outFilePath)
def test_convertCustomNameOK(tmpdir, validCustomName): print "TMP DIR ", tmpdir inDataHeader = ['boolean'] typedefs = {c: {'type':c} for c in inDataHeader} #map(lambda c: {'type':c}, inDataHeader) inDataGeneratorMap = { 'boolean': lambda: random.choice(['true', 'false']), 'string': lambda: 'test string' } inFilePath = tmpdir.mkdir("in").join("intable.csv") inFilePath = str(inFilePath.realpath()) outDir = tmpdir.mkdir("out") outFilePath = outDir.join(validCustomName + '.tde') outFilePath = str(outFilePath.realpath()) inFileData = [] for rowIdx in range(0, 5): row = [] for column in inDataHeader: row.append(inDataGeneratorMap[column]()) inFileData.append(row) with open(inFilePath, 'w') as inFile: csvFile = csv.writer(inFile, delimiter=',') csvFile.writerows([inDataHeader]) csvFile.writerows(inFileData) src.convert2tde(inFilePath, outFilePath, typedefs) outSize = os.path.getsize(outFilePath) assert outSize > 0
def test_tableauException(tmpdir): header = ["tableau"] data = [["1"]] inFilePath, outFilePath = createcsvfile('tableau.csv', tmpdir, header, data) with pytest.raises(SystemExit) as exc: src.convert2tde(inFilePath, outFilePath, {}) src.convert2tde(inFilePath, outFilePath, {}) assert exc.value.code == 1
def test_unicodeStrings(tmpdir): unicodestring = u' the (£670bn)' data= [[unicodestring]] header= ["text"] typedefs = {"text":{"type":"string"}} inFilePath, outFilePath = createcsvfile('invaliddecimal.csv', tmpdir, header, data) src.convert2tde(inFilePath, outFilePath, typedefs) assert file_exists(outFilePath)
def test_failedDecimal(tmpdir, invalidDecimals): data = invalidDecimals header = ["decimal"] inFilePath, outFilePath = createcsvfile('invaliddecimal.csv', tmpdir, header, data) typedefs = {"decimal":{"type":"decimal"}} with pytest.raises(SystemExit) as exc: src.convert2tde(inFilePath, outFilePath, typedefs) assert exc.value.code == 1
def test_failedNumber(tmpdir, invalidNumbers): data = invalidNumbers header = ["number"] inFilePath, outFilePath = createcsvfile('invalidnumber.csv', tmpdir, header, data) typedefs = {"number":{"type":"number"}} with pytest.raises(SystemExit) as exc: src.convert2tde(inFilePath, outFilePath, typedefs) assert exc.value.code == 1
def test_nullbyteConvert(tmpdir): typedefs = {"text":{"type":"string"}} inFilePath = './tests/nullbyte.csv' randstr = str(random.randint(1,100000)) fname = 'nullbyte' outdir = tmpdir.ensure_dir('out', 'files') outFilePath = str(outdir.join(randstr + fname + ".tde").realpath()) src.convert2tde(inFilePath, outFilePath, typedefs) assert file_exists(outFilePath)
def test_customDateInvalidFormat(tmpdir, invalidDateDef): inFilePath = tmpdir.mkdir("in").join("customdate.csv" + str(random.random())) inFilePath = str(inFilePath.realpath()) outDir = tmpdir.mkdir("out") outFilePath = outDir.join(str(random.random()) + "customdate.csv.tde" ) outFilePath = str(outFilePath.realpath()) data = [['testcolumn']] testFormat = "%H:%M %d.%m.%Y" for rowIdx in range(0, 3): value = strTimeProp("00:00 1.1.2015","00:00 1.1.1960", testFormat, random.random()) data.append([value]) with open(inFilePath, 'w') as inFile: csvFile = csv.writer(inFile, delimiter=',') csvFile.writerows(data) typedefs = {'testcolumn':invalidDateDef} with pytest.raises(SystemExit) as exc: src.convert2tde(inFilePath, outFilePath, typedefs) assert exc.value.code == 1
def test_customDateFormat(tmpdir): inFilePath = tmpdir.mkdir("in").join("customdate.csv") inFilePath = str(inFilePath.realpath()) outDir = tmpdir.mkdir("out") outFilePath = outDir.join("customdate.csv.tde") outFilePath = str(outFilePath.realpath()) data = [['testcolumn']] testFormat = "%H:%M %d.%m.%Y" for rowIdx in range(0, testCsvLinesNumber): value = strTimeProp("00:00 1.1.2015","00:00 1.1.1960", testFormat, random.random()) data.append([value]) with open(inFilePath, 'w') as inFile: csvFile = csv.writer(inFile, delimiter=',') csvFile.writerows(data) typedefs = {'testcolumn':{'type':'datetime', 'format': testFormat}} src.convert2tde(inFilePath, outFilePath, typedefs) outSize = os.path.getsize(outFilePath) assert outSize > 0
def test_missing(tmpdir): c1 = ["1","2","","3","4"] c2 = ["asd", "dd", "asd"] c3 = ["1.0", "1", "20", "3.3", ""] c4 = ["", "True", "", "False"] c5 = ["2015-1-1", "", "", "", ""] c6 = ["", "2015-1-1 00:00:00", "", "", ""] data = merge_columns(c1, c2, c3, c4, c5, c6) print data header = ["c1", "c2", "c3", "c4", "c5", "c6"] inFilePath, outFilePath = createcsvfile('missing.csv', tmpdir, header, data) typedefs = { "c1":{"type": "number"}, "c2":{"type": "string"}, "c3":{"type": "decimal"}, "c4": {"type": "boolean"}, "c5": {"type": "date"}, "c6": {"type": "datetime"} } src.convert2tde(inFilePath, outFilePath, typedefs) assert file_exists(outFilePath)