public void TestSimple() { var grid = new HaystackGrid(); grid.AddColumn("id"); grid.AddColumn("dis"); grid.AddColumn("area"); grid.AddRow(new HaystackReference("a"), new HaystackString("Alpha"), new HaystackNumber(1200)); grid.AddRow(new HaystackReference("b"), null, new HaystackNumber(1400)); //.Meta Assert.AreEqual(grid.Meta.Count, 0); // cols //HCol c; Assert.AreEqual(grid.ColumnCount, 3); VerifyCol(grid, 0, "id"); VerifyCol(grid, 1, "dis"); VerifyCol(grid, 2, "area"); // rows Assert.AreEqual(grid.RowCount, 2); Assert.IsFalse(grid.IsEmpty()); var row = grid.Row(0); Assert.IsTrue(row.Get("id").Equals(new HaystackReference("a"))); Assert.IsTrue(row.Get("dis").Equals(new HaystackString("Alpha"))); Assert.IsTrue(row.Get("area").Equals(new HaystackNumber(1200))); row = grid.Row(1); Assert.IsTrue(row.Get("id").Equals(new HaystackReference("b"))); Assert.IsFalse(row.ContainsKey("dis")); Assert.IsTrue(row.Get("area").Equals(new HaystackNumber(1400))); Assert.ThrowsException <HaystackUnknownNameException>(() => row["dis"]); Assert.IsFalse(row.ContainsKey("fooBar")); Assert.ThrowsException <HaystackUnknownNameException>(() => row["fooBar"]); // HaystackRow no-nulls HaystackRow it = grid.Row(0); Assert.IsFalse(it.Count > 3); VerifyRowIterator(it, 0, "id", new HaystackReference("a")); VerifyRowIterator(it, 1, "dis", new HaystackString("Alpha")); VerifyRowIterator(it, 2, "area", new HaystackNumber(1200)); // HaystackRow with nulls it = grid.Row(1); Assert.IsFalse(it.Count > 3); VerifyRowIterator(it, 0, "id", new HaystackReference("b")); VerifyRowIterator(it, 2, "area", new HaystackNumber(1400)); // iterating VerifyGridIterator(grid); }
void VerifyGridEquals(HaystackGrid grid, HaystackDictionary meta, object[] cols, HaystackValue[][] rows) { // meta Assert.IsTrue(grid.Meta.Equals(meta)); // cols Assert.AreEqual(grid.ColumnCount, cols.Length / 2); for (int i = 0; i < grid.ColumnCount; ++i) { Assert.AreEqual(grid.Column(i).Name, cols[i * 2 + 0]); Assert.IsTrue(grid.Column(i).Meta.Equals(cols[i * 2 + 1])); } // rows Assert.AreEqual(grid.RowCount, rows.Length); for (int ri = 0; ri < rows.Length; ++ri) { var expected = rows[ri]; var actual = grid.Row(ri); for (int ci = 0; ci < expected.Length; ++ci) { Assert.AreEqual(expected[ci], actual[grid.Column(ci).Name]); } } }
public void WriteGrid(HaystackGrid grid) { // meta WriteValue("ver:\"").WriteValue(_version).WriteValue(".0\"").WriteMeta(grid.Meta).WriteNewline(); // cols if (grid.ColumnCount == 0) { // technically this shoudl be illegal, but // for robustness handle it here throw new ArgumentException("Grid has no cols", "grid"); } else { for (int i = 0; i < grid.ColumnCount; ++i) { if (i > 0) { WriteValue(','); } WriteColumn(grid.Column(i)); } } WriteNewline(); // rows for (int i = 0; i < grid.RowCount; ++i) { WriteRow(grid, grid.Row(i)); WriteNewline(); } }
/// <summary> /// Read any one record that matches a given filter. /// If no records apply an exception is thrown. /// </summary> /// <param name="filter">Record filter.</param> /// <returns>Matching record.</returns> public async Task <HaystackDictionary> ReadAsync(string filter) { HaystackGrid grid = await ReadAllAsync(filter, 1); if (grid.RowCount > 0) { return(grid.Row(0)); } throw new Exception($"Record not found for: {filter}"); }
/// <summary> /// Read a single record by its unique ID. /// Throws an exception if the record was not found. /// </summary> /// <param name="id">Record ID.</param> /// <returns>Matching record.</returns> public async Task <HaystackDictionary> ReadByIdAsync(HaystackReference id) { HaystackGrid res = await ReadByIdsAsync(new HaystackReference[] { id }); if (res.IsEmpty()) { throw new Exception($"Record not found for: {id}"); } HaystackDictionary rec = res.Row(0); if (!rec.ContainsKey("id")) { throw new Exception($"Record not found for: {id}"); } return(rec); }