Create a fixture for recursive data structure with AutoFixture
Asked Answered
S

1

8

I'm working on a project where I have some recursive data structure and I want to create a fixture for it.

The data structure is XmlCommandElement, it has a single method ToCommand that converts XmlCommandElement to Command.

Each node on the tree can be a XmlCommandElement and/or XmlCommandPropertyElement.

Now, in order to test the behaviour of the method ToCommand I want to fetch XmlCommandElement with some arbitrary data.

I want to control the depth of the tree and the amount of instances of XmlCommandElement and/or XmlCommandPropertyElement per node.

So here is the code I'm using for the fixture:

public class XmlCommandElementFixture : ICustomization
{
    private static readonly Fixture _fixture = new Fixture();

    private XmlCommandElement _xmlCommandElement;

    public int MaxCommandsPerDepth { get; set; }

    public int MaxDepth { get; set; }

    public int MaxPropertiesPerCommand { get; set; }

    public XmlCommandElementFixture BuildCommandTree()
    {
        _xmlCommandElement = new XmlCommandElement();

        var tree = new Stack<XmlCommandElementNode>();

        tree.Push(new XmlCommandElementNode(0, _xmlCommandElement));

        while (tree.Count > 0) {
            var node = tree.Pop();
            node.Command.Key = CreateRandomString();
            node.Command.Properties = CreateProperties();

            if (MaxDepth > node.Depth) {
                var commands = new List<XmlCommandElement>();

                for (var i = 0; i < MaxCommandsPerDepth; i++) {
                    var command = new XmlCommandElement();
                    tree.Push(new XmlCommandElementNode(node.Depth + 1, command));
                    commands.Add(command);
                }

                node.Command.Commands = commands.ToArray();
            }
        }

        return this;
    }

    public void Customize(IFixture fixture)
    {
        fixture.Customize<XmlCommandElement>(c => c.FromFactory(() => _xmlCommandElement)
                                                   .OmitAutoProperties());
    }

    private static string CreateRandomString()
    {
        return _fixture.Create<Generator<string>>().First();
    }

    private XmlCommandPropertyElement[] CreateProperties()
    {
        var properties = new List<XmlCommandPropertyElement>();

        for (var i = 0; i < MaxPropertiesPerCommand; i++) {
            properties.Add(new XmlCommandPropertyElement {
                Key = CreateRandomString(),
                Value = CreateRandomString()
            });
        }

        return properties.ToArray();
    }

    private struct XmlCommandElementNode
    {
        public XmlCommandElementNode(int depth, XmlCommandElement xmlCommandElement)
        {
            Depth = depth;

            Command = xmlCommandElement;
        }

        public XmlCommandElement Command { get; }

        public int Depth { get; }
    }
}

And this is how I'm using it:

xmlCommandElement = new Fixture().Customize(new XmlCommandElementFixture {
    MaxDepth = 2,
    MaxCommandsPerDepth = 3,
    MaxPropertiesPerCommand = 4
}.BuildCommandTree()).Create<XmlCommandElement>();

This works perfectly fine! but the issue I have with it is it isn't generic, the whole point of AutoFixture at least as far as I know is to avoid making specific fixtures.

