public static async Task TestMinimalSelects(IDatabase database) { await DatabaseUnitTest.TruncateData(database); using (DynamicViewSet dynamicViewSet = new DynamicViewSet(database, listener: dataEventTransaction => { })) { DynamicView departmentDynamicView = dynamicViewSet.CreateDynamicView("department"); DynamicView employeeDynamicView = dynamicViewSet.CreateDynamicView("employee"); await dynamicViewSet.StartAsync(); await Task.Delay(200); int preSelectCount = database.SelectCount; await database.InsertAndCommitAsync <string>("employee", new { name = "Joe Sales", department_id = 1, }); await Task.Delay(200); // Should require doing two SELECTs to handle this INSERT (before and after SELECT) Assert.AreEqual(preSelectCount + 1, database.SelectCount); } }
public static async Task TestInsertUpdateDeleteEvents(IDatabase database, object salesDepartmentId, string selectSourceSql, string updateField, object updateValue, int initialCount, int insertCount, int updateCount, int deleteCount, string[] keyFieldNames = null) { List <DataEventTransaction> dataEventTransactionCollector = new List <DataEventTransaction>(); using (DynamicViewSet dynamicViewSet = new DynamicViewSet(database, listener: dataEventTransaction => { dataEventTransactionCollector.Add(dataEventTransaction); })) { dataEventTransactionCollector.Clear(); DynamicView employeeDynamicView = dynamicViewSet.CreateDynamicView(selectSourceSql, name: "xyz", keyFieldNames: keyFieldNames); await dynamicViewSet.StartAsync(); Assert.AreEqual(1, dataEventTransactionCollector.Count); Assert.AreEqual(initialCount, dataEventTransactionCollector[0].dataEvents.Length); Assert.AreEqual(DataEventType.InitialBegin, dataEventTransactionCollector[0].dataEvents[0].dataEventType); Assert.AreEqual(DataEventType.Initial, dataEventTransactionCollector[0].dataEvents[1].dataEventType); Assert.AreEqual(DataEventType.Initial, dataEventTransactionCollector[0].dataEvents[2].dataEventType); Assert.AreEqual(DataEventType.Initial, dataEventTransactionCollector[0].dataEvents[3].dataEventType); Assert.AreEqual(DataEventType.InitialEnd, dataEventTransactionCollector[0].dataEvents[dataEventTransactionCollector[0].dataEvents.Length - 1].dataEventType); // Confirm that an insert event is created dataEventTransactionCollector.Clear(); string joeSalesEmployeeId; using (ITransaction transaction = await database.BeginTransactionAsync()) { // Add Joe Sales employee joeSalesEmployeeId = await transaction.InsertAsync <string>("employee", new { name = "Joe Sales", department_id = salesDepartmentId, }); await transaction.CommitAsync(); } await Task.Delay(500); Assert.AreEqual(insertCount, dataEventTransactionCollector.Count); if (insertCount > 0) { Assert.AreEqual(1, dataEventTransactionCollector[0].dataEvents.Length); Assert.AreEqual(DataEventType.Insert, dataEventTransactionCollector[0].dataEvents[0].dataEventType); } // Confirm that an update event is created dataEventTransactionCollector.Clear(); using (ITransaction transaction = await database.BeginTransactionAsync()) { // Update Joe Sales employee await transaction.UpdateAsync($"UPDATE employee SET {updateField}=@{updateField} WHERE id=@id", new Dict { ["id"] = joeSalesEmployeeId, [updateField] = updateValue }); await transaction.CommitAsync(); } await Task.Delay(200); Assert.AreEqual(updateCount, dataEventTransactionCollector.Count); if (updateCount > 0) { Assert.AreEqual(1, dataEventTransactionCollector[0].dataEvents.Length); Assert.AreEqual(DataEventType.Update, dataEventTransactionCollector[0].dataEvents[0].dataEventType); Assert.AreEqual(updateValue, (dataEventTransactionCollector[0].dataEvents[0] as RecordDataEvent).record[updateField]); } // Confirm that a delete event is created dataEventTransactionCollector.Clear(); using (ITransaction transaction = await database.BeginTransactionAsync()) { // Delete Joe Sales employee await transaction.DeleteAsync("DELETE FROM employee WHERE id=@id", new { id = joeSalesEmployeeId, }); await transaction.CommitAsync(); } await Task.Delay(200); Assert.AreEqual(deleteCount, dataEventTransactionCollector.Count); if (deleteCount > 0) { Assert.AreEqual(1, dataEventTransactionCollector[0].dataEvents.Length); Assert.AreEqual(DataEventType.Delete, dataEventTransactionCollector[0].dataEvents[0].dataEventType); } } }
static async Task Run(IDatabase database) { // Create sample data Console.WriteLine("Creating sample data..."); string spongebobId, patrickId; string todo1Id, todo2Id, todo3Id, todo4Id; using (var transaction = await database.BeginTransactionAsync()) { await transaction.TruncateAsync("user"); await transaction.TruncateAsync("todo"); // Create Spongebob and Patrick user records spongebobId = await transaction.InsertAsync <string>("user", new { name = "Spongebob", }); patrickId = await transaction.InsertAsync <string>("user", new { name = "Patrick", }); // Create some todo records todo1Id = await transaction.InsertAsync <string>("todo", new { name = "Todo #1", user_id = spongebobId, is_done = "N", }); todo2Id = await transaction.InsertAsync <string>("todo", new { name = "Todo #2", user_id = spongebobId, is_done = "N", }); todo3Id = await transaction.InsertAsync <string>("todo", new { name = "Todo #3", user_id = spongebobId, is_done = "Y", }); todo4Id = await transaction.InsertAsync <string>("todo", new { name = "Todo #4", user_id = patrickId, is_done = "N", }); // Don't forget to commit the changes await transaction.CommitAsync(); } // Create a DynamicViewSet that... // - Uses a SELECT statement to define a resultset joining the todo and user tables filtered by isDone="N" // - Echoes all data events to the Console // Note that all the initial matching records are echoed to the Console as Initial events. using (DynamicViewSet dynamicViewSet = await CreateDynamicViewSet(database, "N")) { // This will echo an Insert data event to the Console because it matches the SELECT criteria Console.WriteLine("Inserting Task #5..."); await database.InsertAndCommitAsync <string>("todo", new { name = "Task #5", user_id = spongebobId, is_done = "N", }); await Task.Delay(2500); // This will NOT echo an Insert data event to the Console because it does NOT match the SELECT criteria Console.WriteLine("Inserting Task #6..."); await database.InsertAndCommitAsync <string>("todo", new { name = "Task #6", user_id = spongebobId, is_done = "Y", }); await Task.Delay(2500); // This will echo an Update data event to the Console because it matches the SELECT criteria Console.WriteLine("Updating task name to 'Updated Task #1'..."); await database.UpdateAndCommitAsync("todo", new { id = todo1Id, name = "Updated Task #1" }); await Task.Delay(2500); // This will echo multiple Update data events to the Console because it impacts multiple records in the resultset // Yes, that's pretty cool :) Console.WriteLine("Updating user name to 'Mr. Spongebob'"); await database.UpdateAndCommitAsync("user", new { id = spongebobId, name = "Mr. Spongebob" }); await Task.Delay(2500); } }