Пример #1
0
        public void ShouldExpandTokensLooseMatchingTest()
        {
            Tokenizer tokenizer;
            Mockery mockery;
            IDictionary<string, ITokenReplacementStrategy> mockTokenReplacementStrategies;
            ITokenReplacementStrategy mockTokenReplacementStrategy;
            string tokenizedValue;
            string expandedValue;
            string expectedValue;

            mockery = new Mockery();
            mockTokenReplacementStrategies = mockery.NewMock<IDictionary<string, ITokenReplacementStrategy>>();
            mockTokenReplacementStrategy = mockery.NewMock<ITokenReplacementStrategy>();

            Expect.Once.On(mockTokenReplacementStrategies).Method("TryGetValue").With(new EqualMatcher("myValueSemanticToken"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), ForceTrueMatcher.Instance)).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true));
            Expect.Once.On(mockTokenReplacementStrategy).Method("Evaluate").With(new object[] { null }).Will(Return.Value("testValue"));

            Expect.Once.On(mockTokenReplacementStrategies).Method("TryGetValue").With(new EqualMatcher("myFunctionSemanticToken0"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), ForceTrueMatcher.Instance)).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true));
            Expect.Once.On(mockTokenReplacementStrategy).Method("Evaluate").With(new object[] { new object[] { } }).Will(Return.Value("testValue"));

            Expect.Once.On(mockTokenReplacementStrategies).Method("TryGetValue").With(new EqualMatcher("myFunctionSemanticToken1"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), ForceTrueMatcher.Instance)).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true));
            Expect.Once.On(mockTokenReplacementStrategy).Method("Evaluate").With(new object[] { new object[] { "a" } }).Will(Return.Value("testValue"));

            Expect.Once.On(mockTokenReplacementStrategies).Method("TryGetValue").With(new EqualMatcher("myFunctionSemanticToken2"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), ForceTrueMatcher.Instance)).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true));
            Expect.Once.On(mockTokenReplacementStrategy).Method("Evaluate").With(new object[] { new object[] { "a", "b" } }).Will(Return.Value("testValue"));

            Expect.Once.On(mockTokenReplacementStrategies).Method("TryGetValue").With(new EqualMatcher("myUnkSemanticToken"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), ForceTrueMatcher.Instance)).Will(new SetNamedParameterAction("value", null), Return.Value(false));

            Expect.Once.On(mockTokenReplacementStrategies).Method("TryGetValue").With(new EqualMatcher("myErrSemanticToken"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), ForceTrueMatcher.Instance)).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true));
            Expect.Once.On(mockTokenReplacementStrategy).Method("Evaluate").With(new object[] { null }).Will(Throw.Exception(new Exception()));

            Expect.Once.On(mockTokenReplacementStrategies).Method("TryGetValue").With(new EqualMatcher("a"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), ForceTrueMatcher.Instance)).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true));
            Expect.Once.On(mockTokenReplacementStrategy).Method("Evaluate").With(new object[] { null }).Will(Return.Value(""));

            Expect.Once.On(mockTokenReplacementStrategies).Method("TryGetValue").With(new EqualMatcher("b"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), ForceTrueMatcher.Instance)).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true));
            Expect.Once.On(mockTokenReplacementStrategy).Method("Evaluate").With(new object[] { null }).Will(Return.Value(""));

            Expect.Once.On(mockTokenReplacementStrategies).Method("TryGetValue").With(new EqualMatcher("c"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), ForceTrueMatcher.Instance)).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true));
            Expect.Once.On(mockTokenReplacementStrategy).Method("Evaluate").With(new object[] { null }).Will(Return.Value(""));

            Expect.Once.On(mockTokenReplacementStrategies).Method("TryGetValue").With(new EqualMatcher("d"), new AndMatcher(new ArgumentsMatcher.OutMatcher(), ForceTrueMatcher.Instance)).Will(new SetNamedParameterAction("value", mockTokenReplacementStrategy), Return.Value(true));
            Expect.Once.On(mockTokenReplacementStrategy).Method("Evaluate").With(new object[] { null }).Will(Throw.Exception(new Exception()));

            tokenizer = new Tokenizer(mockTokenReplacementStrategies, false);

            tokenizedValue = "";
            expandedValue = tokenizer.ExpandTokens(tokenizedValue);
            expectedValue = "";
            Assert.AreEqual(expectedValue, expandedValue);

            tokenizedValue = "...{myNoSemanticToken}...";
            expandedValue = tokenizer.ExpandTokens(tokenizedValue);
            expectedValue = "...{myNoSemanticToken}...";
            Assert.AreEqual(expectedValue, expandedValue);

            tokenizedValue = "...${myValueSemanticToken}...";
            expandedValue = tokenizer.ExpandTokens(tokenizedValue);
            expectedValue = "...testValue...";
            Assert.AreEqual(expectedValue, expandedValue);

            tokenizedValue = "...${myFunctionSemanticToken0()}...";
            expandedValue = tokenizer.ExpandTokens(tokenizedValue);
            expectedValue = "...testValue...";
            Assert.AreEqual(expectedValue, expandedValue);

            tokenizedValue = "...${myFunctionSemanticToken1(`a`)}...";
            expandedValue = tokenizer.ExpandTokens(tokenizedValue);
            expectedValue = "...testValue...";
            Assert.AreEqual(expectedValue, expandedValue);

            tokenizedValue = "...${myFunctionSemanticToken2(`a`,  `b`)}...";
            expandedValue = tokenizer.ExpandTokens(tokenizedValue);
            expectedValue = "...testValue...";
            Assert.AreEqual(expectedValue, expandedValue);

            tokenizedValue = "...${myUnkSemanticToken}...";
            expandedValue = tokenizer.ExpandTokens(tokenizedValue);
            expectedValue = "...${myUnkSemanticToken}...";
            Assert.AreEqual(expectedValue, expandedValue);

            tokenizedValue = "...${myErrSemanticToken}...";
            expandedValue = tokenizer.ExpandTokens(tokenizedValue);
            expectedValue = "...${myErrSemanticToken}...";
            Assert.AreEqual(expectedValue, expandedValue);

            tokenizedValue = "...${a}...${c}...${b}...${d}...";
            expandedValue = tokenizer.ExpandTokens(tokenizedValue);
            expectedValue = "............${d}...";
            Assert.AreEqual(expectedValue, expandedValue);

            Assert.IsNotNull(tokenizer.OrderedPreviousExpansionTokens);
            Assert.AreEqual("a,b,c,d", string.Join(",", tokenizer.OrderedPreviousExpansionTokens));

            mockery.VerifyAllExpectationsHaveBeenMet();
        }
