xlnt/source/tests/DumpTestSuite.h

159 lines
4.1 KiB
C
Raw Normal View History

2014-05-09 03:32:12 +08:00
#pragma once
#include <iostream>
#include <cxxtest/TestSuite.h>
2014-05-10 03:54:06 +08:00
#include "../xlnt.h"
2014-05-09 03:32:12 +08:00
class DumpTestSuite : public CxxTest::TestSuite
{
public:
DumpTestSuite()
{
}
void _get_test_filename()
{
2014-05-13 01:42:28 +08:00
NamedTemporaryFile test_file("w", "xlnt.", ".xlsx", false);
2014-05-09 03:32:12 +08:00
test_file.close();
2014-05-13 01:42:28 +08:00
return test_file.name;
2014-05-09 03:32:12 +08:00
}
2014-05-13 01:42:28 +08:00
_COL_CONVERSION_CACHE = dict((get_column_letter(i), i) for i in range(1, 18279));
2014-05-09 03:32:12 +08:00
void test_dump_sheet_title()
{
2014-05-13 01:42:28 +08:00
test_filename = _get_test_filename();
2014-05-09 03:32:12 +08:00
wb = Workbook(optimized_write = True);
ws = wb.create_sheet(title = "Test1");
wb.save(test_filename);
wb2 = load_workbook(test_filename, True);
ws = wb2.get_sheet_by_name("Test1");
2014-05-13 01:42:28 +08:00
TS_ASSERT_EQUALS("Test1", ws.title);
2014-05-09 03:32:12 +08:00
}
void test_dump_sheet()
{
2014-05-13 01:42:28 +08:00
test_filename = _get_test_filename();
2014-05-09 03:32:12 +08:00
wb = Workbook(optimized_write = True);
ws = wb.create_sheet();
letters = [get_column_letter(x + 1) for x in range(20)];
expected_rows = [];
for(auto row : range(20))
{
expected_rows.append(["%s%d" % (letter, row + 1) for letter in letters]);
2014-05-13 01:42:28 +08:00
for(auto row in range(20))
2014-05-09 03:32:12 +08:00
{
expected_rows.append([(row + 1) for letter in letters]);
2014-05-13 01:42:28 +08:00
for(auto row in range(10))
2014-05-09 03:32:12 +08:00
{
expected_rows.append([datetime(2010, ((x % 12) + 1), row + 1) for x in range(len(letters))]);
2014-05-13 01:42:28 +08:00
for(auto row in range(20))
2014-05-09 03:32:12 +08:00
{
expected_rows.append(["=%s%d" % (letter, row + 1) for letter in letters]);
2014-05-13 01:42:28 +08:00
for(auto row in expected_rows)
2014-05-09 03:32:12 +08:00
{
ws.append(row);
}
wb.save(test_filename);
2014-05-13 01:42:28 +08:00
wb2 = load_workbook(test_filename, True);
2014-05-09 03:32:12 +08:00
}
2014-05-13 01:42:28 +08:00
ws = wb2.worksheets[0];
2014-05-09 03:32:12 +08:00
}
}
}
2014-05-13 01:42:28 +08:00
for(auto ex_row, ws_row : zip(expected_rows[:-20], ws.iter_rows()))
2014-05-09 03:32:12 +08:00
{
2014-05-13 01:42:28 +08:00
for(auto ex_cell, ws_cell : zip(ex_row, ws_row))
2014-05-09 03:32:12 +08:00
{
2014-05-13 01:42:28 +08:00
TS_ASSERT_EQUALS(ex_cell, ws_cell.internal_value);
2014-05-09 03:32:12 +08:00
2014-05-13 01:42:28 +08:00
os.remove(test_filename);
2014-05-09 03:32:12 +08:00
}
2014-05-13 01:42:28 +08:00
}
2014-05-09 03:32:12 +08:00
}
void test_table_builder()
{
2014-05-13 01:42:28 +08:00
sb = StringTableBuilder();
2014-05-09 03:32:12 +08:00
2014-05-13 01:42:28 +08:00
result = {"a":0, "b" : 1, "c" : 2, "d" : 3};
2014-05-09 03:32:12 +08:00
2014-05-13 01:42:28 +08:00
for(auto letter in sorted(result.keys()))
{
for x in range(5)
{
sb.add(letter)
2014-05-09 03:32:12 +08:00
2014-05-13 01:42:28 +08:00
table = dict(sb.get_table())
2014-05-09 03:32:12 +08:00
2014-05-13 01:42:28 +08:00
try
{
result_items = result.items()
}
2014-05-09 03:32:12 +08:00
2014-05-13 01:42:28 +08:00
for key, idx in result_items
{
TS_ASSERT_EQUALS(idx, table[key])
}
}
}
2014-05-09 03:32:12 +08:00
}
void test_open_too_many_files()
{
2014-05-13 01:42:28 +08:00
test_filename = _get_test_filename();
wb = Workbook(optimized_write = True);
for i in range(200) over 200 worksheets should raise an OSError("too many open files")
{
wb.create_sheet();
wb.save(test_filename);
os.remove(test_filename);
}
2014-05-09 03:32:12 +08:00
}
void test_create_temp_file()
{
2014-05-13 01:42:28 +08:00
f = dump_worksheet.create_temporary_file();
2014-05-09 03:32:12 +08:00
2014-05-13 01:42:28 +08:00
if(!osp.isfile(f))
{
raise Exception("The file %s does not exist" % f)
}
2014-05-09 03:32:12 +08:00
}
void test_dump_twice()
{
2014-05-13 01:42:28 +08:00
test_filename = _get_test_filename();
2014-05-09 03:32:12 +08:00
2014-05-13 01:42:28 +08:00
wb = Workbook(optimized_write = True);
ws = wb.create_sheet();
ws.append(["hello"]);
2014-05-09 03:32:12 +08:00
2014-05-13 01:42:28 +08:00
wb.save(test_filename);
os.remove(test_filename);
2014-05-09 03:32:12 +08:00
2014-05-13 01:42:28 +08:00
wb.save(test_filename);
2014-05-09 03:32:12 +08:00
}
void test_append_after_save()
{
2014-05-13 01:42:28 +08:00
test_filename = _get_test_filename();
2014-05-09 03:32:12 +08:00
2014-05-13 01:42:28 +08:00
wb = Workbook(optimized_write = True);
ws = wb.create_sheet();
ws.append(["hello"]);
2014-05-09 03:32:12 +08:00
2014-05-13 01:42:28 +08:00
wb.save(test_filename);
os.remove(test_filename);
2014-05-09 03:32:12 +08:00
2014-05-13 01:42:28 +08:00
ws.append(["hello"]);
2014-05-09 03:32:12 +08:00
}
};