DeveelDB  20151217
complete SQL database system, primarly developed for .NET/Mono frameworks
FunctionBuildTests.cs
Go to the documentation of this file.
1 //
2 // Copyright 2010-2014 Deveel
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 using System;
16 
17 using Deveel.Data;
18 using Deveel.Data.Sql.Fluid;
19 using Deveel.Data.Sql.Objects;
20 using Deveel.Data.Types;
21 
22 using NUnit.Framework;
23 
24 namespace Deveel.Data.Routines {
25  [TestFixture]
27  [Test]
28  public void ScalarWithNoArguments() {
29  FunctionProvider factory1 = null;
30  Assert.DoesNotThrow(() => factory1 = new Factory1());
31 
32  IFunction function = null;
33  Assert.DoesNotThrow(() => function = factory1.ResolveFunction("user2"));
34  Assert.IsNotNull(function);
35 
36  InvokeResult result=null;
37  Assert.DoesNotThrow(() => result = function.Execute(Query));
38  Assert.IsNotNull(result);
39  Assert.AreEqual(AdminUserName, result.ReturnValue.Value.ToString());
40  }
41 
42  [Test]
43  public void ScalarWithTwoArgument() {
44  Factory2 factory2 = null;
45  Assert.DoesNotThrow(() => factory2 = new Factory2());
46 
47  IFunction function = null;
48  var args = new DataObject[] {DataObject.BigInt(2), DataObject.Number(new SqlNumber(54))};
49  Assert.DoesNotThrow(() => function = factory2.ResolveFunction("add2", args));
50  Assert.IsNotNull(function);
51 
52  InvokeResult result = null;
53  Assert.DoesNotThrow(() => result = function.Execute(args));
54  Assert.IsNotNull(result);
55 
56  Assert.IsInstanceOf<SqlNumber>(result.ReturnValue.Value);
57 
58  var value = ((SqlNumber) result.ReturnValue.Value).ToInt64();
59  Assert.AreEqual(56, value);
60  }
61 
62  #region Factory1
63 
65  public override string SchemaName {
66  get { return "APP"; }
67  }
68 
69  protected override void OnInit() {
70  Register(config => config.Named("user2")
71  .ReturnsType(PrimitiveTypes.String())
72  .WhenExecute(context => context.Result(DataObject.String(context.Request.User().Name))));
73  }
74  }
75 
76  #endregion
77 
78  #region Factory2
79 
81  public override string SchemaName {
82  get { return "APP"; }
83  }
84 
85  protected override void OnInit() {
86  Register(config => config.Named("add2")
87  .WithNumericParameter("a")
88  .WithNumericParameter("b")
89  .ReturnsNumeric()
90  .WhenExecute(context => {
91  var a = context.EvaluatedArguments[0];
92  var b = context.EvaluatedArguments[1];
93  return context.Result(a.Add(b));
94  }));
95  }
96  }
97 
98  #endregion
99  }
100 }
Provides some helper functions for resolving and creating SqlType instances that are primitive to the...
Defines a routine that is a function, that means it returns a value after its execution.
Definition: IFunction.cs:26
static DataObject Number(SqlNumber value)
Definition: DataObject.cs:552
ISqlObject Value
Gets the underlined value that is handled.
Definition: DataObject.cs:84
static DataObject String(string s)
Definition: DataObject.cs:592
Represents the result of the execution of a routine.
Definition: InvokeResult.cs:25
Represents a dynamic object that encapsulates a defined SqlType and a compatible constant ISqlObject ...
Definition: DataObject.cs:35
DataObject ReturnValue
If the context of the result is a function, gets the return value of the function.
Definition: InvokeResult.cs:59
long IConvertible. ToInt64(IFormatProvider provider)
Definition: SqlNumber.cs:305
IFunction ResolveFunction(Invoke invoke, IQuery query)
static DataObject BigInt(long value)
Definition: DataObject.cs:580