Exemplo n.º 1
0
        public void GetSiteOptionListForSite_DefaultOption_ReturnsCorrectList()
        {
            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.Bool);
            reader.Stub(x => x.GetString("Value")).Return("0");
            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.Bool, target.GetAllOptions()[0].OptionType);

            List<SiteOption> actual = target.GetSiteOptionListForSite(1);
            Assert.AreEqual(1, actual.Count);
            Assert.AreEqual("test", actual[0].Name);
            Assert.IsTrue(actual[0].IsGlobal);


        }
Exemplo n.º 2
0
        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");
                    }
                }
            }
        }