Esempio n. 1
0
        public void CreateSiteOption_SiteSpecificOption_ReturnsCorrectValue()
        {
            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(1);
            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);

            SiteOption actual = target.CreateSiteOption(1, "test", "test");
            Assert.AreEqual(1, actual.SiteId);
        }
Esempio n. 2
0
        public void CreateSiteOption_NoOption_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>();
            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();

            try
            {
                SiteOption actual = target.CreateSiteOption(1, "test", "test");
                throw new Exception("CreateSiteOption should not exist");
            }
            catch (SiteOptionNotFoundException)
            { }
        }
Esempio n. 3
0
        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"]);
            }
        }