public void SetValueInt_ChangesDefaultOption_ReturnsValidObject() { IDnaDiagnostics diag = mocks.DynamicMock<IDnaDiagnostics>(); diag.Stub(x => x.WriteTimedEventToLog("SiteOptionList", "Creating list from database")).Repeat.Once(); diag.Stub(x => x.WriteTimedEventToLog("SiteOptionList", "Created list from database")).Repeat.Once(); IDnaDataReader reader = mocks.DynamicMock<IDnaDataReader>(); reader.Stub(x => x.Read()).Return(true).Repeat.Once(); reader.Stub(x => x.GetInt32("Type")).Return((int)SiteOption.SiteOptionType.Int); reader.Stub(x => x.GetString("Value")).Return("1"); reader.Stub(x => x.GetString("Section")).Return("test"); reader.Stub(x => x.GetString("Name")).Return("test"); reader.Stub(x => x.GetInt32("SiteID")).Return(0); reader.Stub(x => x.GetString("description")).Return("test"); IDnaDataReaderCreator creator = mocks.DynamicMock<IDnaDataReaderCreator>(); creator.Stub(x => x.CreateDnaDataReader("getallsiteoptions")).Return(reader); creator.Stub(x => x.CreateDnaDataReader("setsiteoption")).Return(reader); mocks.ReplayAll(); SiteOptionList target = new SiteOptionList(); target.CreateFromDatabase(creator, diag); Assert.AreEqual(1, target.GetAllOptions().Count); Assert.AreEqual(1, target.GetAllOptionsDictionary().Count); Assert.AreEqual(SiteOption.SiteOptionType.Int, target.GetAllOptions()[0].OptionType); target.SetValueInt(1, "test", "test", 2, creator, diag); Assert.AreEqual(2, target.GetValueInt(1, "test", "test")); }
public void GetValueInt_NoOptions_ThrowsException() { IDnaDiagnostics diag = mocks.DynamicMock<IDnaDiagnostics>(); diag.Stub(x => x.WriteTimedEventToLog("SiteOptionList", "Creating list from database")).Repeat.Once(); diag.Stub(x => x.WriteTimedEventToLog("SiteOptionList", "Created list from database")).Repeat.Once(); IDnaDataReader reader = mocks.DynamicMock<IDnaDataReader>(); reader.Stub(x => x.Read()).Return(false).Repeat.Once(); IDnaDataReaderCreator creator = mocks.DynamicMock<IDnaDataReaderCreator>(); creator.Stub(x => x.CreateDnaDataReader("getallsiteoptions")).Return(reader); mocks.ReplayAll(); SiteOptionList target = new SiteOptionList(); try { target.GetValueInt(1, "", ""); throw new Exception("GetValueInt should have thrown exception"); } catch (SiteOptionNotFoundException) { } }
public void TestSiteOptionListValues() { Console.WriteLine("TestSiteOptionListValues"); List<SiteOptionSpec> list = ReadSiteOptionsDirectly(); SiteOptionList soList = new SiteOptionList(); soList.CreateFromDatabase(ReaderCreator, base.dnaDiagnostics); foreach (SiteOptionSpec s in list) { switch (s.type) { case 0: int v = soList.GetValueInt(s.siteId, s.section, s.name); Assert.AreEqual(int.Parse(s.value), v); try { soList.GetValueBool(s.siteId, s.section, s.name); } catch (SiteOptionInvalidTypeException ex) { Assert.AreEqual("Value is not a bool", ex.Message); } break; case 1: bool sb = false; switch (s.value) { case "0": sb = false; break; case "1": sb = true; break; default: Assert.Fail("Invalid value for bool type", s.value); break; } bool b = soList.GetValueBool(s.siteId, s.section, s.name); Assert.AreEqual(sb, b); try { soList.GetValueInt(s.siteId, s.section, s.name); } catch (SiteOptionInvalidTypeException ex) { Assert.AreEqual("Value is not an int", ex.Message); } break; case 2: string value = soList.GetValueString(s.siteId, s.section, s.name); Assert.AreEqual(s.value, value); try { soList.GetValueString(s.siteId, s.section, s.name); } catch (SiteOptionInvalidTypeException ex) { Assert.AreEqual("Value is not a string", ex.Message); } break; default: Assert.Fail("Unknown site option type", s.type); break; } } foreach (SiteOptionSpec s in list) { SiteOption so = soList.CreateSiteOption(s.siteId, s.section, s.name); Assert.AreEqual(s.siteId, so.SiteId); Assert.AreEqual(s.section, so.Section); Assert.AreEqual(s.name, so.Name); Assert.AreEqual(s.description, so.Description); switch (s.type) { case 0: Assert.AreEqual(SiteOption.SiteOptionType.Int, so.OptionType); break; case 1: Assert.AreEqual(SiteOption.SiteOptionType.Bool, so.OptionType); break; case 2: Assert.AreEqual(SiteOption.SiteOptionType.String, so.OptionType); break; default: Assert.Fail("Unknown site option type", s.type); break; } } try { SiteOption so = soList.CreateSiteOption(999, "not", "there"); } catch (SiteOptionNotFoundException ex) { Assert.AreEqual(999, (int)ex.Data["SiteID"]); Assert.AreEqual("not", (string)ex.Data["Section"]); Assert.AreEqual("there", (string)ex.Data["Name"]); } }
public void CreateFromDatabase_AddsIntOption_ReturnsValidObject() { IDnaDiagnostics diag = mocks.DynamicMock<IDnaDiagnostics>(); diag.Stub(x => x.WriteTimedEventToLog("SiteOptionList", "Creating list from database")).Repeat.Once(); diag.Stub(x => x.WriteTimedEventToLog("SiteOptionList", "Created list from database")).Repeat.Once(); IDnaDataReader reader = GetIntSiteOptionMockReader(); IDnaDataReaderCreator creator = mocks.DynamicMock<IDnaDataReaderCreator>(); creator.Stub(x => x.CreateDnaDataReader("getallsiteoptions")).Return(reader); mocks.ReplayAll(); SiteOptionList target = new SiteOptionList(); target.CreateFromDatabase(creator, diag); Assert.AreEqual(1, target.GetAllOptions().Count); Assert.AreEqual(1, target.GetAllOptionsDictionary().Count); Assert.AreEqual(SiteOption.SiteOptionType.Int, target.GetAllOptions()[0].OptionType); Assert.AreEqual(1, target.GetValueInt(1, "test", "test")); }
public void TestSiteOptionListGetMethods() { Console.WriteLine("TestSiteOptionListGetMethods"); List<SiteOptionSpec> list = ReadSiteOptionsDirectly(); SiteOptionList soList = new SiteOptionList(); soList.CreateFromDatabase(DnaMockery.CreateDatabaseReaderCreator(), dnaDiagnostics); List<SiteOptionSpec> siteZero = new List<SiteOptionSpec>(); List<SiteOptionSpec> siteNoneZero = new List<SiteOptionSpec>(); List<SiteOptionSpec> valueDiffFromSiteZero = new List<SiteOptionSpec>(); foreach (SiteOptionSpec s in list) { switch (s.siteId) { case 0 : siteZero.Add(s); break; default: siteNoneZero.Add(s); break; } } bool foundDiffInt = false; bool foundDiffBool = false; bool foundDiffString = false; foreach (SiteOptionSpec snz in siteNoneZero) { foreach (SiteOptionSpec sz in siteZero) { if (snz.section == sz.section && snz.name == sz.name && snz.value != sz.value) { if (snz.type == 0 && !foundDiffInt) { valueDiffFromSiteZero.Add(snz); foundDiffInt = true; } if (snz.type == 1 && !foundDiffBool) { valueDiffFromSiteZero.Add(snz); foundDiffBool = true; } if (snz.type == 2 && !foundDiffString) { valueDiffFromSiteZero.Add(snz); foundDiffString = true; } } } } foreach (SiteOptionSpec s in valueDiffFromSiteZero) { if (s.type == 0) { int v0 = soList.GetValueInt(0, s.section, s.name); int vs = soList.GetValueInt(s.siteId, s.section, s.name); int v999 = soList.GetValueInt(999, s.section, s.name); Assert.AreNotEqual(v0, vs, "The value of this site option should be different to site zero"); Assert.AreEqual(v0, v999, "Non-existant site should have same value as site zero"); } if (s.type == 1) { bool v0 = soList.GetValueBool(0, s.section, s.name); bool vs = soList.GetValueBool(s.siteId, s.section, s.name); bool v999 = soList.GetValueBool(999, s.section, s.name); Assert.AreNotEqual(v0, vs, "The value of this site option should be different to site zero"); Assert.AreEqual(v0, v999, "Non-existant site should have same value as site zero"); } if (s.type == 2) { string v0 = soList.GetValueString(0, s.section, s.name); string vs = soList.GetValueString(s.siteId, s.section, s.name); string v999 = soList.GetValueString(999, s.section, s.name); Assert.AreNotEqual(v0, vs, "The value of this site option should be different to site zero"); Assert.AreEqual(v0, v999, "Non-existant site should have same value as site zero"); } } try { soList.GetValueInt(0, "NotThere", "gone"); } catch (SiteOptionNotFoundException ex) { Assert.AreEqual(0, (int)ex.Data["SiteID"]); Assert.AreEqual("NotThere", (string)ex.Data["Section"]); Assert.AreEqual("gone", (string)ex.Data["Name"]); } try { soList.GetValueBool(0, "NotThere", "gone"); } catch (SiteOptionNotFoundException ex) { Assert.AreEqual(0, (int)ex.Data["SiteID"]); Assert.AreEqual("NotThere", (string)ex.Data["Section"]); Assert.AreEqual("gone", (string)ex.Data["Name"]); } try { soList.GetValueString(0, "NotThere", "gone"); } catch (SiteOptionNotFoundException ex) { Assert.AreEqual(0, (int)ex.Data["SiteID"]); Assert.AreEqual("NotThere", (string)ex.Data["Section"]); Assert.AreEqual("gone", (string)ex.Data["Name"]); } List<int> siteIdList = new List<int>(); foreach (SiteOptionSpec s in valueDiffFromSiteZero) { int siteId = s.siteId; siteIdList.Add(siteId); List<SiteOption> siteOptionListForSite = soList.GetSiteOptionListForSite(siteId); Assert.AreEqual(siteZero.Count, siteOptionListForSite.Count); foreach (SiteOption so in siteOptionListForSite) { if (so.Section == s.section && so.Name == s.name && so.SiteId == siteId) { bool valuesDiffer = false; switch (so.OptionType) { case SiteOption.SiteOptionType.Int: int a = soList.GetValueInt(0, so.Section, so.Name); int b = soList.GetValueInt(so.SiteId, so.Section, so.Name); valuesDiffer = (a != b); break; case SiteOption.SiteOptionType.Bool: bool x = soList.GetValueBool(0, so.Section, so.Name); bool y = soList.GetValueBool(so.SiteId, so.Section, so.Name); valuesDiffer = (x != y); break; case SiteOption.SiteOptionType.String: string i = soList.GetValueString(0, so.Section, so.Name); string j = soList.GetValueString(so.SiteId, so.Section, so.Name); valuesDiffer = (i != j); break; default: Assert.Fail("Unknown site option type", so.OptionType); break; } Assert.IsTrue(valuesDiffer, "Values should be different"); } } } }