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

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

                Author: ASF GitHub Bot
            Created on: 18/Mar/22 11:56
            Start Date: 18/Mar/22 11:56
    Worklog Time Spent: 10m 
      Work Description: zcsizmadia commented on a change in pull request #1595:
URL: https://github.com/apache/avro/pull/1595#discussion_r829932364



##########
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:
       What it tries to suggest is that the first 2limes of the foreach loop 
should be combined into foreach(Type type in typeNamesToCheck.Select(typeName 
=> assembly.GetType(typeName)).
   
   After that the loop enumerate the types which were created on the fly with 
the Select.
   
   The reasons I don't like that suggestion is this. It is a unit test, and the 
test code readability and understand the test's intention is more important. 
Otherwise I have to decipher what the Select is creating.




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

            Worklog Id:     (was: 743695)
    Remaining Estimate: 70h 40m  (was: 70h 50m)
            Time Spent: 1h 20m  (was: 1h 10m)

> 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 20m
>  Remaining Estimate: 70h 40m
>
> 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