protected Task VerifyUpdate(Action <NodaTimeTypes> mutator, [CallerFilePath] string sourceFile = "")
 {
     SqlRecording.StartRecording();
     mutator(Db.NodaTimeTypes.Single());
     Db.SaveChanges();
     return(Verifier.Verify(SqlRecording.FinishRecording(), sourceFile: sourceFile));
 }
Beispiel #2
0
    public async Task DeleteDatabaseWithOpenConnection()
    {
        var name             = "ToDelete";
        var connectionString = await instance.CreateDatabaseFromTemplate(name);

        await using SqlConnection connection = new(connectionString);
        await connection.OpenAsync();

        await instance.DeleteDatabase(name);

        var deletedState = await instance.ReadDatabaseState(name);

        SqlRecording.StartRecording();
        await instance.CreateDatabaseFromTemplate(name);

        var entries = SqlRecording.FinishRecording();

        var createdState = await instance.ReadDatabaseState(name);

        await Verifier.Verify(new
        {
            entries,
            deletedState,
            createdState
        });
    }
Beispiel #3
0
    public async Task RecordingSpecific()
    {
        await using var database = await sqlInstance.Build();

        var connectionString = database.ConnectionString;

        #region RecordingSpecific

        await using SqlConnection connection = new(connectionString);
        await connection.OpenAsync();

        SqlRecording.StartRecording();
        await using var command = connection.CreateCommand();
        command.CommandText     = "select Value from MyTable";
        var value = await command.ExecuteScalarAsync();

        var entries = SqlRecording.FinishRecording();
        //TODO: optionally filter the results
        await Verifier.Verify(new
        {
            value,
            sql = entries
        });

        #endregion
    }
        protected Task VerifyMethod(Expression <Func <T, T> > selector, [CallerFilePath] string sourceFile = "")
        {
            SqlRecording.StartRecording();
            var value = Query.Select(selector).Single();

            return(Verifier.Verify(value, sourceFile: sourceFile));
        }
Beispiel #5
0
    public async Task Recording()
    {
        await using var database = await sqlInstance.Build();

        SqlRecording.StartRecording();
        var value = MethodUnderTest(database.Connection);
        await Verifier.Verify(value);
    }
Beispiel #6
0
    public async Task DeleteDatabase()
    {
        await instance.CreateDatabaseFromTemplate("ToDelete");

        SqlRecording.StartRecording();
        await instance.DeleteDatabase("ToDelete");

        var entries = SqlRecording.FinishRecording();
        await Verifier.Verify(entries);
    }
Beispiel #7
0
    public async Task WithRebuild()
    {
        Wrapper instance2 = new(s => new SqlConnection(s), "WrapperTests", DirectoryFinder.Find("WrapperTests"));

        SqlRecording.StartRecording();
        instance2.Start(timestamp, _ => throw new());
        await instance2.AwaitStart();

        var entries = SqlRecording.FinishRecording();
        await Verifier.Verify(entries);
    }
Beispiel #8
0
    public async Task CreateDatabase()
    {
        SqlRecording.StartRecording();
        await instance.CreateDatabaseFromTemplate("CreateDatabase");

        var entries = SqlRecording.FinishRecording();
        await Verifier.Verify(
            new
        {
            entries,
            state = await instance.ReadDatabaseState("CreateDatabase")
        });
    }
Beispiel #9
0
    public async Task Recording()
    {
        await using var database = await sqlInstance.Build();

        var connectionString = database.ConnectionString;

        #region Recording

        await using SqlConnection connection = new(connectionString);
        await connection.OpenAsync();

        SqlRecording.StartRecording();
        await using var command = connection.CreateCommand();
        command.CommandText     = "select Value from MyTable";
        var value = await command.ExecuteScalarAsync();

        await Verifier.Verify(value);

        #endregion
    }
Beispiel #10
0
    public async Task RecordingError()
    {
        await using var database = await sqlInstance.Build();

        await using SqlConnection connection = new(database.ConnectionString);
        await connection.OpenAsync();

        SqlRecording.StartRecording();
        await using var command = connection.CreateCommand();
        command.CommandText     = "select * from MyTabl2e";
        try
        {
            await using var dataReader = await command.ExecuteReaderAsync();
        }
        catch
        {
        }

        var commands = SqlRecording.FinishRecording();
        await Verifier.Verify(commands)
        .ScrubLinesContaining("HelpLink.ProdVer");
    }
