public void NestedEmptyResult() {
			var o1 = new OrFilterTerm();
			var o2 = new OrFilterTerm();
			var a1 = new AndFilterTerm(o1, o2);
			var a2 = new AndFilterTerm(a1);

			Assert.AreEqual(null, a2.Token);
		}
		public void SingleNestedTerm() {
			var f1 = new FilterTerm(WorkitemToDo);
			f1.Greater(5);
			var o1 = new OrFilterTerm(f1);
			var o2 = new OrFilterTerm();
			var a1 = new AndFilterTerm(o1, o2);
			var a2 = new AndFilterTerm(a1);

			Assert.AreEqual("Workitem.ToDo>'5'", a2.Token);
		}
		public void Complex2() {
			var termA = new FilterTerm(WorkitemToDo);
			termA.Greater(5);

			var termB = new FilterTerm(WorkitemToDo);
			termB.Less(10);

			var termC = new FilterTerm(WorkitemToDo);
			termC.GreaterOrEqual(20);

			var termD = new FilterTerm(WorkitemToDo);
			termD.LessOrEqual(30);

			var and1 = new AndFilterTerm(termA, termB);
			var and2 = new AndFilterTerm(termC, termD);

			var o = new OrFilterTerm(and1, and2);

			Assert.AreEqual("((Workitem.ToDo>'5';Workitem.ToDo<'10')|(Workitem.ToDo>='20';Workitem.ToDo<='30'))",
							o.Token);
		}
		public void Complex1() {
			var termA = new FilterTerm(WorkitemParent);
			termA.Equal(Oid.FromToken("Theme:5", Meta));
			termA.Equal(Oid.FromToken("Theme:6", Meta));

			var termB = new FilterTerm(WorkitemScope);
			termB.Equal(Oid.FromToken("Scope:0", Meta));

			var termC = new FilterTerm(WorkitemParent);
			termC.NotEqual(Oid.FromToken("Theme:7", Meta));
			termC.NotEqual(Oid.FromToken("Theme:8", Meta));

			var termD = new FilterTerm(WorkitemScope);
			termD.NotEqual(Oid.FromToken("Scope:1", Meta));

			var and1 = new AndFilterTerm(termA, termB);
			var and2 = new AndFilterTerm(termC, termD);

			var o = new OrFilterTerm(and1, and2);
			Assert.AreEqual(
				"((Workitem.Parent='Theme%3a5','Theme%3a6';Workitem.Scope='Scope%3a0')|(Workitem.Parent!='Theme%3a7','Theme%3a8';Workitem.Scope!='Scope%3a1'))",
				o.Token);
		}
		public void QueryStoryChangesWithInequalityFilter() {
			var storyAsset = CreateDisposableStory(InitialStoryName, "Scope:0");

			storyAsset = GetAssetsByOid(storyAsset.Oid.Momentless, attributesToQuery)[0];
			var moment1 = storyAsset.GetAttribute(momentDef).Value;

			storyAsset.SetAttributeValue(nameDef, ChangedStoryName);
			services.Save(storyAsset);

			storyAsset.SetAttributeValue(nameDef, FinalStoryName);
			services.Save(storyAsset);
			storyAsset = GetAssetsByOid(storyAsset.Oid.Momentless, attributesToQuery)[0];
			var moment3 = storyAsset.GetAttribute(momentDef).Value;

			var filter = new FilterTerm(momentDef);
			filter.GreaterOrEqual(moment1);
			var assets = GetAssetsByOid(storyAsset.Oid.Momentless, attributesToQuery, filter, true);
			Assert.AreEqual(3, assets.Count);
			Assert.IsTrue(assets.Any(x => x.GetAttribute(nameDef).Value.Equals(InitialStoryName)));
			Assert.IsTrue(assets.Any(x => x.GetAttribute(nameDef).Value.Equals(ChangedStoryName)));
			Assert.IsTrue(assets.Any(x => x.GetAttribute(nameDef).Value.Equals(FinalStoryName)));

			filter = new FilterTerm(momentDef);
			filter.Greater(moment1);
			assets = GetAssetsByOid(storyAsset.Oid.Momentless, attributesToQuery, filter, true);
			Assert.AreEqual(2, assets.Count);
			Assert.IsFalse(assets.Any(x => x.GetAttribute(nameDef).Value.Equals(InitialStoryName)));
			Assert.IsTrue(assets.Any(x => x.GetAttribute(nameDef).Value.Equals(ChangedStoryName)));
			Assert.IsTrue(assets.Any(x => x.GetAttribute(nameDef).Value.Equals(FinalStoryName)));

			var lessFilter = new FilterTerm(momentDef);
			lessFilter.Less(moment3);
			var greaterFilter = new FilterTerm(momentDef);
			greaterFilter.Greater(moment1);
			var groupFilter = new AndFilterTerm(lessFilter, greaterFilter);
			assets = GetAssetsByOid(storyAsset.Oid.Momentless, attributesToQuery, groupFilter, true);
			Assert.AreEqual(1, assets.Count);
			Assert.IsFalse(assets.Any(x => x.GetAttribute(nameDef).Value.Equals(InitialStoryName)));
			Assert.IsTrue(assets.Any(x => x.GetAttribute(nameDef).Value.Equals(ChangedStoryName)));
			Assert.IsFalse(assets.Any(x => x.GetAttribute(nameDef).Value.Equals(FinalStoryName)));
		}
		public void QueryTrackedEpicsForMultipleProjectsUsingVariables() {
			var metaConnector = new V1APIConnector(VersionOneUrl + "meta.v1/", "admin", "admin");
			var metaModel = new MetaModel(metaConnector);

			var scopeVariable = new QueryVariable("Scope", "Scope:2176");

			var epicType = metaModel.GetAssetType("Epic");

			var query = new Query(epicType);

			var notClosedEpicAttribute = epicType.GetAttributeDefinition("AssetState");

			var notClosedEpicTerm = new FilterTerm(notClosedEpicAttribute);
			notClosedEpicTerm.NotEqual("Closed");

			var scopeAttribute = epicType.GetAttributeDefinition("Scope");
			var scopeTerm = new FilterTerm(scopeAttribute);
			scopeTerm.Operate(FilterTerm.Operator.Equal, scopeVariable);

			var superAndUpAttribute = epicType.GetAttributeDefinition("SuperAndUp").Filter(scopeTerm);
			var superAndUpTerm = new FilterTerm(superAndUpAttribute);
			superAndUpTerm.NotExists();
			
			var filter = new AndFilterTerm(notClosedEpicTerm, scopeTerm, superAndUpTerm);
			query.Filter = filter;
			query.Variables.Add(scopeVariable);
			var builder = new QueryURLBuilder(query);
			var result = builder.ToString();
			Assert.AreEqual("Data/Epic?sel=&where=(Epic.AssetState!='Closed';Epic.Scope=$Scope;-Epic.SuperAndUp[Scope=$Scope])&with=$Scope=Scope%3A2176", result);
		}
		public void QueryTrackedEpicsByProject() {
			var metaConnector = new V1APIConnector(VersionOneUrl + "meta.v1/", "admin", "admin");
			var metaModel = new MetaModel(metaConnector);

			var epicType = metaModel.GetAssetType("Epic");
			var scopeType = metaModel.GetAssetType("Scope");

			var query = new Query(epicType);
			const string scopeId = "Scope:1025";

			var notClosedScopeAttribute = scopeType.GetAttributeDefinition("AssetState");
			var notClosedEpicAttribute = epicType.GetAttributeDefinition("AssetState");

			var notClosedEpicTerm = new FilterTerm(notClosedEpicAttribute);
			notClosedEpicTerm.NotEqual("Closed");

			var notClosedScopeTerm = new FilterTerm(notClosedScopeAttribute);
			notClosedScopeTerm.NotEqual("Closed");
			var scopeAttribute = epicType.GetAttributeDefinition("Scope.ParentMeAndUp");
			scopeAttribute = scopeAttribute.Filter(notClosedScopeTerm);

			var scopeTerm = new FilterTerm(scopeAttribute);
			scopeTerm.Equal(scopeId);
			var superAndUpAttribute = epicType.GetAttributeDefinition("SuperAndUp");
			superAndUpAttribute = superAndUpAttribute.Filter(scopeTerm);
			var superAndUpTerm = new FilterTerm(superAndUpAttribute);
			superAndUpTerm.NotExists();
			
			var filter = new AndFilterTerm(scopeTerm, notClosedEpicTerm, superAndUpTerm);
			query.Filter = filter;
			var builder = new QueryURLBuilder(query);
			var result = builder.ToString();
			Assert.AreEqual("Data/Epic?sel=&where=(Epic.Scope.ParentMeAndUp[AssetState!='Closed']='Scope%3a1025';Epic.AssetState!='Closed';-Epic.SuperAndUp[Scope.ParentMeAndUp[AssetState!='Closed']='Scope:1025'])", result);
		}