deleteTests method

dynamic deleteTests()

Implementation

deleteTests() {
  group('Delete', () {
    late Cursor cursor;

    setUp(() async {
      connection = await connector.connect(_initDb);
      cursor = await connection.cursor();
    });

    tearDown(() async {
      await connection.close();
    });

    group('with single filters', () {
      group('equality', () {
        final collection = schema;

        test('deletes by pk equality', () async {
          await insertRow(connection, "1", "one", 1);
          await insertRow(connection, "2", "two", 2);
          await insertRow(connection, "3", "three", 3);

          var filter = SingleFilter(
            left: Property("id"),
            operator: Operator.equals,
            right: StringValue("1"),
          );

          await cursor.execute(Delete(from: collection, where: filter));

          var op = Query(
            from: collection,
          );
          await cursor.execute(op);
          final res = await cursor.fetchall();
          expect(res.length, 2);
        });

        test('deletes by value equality', () async {
          await insertRow(connection, "1", "one", 1);
          await insertRow(connection, "2", "two", 2);
          await insertRow(connection, "3", "three", 3);

          var filter = SingleFilter(
            left: Property("age"),
            operator: Operator.equals,
            right: NumericValue(2),
          );

          await cursor.execute(Delete(from: collection, where: filter));

          var op = Query(
            from: collection,
          );
          await cursor.execute(op);
          final res = await cursor.fetchall();
          expect(res.length, 2);
          expect(res.map((e) => e["id"]), unorderedEquals(["1", "3"]));
        });

        test('deletes nothing if no match', () async {
          await insertRow(connection, "1", "one", 1);
          await insertRow(connection, "2", "two", 2);
          await insertRow(connection, "3", "three", 3);

          var filter = SingleFilter(
            left: Property("id"),
            operator: Operator.equals,
            right: StringValue("4"),
          );

          await cursor.execute(Delete(from: collection, where: filter));

          var op = Query(
            from: collection,
          );
          await cursor.execute(op);
          final res = await cursor.fetchall();
          expect(res.length, 3);
        });
      }, skip: requireFeature(DatabaseFeature.equalityDelete));

      group('comparison', () {
        final collection = schema;

        test('deletes by pk comparsion', () async {
          await insertRow(connection, "1", "one", 1);
          await insertRow(connection, "2", "two", 2);
          await insertRow(connection, "3", "three", 3);

          var filter = SingleFilter(
            left: Property("id"),
            operator: Operator.greaterThan,
            right: StringValue("1"),
          );

          await cursor.execute(Delete(from: collection, where: filter));

          var op = Query(
            from: collection,
          );
          await cursor.execute(op);
          final res = await cursor.fetchall();
          expect(res.length, 1);
          expect(res[0]["id"], "1");
        });

        test('deletes by value comparsion', () async {
          await insertRow(connection, "1", "one", 1);
          await insertRow(connection, "2", "two", 2);
          await insertRow(connection, "3", "three", 3);

          var filter = SingleFilter(
            left: Property("age"),
            operator: Operator.lessThanOrEqual,
            right: NumericValue(2),
          );

          await cursor.execute(Delete(from: collection, where: filter));

          var op = Query(
            from: collection,
          );
          await cursor.execute(op);
          final res = await cursor.fetchall();
          expect(res.length, 1);
          expect(res[0]["id"], "3");
        });
      }, skip: requireFeature(DatabaseFeature.comparisonDelete));
    });

    group('with combined filters', () {
      test('complex combined filters', () async {
        await insertRow(connection, "a", "a", 1);
        await insertRow(connection, "b", "b", 2);
        await insertRow(connection, "c", "c", 3);
        await insertRow(connection, "d", "d", 4);
        await insertRow(connection, "e", "e", 5);
        await insertRow(connection, "f", "f", 6);

        var op = Delete(
          from: schema,
          where: NotCondition(
            // Negate - select all leaving 4
            not: CombinedFilter(
              combinator: Combinator.and,
              conditions: [
                // age > 1 and !age >= 5 -> n > 1 and n < 5
                // exclude 1, 5 and 6
                CombinedFilter(
                  combinator: Combinator.and,
                  conditions: [
                    SingleFilter(
                      left: Property("age"),
                      operator: Operator.greaterThan,
                      right: NumericValue(1),
                    ),
                    NotCondition(
                      not: SingleFilter(
                        left: Property("age"),
                        operator: Operator.greaterThanOrEqual,
                        right: NumericValue(5),
                      ),
                    ),
                  ],
                ),
                // !(age = 2 or age = 3) -> age != b and age != 3
                // exclude 2 and 3
                NotCondition(
                  not: CombinedFilter(
                    combinator: Combinator.or,
                    conditions: [
                      SingleFilter(
                        left: Property("age"),
                        operator: Operator.equals,
                        right: NumericValue(2),
                      ),
                      SingleFilter(
                        left: Property("age"),
                        operator: Operator.equals,
                        right: NumericValue(3),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ),
        );

        await cursor.execute(op);

        await cursor.execute(Query(from: schema));
        final res = await cursor.fetchall();

        expect(res.length, 1);
        expect(res[0]["age"], 4);
      });
    }, skip: requireFeature(DatabaseFeature.complexDelete));
  });
}