def testToJSCode(self): table = DataTable([("a", "number", "A'"), "b\"", ("c", "timeofday")], [[1], [None, "z", time(1, 2, 3)], [(2, "2$"), "w", time(2, 3, 4)]]) self.assertEqual(3, table.NumberOfRows()) self.assertEqual( ("var mytab = new google.visualization.DataTable();\n" "mytab.addColumn(\"number\", \"A'\", \"a\");\n" "mytab.addColumn(\"string\", \"b\\\"\", \"b\\\"\");\n" "mytab.addColumn(\"timeofday\", \"c\", \"c\");\n" "mytab.addRows(3);\n" "mytab.setCell(0, 0, 1);\n" "mytab.setCell(1, 1, \"z\");\n" "mytab.setCell(1, 2, [1,2,3]);\n" "mytab.setCell(2, 0, 2, \"2$\");\n" "mytab.setCell(2, 1, \"w\");\n" "mytab.setCell(2, 2, [2,3,4]);\n"), table.ToJSCode("mytab")) table = DataTable({("a", "number"): { "b": "date", "c": "datetime" }}, { 1: {}, 2: { "b": date(1, 2, 3) }, 3: { "c": datetime(1, 2, 3, 4, 5, 6, 555000) }, 4: { "c": datetime(1, 2, 3, 4, 5, 6) } }) self.assertEqual(4, table.NumberOfRows()) self.assertEqual( ("var mytab2 = new google.visualization.DataTable();\n" 'mytab2.addColumn("datetime", "c", "c");\n' 'mytab2.addColumn("date", "b", "b");\n' 'mytab2.addColumn("number", "a", "a");\n' 'mytab2.addRows(4);\n' 'mytab2.setCell(0, 2, 1);\n' 'mytab2.setCell(1, 1, new Date(1,1,3));\n' 'mytab2.setCell(1, 2, 2);\n' 'mytab2.setCell(2, 0, new Date(1,1,3,4,5,6,555));\n' 'mytab2.setCell(2, 2, 3);\n' 'mytab2.setCell(3, 0, new Date(1,1,3,4,5,6));\n' 'mytab2.setCell(3, 2, 4);\n'), table.ToJSCode("mytab2", columns_order=["c", "b", "a"]))
def testToJSCode(self): table = DataTable([("a", "number", "A"), "b", ("c", "timeofday")], [[1], [None, "z", time(1, 2, 3)], [(2, "2$"), "w", time(2, 3, 4)]]) self.assertEqual(3, table.NumberOfRows()) self.assertEqual(("var mytab = new google.visualization.DataTable();\n" "mytab.addColumn('number', 'A', 'a');\n" "mytab.addColumn('string', 'b', 'b');\n" "mytab.addColumn('timeofday', 'c', 'c');\n" "mytab.addRows(3);\n" "mytab.setCell(0, 0, 1);\n" "mytab.setCell(1, 1, 'z');\n" "mytab.setCell(1, 2, [1,2,3]);\n" "mytab.setCell(2, 0, 2, '2$');\n" "mytab.setCell(2, 1, 'w');\n" "mytab.setCell(2, 2, [2,3,4]);\n"), table.ToJSCode("mytab")) table = DataTable({("a", "number"): { "b": "date", "c": "datetime" }}, { 1: {}, 2: { "b": date(1, 2, 3) }, 3: { "c": datetime(1, 2, 3, 4, 5, 6) } }) self.assertEqual(3, table.NumberOfRows()) self.assertEqual( ("var mytab2 = new google.visualization.DataTable();\n" "mytab2.addColumn('datetime', 'c', 'c');\n" "mytab2.addColumn('date', 'b', 'b');\n" "mytab2.addColumn('number', 'a', 'a');\n" "mytab2.addRows(3);\n" "mytab2.setCell(0, 2, 1);\n" "mytab2.setCell(1, 1, new Date(1,1,3));\n" "mytab2.setCell(1, 2, 2);\n" "mytab2.setCell(2, 0, new Date(1,1,3,4,5,6));\n" "mytab2.setCell(2, 2, 3);\n"), table.ToJSCode("mytab2", columns_order=["c", "b", "a"]))
def testOrderBy(self): data = [("b", 3), ("a", 3), ("a", 2), ("b", 1)] description = ["col1", ("col2", "number", "Second Column")] table = DataTable(description, data) table_num_sorted = DataTable(description, sorted(data, key=lambda x: (x[1], x[0]))) table_str_sorted = DataTable(description, sorted(data, key=lambda x: x[0])) table_diff_sorted = DataTable( description, sorted(sorted(data, key=lambda x: x[1]), key=lambda x: x[0], reverse=True)) self.assertEqual(table_num_sorted.ToJSon(), table.ToJSon(order_by=("col2", "col1"))) self.assertEqual(table_num_sorted.ToJSCode("mytab"), table.ToJSCode("mytab", order_by=("col2", "col1"))) self.assertEqual(table_str_sorted.ToJSon(), table.ToJSon(order_by="col1")) self.assertEqual(table_str_sorted.ToJSCode("mytab"), table.ToJSCode("mytab", order_by="col1")) self.assertEqual(table_diff_sorted.ToJSon(), table.ToJSon(order_by=[("col1", "desc"), "col2"])) self.assertEqual( table_diff_sorted.ToJSCode("mytab"), table.ToJSCode("mytab", order_by=[("col1", "desc"), "col2"]))
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"))