-
Notifications
You must be signed in to change notification settings - Fork 1.7k
AVRO-3458: Added tests for GenericRecord #1606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
b8601c0
AVRO-3360 Updated XML documentation
d01699a
Revert "AVRO-3360 Updated XML documentation"
83093e1
Merge branch 'apache:master' into master
KyleSchoonover c992da4
Merge branch 'apache:master' into master
KyleSchoonover fa5f7c2
Merge branch 'apache:master' into master
KyleSchoonover 6989e61
Merge branch 'apache:master' into master
KyleSchoonover d06604a
Merge branch 'apache:master' into master
KyleSchoonover 2b39bb0
Merge branch 'apache:master' into master
KyleSchoonover f8a7611
Merge branch 'apache:master' into master
KyleSchoonover 3e1eb47
AVRO-3458 Added tests for GenericRecord
KyleSchoonover a5fa2ef
Merge branch 'apache:master' into master
KyleSchoonover 0216b4d
Merge branch 'apache:master' into master
KyleSchoonover cefce73
Moved Schema to const
KyleSchoonover 74cdc41
using discard
KyleSchoonover e3a5376
Empty
KyleSchoonover d97a23c
Merge branch 'apache:master' into master
KyleSchoonover 1381616
Merge branch 'master' into AVRO-3458
KyleSchoonover e42c4f6
Add license
KyleSchoonover e1db956
Merge branch 'apache:master' into master
KyleSchoonover e586782
Merge branch 'apache:master' into master
KyleSchoonover 9ab3fb2
Merge branch 'apache:master' into master
KyleSchoonover e9301fd
Merge branch 'master' into AVRO-3458
KyleSchoonover File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
202 changes: 202 additions & 0 deletions
202
lang/csharp/src/apache/test/Generic/GenericRecordTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using Avro.Generic; | ||
| using NUnit.Framework; | ||
|
|
||
| namespace Avro.test.Generic | ||
| { | ||
| [TestFixture] | ||
| public class GenericRecordTests | ||
| { | ||
| private const string baseSchema = "{\"type\":\"record\",\"name\":\"r\",\"fields\":" + | ||
| "[{\"name\":\"f2\",\"type\":\"int\"},{\"name\":\"f1\",\"type\":\"boolean\"}]}"; | ||
|
|
||
| [Test] | ||
| public void TestAddByFieldNameThrows() | ||
| { | ||
| GenericRecord genericRecord = GetBaseGenericRecord(); | ||
|
|
||
| // Field does not exist | ||
| Assert.Throws<AvroException>(() => { genericRecord.Add("badField", "test"); }); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestAddByPosition() | ||
| { | ||
| GenericRecord genericRecord = GetBaseGenericRecord(); | ||
|
|
||
| genericRecord.Add(0, 2); | ||
|
|
||
| object value = genericRecord.GetValue(0); | ||
|
|
||
| Assert.IsNotNull(value); | ||
| Assert.IsTrue(value is int); | ||
| Assert.AreEqual(2, (int)value); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestAddByPositionThrows() | ||
| { | ||
| GenericRecord genericRecord = GetBaseGenericRecord(); | ||
|
|
||
| Assert.Throws<IndexOutOfRangeException>(() => { genericRecord.Add(2, 2); }); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestEquals() | ||
| { | ||
| GenericRecord genericRecord = GetBaseGenericRecord(); | ||
| GenericRecord genericRecord2 = GetBaseGenericRecord(); | ||
|
|
||
| Assert.IsTrue(genericRecord.Equals(genericRecord2)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestEqualsNotEqual() | ||
| { | ||
| GenericRecord genericRecord = GetBaseGenericRecord(); | ||
| GenericRecord genericRecord2 = GetBaseGenericRecord(); | ||
| genericRecord2.Add(0, 2); | ||
|
|
||
| Assert.IsFalse(genericRecord.Equals(genericRecord2)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestEqualsObject() | ||
| { | ||
| GenericRecord genericRecord = GetBaseGenericRecord(); | ||
| object genericRecord2 = genericRecord; | ||
|
|
||
| Assert.IsTrue(genericRecord.Equals(genericRecord2)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestEqualsObjectNotEqual() | ||
| { | ||
| GenericRecord genericRecord = GetBaseGenericRecord(); | ||
| GenericRecord genericRecord2 = GetBaseGenericRecord(); | ||
| genericRecord2.Add(0, 2); | ||
|
|
||
| Assert.IsFalse(genericRecord.Equals((object)genericRecord2)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestEqualsObjectNullObject() | ||
| { | ||
| GenericRecord genericRecord = GetBaseGenericRecord(); | ||
|
|
||
| Assert.IsFalse(genericRecord.Equals((object)null)); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestGetHashCode() | ||
| { | ||
| int hashCode = GetBaseGenericRecord().GetHashCode(); | ||
| Assert.IsTrue(hashCode > 0); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestGetValue() | ||
| { | ||
| GenericRecord genericRecord = GetBaseGenericRecord(); | ||
|
|
||
| object value = genericRecord.GetValue(0); | ||
|
|
||
| Assert.IsNotNull(value); | ||
| Assert.IsTrue(value is int); | ||
| Assert.AreEqual(1, (int)value); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestKeyValueLookup() | ||
| { | ||
| GenericRecord genericRecord = GetBaseGenericRecord(); | ||
|
|
||
| // Key Exists | ||
| object existingKey = genericRecord["f2"]; | ||
| Assert.IsNotNull(existingKey); | ||
| Assert.IsTrue(existingKey is int); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestKeyValueLookupThrows() | ||
| { | ||
| GenericRecord genericRecord = GetBaseGenericRecord(); | ||
|
|
||
| // Key does not exist | ||
| Assert.Throws<KeyNotFoundException>(() => { object missingKey = genericRecord["badField"]; }); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestToString() | ||
| { | ||
| GenericRecord genericRecord = GetBaseGenericRecord(); | ||
| string str = genericRecord.ToString(); | ||
| string expectedValue = "Schema: {\"type\":\"record\",\"name\":\"r\",\"fields\":" + | ||
| "[{\"name\":\"f2\",\"type\":\"int\"},{\"name\":\"f1\",\"type\":" + | ||
| "\"boolean\"}]}, contents: { f2: 1, f1: True, }"; | ||
|
|
||
| Assert.AreEqual(expectedValue, str); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestTryGetValue() | ||
| { | ||
| GenericRecord genericRecord = GetBaseGenericRecord(); | ||
|
|
||
| // Value exists | ||
| bool returnResult = genericRecord.TryGetValue("f2", out object result); | ||
|
|
||
| Assert.IsTrue(returnResult); | ||
| Assert.IsNotNull(result); | ||
| Assert.IsTrue(result is int); | ||
| Assert.AreEqual(1, (int)result); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestTryGetValueByPosition() | ||
| { | ||
| GenericRecord genericRecord = GetBaseGenericRecord(); | ||
|
|
||
| bool returnResult = genericRecord.TryGetValue(0, out object value); | ||
|
|
||
| Assert.IsTrue(returnResult); | ||
| Assert.IsNotNull(value); | ||
| Assert.IsTrue(value is int); | ||
| Assert.AreEqual(1, (int)value); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestTryGetValueByPositionNotFound() | ||
| { | ||
| GenericRecord genericRecord = GetBaseGenericRecord(); | ||
|
|
||
| bool returnResult = genericRecord.TryGetValue(3, out object value); | ||
|
|
||
| Assert.IsFalse(returnResult); | ||
| Assert.IsNull(value); | ||
| } | ||
|
|
||
| [Test] | ||
| public void TestTryGetValueNotFound() | ||
| { | ||
| GenericRecord genericRecord = GetBaseGenericRecord(); | ||
|
|
||
| // Value exists | ||
| bool returnResult = genericRecord.TryGetValue("badField", out object result); | ||
|
|
||
| Assert.IsFalse(returnResult); | ||
| Assert.IsNull(result); | ||
| } | ||
|
|
||
| private GenericRecord GetBaseGenericRecord() | ||
| { | ||
| RecordSchema testSchema = Schema.Parse(baseSchema) as RecordSchema; | ||
| GenericRecord genericRecord = new GenericRecord(testSchema); | ||
| genericRecord.Add("f2", 1); | ||
| genericRecord.Add("f1", true); | ||
|
|
||
| return genericRecord; | ||
| } | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.