Пример #1
0
    public async Task XmlRoundTripsToActualRedisServer()
    {
        var connStr = TestRedisServer.GetConnectionString();

        _output.WriteLine("Attempting to connect to " + connStr);

        var      guid = Guid.NewGuid().ToString();
        RedisKey key  = "Test:DP:Key" + guid;

        try
        {
            using (var redis = await ConnectionMultiplexer.ConnectAsync(connStr).TimeoutAfter(TimeSpan.FromMinutes(1)))
            {
                var repo    = new RedisXmlRepository(() => redis.GetDatabase(), key);
                var element = new XElement("HelloRedis", guid);
                repo.StoreElement(element, guid);
            }

            using (var redis = await ConnectionMultiplexer.ConnectAsync(connStr).TimeoutAfter(TimeSpan.FromMinutes(1)))
            {
                var repo     = new RedisXmlRepository(() => redis.GetDatabase(), key);
                var elements = repo.GetAllElements();

                Assert.Contains(elements, e => e.Name == "HelloRedis" && e.Value == guid);
            }
        }
        finally
        {
            // cleanup
            using (var redis = await ConnectionMultiplexer.ConnectAsync(connStr).TimeoutAfter(TimeSpan.FromMinutes(1)))
            {
                await redis.GetDatabase().KeyDeleteAsync(key);
            }
        }
    }
Пример #2
0
    public void GetAllElements_ThrowsParsingException()
    {
        var database = new Mock <IDatabase>();

        database.Setup(d => d.ListRange("Key", 0, -1, CommandFlags.None)).Returns(new RedisValue[]
        {
            "<Element1/>",
            "<Element2",
        }).Verifiable();
        var repo = new RedisXmlRepository(() => database.Object, "Key");

        Assert.Throws <XmlException>(() => repo.GetAllElements());
    }
Пример #3
0
    public void GetAllElements_ReturnsAllXmlValuesForGivenKey()
    {
        var database = new Mock <IDatabase>();

        database.Setup(d => d.ListRange("Key", 0, -1, CommandFlags.None)).Returns(new RedisValue[]
        {
            "<Element1/>",
            "<Element2/>",
        }).Verifiable();
        var repo = new RedisXmlRepository(() => database.Object, "Key");

        var elements = repo.GetAllElements().ToArray();

        database.Verify();
        Assert.Equal(new XElement("Element1").ToString(), elements[0].ToString());
        Assert.Equal(new XElement("Element2").ToString(), elements[1].ToString());
    }