public void HandleDateParam() { // Brian: Not sure tha these tests SHOULD pass... // a time component on the Date? var p1 = new DateValue(new DateTimeOffset(1972, 11, 30, 15, 20, 49, TimeSpan.Zero)); Assert.AreEqual("1972-11-30", p1.ToString()); // we can parse a valid FHIR datetime and strip the time part off // (but it must be a valid FHIR datetime) var p2 = DateValue.Parse("1972-11-30T18:45:36Z"); Assert.AreEqual("1972-11-30", p2.ToString()); var crit = Criterium.Parse("paramX=1972-11-30"); var p3 = ((UntypedValue)crit.Operand).AsDateValue(); Assert.AreEqual("1972-11-30", p3.Value); try { // Test with an invalid FHIR datetime (no timezone specified) var p4 = DateValue.Parse("1972-11-30T18:45:36"); Assert.Fail("The datetime [1972-11-30T18:45:36] does not have a timezone, hence should fail parsing as a datevalue (via fhirdatetime)"); } catch (ArgumentException) { } }
public void ParseCriteriumDSTU1() { var crit = Criterium.Parse("paramX=18"); Assert.AreEqual("paramX", crit.ParamName); Assert.IsNull(crit.Modifier); Assert.AreEqual("18", crit.Operand.ToString()); Assert.AreEqual(Operator.EQ, crit.Operator); crit = Criterium.Parse("paramX=>18"); Assert.AreEqual("paramX", crit.ParamName); Assert.IsNull(crit.Modifier); Assert.AreEqual("18", crit.Operand.ToString()); Assert.AreEqual(Operator.GT, crit.Operator); crit = Criterium.Parse("paramX:modif1=~18"); Assert.AreEqual("paramX", crit.ParamName); Assert.AreEqual("18", crit.Operand.ToString()); Assert.AreEqual("modif1", crit.Modifier); Assert.AreEqual(Operator.APPROX, crit.Operator); crit = Criterium.Parse("paramX:missing=true"); Assert.AreEqual("paramX", crit.ParamName); Assert.IsNull(crit.Operand); Assert.IsNull(crit.Modifier); Assert.AreEqual(Operator.ISNULL, crit.Operator); crit = Criterium.Parse("paramX:missing=false"); Assert.AreEqual("paramX", crit.ParamName); Assert.IsNull(crit.Operand); Assert.IsNull(crit.Modifier); Assert.AreEqual(Operator.NOTNULL, crit.Operator); }
/// <summary> /// Change something like Condition/subject:Patient=Patient/10014 /// to Condition/subject:Patient.internal_id=Patient/10014, so it is correctly handled as a chained parameter, /// including the filtering on the type in the modifier (if any). /// </summary> /// <param name="criteria"></param> /// <param name="resourceType"></param> /// <returns></returns> private List <Criterium> NormalizeNonChainedReferenceCriteria(List <Criterium> criteria, string resourceType) { var result = new List <Criterium>(); foreach (var crit in criteria) { var critSp = crit.FindSearchParamDefinition(resourceType); if (critSp != null && critSp.Type == Conformance.SearchParamType.Reference && crit.Type != Operator.CHAIN) { var subCrit = new Criterium(); subCrit.ParamName = InternalField.ID; subCrit.Type = crit.Type; subCrit.Operand = crit.Operand; var superCrit = new Criterium(); superCrit.ParamName = crit.ParamName; superCrit.Modifier = crit.Modifier; superCrit.Type = Operator.CHAIN; superCrit.Operand = subCrit; result.Add(superCrit); } else { result.Add(crit); } } return(result); }
public Criterium Clone() { Criterium result = new Criterium(); result.Modifier = Modifier; result.Operand = (Operand is Criterium) ? (Operand as Criterium).Clone() : Operand; result.Operator = Operator; result.ParamName = ParamName; return(result); }
public void HandleDateTimeParam() { var p1 = new FhirDateTime(new DateTimeOffset(1972, 11, 30, 15, 20, 49, TimeSpan.Zero)); Assert.AreEqual("1972-11-30T15:20:49+00:00", p1.Value.ToString()); var crit = Criterium.Parse("paramX=1972-11-30T18:45:36Z"); var p3 = ((UntypedValue)crit.Operand).AsDateValue(); Assert.AreEqual("1972-11-30", p3.Value); var p4 = ((UntypedValue)crit.Operand).AsDateTimeValue(); Assert.AreEqual("1972-11-30T18:45:36Z", p4.Value); }
public void HandleReferenceParam() { var p1 = new ReferenceValue("2"); Assert.AreEqual("2", p1.Value); var p2 = new ReferenceValue("http://server.org/fhir/Patient/1"); Assert.AreEqual("http://server.org/fhir/Patient/1", p2.Value); var crit = Criterium.Parse(@"paramX=http://server.org/\$4/fhir/Patient/1"); var p3 = ((UntypedValue)crit.Operand).AsReferenceValue(); Assert.AreEqual("http://server.org/$4/fhir/Patient/1", p3.Value); }
public void HandleTokenParam() { var p1 = new TokenValue("NOK", "http://somewhere.nl/codes"); Assert.AreEqual("http://somewhere.nl/codes|NOK", p1.ToString()); var p2 = new TokenValue("y|n", "http://some|where.nl/codes"); Assert.AreEqual(@"http://some\|where.nl/codes|y\|n", p2.ToString()); var p3 = new TokenValue("NOK", matchAnyNamespace: true); Assert.AreEqual("NOK", p3.ToString()); var p4 = new TokenValue("NOK", matchAnyNamespace: false); Assert.AreEqual("|NOK", p4.ToString()); var p5 = TokenValue.Parse("http://somewhere.nl/codes|NOK"); Assert.AreEqual("http://somewhere.nl/codes", p5.Namespace); Assert.AreEqual("NOK", p5.Value); Assert.IsFalse(p4.AnyNamespace); var p6 = TokenValue.Parse(@"http://some\|where.nl/codes|y\|n"); Assert.AreEqual(@"http://some|where.nl/codes", p6.Namespace); Assert.AreEqual("y|n", p6.Value); Assert.IsFalse(p6.AnyNamespace); var p7 = TokenValue.Parse("|NOK"); Assert.AreEqual(null, p7.Namespace); Assert.AreEqual("NOK", p7.Value); Assert.IsFalse(p7.AnyNamespace); var p8 = TokenValue.Parse("NOK"); Assert.AreEqual(null, p8.Namespace); Assert.AreEqual("NOK", p8.Value); Assert.IsTrue(p8.AnyNamespace); var crit = Criterium.Parse("paramX=|NOK"); var p9 = ((UntypedValue)crit.Operand).AsTokenValue(); Assert.AreEqual("NOK", p9.Value); Assert.IsFalse(p9.AnyNamespace); }
/// <summary> /// CloseCriterium("patient.name=\"Teun\"") -> "patient=id1,id2" /// </summary> /// <param name="resourceType"></param> /// <param name="crit"></param> /// <returns></returns> private Criterium CloseCriterium(Criterium crit, string resourceType) { List <string> targeted = crit.GetTargetedReferenceTypes(resourceType); List <string> allKeys = new List <string>(); foreach (var target in targeted) { var keys = CollectKeys(target, new List <Criterium> { (Criterium)crit.Operand }); //Recursive call to CollectKeys! allKeys.AddRange(keys.Select(k => k.ToString())); } crit.Type = Operator.IN; crit.Operand = ChoiceValue.Parse(String.Join(",", allKeys)); return(crit); }
private List <Criterium> parseCriteria(F.Query query, SearchResults results) { var result = new List <Criterium>(); foreach (var c in query.Criteria) { try { result.Add(Criterium.Parse(c)); } catch (Exception ex) { results.AddIssue(String.Format("Could not parse parameter [{0}] for reason [{1}].", c.ToString(), ex.Message)); } } return(result); }
private static IMongoQuery TagQuery(Criterium crit, Uri tagscheme) { if (crit.Type == Operator.IN) { IEnumerable <ValueExpression> opMultiple = ((ChoiceValue)crit.Operand).Choices; var optionQueries = new List <IMongoQuery>(); foreach (var choice in opMultiple) { Criterium option = new Criterium(); option.Type = Operator.EQ; option.Operand = choice; option.Modifier = crit.Modifier; option.ParamName = crit.ParamName; optionQueries.Add(TagQuery(option, tagscheme)); } return(M.Query.Or(optionQueries)); } //From here there's only 1 operand. IMongoQuery schemeQuery = M.Query.EQ(InternalField.TAGSCHEME, tagscheme.AbsoluteUri); IMongoQuery argQuery; var operand = (ValueExpression)crit.Operand; switch (crit.Modifier) { case Modifier.PARTIAL: argQuery = StringQuery(InternalField.TAGTERM, Operator.EQ, Modifier.NONE, operand); break; case Modifier.TEXT: argQuery = StringQuery(InternalField.TAGLABEL, Operator.EQ, Modifier.NONE, operand); break; case Modifier.NONE: case null: argQuery = StringQuery(InternalField.TAGTERM, Operator.EQ, Modifier.EXACT, operand); break; default: throw new ArgumentException(String.Format("Invalid modifier {0} in parameter {1}", crit.Modifier, crit.ParamName)); } return(M.Query.ElemMatch(InternalField.TAG, M.Query.And(schemeQuery, argQuery))); }
public void ParseChain() { var crit = Criterium.Parse("par1:type1.par2.par3:text=hoi"); Assert.IsTrue(crit.Operator == Operator.CHAIN); Assert.AreEqual("type1", crit.Modifier); Assert.IsTrue(crit.Operand is Criterium); crit = crit.Operand as Criterium; Assert.IsTrue(crit.Operator == Operator.CHAIN); Assert.AreEqual(null, crit.Modifier); Assert.IsTrue(crit.Operand is Criterium); crit = crit.Operand as Criterium; Assert.IsTrue(crit.Operator == Operator.EQ); Assert.AreEqual("text", crit.Modifier); Assert.IsTrue(crit.Operand is UntypedValue); }
public void HandleNumberParam() { var p1 = new NumberValue(18); Assert.AreEqual("18", p1.ToString()); var p2 = NumberValue.Parse("18"); Assert.AreEqual(18M, p2.Value); var p3 = NumberValue.Parse("18.00"); Assert.AreEqual(18.00M, p3.Value); var crit = Criterium.Parse("paramX=18.34"); var p4 = ((UntypedValue)crit.Operand).AsNumberValue(); Assert.AreEqual(18.34M, p4.Value); }
public void HandleStringParam() { var p1 = new StringValue("Hello, world"); Assert.AreEqual(@"Hello\, world", p1.ToString()); var p2 = new StringValue("Pay $300|Pay $100|"); Assert.AreEqual(@"Pay \$300\|Pay \$100\|", p2.ToString()); var p3 = StringValue.Parse(@"Pay \$300\|Pay \$100\|"); Assert.AreEqual("Pay $300|Pay $100|", p3.Value); var crit = Criterium.Parse(@"paramX=Hello\, world"); var p4 = ((UntypedValue)crit.Operand).AsStringValue(); Assert.AreEqual("Hello, world", p4.Value); }
internal static IMongoQuery ToFilter(this Criterium crit, string resourceType) { //Maybe it's a generic parameter. MethodInfo methodForParameter = FixedQueries.Find(m => m.Name.Equals(crit.ParamName + "FixedQuery")); if (methodForParameter != null) { return((IMongoQuery)methodForParameter.Invoke(null, new object[] { crit })); } //Otherwise it should be a parameter as defined in the metadata var critSp = FindSearchParamDefinition(crit, resourceType); if (critSp != null) { return(CreateFilter(critSp, crit.Type, crit.Modifier, crit.Operand)); } throw new ArgumentException(String.Format("Resource {0} has no parameter with the name {1}.", resourceType, crit.ParamName)); }
internal static List <string> GetTargetedReferenceTypes(this Criterium chainCriterium, string resourceType) { if (chainCriterium.Type != Operator.CHAIN) { throw new ArgumentException("Targeted reference types are only relevent for chained criteria."); } var critSp = chainCriterium.FindSearchParamDefinition(resourceType); var modifier = chainCriterium.Modifier; var nextInChain = (Criterium)chainCriterium.Operand; var nextParameter = nextInChain.ParamName; // The modifier contains the type of resource that the referenced resource must be. It is optional. // If not present, search all possible types of resources allowed at this reference. // If it is present, it should be of one of the possible types. var searchResourceTypes = GetTargetedReferenceTypes(critSp, modifier); // Afterwards, filter on the types that actually have the requested searchparameter. return(searchResourceTypes.Where(rt => InternalField.All.Contains(nextParameter) || ModelInfo.SearchParameters.Exists(sp => rt.Equals(sp.Resource) && nextParameter.Equals(sp.Name))).ToList()); }
public void SerializeChain() { var crit = new Criterium { ParamName = "par1", Modifier = "type1", Operator = Operator.CHAIN, Operand = new Criterium { ParamName = "par2", Operator = Operator.CHAIN, Operand = new Criterium { ParamName = "par3", Modifier = "text", Operator = Operator.EQ, Operand = new StringValue("hoi") } } }; Assert.AreEqual("par1:type1.par2.par3:text=hoi", crit.ToString()); }
public void HandleQuantityParam() { var p1 = new QuantityValue(3.141M, "http://unitsofmeasure.org", "mg"); Assert.AreEqual("3.141|http://unitsofmeasure.org|mg", p1.ToString()); var p2 = new QuantityValue(3.141M, "mg"); Assert.AreEqual("3.141||mg", p2.ToString()); var p3 = new QuantityValue(3.141M, "http://system.com/id$4", "$/d"); Assert.AreEqual(@"3.141|http://system.com/id\$4|\$/d", p3.ToString()); var p4 = QuantityValue.Parse("3.141|http://unitsofmeasure.org|mg"); Assert.AreEqual(3.141M, p4.Number); Assert.AreEqual("http://unitsofmeasure.org", p4.Namespace); Assert.AreEqual("mg", p4.Unit); var p5 = QuantityValue.Parse("3.141||mg"); Assert.AreEqual(3.141M, p5.Number); Assert.IsNull(p5.Namespace); Assert.AreEqual("mg", p5.Unit); var p6 = QuantityValue.Parse(@"3.141|http://system.com/id\$4|\$/d"); Assert.AreEqual(3.141M, p6.Number); Assert.AreEqual("http://system.com/id$4", p6.Namespace); Assert.AreEqual("$/d", p6.Unit); var crit = Criterium.Parse("paramX=3.14||mg"); var p7 = ((UntypedValue)crit.Operand).AsQuantityValue(); Assert.AreEqual(3.14M, p7.Number); Assert.IsNull(p7.Namespace); Assert.AreEqual("mg", p7.Unit); }
public void SerializeCriterium() { var crit = new Criterium { ParamName = "paramX", Modifier = "modif1", Operand = new NumberValue(18), Operator = Operator.GTE }; Assert.AreEqual("paramX:modif1=ge18", crit.ToString()); crit = new Criterium { ParamName = "paramX", Operand = new NumberValue(18) }; Assert.AreEqual("paramX=18", crit.ToString()); crit = new Criterium { ParamName = "paramX", Operator = Operator.ISNULL }; Assert.AreEqual("paramX:missing=true", crit.ToString()); crit = new Criterium { ParamName = "paramX", Operator = Operator.NOTNULL }; Assert.AreEqual("paramX:missing=false", crit.ToString()); }
private static IMongoQuery CompositeQuery(ModelInfo.SearchParamDefinition parameterDef, Operator optor, String modifier, ValueExpression operand) { if (optor == Operator.IN) { var choices = ((ChoiceValue)operand); var queries = new List <IMongoQuery>(); foreach (var choice in choices.Choices) { queries.Add(CompositeQuery(parameterDef, Operator.EQ, modifier, choice)); } return(M.Query.Or(queries)); } else if (optor == Operator.EQ) { var typedOperand = (CompositeValue)operand; var queries = new List <IMongoQuery>(); var components = typedOperand.Components; var subParams = parameterDef.CompositeParams; if (components.Count() != subParams.Count()) { throw new ArgumentException(String.Format("Parameter {0} requires exactly {1} composite values, not the currently provided {2} values.", parameterDef.Name, subParams.Count(), components.Count())); } for (int i = 0; i < subParams.Count(); i++) { var subCrit = new Criterium(); subCrit.Type = Operator.EQ; subCrit.ParamName = subParams[i]; subCrit.Operand = components[i]; subCrit.Modifier = modifier; queries.Add(subCrit.ToFilter(parameterDef.Resource)); } return(M.Query.And(queries)); } throw new ArgumentException(String.Format("Invalid operator {0} on composite parameter {1}", optor.ToString(), parameterDef.Name)); }
internal static ModelInfo.SearchParamDefinition FindSearchParamDefinition(this Criterium crit, string resourceType) { var sp = ModelInfo.SearchParameters; return(sp.Find(p => p.Name == crit.ParamName && p.Resource == resourceType)); }
internal static IMongoQuery _profileFixedQuery(Criterium crit) { return(TagQuery(crit, Tag.FHIRTAGSCHEME_PROFILE)); }
internal static IMongoQuery _securityFixedQuery(Criterium crit) { return(TagQuery(crit, Tag.FHIRTAGSCHEME_SECURITY)); }
internal static IMongoQuery internal_idFixedQuery(Criterium crit) { return(StringQuery(InternalField.ID, crit.Type, "exact", (ValueExpression)crit.Operand)); }
public void ParseComparatorOperatorForDate() { var criterium = Criterium.Parse("birthdate=lt2018-01-01"); Assert.AreEqual(Operator.LT, criterium.Operator); }
public void ParseComparatorOperatorForNumber() { var criterium = Criterium.Parse("length=gt20"); Assert.AreEqual(Operator.GT, criterium.Operator); }
public void ParseCriteriumDSTU2() { var crit = Criterium.Parse("birthdate=2018-01-01"); Assert.AreEqual("birthdate", crit.ParamName); Assert.IsNull(crit.Modifier); Assert.AreEqual("2018-01-01", crit.Operand.ToString()); Assert.AreEqual(Operator.EQ, crit.Operator); crit = Criterium.Parse("birthdate=eq2018-01-01"); Assert.AreEqual("birthdate", crit.ParamName); Assert.IsNull(crit.Modifier); Assert.AreEqual("2018-01-01", crit.Operand.ToString()); Assert.AreEqual(Operator.EQ, crit.Operator); crit = Criterium.Parse("birthdate=ne2018-01-01"); Assert.AreEqual("birthdate", crit.ParamName); Assert.IsNull(crit.Modifier); Assert.AreEqual("2018-01-01", crit.Operand.ToString()); Assert.AreEqual(Operator.NOT_EQUAL, crit.Operator); crit = Criterium.Parse("birthdate=gt2018-01-01"); Assert.AreEqual("birthdate", crit.ParamName); Assert.IsNull(crit.Modifier); Assert.AreEqual("2018-01-01", crit.Operand.ToString()); Assert.AreEqual(Operator.GT, crit.Operator); crit = Criterium.Parse("birthdate=ge2018-01-01"); Assert.AreEqual("birthdate", crit.ParamName); Assert.IsNull(crit.Modifier); Assert.AreEqual("2018-01-01", crit.Operand.ToString()); Assert.AreEqual(Operator.GTE, crit.Operator); crit = Criterium.Parse("birthdate=lt2018-01-01"); Assert.AreEqual("birthdate", crit.ParamName); Assert.IsNull(crit.Modifier); Assert.AreEqual("2018-01-01", crit.Operand.ToString()); Assert.AreEqual(Operator.LT, crit.Operator); crit = Criterium.Parse("birthdate=le2018-01-01"); Assert.AreEqual("birthdate", crit.ParamName); Assert.IsNull(crit.Modifier); Assert.AreEqual("2018-01-01", crit.Operand.ToString()); Assert.AreEqual(Operator.LTE, crit.Operator); crit = Criterium.Parse("birthdate:modif1=ap2018-01-01"); Assert.AreEqual("birthdate", crit.ParamName); Assert.AreEqual("2018-01-01", crit.Operand.ToString()); Assert.AreEqual("modif1", crit.Modifier); Assert.AreEqual(Operator.APPROX, crit.Operator); crit = Criterium.Parse("birthdate:missing=true"); Assert.AreEqual("birthdate", crit.ParamName); Assert.IsNull(crit.Operand); Assert.IsNull(crit.Modifier); Assert.AreEqual(Operator.ISNULL, crit.Operator); crit = Criterium.Parse("birthdate:missing=false"); Assert.AreEqual("birthdate", crit.ParamName); Assert.IsNull(crit.Operand); Assert.IsNull(crit.Modifier); Assert.AreEqual(Operator.NOTNULL, crit.Operator); }
public Criterium Clone() { Criterium result = new Criterium(); result.Modifier = Modifier; result.Operand = (Operand is Criterium) ? (Operand as Criterium).Clone() : Operand; result.Operator = Operator; result.ParamName = ParamName; return result; }
internal static IMongoQuery _tagFixedQuery(Criterium crit) { return(TagQuery(crit, Tag.FHIRTAGSCHEME_GENERAL)); }
public void ParseComparatorOperatorForQuantity() { var criterium = Criterium.Parse("value-quantity=le5.4|http://unitsofmeasure.org|mg"); Assert.AreEqual(Operator.LTE, criterium.Operator); }
public void ParseCriteriumDSTU2() { var crit = Criterium.Parse("paramX=18"); Assert.AreEqual("paramX", crit.ParamName); Assert.IsNull(crit.Modifier); Assert.AreEqual("18", crit.Operand.ToString()); Assert.AreEqual(Operator.EQ, crit.Operator); crit = Criterium.Parse("paramX=eq18"); Assert.AreEqual("paramX", crit.ParamName); Assert.IsNull(crit.Modifier); Assert.AreEqual("18", crit.Operand.ToString()); Assert.AreEqual(Operator.EQ, crit.Operator); crit = Criterium.Parse("paramX=ne18"); Assert.AreEqual("paramX", crit.ParamName); Assert.IsNull(crit.Modifier); Assert.AreEqual("18", crit.Operand.ToString()); Assert.AreEqual(Operator.NOT_EQUAL, crit.Operator); crit = Criterium.Parse("paramX=gt18"); Assert.AreEqual("paramX", crit.ParamName); Assert.IsNull(crit.Modifier); Assert.AreEqual("18", crit.Operand.ToString()); Assert.AreEqual(Operator.GT, crit.Operator); crit = Criterium.Parse("paramX=ge18"); Assert.AreEqual("paramX", crit.ParamName); Assert.IsNull(crit.Modifier); Assert.AreEqual("18", crit.Operand.ToString()); Assert.AreEqual(Operator.GTE, crit.Operator); crit = Criterium.Parse("paramX=lt18"); Assert.AreEqual("paramX", crit.ParamName); Assert.IsNull(crit.Modifier); Assert.AreEqual("18", crit.Operand.ToString()); Assert.AreEqual(Operator.LT, crit.Operator); crit = Criterium.Parse("paramX=le18"); Assert.AreEqual("paramX", crit.ParamName); Assert.IsNull(crit.Modifier); Assert.AreEqual("18", crit.Operand.ToString()); Assert.AreEqual(Operator.LTE, crit.Operator); crit = Criterium.Parse("paramX:modif1=ap18"); Assert.AreEqual("paramX", crit.ParamName); Assert.AreEqual("18", crit.Operand.ToString()); Assert.AreEqual("modif1", crit.Modifier); Assert.AreEqual(Operator.APPROX, crit.Operator); crit = Criterium.Parse("paramX:missing=true"); Assert.AreEqual("paramX", crit.ParamName); Assert.IsNull(crit.Operand); Assert.IsNull(crit.Modifier); Assert.AreEqual(Operator.ISNULL, crit.Operator); crit = Criterium.Parse("paramX:missing=false"); Assert.AreEqual("paramX", crit.ParamName); Assert.IsNull(crit.Operand); Assert.IsNull(crit.Modifier); Assert.AreEqual(Operator.NOTNULL, crit.Operator); }