AgainstWhiteSpace() public static méthode

public static AgainstWhiteSpace ( string argumentName, string value ) : void
argumentName string
value string
Résultat void
Exemple #1
0
 public static string GetShortName(string name)
 {
     Guard.AgainstWhiteSpace(name, nameof(name));
     return(name.Replace(" ", "-")
            .Replace("'", "")
            .ToLowerInvariant());
 }
Exemple #2
0
        public SqlInstance(
            string name,
            Func <SqlConnection, Task> buildTemplate,
            string?directory    = null,
            DateTime?timestamp  = null,
            ushort templateSize = 3)
        {
            Guard.AgainstNull(nameof(buildTemplate), buildTemplate);
            Guard.AgainstWhiteSpace(nameof(directory), directory);
            Guard.AgainstNullWhiteSpace(nameof(name), name);
            if (directory == null)
            {
                directory = DirectoryFinder.Find(name);
            }
            DateTime resultTimestamp;

            if (timestamp == null)
            {
                var callingAssembly = Assembly.GetCallingAssembly();
                resultTimestamp = Timestamp.LastModified(callingAssembly);
            }
            else
            {
                resultTimestamp = timestamp.Value;
            }
            wrapper = new Wrapper(name, directory, templateSize);
            wrapper.Start(resultTimestamp, buildTemplate);
        }
Exemple #3
0
        public Task <HttpResponseMessage> ExecuteGet(HttpClient client, string query, object?variables = null, Action <HttpHeaders>?headerAction = null)
        {
            Guard.AgainstWhiteSpace(nameof(query), query);
            var compressed             = CompressQuery(query);
            var variablesString        = ToJson(variables);
            var getUri                 = $"{uri}?query={compressed}&variables={variablesString}";
            HttpRequestMessage request = new(HttpMethod.Get, getUri);

            headerAction?.Invoke(request.Headers);
            return(client.SendAsync(request));
        }
Exemple #4
0
        static string GetInstanceName(string?scopeSuffix)
        {
            Guard.AgainstWhiteSpace(nameof(scopeSuffix), scopeSuffix);

            if (scopeSuffix == null)
            {
                return(typeof(TDbContext).Name);
            }

            return($"{typeof(TDbContext).Name}_{scopeSuffix}");
        }
Exemple #5
0
        /// <summary>
        ///   Build database with a name based on the calling Method.
        /// </summary>
        /// <param name="testFile">
        /// The path to the test class.
        /// Used to make the database name unique per test type.
        /// </param>
        /// <param name="databaseSuffix">
        /// For Xunit theories add some text based on the inline data
        /// to make the db name unique.
        /// </param>
        /// <param name="memberName">
        /// Used to make the db name unique per method.
        /// Will default to the caller method name is used.
        /// </param>
        public Task <SqlDatabase> Build(
            [CallerFilePath] string testFile     = "",
            string?databaseSuffix                = null,
            [CallerMemberName] string memberName = "")
        #endregion
        {
            Guard.AgainstNullWhiteSpace(nameof(testFile), testFile);
            Guard.AgainstNullWhiteSpace(nameof(memberName), memberName);
            Guard.AgainstWhiteSpace(nameof(databaseSuffix), databaseSuffix);

            var testClass = Path.GetFileNameWithoutExtension(testFile);

            var name = DbNamer.DeriveDbName(databaseSuffix, memberName, testClass);

            return(Build(name));
        }
Exemple #6
0
        /// <summary>
        ///   Build DB with a name based on the calling Method.
        /// </summary>
        /// <param name="data">The seed data.</param>
        /// <param name="testFile">The path to the test class. Used to make the db name unique per test type.</param>
        /// <param name="databaseSuffix">For Xunit theories add some text based on the inline data to make the db name unique.</param>
        /// <param name="memberName">Used to make the db name unique per method. Will default to the caller method name is used.</param>
        public Task <SqlDatabase <TDbContext> > Build(
            IEnumerable <object>?data,
            [CallerFilePath] string testFile     = "",
            string?databaseSuffix                = null,
            [CallerMemberName] string memberName = "")
        {
            Guard.AgainstNullWhiteSpace(nameof(testFile), testFile);
            Guard.AgainstNullWhiteSpace(nameof(memberName), memberName);
            Guard.AgainstWhiteSpace(nameof(databaseSuffix), databaseSuffix);

            var testClass = Path.GetFileNameWithoutExtension(testFile);

            var dbName = DbNamer.DeriveDbName(databaseSuffix, memberName, testClass);

            return(Build(dbName, data));
        }