So what I would really like to do is something like this (found it here but it doesn't work for me.):

var fixture = new Fixture();
fixture.Behaviors.OfType<ThrowingRecursionBehavior>()
       .ToList()
       .ForEach(b => fixture.Behaviors.Remove(b));
fixture.Behaviors.Add(new DepthThrowingRecursionBehavior(2));
fixture.Behaviors.Add(new OmitOnRecursionForRequestBehavior(typeof(XmlCommandElement), 3));
fixture.Behaviors.Add(new OmitOnRecursionForRequestBehavior(typeof(XmlCommandPropertyElement), 4));

xmlCommandElement = fixture.Create<XmlCommandElement>();

Here is all the code for reference:

Interfaces:

public interface ICommandCollection : IEnumerable<ICommand>
{
    ICommand this[string commandName] { get; }

    void Add(ICommand command);
}

public interface ICommandPropertyCollection : IEnumerable<ICommandProperty>
{
    string this[string key] { get; }

    void Add(ICommandProperty property);
}

public interface ICommandProperty
{
    string Key { get; }

    string Value { get; }
}

public interface ICommand
{
    ICommandCollection Children { get; set; }

    string Key { get; }

    ICommandPropertyCollection Properties { get; }
}

public interface ICommandConvertible
{
    ICommand ToCommand();
}

Classes:

public sealed class CommandPropertyCollection : ICommandPropertyCollection
{
    private readonly IDictionary<string, ICommandProperty> _properties;

    public CommandPropertyCollection()
    {
        _properties = new ConcurrentDictionary<string, ICommandProperty>();
    }

    public string this[string key]
    {
        get
        {
            ICommandProperty property = null;

            _properties.TryGetValue(key, out property);

            return property.Value;
        }
    }

    public void Add(ICommandProperty property)
    {
        _properties.Add(property.Key, property);
    }

    public IEnumerator<ICommandProperty> GetEnumerator()
    {
        return _properties.Values.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

public sealed class CommandProperty : ICommandProperty
{
    public CommandProperty(string key, string value)
    {
        Key = key;

        Value = value;
    }

    public string Key { get; }

    public string Value { get; }
}

public sealed class Command : ICommand
{
    public Command(string key, ICommandPropertyCollection properties)
    {
        Key = key;

        Properties = properties;
    }

    public ICommandCollection Children { get; set; }

    public string Key { get; }

    public ICommandPropertyCollection Properties { get; }
}

public class XmlCommandPropertyElement : ICommandPropertyConvertible
{
    [XmlAttribute("key")]
    public string Key { get; set; }

    [XmlAttribute("value")]
    public string Value { get; set; }

    public ICommandProperty ToCommandProperty()
    {
        return new CommandProperty(Key, Value);
    }
}

Finally, the class I'm trying to test is as follow:

public class XmlCommandElement : ICommandConvertible
{
    [XmlArray]
    [XmlArrayItem("Command", typeof(XmlCommandElement))]
    public XmlCommandElement[] Commands { get; set; }

    [XmlAttribute("key")]
    public string Key { get; set; }

    [XmlArray]
    [XmlArrayItem("Property", typeof(XmlCommandPropertyElement))]
    public XmlCommandPropertyElement[] Properties { get; set; }

    public ICommand ToCommand()
    {
        ICommandPropertyCollection properties = new CommandPropertyCollection();

        foreach (var property in Properties) {
            properties.Add(property.ToCommandProperty());
        }

        ICommand command = new Command(Key, properties);

        return command;
    }
}

The test itself looks like this:

namespace Yalla.Tests.Commands
{
    using Fixtures;

    using FluentAssertions;

    using Ploeh.AutoFixture;

    using Xbehave;

    using Yalla.Commands;
    using Yalla.Commands.Xml;

    public class XmlCommandElementTests
    {
        [Scenario]
        public void ConvertToCommand(XmlCommandElement xmlCommandElement, ICommand command)
        {
            $"Given an {nameof(XmlCommandElement)}"
                .x(() =>
                {
                    xmlCommandElement = new Fixture().Customize(new XmlCommandElementFixture {
                        MaxDepth = 2,
                        MaxCommandsPerDepth = 3,
                        MaxPropertiesPerCommand = 4
                    }.BuildCommandTree()).Create<XmlCommandElement>();
                });

            $"When the object is converted into {nameof(ICommand)}"
                .x(() => command = xmlCommandElement.ToCommand());

            "Then we need to have a root object with a key"
                .x(() => command.Key.Should().NotBeNullOrEmpty());

            "And 4 properties as its children"
                .x(() => command.Properties.Should().HaveCount(4));
        }
    }
}

Thanks to Mark Seemann! the final solution looks like this:

public class RecursiveCustomization : ICustomization
{
    public int MaxDepth { get; set; }

    public int MaxElements { get; set; }

    public void Customize(IFixture fixture)
    {
        fixture.Behaviors
               .OfType<ThrowingRecursionBehavior>()
               .ToList()
               .ForEach(b => fixture.Behaviors.Remove(b));
        fixture.Behaviors.Add(new OmitOnRecursionBehavior(MaxDepth));
        fixture.RepeatCount = MaxElements;
    }
}

And can be used like this:

xmlCommandElement = new Fixture().Customize(new RecursiveCustomization {
    MaxDepth = 2,
    MaxElements = 3
}).Create<XmlCommandElement>();
Sensual answered 2/6, 2016 at 7:8 Comment(6)
You should have small units to test, very small method, very small classes. If you watching data go through an entire process then you are doing integration tests which can be larger. However, you can just quickly test your tests to make sure they're giving the right output for you test, however, i think it is trying to show you a code smell, and that you should refactor your methods to be smaller.Midkiff
@CallumLinington thank you for the comment! but I'm not sure how is it related, I have recursive data structure that I want to load it with some data in order to test it, this fixture class is responsible to generate the data, it isn't the class I'm actually testing! the class I'm actually testing is XmlCommandElement.Sensual
Does this answer your question? https://mcmap.net/q/735896/-creating-recursive-tree-with-autofixture/126014Tight
@MarkSeemann I tried it and it doesn't work for me, I get "System.InvalidOperationException".Sensual
What's XmlCommandPropertyElement? What's ICommand? What's CommandPropertyCollection? ICommandPropertyCollection? What are you actually trying to accomplish?Tight
@MarkSeemann I've updated the post with more information, I hope it's clear now, if not please let me know and I'll elaborate farther.Sensual
T
12

You can fairly easily create a small tree by changing the Fixture's recursion behaviour:

[Fact]
public void CreateSmallTree()
{
    var fixture = new Fixture();
    fixture.Behaviors
        .OfType<ThrowingRecursionBehavior>()
        .ToList()
        .ForEach(b => fixture.Behaviors.Remove(b));
    fixture.Behaviors.Add(new OmitOnRecursionBehavior(recursionDepth: 2));

    var xce = fixture.Create<XmlCommandElement>();

    Assert.NotEmpty(xce.Commands);
}

The above test passes.

Tight answered 5/6, 2016 at 10:24 Comment(3)
Great! beyond depth can I control the amount of generated instances? like having say 5 instances of XmlCommandElement and 6 XmlCommandPropertyElement per node?Sensual
@EyalSolnik You can use RepeatCount to universally change how many elements are generated whenever 'many' elements are requested, but not more granular than that.Tight
I can't thank you enough! thank you! I just didn't want to rely on default values on my tests and RepeatCount gets the job done!Sensual

© 2022 - 2024 — McMap. All rights reserved.