Beispiel #11
0
    static async Task RunQuery(
        SqlDatabase <IntegrationDbContext> database,
        string query,
        Inputs?inputs,
        Filters?filters,
        bool disableTracking,
        object[] entities,
        [CallerFilePath] string sourceFile = "")
    {
        var dbContext = database.Context;

        dbContext.AddRange(entities);
        await dbContext.SaveChangesAsync();

        var services = new ServiceCollection();

        services.AddSingleton <Query>();
        services.AddSingleton <Mutation>();
        services.AddSingleton(database.Context);
        foreach (var type in GetGraphQlTypes())
        {
            services.AddSingleton(type);
        }

        await using var context = database.NewDbContext();
        SqlRecording.StartRecording();
        try
        {
            var result = await QueryExecutor.ExecuteQuery(query, services, context, inputs, filters, disableTracking);
            await Verify(result, sourceFile : sourceFile).ScrubInlineGuids();
        }
        catch (ExecutionError executionError)
        {
            await Verify(executionError.Message, sourceFile : sourceFile);
        }
    }
    public async Task Connection_parent_child()
    {
        var          query   = @"
{
  parentEntitiesConnection(first:2, after: '0') {
    totalCount
    edges {
      cursor
      node {
        property
        children
        {
          property
        }
      }
    }
    items {
      property
      children
      {
        property
      }
    }
  }
}
";
        ParentEntity entity1 = new()
        {
            Property = "Value1"
        };
        ChildEntity entity2 = new()
        {
            Property = "Value2"
        };
        ChildEntity entity3 = new()
        {
            Property = "Value3"
        };

        entity1.Children.Add(entity2);
        entity1.Children.Add(entity3);
        ParentEntity entity4 = new()
        {
            Property = "Value4"
        };
        ChildEntity entity5 = new()
        {
            Property = "Value5"
        };

        entity4.Children.Add(entity5);

        await using var database = await sqlInstance.Build();
        await RunQuery(database, query, null, null, false, new object[] { entity1, entity2, entity3, entity4, entity5 });
    }

    [Fact]
    public async Task Child_parent_with_alias()
    {
        var query = @"
{
  childEntities (orderBy: {path: ""property""})
  {
    parentAlias
    {
      property
    }
  }
}";

        ParentEntity entity1 = new()
        {
            Property = "Value1"
        };
        ChildEntity entity2 = new()
        {
            Property = "Value2",
            Parent   = entity1
        };
        ChildEntity entity3 = new()
        {
            Property = "Value3",
            Parent   = entity1
        };

        entity1.Children.Add(entity2);
        entity1.Children.Add(entity3);
        ParentEntity entity4 = new()
        {
            Property = "Value4"
        };
        ChildEntity entity5 = new()
        {
            Property = "Value5",
            Parent   = entity4
        };

        entity4.Children.Add(entity5);

        await using var database = await sqlInstance.Build();
        await RunQuery(database, query, null, null, false, new object[] { entity1, entity2, entity3, entity4, entity5 });
    }

    [Fact(Skip = "TODO")]
    public async Task Multiple_nested_AddQueryField()
    {
        var query = @"
{
  queryFieldWithInclude
  {
    includeNonQueryableB
    {
      id
    }
  }
}";
        IncludeNonQueryableA level2 = new();
        IncludeNonQueryableB level1 = new()
        {
            IncludeNonQueryableA = level2,
        };

        level1.IncludeNonQueryableA = level2;

        await using var database = await sqlInstance.Build();
        await RunQuery(database, query, null, null, false, new object[] { level1, level2 });
    }

    [Fact(Skip = "fix order")]
    public async Task Skip_level()
    {
        var query = @"
{
  skipLevel
  {
    level3Entity
    {
      property
    }
  }
}";

        Level3Entity level3 = new()
        {
            Property = "Value"
        };
        Level2Entity level2 = new()
        {
            Level3Entity = level3
        };
        Level1Entity level1 = new()
        {
            Level2Entity = level2
        };

        var database = await sqlInstance.Build();

        await RunQuery(database, query, null, null, false, new object[] { level1, level2, level3 });
    }

    [Fact]
    public async Task Multiple_nested()
    {
        var query = @"
{
  level1Entities
  {
    level2Entity
    {
      level3Entity
      {
        property
      }
    }
  }
}";

        Level3Entity level3 = new()
        {
            Property = "Value"
        };
        Level2Entity level2 = new()
        {
            Level3Entity = level3
        };
        Level1Entity level1 = new()
        {
            Level2Entity = level2
        };

        await using var database = await sqlInstance.Build();
        await RunQuery(database, query, null, null, false, new object[] { level1, level2, level3 });
    }

    [Fact]
    public async Task Null_on_nested()
    {
        var query = @"
{
  level1Entities(where: {path: 'Level2Entity.Level3EntityId', comparison: equal, value: '00000000-0000-0000-0000-000000000003'})
  {
    level2Entity
    {
      level3Entity
      {
        property
      }
    }
  }
}";

        Level3Entity level3a = new()
        {
            Id       = Guid.Parse("00000000-0000-0000-0000-000000000003"),
            Property = "Valuea"
        };
        Level2Entity level2a = new()
        {
            Level3Entity = level3a
        };
        Level1Entity level1a = new()
        {
            Level2Entity = level2a
        };

        Level2Entity level2b = new();
        Level1Entity level1b = new()
        {
            Level2Entity = level2b
        };

        await using var database = await sqlInstance.Build();
        await RunQuery(database, query, null, null, false, new object[] { level1b, level2b, level1a, level2a, level3a });
    }

    [Fact]
    public async Task Query_Cyclic()
    {
        var query = @"
{
  childEntities (orderBy: {path: ""property""})
  {
    property
    parent
    {
      property
      children
      {
        property
        parent
        {
          property
        }
      }
    }
  }
}";

        ParentEntity entity1 = new()
        {
            Property = "Value1"
        };
        ChildEntity entity2 = new()
        {
            Property = "Value2",
            Parent   = entity1
        };
        ChildEntity entity3 = new()
        {
            Property = "Value3",
            Parent   = entity1
        };

        entity1.Children.Add(entity2);
        entity1.Children.Add(entity3);
        ParentEntity entity4 = new()
        {
            Property = "Value4"
        };
        ChildEntity entity5 = new()
        {
            Property = "Value5",
            Parent   = entity4
        };

        entity4.Children.Add(entity5);

        await using var database = await sqlInstance.Build();
        await RunQuery(database, query, null, null, false, new object[] { entity1, entity2, entity3, entity4, entity5 });
    }

    [Fact]
    public async Task Query_NoTracking()
    {
        var query = @"
{
  childEntities (orderBy: {path: ""property""})
  {
    property
    parent
    {
      property
    }
  }
}";

        ParentEntity entity1 = new()
        {
            Property = "Value1"
        };
        ChildEntity entity2 = new()
        {
            Property = "Value2",
            Parent   = entity1
        };
        ChildEntity entity3 = new()
        {
            Property = "Value3",
            Parent   = entity1
        };

        entity1.Children.Add(entity2);
        entity1.Children.Add(entity3);
        ParentEntity entity4 = new()
        {
            Property = "Value4"
        };
        ChildEntity entity5 = new()
        {
            Property = "Value5",
            Parent   = entity4
        };

        entity4.Children.Add(entity5);

        await using var database = await sqlInstance.Build();
        await RunQuery(database, query, null, null, true, new object[] { entity1, entity2, entity3, entity4, entity5 });
    }

    [Fact]
    public async Task Child_parent()
    {
        var query = @"
{
  childEntities (orderBy: {path: ""property""})
  {
    property
    parent
    {
      property
    }
  }
}";

        ParentEntity entity1 = new()
        {
            Property = "Value1"
        };
        ChildEntity entity2 = new()
        {
            Property = "Value2",
            Parent   = entity1
        };
        ChildEntity entity3 = new()
        {
            Property = "Value3",
            Parent   = entity1
        };

        entity1.Children.Add(entity2);
        entity1.Children.Add(entity3);
        ParentEntity entity4 = new()
        {
            Property = "Value4"
        };
        ChildEntity entity5 = new()
        {
            Property = "Value5",
            Parent   = entity4
        };

        entity4.Children.Add(entity5);

        await using var database = await sqlInstance.Build();
        await RunQuery(database, query, null, null, false, new object[] { entity1, entity2, entity3, entity4, entity5 });
    }

    [Fact]
    public async Task With_null_navigation_property()
    {
        var query = @"
{
  childEntities(where: {path: 'ParentId', comparison: equal, value: '00000000-0000-0000-0000-000000000001'}, orderBy: {path: ""property""})
  {
    property
    parent
    {
      property
    }
  }
}";

        ParentEntity entity1 = new()
        {
            Id       = Guid.Parse("00000000-0000-0000-0000-000000000001"),
            Property = "Value1"
        };
        ChildEntity entity2 = new()
        {
            Property = "Value2",
            Parent   = entity1
        };
        ChildEntity entity3 = new()
        {
            Property = "Value3",
            Parent   = entity1
        };

        entity1.Children.Add(entity2);
        entity1.Children.Add(entity3);
        ChildEntity entity5 = new()
        {
            Property = "Value5"
        };

        await using var database = await sqlInstance.Build();
        await RunQuery(database, query, null, null, false, new object[] { entity1, entity2, entity3, entity5 });
    }

    [Fact(Skip = "fix order")]
    public async Task MisNamedQuery()
    {
        var query = @"
{
  misNamed
  {
    misNamedChildren
    {
      id
    }
  }
}";

        WithMisNamedQueryParentEntity entity1 = new();
        WithMisNamedQueryChildEntity  entity2 = new()
        {
            Parent = entity1
        };
        WithMisNamedQueryChildEntity entity3 = new()
        {
            Parent = entity1
        };

        entity1.Children.Add(entity2);
        entity1.Children.Add(entity3);
        WithMisNamedQueryParentEntity entity4 = new();
        WithMisNamedQueryChildEntity  entity5 = new()
        {
            Parent = entity4
        };

        entity4.Children.Add(entity5);

        await using var database = await sqlInstance.Build();
        await RunQuery(database, query, null, null, false, new object[] { entity1, entity2, entity3, entity4, entity5 });
    }

    [Fact(Skip = "fix order")]
    public async Task Parent_child()
    {
        var query = @"
{
  parentEntities
  {
    property
    children
    {
      property
    }
  }
}";

        ParentEntity entity1 = new()
        {
            Property = "Value1"
        };
        ChildEntity entity2 = new()
        {
            Property = "Value2",
            Parent   = entity1
        };
        ChildEntity entity3 = new()
        {
            Property = "Value3",
            Parent   = entity1
        };

        entity1.Children.Add(entity2);
        entity1.Children.Add(entity3);
        ParentEntity entity4 = new()
        {
            Property = "Value4"
        };
        ChildEntity entity5 = new()
        {
            Property = "Value5",
            Parent   = entity4
        };

        entity4.Children.Add(entity5);

        await using var database = await sqlInstance.Build();
        await RunQuery(database, query, null, null, false, new object[] { entity1, entity2, entity3, entity4, entity5 });
    }

    [Fact]
    public async Task Many_children()
    {
        var query = @"
{
  manyChildren
  {
    child1
    {
      id
    }
  }
}";

        WithManyChildrenEntity parent = new();
        Child1Entity           child1 = new()
        {
            Parent = parent
        };
        Child2Entity child2 = new()
        {
            Parent = parent
        };

        parent.Child1 = child1;
        parent.Child2 = child2;

        await using var database = await sqlInstance.Build();
        await RunQuery(database, query, null, null, false, new object[] { parent, child1, child2 });
    }

    [Fact(Skip = "Broke with gql 4")]
    public async Task InheritedEntityInterface()
    {
        var query = @"
{
  interfaceGraphConnection {
    items {
      ...inheritedEntityFields
    }
  }
}
fragment inheritedEntityFields on Interface {
  property
  childrenFromInterface(orderBy: {path: ""property""})
  {
    items {
      ...childEntityFields
    }
  }
  ... on DerivedWithNavigation {
    childrenFromDerived(orderBy: {path: ""property""})
    {
      items {
        ...childEntityFields
      }
    }
  }
}
fragment childEntityFields on DerivedChild {
  property
}";

        DerivedEntity derivedEntity1 = new()
        {
            Id       = Guid.Parse("00000000-0000-0000-0000-000000000001"),
            Property = "Value1"
        };
        DerivedChildEntity childEntity1 = new()
        {
            Property = "Value2",
            Parent   = derivedEntity1
        };
        DerivedChildEntity childEntity2 = new()
        {
            Property = "Value3",
            Parent   = derivedEntity1
        };

        derivedEntity1.ChildrenFromBase.Add(childEntity1);
        derivedEntity1.ChildrenFromBase.Add(childEntity2);

        DerivedWithNavigationEntity derivedEntity2 = new()
        {
            Id       = Guid.Parse("00000000-0000-0000-0000-000000000002"),
            Property = "Value4"
        };
        DerivedChildEntity childEntity3 = new()
        {
            Property = "Value5",
            Parent   = derivedEntity2
        };
        DerivedChildEntity childEntity4 = new()
        {
            Property    = "Value6",
            TypedParent = derivedEntity2
        };

        derivedEntity2.ChildrenFromBase.Add(childEntity3);
        derivedEntity2.Children.Add(childEntity4);

        await using var database = await sqlInstance.Build();
        await RunQuery(database, query, null, null, false, new object[] { derivedEntity1, childEntity1, childEntity2, derivedEntity2, childEntity3, childEntity4 });
    }

    [Fact]
    public async Task ManyToManyRightWhereAndInclude()
    {
        var query = @"
{
  manyToManyLeftEntities (where: {path: 'rights[rightName]', comparison: equal, value: ""Right2""})
  {
    leftName
    rights
    {
      rightName
    }
  }
}";

        ManyToManyMiddleEntity middle11 = new()
        {
            ManyToManyLeftEntity = new()
            {
                Id       = "Left1Id",
                LeftName = "Left1"
            },
            ManyToManyRightEntity = new()
            {
                Id        = "Right1Id",
                RightName = "Right1"
            }
        };

        ManyToManyMiddleEntity middle12 = new()
        {
            ManyToManyLeftEntity  = middle11.ManyToManyLeftEntity,
            ManyToManyRightEntity = new()
            {
                Id        = "Right2Id",
                RightName = "Right2"
            }
        };

        ManyToManyMiddleEntity middle21 = new()
        {
            ManyToManyLeftEntity = new()
            {
                Id       = "Left2Id",
                LeftName = "Left2"
            },
            ManyToManyRightEntity = middle11.ManyToManyRightEntity
        };

        await using var database = await sqlInstance.Build();
        await RunQuery(database, query, null, null, false, new object[] { middle11, middle12, middle21 });
    }

    [Fact]
    public async Task ManyToManyLeftWhereAndInclude()
    {
        var query = @"
{
  manyToManyRightEntities (where: {path: 'lefts[leftName]', comparison: equal, value: ""Left2""})
  {
    rightName
    lefts
    {
      leftName
    }
  }
}";

        ManyToManyMiddleEntity middle11 = new()
        {
            ManyToManyLeftEntity = new()
            {
                Id       = "Left1Id",
                LeftName = "Left1"
            },
            ManyToManyRightEntity = new()
            {
                Id        = "Right1Id",
                RightName = "Right1"
            }
        };

        ManyToManyMiddleEntity middle12 = new()
        {
            ManyToManyLeftEntity  = middle11.ManyToManyLeftEntity,
            ManyToManyRightEntity = new()
            {
                Id        = "Right2Id",
                RightName = "Right2"
            }
        };

        ManyToManyMiddleEntity middle21 = new()
        {
            ManyToManyLeftEntity = new()
            {
                Id       = "Left2Id",
                LeftName = "Left2"
            },
            ManyToManyRightEntity = middle11.ManyToManyRightEntity
        };

        await using var database = await sqlInstance.Build();
        await RunQuery(database, query, null, null, false, new object[] { middle11, middle12, middle21 });
    }

    static async Task RunQuery(
        SqlDatabase <IntegrationDbContext> database,
        string query,
        Inputs?inputs,
        Filters?filters,
        bool disableTracking,
        object[] entities,
        [CallerFilePath] string sourceFile = "")
    {
        var dbContext = database.Context;

        dbContext.AddRange(entities);
        await dbContext.SaveChangesAsync();

        ServiceCollection services = new();

        services.AddSingleton <Query>();
        services.AddSingleton <Mutation>();
        services.AddSingleton(database.Context);
        foreach (var type in GetGraphQlTypes())
        {
            services.AddSingleton(type);
        }

        SqlRecording.StartRecording();
        await using var context = database.NewDbContext();
        try
        {
            var result = await QueryExecutor.ExecuteQuery(query, services, context, inputs, filters, disableTracking);

            await Verifier.Verify(result, sourceFile : sourceFile).ScrubInlineGuids();
        }
        catch (ExecutionError executionError)
        {
            await Verifier.Verify(executionError.Message, sourceFile : sourceFile);
        }
    }

    static IEnumerable <Type> GetGraphQlTypes()
    {
        return(typeof(IntegrationTests).Assembly
               .GetTypes()
               .Where(x => !x.IsAbstract && typeof(GraphType).IsAssignableFrom(x)));
    }
}
 protected Task VerifyQuery(Expression <Func <T, bool> > predicate, [CallerFilePath] string sourceFile = "")
 {
     SqlRecording.StartRecording();
     _ = Query.Single(predicate);
     return(Verifier.Verify(SqlRecording.FinishRecording(), sourceFile: sourceFile));
 }