Exemple #7
0
        public SqlInstance(
            Func <DbContextOptionsBuilder <TDbContext>, TDbContext> constructInstance,
            Func <TDbContext, Task>?buildTemplate = null,
            string?instanceSuffix = null,
            DateTime?timestamp    = null,
            ushort templateSize   = 3)
        {
            Guard.AgainstWhiteSpace(nameof(instanceSuffix), instanceSuffix);
            Guard.AgainstNull(nameof(constructInstance), constructInstance);
            var instanceName = GetInstanceName(instanceSuffix);
            var directory    = DirectoryFinder.Find(instanceName);

            var convertedBuildTemplate = BuildTemplateConverter.Convert(constructInstance, buildTemplate);

            Init(convertedBuildTemplate, constructInstance, instanceName, directory, timestamp, templateSize);
        }
Exemple #8
0
        public Task <HttpResponseMessage> ExecutePost(HttpClient client, string query, object?variables = null, Action <HttpHeaders>?headerAction = null)
        {
            Guard.AgainstWhiteSpace(nameof(query), query);
            query = CompressQuery(query);
            var body = new
            {
                query,
                variables
            };
            HttpRequestMessage request = new(HttpMethod.Post, uri)
            {
                Content = new StringContent(ToJson(body), Encoding.UTF8, "application/json")
            };

            headerAction?.Invoke(request.Headers);
            return(client.SendAsync(request));
        }
    FieldType BuildQueryField <TSource, TReturn>(
        Type?itemGraphType,
        string name,
        Func <ResolveEfFieldContext <TDbContext, TSource>, IQueryable <TReturn> >?resolve,
        IEnumerable <QueryArgument>?arguments,
        string?description)
        where TReturn : class
    {
        Guard.AgainstWhiteSpace(nameof(name), name);

        var hasId     = keyNames.ContainsKey(typeof(TReturn));
        var fieldType = new FieldType
        {
            Name        = name,
            Description = description,
            Type        = MakeListGraphType <TReturn>(itemGraphType),
            Arguments   = ArgumentAppender.GetQueryArguments(arguments, hasId, true),
        };

        if (resolve is not null)
        {
            fieldType.Resolver = new AsyncFieldResolver <TSource, IEnumerable <TReturn> >(
                async context =>
            {
                var fieldContext = BuildContext(context);
                var names        = GetKeyNames <TReturn>();
                var query        = resolve(fieldContext);
                if (disableTracking)
                {
                    query = query.AsNoTracking();
                }

                query = includeAppender.AddIncludes(query, context);
                query = query.ApplyGraphQlArguments(context, names, true);

                QueryLogger.Write(query);

                var list = await query
                           .ToListAsync(context.CancellationToken);
                return(await fieldContext.Filters.ApplyFilter(list, context.UserContext));
            });
        }

        return(fieldType);
    }
Exemple #10
0
        public SqlInstance(
            string name,
            Func <DbConnection, Task> buildTemplate,
            string?directory    = null,
            DateTime?timestamp  = null,
            ushort templateSize = 3)
        {
            Guard.AgainstNull(nameof(buildTemplate), buildTemplate);
            Guard.AgainstWhiteSpace(nameof(directory), directory);
            Guard.AgainstNullWhiteSpace(nameof(name), name);
            directory = DirectoryFinder.Find(name);
            DirectoryCleaner.CleanInstance(directory);
            var callingAssembly = Assembly.GetCallingAssembly();
            var resultTimestamp = GetTimestamp(timestamp, buildTemplate, callingAssembly);

            Wrapper = new Wrapper(s => new SqlConnection(s), name, directory, templateSize);
            Wrapper.Start(resultTimestamp, buildTemplate);
        }
Exemple #11
0
        public SqlInstance(
            string name,
            Action <SqlConnection> buildTemplate,
            string directory    = null,
            DateTime?timestamp  = null,
            ushort templateSize = 3)
        {
            Guard.AgainstNull(nameof(buildTemplate), buildTemplate);
            Guard.AgainstWhiteSpace(nameof(directory), directory);
            Guard.AgainstNullWhiteSpace(nameof(name), name);
            if (directory == null)
            {
                directory = DirectoryFinder.Find(name);
            }

            wrapper = new Wrapper(name, directory, templateSize);
            wrapper.Start(timestamp.GetValueOrDefault(DateTime.Now), buildTemplate);
        }
