Commit c7cba753 authored by Marc Gravell's avatar Marc Gravell

auto-load redisearch.so for the FT tests

parent c653e62e
using System;
using System;
using Xunit;
using StackExchange.Redis;
using System.Collections.Generic;
using System.Linq;
using Xunit.Abstractions;
using System.IO;
namespace NRediSearch.Test
{
......@@ -10,10 +12,59 @@ public class ExampleUsage : IDisposable
{
private ConnectionMultiplexer conn;
private IDatabase db;
public ExampleUsage()
private ITestOutputHelper Output { get; }
public ExampleUsage(ITestOutputHelper output)
{
conn = ConnectionMultiplexer.Connect("127.0.0.1:6379");
Output = output;
const string ep = "127.0.0.1:6379";
var options = new ConfigurationOptions
{
EndPoints = {ep},
AllowAdmin = true
};
conn = ConnectionMultiplexer.Connect(options);
db = conn.GetDatabase();
var server = conn.GetServer(ep);
var arr = (RedisResult[])server.Execute("module", "list");
bool found = false;
foreach(var module in arr)
{
var parsed = Parse(module);
if(parsed.TryGetValue("name", out var val) && val == "ft")
{
found = true;
if(parsed.TryGetValue("ver", out val))
Output.WriteLine($"Version: {val}");
break;
}
}
if (!found)
{
Output.WriteLine("Module not found; attempting to load...");
var config = server.Info("server").SelectMany(_ => _).FirstOrDefault(x => x.Key == "config_file").Value;
if(!string.IsNullOrEmpty(config))
{
var i = config.LastIndexOf('/');
var modulePath = config.Substring(0, i + 1) + "redisearch.so";
var result = server.Execute("module", "load", modulePath);
Output.WriteLine((string)result);
}
}
}
static Dictionary<string, RedisValue> Parse(RedisResult module)
{
var data = new Dictionary<string, RedisValue>();
var lines = (RedisResult[])module;
for(int i = 0; i < lines.Length;)
{
var key = (string)lines[i++];
var value = (RedisValue)lines[i++];
data[key] = value;
}
return data;
}
public void Dispose()
......@@ -48,8 +99,8 @@ public void BasicUsage()
{
Console.WriteLine(ex.Message);
Console.WriteLine("Module not installed, aborting");
return; // the module isn't installed
}
throw;
}
Assert.True(result);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment