def testAppendData(self): # We check a few examples where the format of the data does not match the # description and hen a few valid examples. The test for the content itself # is done inside the ToJSCode and ToJSon functions. table = DataTable([("a", "number"), ("b", "string")]) self.assertEqual(0, table.NumberOfRows()) self.assertRaises(DataTableException, table.AppendData, [[1, "a", True]]) self.assertRaises(DataTableException, table.AppendData, { 1: ["a"], 2: ["b"] }) self.assertEqual(None, table.AppendData([[1, "a"], [2, "b"]])) self.assertEqual(2, table.NumberOfRows()) self.assertEqual(None, table.AppendData([[3, "c"], [4]])) self.assertEqual(4, table.NumberOfRows()) table = DataTable({"a": "number", "b": "string"}) self.assertEqual(0, table.NumberOfRows()) self.assertRaises(DataTableException, table.AppendData, [[1, "a"]]) self.assertRaises(DataTableException, table.AppendData, {5: { "b": "z" }}) self.assertEqual(None, table.AppendData([{"a": 1, "b": "z"}])) self.assertEqual(1, table.NumberOfRows()) table = DataTable({("a", "number"): [("b", "string")]}) self.assertEqual(0, table.NumberOfRows()) self.assertRaises(DataTableException, table.AppendData, [[1, "a"]]) self.assertRaises(DataTableException, table.AppendData, {5: { "b": "z" }}) self.assertEqual(None, table.AppendData({5: ["z"], 6: ["w"]})) self.assertEqual(2, table.NumberOfRows()) table = DataTable({("a", "number"): {"b": "string", "c": "number"}}) self.assertEqual(0, table.NumberOfRows()) self.assertRaises(DataTableException, table.AppendData, [[1, "a"]]) self.assertRaises(DataTableException, table.AppendData, {1: ["a", 2]}) self.assertEqual( None, table.AppendData({ 5: { "b": "z", "c": 6 }, 7: { "c": 8 }, 9: {} })) self.assertEqual(3, table.NumberOfRows())
def testToCsv(self): init_data_csv = "\r\n".join( ["A,\"b\"\"\",c", "1,,", ",zz'top,true", ""]) table = DataTable([("a", "number", "A"), "b\"", ("c", "boolean")], [[(1, "$1")], [None, "zz'top", True]]) self.assertEqual(init_data_csv, table.ToCsv()) table.AppendData([[-1, "w", False]]) init_data_csv = "%s%s\r\n" % (init_data_csv, "-1,w,false") self.assertEqual(init_data_csv, table.ToCsv()) init_data_csv = "\r\n".join([ "T,d,dt", "01:02:03,1901-02-03,", "\"time \"\"2 3 4\"\"\",1902-03-04,1901-02-03 04:05:06", ",1903-04-05,", "" ]) table = DataTable({ ("d", "date"): [("t", "timeofday", "T"), ("dt", "datetime")] }) table.LoadData({ date(1901, 2, 3): [time(1, 2, 3)], date(1902, 3, 4): [(time(2, 3, 4), 'time "2 3 4"'), datetime(1901, 2, 3, 4, 5, 6)], date(1903, 4, 5): [] }) self.assertEqual(init_data_csv, table.ToCsv(columns_order=["t", "d", "dt"]))
def testToCsv(self): init_data_csv = "\n".join( ['"A", "b", "c"', '1, "", ""', '"", "zz\'top", true']) table = DataTable([("a", "number", "A"), "b", ("c", "boolean")], [[(1, "$1")], [None, "zz'top", True]]) self.assertEqual(init_data_csv, table.ToCsv()) table.AppendData([[-1, "w", False]]) init_data_csv = "%s\n%s" % (init_data_csv, '-1, "w", false') self.assertEquals(init_data_csv, table.ToCsv()) init_data_csv = "\n".join([ '"T", "d", "dt"', '"[1,2,3]", "new Date(1,1,3)", ""', '"time ""2 3 4""", "new Date(2,2,4)", "new Date(1,1,3,4,5,6)"', '"", "new Date(3,3,5)", ""' ]) table = DataTable({ ("d", "date"): [("t", "timeofday", "T"), ("dt", "datetime")] }) table.LoadData({ date(1, 2, 3): [time(1, 2, 3)], date(2, 3, 4): [(time(2, 3, 4), 'time "2 3 4"'), datetime(1, 2, 3, 4, 5, 6)], date(3, 4, 5): [] }) self.assertEqual(init_data_csv, table.ToCsv(columns_order=["t", "d", "dt"]))
def testToJSon(self): # The json of the initial data we load to the table. init_data_json = ("{cols:" "[{id:'a',label:'A',type:'number'}," "{id:'b',label:'b',type:'string'}," "{id:'c',label:'c',type:'boolean'}]," "rows:[" "{c:[{v:1},,{v:null}]}," "{c:[,{v:'z'},{v:true}]}" "]}") table = DataTable([("a", "number", "A"), "b", ("c", "boolean")], [[1], [None, "z", True]]) self.assertEqual(2, table.NumberOfRows()) self.assertEqual(init_data_json, table.ToJSon()) table.AppendData([[-1, "w", False]]) self.assertEqual(3, table.NumberOfRows()) self.assertEqual( init_data_json[:-2] + ",{c:[{v:-1},{v:'w'},{v:false}]}]}", table.ToJSon()) cols_json = ("{cols:" "[{id:'t',label:'T',type:'timeofday'}," "{id:'d',label:'d',type:'date'}," "{id:'dt',label:'dt',type:'datetime'}],") table = DataTable({ ("d", "date"): [("t", "timeofday", "T"), ("dt", "datetime")] }) table.LoadData({date(1, 2, 3): [time(1, 2, 3)]}) self.assertEqual(1, table.NumberOfRows()) self.assertEqual( cols_json + "rows:[{c:[{v:[1,2,3]},{v:new Date(1,1,3)},{v:null}]}]}", table.ToJSon(columns_order=["t", "d", "dt"])) table.LoadData({ date(2, 3, 4): [(time(2, 3, 4), "time 2 3 4"), datetime(1, 2, 3, 4, 5, 6)], date(3, 4, 5): [] }) self.assertEqual(2, table.NumberOfRows()) self.assertEqual((cols_json + "rows:[" "{c:[{v:[2,3,4],f:'time 2 3 4'},{v:new Date(2,2,4)}," "{v:new Date(1,1,3,4,5,6)}]}," "{c:[,{v:new Date(3,3,5)},{v:null}]}]}"), table.ToJSon(columns_order=["t", "d", "dt"])) json = ("{cols:[{id:\"a'\",label:\"a'\",type:'string'}," "{id:'b',label:\"bb'\",type:'number'}]," "rows:[{c:[{v:'a1'},{v:1}]},{c:[{v:'a2'},{v:2}]}," "{c:[{v:'a3'},{v:3}]}]}") table = DataTable({"a'": ("b", "number", "bb'", {})}, { "a1": 1, "a2": 2, "a3": 3 }) self.assertEqual(3, table.NumberOfRows()) self.assertEqual(json, table.ToJSon())
def testCustomProperties(self): # The json of the initial data we load to the table. json = ( "{cols:" "[{id:'a',label:'A',type:'number',p:{'col_cp':'col_v'}}," "{id:'b',label:'b',type:'string'}," "{id:'c',label:'c',type:'boolean'}]," "rows:[" "{c:[{v:1},,{v:null,p:{'null_cp':'null_v'}}],p:{'row_cp':'row_v'}}," "{c:[,{v:'z',p:{'cell_cp':'cell_v'}},{v:true}]}," "{c:[{v:3},,{v:null}],p:{'row_cp2':'row_v2'}}]," "p:{'global_cp':'global_v'}" "}") jscode = ("var mytab = new google.visualization.DataTable();\n" "mytab.setTableProperties({'global_cp':'global_v'});\n" "mytab.addColumn('number', 'A', 'a');\n" "mytab.setColumnProperties(0, {'col_cp':'col_v'});\n" "mytab.addColumn('string', 'b', 'b');\n" "mytab.addColumn('boolean', 'c', 'c');\n" "mytab.addRows(3);\n" "mytab.setCell(0, 0, 1);\n" "mytab.setCell(0, 2, null, null, {'null_cp':'null_v'});\n" "mytab.setRowProperties(0, {'row_cp':'row_v'});\n" "mytab.setCell(1, 1, 'z', null, {'cell_cp':'cell_v'});\n" "mytab.setCell(1, 2, true);\n" "mytab.setCell(2, 0, 3);\n" "mytab.setRowProperties(2, {'row_cp2':'row_v2'});\n") table = DataTable([("a", "number", "A", { "col_cp": "col_v" }), "b", ("c", "boolean")], custom_properties={"global_cp": "global_v"}) table.AppendData([[1, None, (None, None, { "null_cp": "null_v" })]], custom_properties={"row_cp": "row_v"}) table.AppendData([[None, ("z", None, { "cell_cp": "cell_v" }), True], [3]]) table.SetRowsCustomProperties(2, {"row_cp2": "row_v2"}) self.assertEqual(json, table.ToJSon()) self.assertEqual(jscode, table.ToJSCode("mytab"))
def testCustomProperties(self): # The json of the initial data we load to the table. json_obj = { "cols": [{ "id": "a", "label": "A", "type": "number", "p": { "col_cp": "col_v" } }, { "id": "b", "label": "b", "type": "string" }, { "id": "c", "label": "c", "type": "boolean" }], "rows": [{ "c": [{ "v": 1 }, None, { "v": None, "p": { "null_cp": "null_v" } }], "p": { "row_cp": "row_v" } }, { "c": [None, { "v": "z", "p": { "cell_cp": "cell_v" } }, { "v": True }] }, { "c": [{ "v": 3 }, None, None], "p": { "row_cp2": "row_v2" } }], "p": { "global_cp": "global_v" } } jscode = ( "var mytab = new google.visualization.DataTable();\n" "mytab.setTableProperties({\"global_cp\":\"global_v\"});\n" "mytab.addColumn(\"number\", \"A\", \"a\");\n" "mytab.setColumnProperties(0, {\"col_cp\":\"col_v\"});\n" "mytab.addColumn(\"string\", \"b\", \"b\");\n" "mytab.addColumn(\"boolean\", \"c\", \"c\");\n" "mytab.addRows(3);\n" "mytab.setCell(0, 0, 1);\n" "mytab.setCell(0, 2, null, null, {\"null_cp\":\"null_v\"});\n" "mytab.setRowProperties(0, {\"row_cp\":\"row_v\"});\n" "mytab.setCell(1, 1, \"z\", null, {\"cell_cp\":\"cell_v\"});\n" "mytab.setCell(1, 2, true);\n" "mytab.setCell(2, 0, 3);\n" "mytab.setRowProperties(2, {\"row_cp2\":\"row_v2\"});\n") table = DataTable([("a", "number", "A", { "col_cp": "col_v" }), "b", ("c", "boolean")], custom_properties={"global_cp": "global_v"}) table.AppendData([[1, None, (None, None, { "null_cp": "null_v" })]], custom_properties={"row_cp": "row_v"}) table.AppendData([[None, ("z", None, { "cell_cp": "cell_v" }), True], [3]]) table.SetRowsCustomProperties(2, {"row_cp2": "row_v2"}) self.assertEqual(json.dumps(json_obj, separators=(",", ":")), table.ToJSon()) self.assertEqual(jscode, table.ToJSCode("mytab"))
def testToJSon(self): json_obj = { "cols": [{ "id": "a", "label": "A", "type": "number" }, { "id": "b", "label": "b", "type": "string" }, { "id": "c", "label": "c", "type": "boolean" }], "rows": [{ "c": [{ "v": 1 }, None, None] }, { "c": [None, { "v": "z" }, { "v": True }] }, { "c": [None, { "v": "\\u05d0" }, None] }, { "c": [None, { "v": "\\u05d1" }, None] }] } table = DataTable([("a", "number", "A"), "b", ("c", "boolean")], [[1], [None, "z", True], [None, "\\u05d0"], [None, "\\u05d1".encode("utf-8")]]) self.assertEqual(4, table.NumberOfRows()) self.assertEqual( json.dumps(json_obj, separators=(",", ":"), ensure_ascii=False).encode("utf-8"), table.ToJSon()) table.AppendData([[-1, "w", False]]) self.assertEqual(5, table.NumberOfRows()) json_obj["rows"].append({"c": [{"v": -1}, {"v": "w"}, {"v": False}]}) self.assertEqual( json.dumps(json_obj, separators=(",", ":"), ensure_ascii=False).encode("utf-8"), table.ToJSon()) json_obj = { "cols": [{ "id": "t", "label": "T", "type": "timeofday" }, { "id": "d", "label": "d", "type": "date" }, { "id": "dt", "label": "dt", "type": "datetime" }], "rows": [{ "c": [{ "v": [1, 2, 3] }, { "v": "Date(1,1,3)" }, None] }] } table = DataTable({ ("d", "date"): [("t", "timeofday", "T"), ("dt", "datetime")] }) table.LoadData({date(1, 2, 3): [time(1, 2, 3)]}) self.assertEqual(1, table.NumberOfRows()) self.assertEqual(json.dumps(json_obj, separators=(",", ":")), table.ToJSon(columns_order=["t", "d", "dt"])) json_obj["rows"] = [{ "c": [{ "v": [2, 3, 4], "f": "time 2 3 4" }, { "v": "Date(2,2,4)" }, { "v": "Date(1,1,3,4,5,6,555)" }] }, { "c": [None, { "v": "Date(3,3,5)" }, None] }] table.LoadData({ date(2, 3, 4): [(time(2, 3, 4), "time 2 3 4"), datetime(1, 2, 3, 4, 5, 6, 555000)], date(3, 4, 5): [] }) self.assertEqual(2, table.NumberOfRows()) self.assertEqual(json.dumps(json_obj, separators=(",", ":")), table.ToJSon(columns_order=["t", "d", "dt"])) json_obj = { "cols": [{ "id": "a\"", "label": "a\"", "type": "string" }, { "id": "b", "label": "bb\"", "type": "number" }], "rows": [{ "c": [{ "v": "a1" }, { "v": 1 }] }, { "c": [{ "v": "a2" }, { "v": 2 }] }, { "c": [{ "v": "a3" }, { "v": 3 }] }] } table = DataTable({"a\"": ("b", "number", "bb\"", {})}, { "a1": 1, "a2": 2, "a3": 3 }) self.assertEqual(3, table.NumberOfRows()) self.assertEqual(json.dumps(json_obj, separators=(",", ":")), table.ToJSon())