Пример #1
0
        public static string GetSampleData(CProtoMessageField childField, COperationIs operationIs)
        {
            var dataType     = SqlMapper.SqlDbTypeToDbType(SqlMapper.GrpcTypeToSqlDbType(childField.FieldType));
            var isIdentity   = false;
            var isForeignKey = false;

            if (childField.DerivedFrom is CColumn)
            {
                var column = childField.DerivedFrom as CColumn;
                isIdentity = column.IsIdentity;
                dataType   = column.ColumnType;
            }
            else if (childField.DerivedFrom is CStoredProcedureParameter)
            {
                var parameter = childField.DerivedFrom as CStoredProcedureParameter;
                isIdentity   = parameter.SourceColumn.IsIdentity;
                isForeignKey = parameter.SourceColumn.ForeignKeyColumn.Count > 0;
                dataType     = parameter.ParameterType;
            }


            if (isForeignKey && dataType == DbType.Int64)
            {
                return("_currentDbIdentityValue");
            }
            if (isForeignKey && dataType == DbType.Int32)
            {
                return("_currentDbIdentityValue");
            }

            if (isIdentity)
            {
                if (operationIs == COperationIs.Update)
                {
                    return("_currentDbIdentityValue");
                }
                else
                {
                    return("0");
                }
            }

            if (_sampleData.ContainsKey(childField.FieldName))
            {
                return(LookupSample(childField));
            }

            var sampleData = GenerateSampleData(dataType);

            _sampleData.Add(childField.FieldName, sampleData);
            return(sampleData);
        }
        private CMethod GetTestMethod(CProtoRpc rpc, COperationIs operationIs)
        {
            if (rpc.OperationIs.HasFlag(COperationIs.Bulk))
            {
                return(null);
            }
            var codeWriter = new CodeWriter();

            var methodName = $"{rpc.RpcName}";

            if (operationIs == COperationIs.Add)
            {
                methodName += "Add";
            }
            else if (operationIs == COperationIs.Update)
            {
                methodName += "Update";
            }
            var method = new CMethod
            {
                MethodIs    = operationIs,
                DerivedFrom = rpc,
                ReturnType  = "Task",
                IsAsync     = true,
                IsStatic    = false,
                MethodName  = methodName,
                Parameter   = new List <CParameter>
                {
                    new CParameter
                    {
                        Type          = $"{rpc.ProtoService.ServiceName}.{rpc.ProtoService.ServiceName}Client",
                        ParameterName = "client"
                    }
                }
            };

            //method.Attribute.Add(new SMethodAttribute { AttributeName = "TestMethod" });

            codeWriter.WriteLine("//Arrange");
            codeWriter.WriteLine(string.Empty);
            codeWriter.WriteLine($"var request = new {rpc.Request.MessageName}();");

            if (!rpc.OperationIs.HasFlag(COperationIs.Bulk)) //ignore, for now
            {
                foreach (var field in rpc.Request.ProtoField)
                {
                    if (field.FieldType == GrpcType.__enum)
                    {
                        var childEnum = rpc.ProtoService.ProtoFile.ProtoEnum.First(pm =>
                                                                                   pm.EnumName.ToLower() == field.EnumType.ToLower());
                    }
                    else if (field.FieldType == GrpcType.__map)
                    {
                        //todo:
                    }
                    else if (field.FieldType == GrpcType.__message)
                    {
                        var childMessage = rpc.ProtoService.ProtoFile.ProtoMessage.First(pm =>
                                                                                         pm.MessageName.ToLower() == field.MessageType.ToLower());
                        if (!field.Repeated)
                        {
                            codeWriter.WriteLine($"request.{field.FieldName} = new {field.FieldName}();");
                            foreach (var childField in childMessage.ProtoField)
                            {
                                if (childField.IsScalar && !childField.Repeated)
                                {
                                    codeWriter.WriteLine(
                                        $"request.{field.FieldName}.{childField.FieldName} = {SampleDataService.GetSampleData(childField, operationIs)}; ");
                                }
                                else if (childField.IsScalar && childField.Repeated)
                                {
                                    codeWriter.WriteLine(
                                        $"request.{field.FieldName}.{childField.FieldName}.Add({SampleDataService.GetSampleData(childField, operationIs)}); ");
                                }
                            }
                        }
                        else
                        {
                            codeWriter.WriteLine($"var {field.FieldName.ToLower()} = new {field.MessageType}();");
                            foreach (var childField in childMessage.ProtoField)
                            {
                                if (childField.IsScalar && !childField.Repeated)
                                {
                                    codeWriter.WriteLine(
                                        $"{field.FieldName.ToLower()}.{childField.FieldName} = {SampleDataService.GetSampleData(childField, operationIs)}; ");
                                }
                                else if (childField.IsScalar && childField.Repeated)
                                {
                                    codeWriter.WriteLine(
                                        $"{field.FieldName.ToLower()}.{childField.FieldName}.Add({SampleDataService.GetSampleData(childField, operationIs)}); ");
                                }
                            }
                            codeWriter.WriteLine($"request.{field.FieldName}.Add({field.FieldName.ToLower()});");
                        }
                    }
                    else
                    {
                        if (field.Repeated)
                        {
                            codeWriter.WriteLine(
                                $"request.{field.FieldName}.Add({SampleDataService.GetSampleData(field, operationIs)}); ");
                        }
                        else
                        {
                            if (rpc.DerivedFrom is CStoredProcedure &&
                                (rpc.DerivedFrom as CStoredProcedure).DataOperationIs.HasFlag(COperationIs.Delete))
                            {
                                if (field.FieldType == GrpcType.__int64 || field.FieldType == GrpcType.__int32)
                                {
                                    codeWriter.WriteLine($"request.{field.FieldName}  = _currentDbIdentityValue;");
                                }
                                else
                                {
                                    codeWriter.WriteLine(
                                        $"request.{field.FieldName}  = {SampleDataService.GetSampleData(field, operationIs)};");
                                }
                            }
                            else
                            {
                                codeWriter.WriteLine(
                                    $"request.{field.FieldName} = {SampleDataService.GetSampleData(field, operationIs)}; ");
                            }
                        }
                    }
                }
            }

            codeWriter.WriteLine(string.Empty);
            codeWriter.WriteLine("//Act");

            codeWriter.WriteLine(string.Empty);

            codeWriter.WriteLine($"var response = await client.{rpc.RpcName}Async(request);");

            codeWriter.WriteLine(string.Empty);
            //codeWriter.WriteLine("//Assert");
            if (rpc.ResponseIsList())
            {
                codeWriter.WriteLine($"Console.BackgroundColor = ConsoleColor.DarkGreen;");
                codeWriter.WriteLine(
                    $@"Console.WriteLine($""{{response.{rpc.Response.ProtoField.First().FieldName}.Count}} {rpc.Response.ProtoField.First().FieldName} records returned"");");
            }
            else
            {
                codeWriter.WriteLine($"Console.BackgroundColor = ConsoleColor.DarkGreen;");
                foreach (var field in rpc.Response.ProtoField)
                {
                    codeWriter.WriteLine(
                        $@"Console.WriteLine($""{{response.{field.FieldName}}}"");");
                }
            }
            //codeWriter.WriteLine(string.Empty);
            // codeWriter.WriteLine($@"Assert.Fail(""{method.MethodName}() test is not implemented"");");

            method.CodeSnippet = codeWriter.ToString();

            return(method);
        }