Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        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);
                }
            }
        }