Attribute Testing Mostly a Success

Metadata attribute testing was mostly a success; there were only a few slight problems.  In looking at the tests, I created a method to create a common setup for the root Entity object:

public MetadataEntry InitializeTest()
{
    MetadataEntry testEntry = new MetadataEntry("Test");
    testEntry.Attributes.Add(new MetadataAttribute("Parent", null));
    testEntry.Attributes.Add(new MetadataAttribute("Parent2"));
    testEntry.Attributes.Add(new MetadataAttribute("ID", 1));

    return testEntry;
}

The initial tests I created tested the existing properties I created above.  I checked the value that was provided when I created the attributes, to ensure that they were null.  Both of these tests succeeded.

[Test()]
public void TestAddAttributeWithNullValue()
{
    MetadataEntry testEntry = this.InitializeTest();
    Assert.IsNotNull(testEntry.Attributes["Parent"]);
    Assert.IsNull(testEntry.Attributes["Parent"].Value);
}

[Test()]
public void TestAddAttributeWithNoValue()
{
    MetadataEntry testEntry = this.InitializeTest();
    Assert.IsNotNull(testEntry.Attributes["Parent2"]);
    Assert.IsNull(testEntry.Attributes["Parent2"].Value);
}

The next test makes sure that an error is thrown when a duplicate is added.  I add a duplicate Name attribute and an error is thrown.

[Test()]
public void TestAddDuplicateAttribute()
{
    MetadataEntry testEntry = this.InitializeTest();
    try { testEntry.Attributes.Add(new MetadataAttribute("Name", "new Name")); }
    catch { return; }

    Assert.Fail("An error should have been thrown for a duplicate attribute");
}

I tested the adding of multiple items, which adds the items to the end of the list.  I added three items, ensured that the counts were correct, and everything ran successfully; three new items at the end of the list.

[Test()]
public void TestAddRangeOfAttributes()
{
    MetadataEntry testEntry = this.InitializeTest();
    MetadataAttribute[] attributes = new MetadataAttribute[3];
    attributes[0] = new MetadataAttribute("TestAddRange1", "Test 1");
    attributes[1] = new MetadataAttribute("TestAddRange2", "Test 2");
    attributes[2] = new MetadataAttribute("TestAddRange3", "Test 3");

    int count = testEntry.Attributes.Count;
    testEntry.Attributes.AddRange(attributes);

    Assert.IsTrue(testEntry.Attributes.Count == (count + 3));
    Assert.AreEqual("TestAddRange1", testEntry.Attributes[count].Name);
    Assert.AreEqual("TestAddRange2", testEntry.Attributes[count + 1].Name);
    Assert.AreEqual("TestAddRange3", testEntry.Attributes[count + 2].Name);
}

This checks the name property of the item, to ensure the constructor does its job.  It does, and the test succeeds.

[Test()]
public void TestNameAttribute()
{
    MetadataEntry testEntry = this.InitializeTest();
    Assert.AreEqual("Test", testEntry.Name);
}

One of my new overloads for Contains uses the name property to ensure the item exists.  I use this method to make sure that it finds the item with the name Parent, and it does successfully.

[Test()]
public void TestContainsAttribute()
{
    MetadataEntry testEntry = this.InitializeTest();
    Assert.IsTrue(testEntry.Attributes.Contains("Parent"));
}

Insert method inserts a non-duplicate item into the list, which inserts it at line 1.  This is odd, because of a problem with InsertRange (which I'll explain later).  Anyway, this test runs successfully.

[Test()]
public void TestInsertAttribute()
{
    MetadataEntry testEntry = this.InitializeTest();
    testEntry.Attributes.Insert(1, new MetadataAttribute("Cost", 12));
    Assert.AreEqual("Cost", testEntry.Attributes[1].Name);
    Assert.AreEqual(12, testEntry.Attributes[1].GetValue<int>());
}

This method tests removing an item with an attribute reference.  This is a test synonymous with the other collection, and it works great. 

[Test()]
public void TestRemoveAttribute()
{
    MetadataEntry testEntry = this.InitializeTest();

    MetadataAttribute attribute = testEntry.Attributes["Parent2"];
    Assert.AreEqual("Parent2", attribute.Name);

    int count = testEntry.Attributes.Count;
    testEntry.Attributes.Remove(attribute);
    Assert.IsTrue(testEntry.Attributes.Count == --count);
    Assert.IsTrue(!testEntry.Attributes.Contains("Parent2"));
}

On to the next problems that I will have to work on.

Comments

No Comments