[ 
https://issues.apache.org/jira/browse/AVRO-3446?focusedWorklogId=743415&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-743415
 ]

ASF GitHub Bot logged work on AVRO-3446:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 17/Mar/22 20:26
            Start Date: 17/Mar/22 20:26
    Worklog Time Spent: 10m 
      Work Description: martin-g commented on a change in pull request #1595:
URL: https://github.com/apache/avro/pull/1595#discussion_r829464774



##########
File path: lang/csharp/src/apache/main/CodeGen/CodeGenUtil.cs
##########
@@ -88,6 +88,9 @@ is regenerated
  
------------------------------------------------------------------------------");
 
             // Visual Studio 2010 
https://msdn.microsoft.com/en-us/library/x53a06bb.aspx
+            // Note:
+            //  1. Contextual keywords are not reserved keywords e.g. value, 
partial
+            //  2. __arglist, __makeref", __reftype, __refvalue are 
undocumented keywords, but recognised by the C# compiler

Review comment:
       ```suggestion
               //  2. __arglist, __makeref, __reftype, __refvalue are 
undocumented keywords, but recognised by the C# compiler
   ```

##########
File path: lang/csharp/src/apache/test/AvroGen/AvroGenHelper.cs
##########
@@ -0,0 +1,155 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Reflection;
+using System.Text;
+using Microsoft.CodeAnalysis;
+using Microsoft.CodeAnalysis.CSharp;
+using Microsoft.CodeAnalysis.Emit;
+using NUnit.Framework;
+
+namespace Avro.Test.AvroGen
+{
+    class AvroGenToolResult
+    {
+        public int ExitCode { get; set; }
+        public string[] StdOut { get; set; }
+        public string[] StdErr { get; set; }
+    }
+
+    class AvroGenHelper
+    {
+        public static AvroGenToolResult RunAvroGenTool(params string[] args)
+        {
+            // Save stdout and stderr
+            TextWriter conOut = Console.Out;
+            TextWriter conErr = Console.Error;
+
+            try
+            {
+                AvroGenToolResult result = new AvroGenToolResult();
+                StringBuilder strBuilderOut = new StringBuilder();
+                StringBuilder strBuilderErr = new StringBuilder();
+
+                using (StringWriter writerOut = new 
StringWriter(strBuilderOut))
+                using (StringWriter writerErr = new 
StringWriter(strBuilderErr))
+                {
+                    writerOut.NewLine = "\n";
+                    writerErr.NewLine = "\n";
+
+                    // Overwrite stdout and stderr to be able to capture 
console output
+                    Console.SetOut(writerOut);
+                    Console.SetError(writerErr);
+
+                    result.ExitCode = AvroGenTool.Main(args.ToArray());
+
+                    writerOut.Flush();
+                    writerErr.Flush();
+
+                    result.StdOut = strBuilderOut.Length == 0 ? 
Array.Empty<string>() : strBuilderOut.ToString().Split(writerOut.NewLine);
+                    result.StdErr = strBuilderErr.Length == 0 ? 
Array.Empty<string>() : strBuilderErr.ToString().Split(writerErr.NewLine);
+                }
+
+                return result;
+            }
+            finally
+            {
+                // Restore console
+                Console.SetOut(conOut);
+                Console.SetError(conErr);
+            }
+        }
+
+        public static Assembly 
CompileCSharpFilesIntoLibrary(IEnumerable<string> sourceFiles, string 
assemblyName = null, bool loadAssembly = true)
+        {
+            // CReate random assenbly name if not specified

Review comment:
       ```suggestion
               // Create random assembly name if not specified
   ```

##########
File path: lang/csharp/src/apache/test/AvroGen/AvroGenTests.cs
##########
@@ -0,0 +1,664 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+using System;
+using System.IO;
+using System.Linq;
+using System.Reflection;
+using System.Collections.Generic;
+using Microsoft.CodeAnalysis;
+using Microsoft.CodeAnalysis.CSharp;
+using Microsoft.CodeAnalysis.Emit;
+using NUnit.Framework;
+using Avro.Specific;
+
+namespace Avro.Test.AvroGen
+{
+    [TestFixture]
+
+    class AvroGenTests
+    {
+        private const string _customConversionWithLogicalTypes = @"
+{
+  ""namespace"": ""org.apache.avro.codegentest.testdata"",
+  ""type"": ""record"",
+  ""name"": ""CustomConversionWithLogicalTypes"",
+  ""doc"" : ""Test custom conversion and logical types in generated Java 
classes"",
+  ""fields"": [
+    {
+      ""name"": ""customEnum"",
+      ""type"": [""null"", {
+        ""namespace"": ""org.apache.avro.codegentest.testdata"",
+        ""name"": ""CustomAvroEnum"",
+        ""type"": ""enum"",
+        ""logicalType"": ""custom-enum"",
+        ""symbols"": [""ONE"", ""TWO"", ""THREE""]
+    }]
+    }]
+}
+";
+
+        private const string _logicalTypesWithCustomConversion = @"
+{
+""namespace"": ""org.apache.avro.codegentest.testdata"",
+  ""type"": ""record"",
+  ""name"": ""LogicalTypesWithCustomConversion"",
+  ""doc"" : ""Test unions with logical types in generated Java classes"",
+  ""fields"": [
+    {""name"": ""nullableCustomField"",  ""type"": [""null"", {""type"": 
""bytes"", ""logicalType"": ""decimal"", ""precision"": 9, ""scale"": 2}], 
""default"": null},
+    { ""name"": ""nonNullCustomField"",  ""type"": { ""type"": ""bytes"", 
""logicalType"": ""decimal"", ""precision"": 9, ""scale"": 2} },
+    { ""name"": ""nullableFixedSizeString"",  ""type"": [""null"", { ""type"": 
""bytes"", ""logicalType"": ""fixed-size-string"", ""minLength"": 1, 
""maxLength"": 50}], ""default"": null},
+    { ""name"": ""nonNullFixedSizeString"",  ""type"": { ""type"": ""bytes"", 
""logicalType"": ""fixed-size-string"", ""minLength"": 1, ""maxLength"": 50} }
+  ]
+}
+";
+
+        private const string _logicalTypesWithDefaults = @"
+{
+""namespace"": ""org.apache.avro.codegentest.testdata"",
+  ""type"": ""record"",
+  ""name"": ""LogicalTypesWithDefaults"",
+  ""doc"" : ""Test logical types and default values in generated Java 
classes"",
+  ""fields"": [
+    {""name"": ""nullableDate"",  ""type"": [{""type"": ""int"", 
""logicalType"": ""date""}, ""null""], ""default"": 1234},
+    { ""name"": ""nonNullDate"",  ""type"": { ""type"": ""int"", 
""logicalType"": ""date""}, ""default"": 1234}
+  ]
+}";
+
+        private const string _nestedLogicalTypesArray = @"
+{""namespace"": ""org.apache.avro.codegentest.testdata"",
+  ""type"": ""record"",
+  ""name"": ""NestedLogicalTypesArray"",
+  ""doc"" : ""Test nested types with logical types in generated Java classes"",
+  ""fields"": [
+    {
+      ""name"": ""arrayOfRecords"",
+      ""type"": {
+        ""type"": ""array"",
+        ""items"": {
+          ""namespace"": ""org.apache.avro.codegentest.testdata"",
+          ""name"": ""RecordInArray"",
+          ""type"": ""record"",
+          ""fields"": [
+            {
+              ""name"": ""nullableDateField"",
+              ""type"": [""null"", {""type"": ""int"", ""logicalType"": 
""date""}]
+            }
+          ]
+        }
+      }
+    }]
+}
+";
+
+        private const string _nestedLogicalTypesMap = @"
+{""namespace"": ""org.apache.avro.codegentest.testdata"",
+  ""type"": ""record"",
+  ""name"": ""NestedLogicalTypesMap"",
+  ""doc"" : ""Test nested types with logical types in generated Java classes"",
+  ""fields"": [
+    {
+      ""name"": ""mapOfRecords"",
+      ""type"": {
+        ""type"": ""map"",
+        ""values"": {
+          ""namespace"": ""org.apache.avro.codegentest.testdata"",
+          ""name"": ""RecordInMap"",
+          ""type"": ""record"",
+          ""fields"": [
+            {
+              ""name"": ""nullableDateField"",
+              ""type"": [""null"", {""type"": ""int"", ""logicalType"": 
""date""}]
+            }
+          ]
+        }
+      }
+    }]
+}";
+
+        private const string _nestedLogicalTypesRecord = @"
+{""namespace"": ""org.apache.avro.codegentest.testdata"",
+  ""type"": ""record"",
+  ""name"": ""NestedLogicalTypesRecord"",
+  ""doc"" : ""Test nested types with logical types in generated Java classes"",
+  ""fields"": [
+    {
+      ""name"": ""nestedRecord"",
+      ""type"": {
+        ""namespace"": ""org.apache.avro.codegentest.testdata"",
+        ""type"": ""record"",
+        ""name"": ""NestedRecord"",
+        ""fields"": [
+          {
+            ""name"": ""nullableDateField"",
+            ""type"": [""null"", {""type"": ""int"", ""logicalType"": 
""date""}]
+          }
+        ]
+      }
+    }]
+}";
+
+        private const string _nestedLogicalTypesUnionFixedDecimal = @"
+{""namespace"": ""org.apache.avro.codegentest.testdata"",
+  ""type"": ""record"",
+  ""name"": ""NestedLogicalTypesUnionFixedDecimal"",
+  ""doc"" : ""Test nested types with logical types in generated Java classes"",
+  ""fields"": [
+    {
+      ""name"": ""unionOfFixedDecimal"",
+      ""type"": [""null"", {
+        ""namespace"": ""org.apache.avro.codegentest.testdata"",
+        ""name"": ""FixedInUnion"",
+        ""type"": ""fixed"",
+        ""size"": 12,
+        ""logicalType"": ""decimal"",
+        ""precision"": 28,
+        ""scale"": 15
+      }]
+    }]
+}";
+
+        private const string _nestedLogicalTypesUnion = @"
+{""namespace"": ""org.apache.avro.codegentest.testdata"",
+  ""type"": ""record"",
+  ""name"": ""NestedLogicalTypesUnion"",
+  ""doc"" : ""Test nested types with logical types in generated Java classes"",
+  ""fields"": [
+    {
+      ""name"": ""unionOfRecords"",
+      ""type"": [""null"", {
+        ""namespace"": ""org.apache.avro.codegentest.testdata"",
+        ""name"": ""RecordInUnion"",
+        ""type"": ""record"",
+        ""fields"": [
+          {
+            ""name"": ""nullableDateField"",
+            ""type"": [""null"", {""type"": ""int"", ""logicalType"": 
""date""}]
+          }
+        ]
+      }]
+    }]
+}";
+
+        private const string _nestedSomeNamespaceRecord = @"
+{""namespace"": ""org.apache.avro.codegentest.some"",
+  ""type"": ""record"",
+  ""name"": ""NestedSomeNamespaceRecord"",
+  ""doc"" : ""Test nested types with different namespace than the outer type"",
+  ""fields"": [
+    {
+      ""name"": ""nestedRecord"",
+      ""type"": {
+        ""namespace"": ""org.apache.avro.codegentest.other"",
+        ""type"": ""record"",
+        ""name"": ""NestedOtherNamespaceRecord"",
+        ""fields"": [
+          {
+            ""name"": ""someField"",
+            ""type"": ""int""
+          }
+        ]
+      }
+    }]
+}";
+
+        private const string _nullableLogicalTypesArray = @"
+{""namespace"": ""org.apache.avro.codegentest.testdata"",
+  ""type"": ""record"",
+  ""name"": ""NullableLogicalTypesArray"",
+  ""doc"" : ""Test nested types with logical types in generated Java classes"",
+  ""fields"": [
+    {
+      ""name"": ""arrayOfLogicalType"",
+      ""type"": {
+        ""type"": ""array"",
+        ""items"": [""null"", {""type"": ""int"", ""logicalType"": ""date""}]
+      }
+    }]
+}";
+
+        private const string _nullableLogicalTypes = @"
+{""namespace"": ""org.apache.avro.codegentest.testdata"",
+  ""type"": ""record"",
+  ""name"": ""NullableLogicalTypes"",
+  ""doc"" : ""Test unions with logical types in generated Java classes"",
+  ""fields"": [
+    {""name"": ""nullableDate"",  ""type"": [""null"", {""type"": ""int"", 
""logicalType"": ""date""}], ""default"": null}
+  ]
+}";
+
+        private const string _stringLogicalType = @"
+{
+  ""namespace"": ""org.apache.avro.codegentest.testdata"",
+  ""type"": ""record"",
+  ""name"": ""StringLogicalType"",
+  ""doc"": ""Test logical type applied to field of type string"",
+  ""fields"": [
+    {
+      ""name"": ""someIdentifier"",
+      ""type"": {
+        ""type"": ""string"",
+        ""logicalType"": ""uuid""
+      }
+},
+    {
+    ""name"": ""someJavaString"",
+      ""type"": ""string"",
+      ""doc"": ""Just to ensure no one removed <stringType>String</stringType> 
because this is the basis of this test""
+    }
+  ]
+}";
+
+        private Assembly TestSchema(
+            string schema,
+            IEnumerable<string> typeNamesToCheck = null,
+            IEnumerable<KeyValuePair<string, string>> namespaceMapping = null,
+            IEnumerable<string> generatedFilesToCheck = null)
+        {
+            // Create temp folder
+            string outputDir = AvroGenHelper.CreateEmptyTemporyFolder(out 
string uniqueId);
+
+            try
+            {
+                // Save schema
+                string schemaFileName = Path.Combine(outputDir, 
$"{uniqueId}.avsc");
+                System.IO.File.WriteAllText(schemaFileName, schema);
+
+                // Generate from schema file
+                Assert.That(AvroGenTool.GenSchema(schemaFileName, outputDir, 
namespaceMapping ?? new Dictionary<string, string>()), Is.EqualTo(0));
+
+                // Check if all generated files exist
+                if (generatedFilesToCheck != null)
+                {
+                    foreach (string generatedFile in generatedFilesToCheck)
+                    {
+                        Assert.That(new FileInfo(Path.Combine(outputDir, 
generatedFile)), Does.Exist);
+                    }
+                }
+
+                // Compile into netstandard library and load assembly
+                Assembly assembly = 
AvroGenHelper.CompileCSharpFilesIntoLibrary(
+                    new DirectoryInfo(outputDir)
+                        .EnumerateFiles("*.cs", SearchOption.AllDirectories)
+                        .Select(fi => fi.FullName),
+                        uniqueId);
+
+                if (typeNamesToCheck != null)
+                {
+                    // Check if the compiled code has the same number of types 
defined as the check list
+                    Assert.That(typeNamesToCheck.Count(), 
Is.EqualTo(assembly.DefinedTypes.Count()));
+
+                    // Check if types available in compiled assembly
+                    foreach (string typeName in typeNamesToCheck)
+                    {
+                        Type type = assembly.GetType(typeName);
+                        Assert.That(type, Is.Not.Null);
+
+                        // Instantiate
+                        object obj = Activator.CreateInstance(type);
+                        Assert.That(obj, Is.Not.Null);
+                    }
+                }

Review comment:
       do we care about the above CodeQL suggestion ?




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@avro.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Issue Time Tracking
-------------------

            Worklog Id:     (was: 743415)
    Remaining Estimate: 70h 50m  (was: 71h)
            Time Spent: 1h 10m  (was: 1h)

> Add avrogen unit tests
> ----------------------
>
>                 Key: AVRO-3446
>                 URL: https://issues.apache.org/jira/browse/AVRO-3446
>             Project: Apache Avro
>          Issue Type: Improvement
>          Components: csharp
>            Reporter: Zoltan Csizmadia
>            Priority: Minor
>              Labels: pull-request-available
>   Original Estimate: 72h
>          Time Spent: 1h 10m
>  Remaining Estimate: 70h 50m
>
> Currently there is no unit test coverage for C# avrogen tool. It makes hard 
> to add new features  to it without having a trusted unit test environment.



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to