Пример #2
0
        private static void WriteSqlQuery(IEnumerable<SqlQuery> sqlQueries, IAssociativeXmlObject parentAssociativeXmlObject, Type connectionType, string connectionString, bool getSchemaOnly)
        {
            ArrayConstruct arrayConstruct;
            ObjectConstruct objectConstruct;
            PropertyConstruct propertyConstruct;
            Tokenizer tokenizer;

            IList<IDictionary<string, object>> objs;
            string commandText;
            int rcecordsAffected;

            if ((object)sqlQueries == null)
                throw new ArgumentNullException("sqlQueries");

            if ((object)parentAssociativeXmlObject == null)
                throw new ArgumentNullException("parentAssociativeXmlObject");

            if ((object)connectionType == null)
                throw new ArgumentNullException("connectionType");

            if ((object)connectionString == null)
                throw new ArgumentNullException("connectionString");

            if (DataType.IsWhiteSpace(connectionString))
                throw new ArgumentOutOfRangeException("connectionString");

            tokenizer = new Tokenizer(true);

            foreach (SqlQuery sqlQuery in sqlQueries.OrderBy(c => c.Key).ThenBy(c => c.Order))
            {
                arrayConstruct = new ArrayConstruct();
                arrayConstruct.Name = sqlQuery.Key;
                parentAssociativeXmlObject.Items.Add(arrayConstruct);

                commandText = tokenizer.ExpandTokens(sqlQuery.Text, new DynamicWildcardTokenReplacementStrategy(new object[] { parentAssociativeXmlObject }));

                using (IUnitOfWorkContext unitOfWorkContext = UnitOfWorkContext.Create(connectionType, connectionString, false))
                {
                    if (!getSchemaOnly)
                        objs = unitOfWorkContext.ExecuteDictionary(sqlQuery.Type, commandText, null, out rcecordsAffected);
                    else
                        objs = unitOfWorkContext.ExecuteSchema(sqlQuery.Type, commandText, null);
                }

                if ((object)objs != null)
                {
                    propertyConstruct = new PropertyConstruct();
                    propertyConstruct.Name = "RowCount";
                    propertyConstruct.RawValue = objs.Count;
                    arrayConstruct.Items.Add(propertyConstruct);

                    foreach (IDictionary<string, object> obj in objs)
                    {
                        objectConstruct = new ObjectConstruct();
                        arrayConstruct.Items.Add(objectConstruct);

                        if ((object)obj != null)
                        {
                            foreach (KeyValuePair<string, object> keyValuePair in obj)
                            {
                                propertyConstruct = new PropertyConstruct();
                                propertyConstruct.Name = keyValuePair.Key;
                                propertyConstruct.RawValue = keyValuePair.Value;

                                objectConstruct.Items.Add(propertyConstruct);
                            }
                        }

                        // correlated
                        WriteSqlQuery(sqlQuery.SubQueries, objectConstruct, connectionType, connectionString, getSchemaOnly);
                    }
                }
            }
        }