Exemple #12
0
        public FieldType AddNavigationField <TSource, TReturn>(
            ComplexGraphType <TSource> graph,
            string name,
            Func <ResolveEfFieldContext <TDbContext, TSource>, TReturn?>?resolve = null,
            Type?graphType = null,
            IEnumerable <string>?includeNames = null,
            string?description = null)
            where TReturn : class
        {
            Guard.AgainstWhiteSpace(nameof(name), name);

            graphType ??= GraphTypeFinder.FindGraphType <TReturn>();

            FieldType field = new()
            {
                Name        = name,
                Type        = graphType,
                Description = description
            };

            IncludeAppender.SetIncludeMetadata(field, name, includeNames);

            if (resolve != null)
            {
                field.Resolver = new AsyncFieldResolver <TSource, TReturn?>(
                    async context =>
                {
                    var fieldContext = BuildContext(context);

                    var result = resolve(fieldContext);
                    if (await fieldContext.Filters.ShouldInclude(context.UserContext, result))
                    {
                        return(result);
                    }

                    return(null);
                });
            }

            return(graph.AddField(field));
        }
    }
    public void AddNavigationConnectionField <TSource, TReturn>(
        ComplexGraphType <TSource> graph,
        string name,
        Func <ResolveEfFieldContext <TDbContext, TSource>, IEnumerable <TReturn> >?resolve = null,
        Type?itemGraphType = null,
        IEnumerable <QueryArgument>?arguments = null,
        IEnumerable <string>?includeNames     = null,
        int pageSize       = 10,
        string?description = null)
        where TReturn : class
    {
        Guard.AgainstWhiteSpace(nameof(name), name);
        Guard.AgainstNegative(nameof(pageSize), pageSize);

        itemGraphType ??= GraphTypeFinder.FindGraphType <TReturn>();

        var addConnectionT = addEnumerableConnection.MakeGenericMethod(typeof(TSource), itemGraphType, typeof(TReturn));

        addConnectionT.Invoke(this, new object?[] { graph, name, resolve, pageSize, description, arguments, includeNames });
    }
Exemple #14
0
    public FieldType AddNavigationListField <TSource, TReturn>(
        ComplexGraphType <TSource> graph,
        string name,
        Func <ResolveEfFieldContext <TDbContext, TSource>, IEnumerable <TReturn> >?resolve = null,
        Type?itemGraphType = null,
        IEnumerable <QueryArgument>?arguments = null,
        IEnumerable <string>?includeNames     = null,
        string?description = null)
        where TReturn : class
    {
        Guard.AgainstWhiteSpace(nameof(name), name);

        var hasId = keyNames.ContainsKey(typeof(TReturn));
        var field = new FieldType
        {
            Name        = name,
            Description = description,
            Type        = MakeListGraphType <TReturn>(itemGraphType),
            Arguments   = ArgumentAppender.GetQueryArguments(arguments, hasId, true),
        };

        IncludeAppender.SetIncludeMetadata(field, name, includeNames);

        if (resolve is not null)
        {
            field.Resolver = new AsyncFieldResolver <TSource, IEnumerable <TReturn> >(
                context =>
            {
                var fieldContext = BuildContext(context);
                var result       = resolve(fieldContext);
                result           = result.ApplyGraphQlArguments(hasId, context);
                return(fieldContext.Filters.ApplyFilter(result, context.UserContext) !);
            });
        }

        return(graph.AddField(field));
    }
 public static string MapForElectorate(string name)
 {
     Guard.AgainstWhiteSpace(nameof(name), name);
     return(MapForElectorate(DataLoader.FindElectorate(name)));
 }
Exemple #16
0
 public ElectorateNotFoundException(string name)
 {
     Guard.AgainstWhiteSpace(nameof(name), name);
     Name = name;
 }
Exemple #17
0
 public static string Query(string query)
 {
     Guard.AgainstWhiteSpace(nameof(query), query);
     query = Regex.Replace(query, @"\s+", " ");
     return(Regex.Replace(query, @"\s*(\[|\]|\{|\}|\(|\)|:|\,)\s*", "$1"));
 }
Exemple #18
0
        FieldType BuildSingleField <TSource, TReturn>(
            string name,
            Func <ResolveEfFieldContext <TDbContext, TSource>, IQueryable <TReturn> > resolve,
            Func <ResolveEfFieldContext <TDbContext, TSource>, TReturn, Task>?mutate,
            IEnumerable <QueryArgument>?arguments,
            Type?graphType,
            bool nullable,
            string?description)
            where TReturn : class
        {
            Guard.AgainstWhiteSpace(nameof(name), name);

            graphType ??= GraphTypeFinder.FindGraphType <TReturn>(nullable);

            var hasId = keyNames.ContainsKey(typeof(TReturn));

            return(new()
            {
                Name = name,
                Type = graphType,
                Description = description,

                Arguments = ArgumentAppender.GetQueryArguments(arguments, hasId, false),

                Resolver = new AsyncFieldResolver <TSource, TReturn?>(
                    async context =>
                {
                    var efFieldContext = BuildContext(context);

                    var names = GetKeyNames <TReturn>();

                    var query = resolve(efFieldContext);
                    if (disableTracking)
                    {
                        query = query.AsNoTracking();
                    }

                    query = includeAppender.AddIncludes(query, context);
                    query = query.ApplyGraphQlArguments(context, names, false);
                    var single = await query.SingleOrDefaultAsync(context.CancellationToken);

                    if (single != null)
                    {
                        if (await efFieldContext.Filters.ShouldInclude(context.UserContext, single))
                        {
                            if (mutate != null)
                            {
                                await mutate.Invoke(efFieldContext, single);
                            }

                            return single;
                        }
                    }

                    if (nullable)
                    {
                        return null;
                    }

                    throw new ExecutionError("Not found");
                })
            });
        }