-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#349 Added implementation of SPARQL DATATYPE expression
- Loading branch information
Showing
9 changed files
with
375 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
257 changes: 257 additions & 0 deletions
257
RDFSharp.Test/Query/Mirella/Algebra/Expressions/RDFDatatypeExpressionTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,257 @@ | ||
/* | ||
Copyright 2012-2024 Marco De Salvo | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific Datatypeuage governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using System.Data; | ||
using RDFSharp.Model; | ||
using RDFSharp.Query; | ||
|
||
namespace RDFSharp.Test.Query | ||
{ | ||
[TestClass] | ||
public class RDFDatatypeExpressionTest | ||
{ | ||
#region Tests | ||
[TestMethod] | ||
public void ShouldCreateDatatypeExpressionWithExpression() | ||
{ | ||
RDFDatatypeExpression expression = new RDFDatatypeExpression( | ||
new RDFAddExpression(new RDFVariable("?V1"), new RDFVariable("?V2"))); | ||
|
||
Assert.IsNotNull(expression); | ||
Assert.IsNotNull(expression.LeftArgument); | ||
Assert.IsNull(expression.RightArgument); | ||
Assert.IsTrue(expression.ToString().Equals("(DATATYPE((?V1 + ?V2)))")); | ||
Assert.IsTrue(expression.ToString([]).Equals("(DATATYPE((?V1 + ?V2)))")); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldCreateDatatypeExpressionWithVariable() | ||
{ | ||
RDFDatatypeExpression expression = new RDFDatatypeExpression( | ||
new RDFVariable("?V1")); | ||
|
||
Assert.IsNotNull(expression); | ||
Assert.IsNotNull(expression.LeftArgument); | ||
Assert.IsNull(expression.RightArgument); | ||
Assert.IsTrue(expression.ToString().Equals("(DATATYPE(?V1))")); | ||
Assert.IsTrue(expression.ToString([]).Equals("(DATATYPE(?V1))")); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldThrowExceptionOnCreatingDatatypeExpressionWithExpressionBecauseNullLeftArgument() | ||
=> Assert.ThrowsException<RDFQueryException>(() => new RDFDatatypeExpression(null as RDFExpression)); | ||
|
||
[TestMethod] | ||
public void ShouldThrowExceptionOnCreatingDatatypeExpressionWithVariableBecauseNullLeftArgument() | ||
=> Assert.ThrowsException<RDFQueryException>(() => new RDFDatatypeExpression(null as RDFVariable)); | ||
|
||
[TestMethod] | ||
public void ShouldApplyExpressionWithExpressionAndCalculateResultOnNull() | ||
{ | ||
DataTable table = new DataTable(); | ||
table.Columns.Add("?A", typeof(string)); | ||
DataRow row = table.NewRow(); | ||
row["?A"] = null; | ||
table.Rows.Add(row); | ||
table.AcceptChanges(); | ||
|
||
RDFDatatypeExpression expression = new RDFDatatypeExpression( | ||
new RDFVariableExpression(new RDFVariable("?A"))); | ||
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); | ||
|
||
Assert.IsNotNull(expressionResult); | ||
Assert.IsTrue(expressionResult.Equals(RDFVocabulary.XSD.STRING)); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldApplyExpressionWithExpressionAndCalculateResultOnResource() | ||
{ | ||
DataTable table = new DataTable(); | ||
table.Columns.Add("?A", typeof(string)); | ||
DataRow row = table.NewRow(); | ||
row["?A"] = new RDFResource("ex:subj").ToString(); | ||
table.Rows.Add(row); | ||
table.AcceptChanges(); | ||
|
||
RDFDatatypeExpression expression = new RDFDatatypeExpression( | ||
new RDFVariableExpression(new RDFVariable("?A"))); | ||
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); | ||
|
||
Assert.IsNull(expressionResult); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldApplyExpressionWithExpressionAndCalculateResultOnPlainLiteral() | ||
{ | ||
DataTable table = new DataTable(); | ||
table.Columns.Add("?A", typeof(string)); | ||
DataRow row = table.NewRow(); | ||
row["?A"] = new RDFPlainLiteral("hello").ToString(); | ||
table.Rows.Add(row); | ||
table.AcceptChanges(); | ||
|
||
RDFDatatypeExpression expression = new RDFDatatypeExpression( | ||
new RDFVariableExpression(new RDFVariable("?A"))); | ||
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); | ||
|
||
Assert.IsNotNull(expressionResult); | ||
Assert.IsTrue(expressionResult.Equals(RDFVocabulary.XSD.STRING)); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldApplyExpressionWithExpressionAndCalculateResultOnPlainLiteralWithLanguage() | ||
{ | ||
DataTable table = new DataTable(); | ||
table.Columns.Add("?A", typeof(string)); | ||
DataRow row = table.NewRow(); | ||
row["?A"] = new RDFPlainLiteral("hello","en-US").ToString(); | ||
table.Rows.Add(row); | ||
table.AcceptChanges(); | ||
|
||
RDFDatatypeExpression expression = new RDFDatatypeExpression( | ||
new RDFVariableExpression(new RDFVariable("?A"))); | ||
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); | ||
|
||
Assert.IsNotNull(expressionResult); | ||
Assert.IsTrue(expressionResult.Equals(RDFVocabulary.RDF.LANG_STRING)); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldApplyExpressionWithExpressionAndCalculateResultOnTypedLiteral() | ||
{ | ||
DataTable table = new DataTable(); | ||
table.Columns.Add("?A", typeof(string)); | ||
DataRow row = table.NewRow(); | ||
row["?A"] = new RDFTypedLiteral("hello", RDFModelEnums.RDFDatatypes.RDFS_LITERAL).ToString(); | ||
table.Rows.Add(row); | ||
table.AcceptChanges(); | ||
|
||
RDFDatatypeExpression expression = new RDFDatatypeExpression( | ||
new RDFVariableExpression(new RDFVariable("?A"))); | ||
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); | ||
|
||
Assert.IsNotNull(expressionResult); | ||
Assert.IsTrue(expressionResult.Equals(RDFVocabulary.RDFS.LITERAL)); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldApplyExpressionWithExpressionAndNotCalculateResultBecauseNotBoundVariable() | ||
{ | ||
DataTable table = new DataTable(); | ||
table.Columns.Add("?A", typeof(string)); | ||
DataRow row = table.NewRow(); | ||
row["?A"] = new RDFTypedLiteral("45", RDFModelEnums.RDFDatatypes.XSD_NORMALIZEDSTRING).ToString(); | ||
table.Rows.Add(row); | ||
table.AcceptChanges(); | ||
|
||
RDFDatatypeExpression expression = new RDFDatatypeExpression( | ||
new RDFVariableExpression(new RDFVariable("?Q"))); | ||
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); | ||
|
||
Assert.IsNull(expressionResult); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldApplyExpressionWithVariableAndCalculateResultOnResource() | ||
{ | ||
DataTable table = new DataTable(); | ||
table.Columns.Add("?A", typeof(string)); | ||
DataRow row = table.NewRow(); | ||
row["?A"] = new RDFResource("ex:subj").ToString(); | ||
table.Rows.Add(row); | ||
table.AcceptChanges(); | ||
|
||
RDFDatatypeExpression expression = new RDFDatatypeExpression( | ||
new RDFVariable("?A")); | ||
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); | ||
|
||
Assert.IsNull(expressionResult); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldApplyExpressionWithVariableAndCalculateResultOnPlainLiteral() | ||
{ | ||
DataTable table = new DataTable(); | ||
table.Columns.Add("?A", typeof(string)); | ||
DataRow row = table.NewRow(); | ||
row["?A"] = new RDFPlainLiteral("hello").ToString(); | ||
table.Rows.Add(row); | ||
table.AcceptChanges(); | ||
|
||
RDFDatatypeExpression expression = new RDFDatatypeExpression( | ||
new RDFVariable("?A")); | ||
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); | ||
|
||
Assert.IsNotNull(expressionResult); | ||
Assert.IsTrue(expressionResult.Equals(RDFVocabulary.XSD.STRING)); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldApplyExpressionWithVariableAndCalculateResultOnPlainLiteralWithLanguage() | ||
{ | ||
DataTable table = new DataTable(); | ||
table.Columns.Add("?A", typeof(string)); | ||
DataRow row = table.NewRow(); | ||
row["?A"] = new RDFPlainLiteral("hello", "en-US").ToString(); | ||
table.Rows.Add(row); | ||
table.AcceptChanges(); | ||
|
||
RDFDatatypeExpression expression = new RDFDatatypeExpression( | ||
new RDFVariable("?A")); | ||
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); | ||
|
||
Assert.IsNotNull(expressionResult); | ||
Assert.IsTrue(expressionResult.Equals(RDFVocabulary.RDF.LANG_STRING)); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldApplyExpressionWithVariableAndCalculateResultOnTypedLiteral() | ||
{ | ||
DataTable table = new DataTable(); | ||
table.Columns.Add("?A", typeof(string)); | ||
DataRow row = table.NewRow(); | ||
row["?A"] = new RDFTypedLiteral("hello", RDFModelEnums.RDFDatatypes.RDFS_LITERAL).ToString(); | ||
table.Rows.Add(row); | ||
table.AcceptChanges(); | ||
|
||
RDFDatatypeExpression expression = new RDFDatatypeExpression( | ||
new RDFVariable("?A")); | ||
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); | ||
|
||
Assert.IsNotNull(expressionResult); | ||
Assert.IsTrue(expressionResult.Equals(RDFVocabulary.RDFS.LITERAL)); | ||
} | ||
|
||
[TestMethod] | ||
public void ShouldApplyExpressionWithVariableAndNotCalculateResultBecauseNotBoundVariable() | ||
{ | ||
DataTable table = new DataTable(); | ||
table.Columns.Add("?A", typeof(string)); | ||
DataRow row = table.NewRow(); | ||
row["?A"] = new RDFTypedLiteral("45", RDFModelEnums.RDFDatatypes.XSD_NORMALIZEDSTRING).ToString(); | ||
table.Rows.Add(row); | ||
table.AcceptChanges(); | ||
|
||
RDFDatatypeExpression expression = new RDFDatatypeExpression( | ||
new RDFVariable("?Q")); | ||
RDFPatternMember expressionResult = expression.ApplyExpression(table.Rows[0]); | ||
|
||
Assert.IsNull(expressionResult); | ||
} | ||
#endregion | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.