/// <summary> /// Generates MySql configuration from TempDbMySqlServerSettings /// </summary> /// <param name="tempDbMySqlSettings"></param> /// <returns></returns> /// <exception cref="ArgumentNullException"></exception> public string GenerateFor(TempDbMySqlServerSettings tempDbMySqlSettings) { if (tempDbMySqlSettings == null) { throw new ArgumentNullException(nameof(tempDbMySqlSettings)); } var iniFile = new INI.INIFile(); iniFile.AddSection(SECTION); tempDbMySqlSettings.GetType() .GetProperties() .Select(prop => GetSetting(prop, tempDbMySqlSettings)) .Where(y => y.Key?.Length > 0) .ForEach(x => WriteSetting(iniFile, x)); tempDbMySqlSettings .CustomConfiguration ?.Select(x => new KeyValuePair <string, string>(x.Key, x.Value)) .ForEach(x => WriteSetting(iniFile, x)); return(iniFile.ToString()); }
public void WhenCustomSettingsSetToNull_ShouldStillGenerate() { // Arrange var defaultIni = new INI.INIFile(); defaultIni.Parse(DEFAULTS); var sut = Create(); var settings = new TempDbMySqlServerSettings { CustomConfiguration = null }; // Act var rawResult = sut.GenerateFor(settings); var resultIni = new INI.INIFile(); resultIni.Parse(rawResult); // Assert Expect(defaultIni.Sections) .To.Be.Equivalent.To(resultIni.Sections); }
public void WhenCustomSettingIsDuplicateOfFirstClassSetting_ShouldOverrideFirstClassSetting() { // Arrange var defaultIni = new INI.INIFile(); defaultIni.Parse(DEFAULTS); var sut = Create(); var settings = new TempDbMySqlServerSettings { MaxConnections = -GetRandomInt(), CustomConfiguration = { ["max_connections"] = GetRandomInt(1000, 2000).ToString() } }; // Act var rawResult = sut.GenerateFor(settings); var resultIni = new INI.INIFile(); resultIni.Parse(rawResult); // Assert var resultValue = resultIni.GetValue(MySqlConfigGenerator.SECTION, "max_connections"); Expect(resultValue).To.Equal(settings.CustomConfiguration["max_connections"]); }
public void WhenHasCustomSettings_ShouldEmitCustomSettings() { // Arrange var defaultIni = new INI.INIFile(); defaultIni.Parse(DEFAULTS); var sut = Create(); var settings = new TempDbMySqlServerSettings(); var key = GetRandomString(32); var value = GetRandomString(32); settings.CustomConfiguration[key] = value; // Act var rawResult = sut.GenerateFor(settings); var resultIni = new INI.INIFile(); resultIni.Parse(rawResult); // Assert var resultValue = resultIni.GetValue(MySqlConfigGenerator.SECTION, key); Expect(resultValue).To.Equal(value); }
private void WriteSetting( INI.INIFile iniFile, KeyValuePair <string, string> setting) { iniFile[SECTION][setting.Key] = setting.Value; }
public void WhenCreatingOnDefaultSettings_ShouldCreateDefaultConfig() { // Arrange var defaultIni = new INI.INIFile(); defaultIni.Parse(DEFAULTS); var sut = Create(); // Pre-Assert // Act var rawResult = sut.GenerateFor(new TempDbMySqlServerSettings()); var resultIni = new INI.INIFile(); resultIni.Parse(rawResult); // Assert Expect(defaultIni.Sections) .To.Be.Equivalent.To(resultIni.Sections); defaultIni.Sections.ForEach(section => { var expectedSettings = defaultIni[section]; // socket is unix-specific and randomly generated var resultSettings = resultIni[section].Where(kvp => kvp.Key != "socket") .ToDictionary(kvp => kvp.Key, kvp => kvp.Value); var socketSetting = resultIni[section].Where(kvp => kvp.Key == "socket"); if (socketSetting.Any()) { var kvp = socketSetting.First(); var parts = kvp.Value.Split('/'); Expect(parts).To.Contain.Only(3).Items(); Expect(parts[0]).To.Be.Empty(); Expect(parts[1]).To.Equal("tmp"); Expect(parts[2]).To.End.With(".socket"); var noExt = Path.GetFileNameWithoutExtension(parts[2]); var subParts = noExt.Split('-'); Expect(subParts[0]).To.Equal("mysql"); Expect(subParts[1]).To.Equal("temp"); var guidPart = subParts.Skip(2).JoinWith("-"); Expect(Guid.TryParse(guidPart, out var _)).To.Be.True(); } expectedSettings.ForEach(kvp => { Expect(resultSettings.TryGetValue(kvp.Key, out var value)) .To.Be.True($"result is missing setting '{kvp.Key}'"); Expect(value).To.Equal(kvp.Value, $"Mismatched values for setting '{kvp.Key}'"); }); Expect(resultSettings.Count) .To.Equal(expectedSettings.Count, () => { var extraKeys = resultSettings.Keys.Except( expectedSettings.Keys); var extraSettings = resultSettings.Where( kvp => extraKeys.Contains(kvp.Key) ).Select(KvpLine); var missingKeys = expectedSettings.Keys.Except( resultSettings.Keys); var missingSettings = expectedSettings.Where( kvp => missingKeys.Contains(kvp.Key) ).Select(KvpLine); var final = new List <string>(); if (missingSettings.Any()) { final.Add("Missing settings;"); final.AddRange(missingSettings); } if (extraSettings.Any()) { final.Add("Extra settings:"); final.AddRange(extraSettings); } return(final.JoinWith("\n")); string KvpLine(KeyValuePair <string, string> kvp) { return($"{kvp.Key} = {kvp.Value}"); } }); }); }