public void SerializeFieldName() { var foobar = foobarTable (); var tables = new List<OracularTable> { foobar }; var specs = new List<OracularSpec> (); var config = new OracularConfig (tables, specs); var foobarReference = new Reference (new [] { "Foobar", "Id" }); var sql = foobarReference.Walk (new Sqlizer (foobar, config)); Assert.AreEqual ("[Foobar].[Id]", sql); }
public void CheckJoinTables() { var justAnId = new List<FieldConfig> { new FieldConfig("Id", null) }; var potatoFields = new List<FieldConfig> { new FieldConfig("Id", null), new FieldConfig("FoobarId", null), new FieldConfig("Type", null) }; var foobarRelationship = new ParentConfig ("Foobar", null, null); var tables = new List<OracularTable> { new OracularTable("Foobar", null, null, justAnId), new OracularTable("Potato", null, new List<ParentConfig>{foobarRelationship}, potatoFields) }; var specs = new List<OracularSpec>(); var config = new OracularConfig (tables, specs); var checker = new RefChecker (config); var fingerlingPotatoes = new BinaryOperation("=", new Reference(new [] { "Potato", "Type" }), new StringLiteral("Fingerling") ); var call = new MacroExpansion( new Reference(new [] { "ANY" }), new AstNode[] { new Reference(new [] { "Potato" }), fingerlingPotatoes } ); var initial = new []{ "Foobar" }; var result = call.Walk (checker, initial); Assert.AreEqual (2, result.Length); Assert.Contains ("Foobar", result); Assert.Contains ("Potato", result); }
public void SerializeBooleanField() { var idAndBoolean = new List<FieldConfig> { new FieldConfig("Id", null), new FieldConfig("IsBaz", FieldType.Boolean) }; var foobar = new OracularTable ("Foobar", null, null, idAndBoolean); var tables = new List<OracularTable> { foobar }; var specs = new List<OracularSpec> (); var config = new OracularConfig (tables, specs); var foobarReference = new Reference (new [] { "Foobar", "IsBaz" }); var sql = foobarReference.Walk (new Sqlizer (foobar, config)); Assert.AreEqual ("[Foobar].[IsBaz] = 1", sql); }
public void ExpectJoinTableToExist() { var justAnId = new List<FieldConfig> { new FieldConfig("Id", null) }; var tables = new List<OracularTable> { new OracularTable("Foobar", null, null, justAnId) }; var specs = new List<OracularSpec>(); var config = new OracularConfig (tables, specs); var checker = new RefChecker (config); var fingerlingPotatoes = new BinaryOperation("=", new Reference(new [] { "Potato", "Type" }), new StringLiteral("Fingerling") ); var call = new MacroExpansion( new Reference(new [] { "ANY" }), new [] { fingerlingPotatoes } ); var initial = new []{ "Foobar" }; var ex = Assert.Throws<RefCheckException> (() => call.Walk (checker, initial)); Assert.That (REFERENCE_RE.IsMatch (ex.Message)); }
public void CheckReferenceRoot() { var justAnId = new List<FieldConfig> { new FieldConfig("Id", null) }; var tables = new List<OracularTable> { new OracularTable("Foobar", null, null, justAnId) }; var config = new OracularConfig (tables, new List<OracularSpec> ()); var checker = new RefChecker (config); var reference = new Reference (new []{ "Foobar" }); var initial = new []{ "Foobar" }; var result = reference.Walk (checker, initial); Assert.AreEqual (1, result.Length); Assert.Contains ("Foobar", result); }
public void CheckSpecReferences() { var justAnId = new List<FieldConfig> { new FieldConfig("Id", null) }; var tables = new List<OracularTable> { new OracularTable("Foobar", null, null, justAnId) }; var specs = new List<OracularSpec> { new OracularSpec("isBaz", "Foobar", "Foobar.Id != null") }; var config = new OracularConfig (tables, specs); var checker = new RefChecker (config); var reference = new Reference (new [] { "Foobar" }); var fn = new Reference (new [] { "isBaz" }); var call = new MacroExpansion (fn, new [] { reference }); var initial = new []{ "Foobar" }; var result = call.Walk (checker, initial); Assert.AreEqual (1, result.Length); Assert.Contains ("Foobar", result); }
public void SerializeTableAsAlias() { var foobar = foobarTable (); var tables = new List<OracularTable> { foobar }; var specs = new List<OracularSpec> (); var config = new OracularConfig (tables, specs); var foobarReference = new Reference (new [] { "Foobar" }); var sql = foobarReference.Walk (new Sqlizer (foobar, config, "Alias")); Assert.AreEqual ("[Alias]", sql); }
public void ExpectParentToExist() { var justAnId = new List<FieldConfig> { new FieldConfig("Id", null) }; var tables = new List<OracularTable> { new OracularTable("Foobar", null, null, justAnId) }; var config = new OracularConfig (tables, new List<OracularSpec> ()); var reference = new Reference (new string[]{ "Foobar", "Parent", "Test" }); var ex = Assert.Throws<TypeCheckException> (() => reference.Walk (new TypeChecker (config))); Assert.That (ex.Message, Is.StringContaining ("parent")); }
public void CheckSpecAgainstTable() { var justAnId = new List<FieldConfig> { new FieldConfig("Id", null) }; var tables = new List<OracularTable> { new OracularTable("Foobar", null, null, justAnId) }; var specs = new List<OracularSpec> { new OracularSpec("isBaz", "Foobar", "Foobar.Id = null") }; var config = new OracularConfig (tables, specs); var fnRef = new Reference (new string[]{ "isBaz" }); var foobarRef = new Reference (new string[]{ "Foobar" }); var isFoobarBaz = new MacroExpansion (fnRef, new AstNode[]{ foobarRef }); var type = isFoobarBaz.Walk (new TypeChecker (config)); Assert.AreEqual (SpecType.Boolean, type.Type); }
public void SerializeSpecReferences() { var justAnId = new List<FieldConfig> { new FieldConfig("Id", null) }; var idAndBarId = new List<FieldConfig> { new FieldConfig("Id", null), new FieldConfig("BarId", null) }; var barRelationship = new List<ParentConfig> { new ParentConfig("Bar", null, null) }; var foo = new OracularTable ("Foo", null, barRelationship, idAndBarId); var tables = new List<OracularTable> { foo, new OracularTable("Bar", null, null, justAnId) }; var specs = new List<OracularSpec> { new OracularSpec("barHasId", "Bar", "Bar.Id != null") }; var config = new OracularConfig (tables, specs); var foobarReference = new Reference (new [] { "Foo", "Bar" }); var specReference = new Reference (new [] { "barHasId" }); var macroExpansion = new MacroExpansion (specReference, new [] { foobarReference }); var builder = new Sqlizer (foo, config); var sql = macroExpansion.Walk (builder); Assert.AreEqual ("([Foo.Bar].[Id] != NULL)", sql); Assert.AreEqual (1, builder.JoinTables.Count ()); var joinClause = builder.JoinTables.First(); Assert.AreEqual ("INNER JOIN [Bar] [Foo.Bar] ON [Foo.Bar].[Id] = [Foo].[BarId]", joinClause); }
public void ExpectNestedSpecToRefcheck() { var justAnId = new List<FieldConfig> { new FieldConfig("Id", null) }; var potatoFields = new List<FieldConfig> { new FieldConfig("Id", null), new FieldConfig("FoobarId", null) }; var foobarRelationship = new ParentConfig ("Foobar", null, null); var tables = new List<OracularTable> { new OracularTable("Foobar", null, null, justAnId), new OracularTable("Potato", null, new List<ParentConfig>{foobarRelationship}, potatoFields) }; var specs = new List<OracularSpec>(); var config = new OracularConfig (tables, specs); var checker = new RefChecker (config); var fingerlingPotatoes = new MacroExpansion( new Reference(new [] { "isFingerling" }), new [] { new Reference(new [] { "Potato" }) } ); var call = new MacroExpansion( new Reference(new [] { "ANY" }), new AstNode[] { new Reference(new [] { "Potato" }), fingerlingPotatoes } ); var initial = new []{ "Foobar" }; var ex = Assert.Throws<RefCheckException> (() => call.Walk (checker, initial)); Assert.That (REFERENCE_RE.IsMatch (ex.Message)); }
public void ExpectSpecToExist() { var justAnId = new List<FieldConfig> { new FieldConfig("Id", null) }; var tables = new List<OracularTable> { new OracularTable("Foobar", null, null, justAnId) }; var specs = new List<OracularSpec>(); var config = new OracularConfig (tables, specs); var checker = new RefChecker (config); var reference = new Reference (new [] { "Foobar" }); var fn = new Reference (new [] { "isBaz" }); var call = new MacroExpansion (fn, new [] { reference }); var initial = new []{ "Foobar" }; var ex = Assert.Throws<RefCheckException>(() => call.Walk (checker, initial)); Assert.That (REFERENCE_RE.IsMatch (ex.Message)); }
public void SerializeNestedReducers() { var bazFields = new List<FieldConfig> { new FieldConfig("Id", null) }; var barFields = new List<FieldConfig> { new FieldConfig("Id", null), new FieldConfig("BazId", null) }; var barbazRelationship = new List<ParentConfig> { new ParentConfig("Baz", null, null) }; var fooFields = new List<FieldConfig> { new FieldConfig("Id", null), new FieldConfig("BarId", null), new FieldConfig("IsQux", FieldType.Boolean) }; var foobarRelationship = new List<ParentConfig> { new ParentConfig("Bar", null, null) }; var baz = new OracularTable ("Baz", null, null, bazFields); var tables = new List<OracularTable> { new OracularTable ("Foo", null, foobarRelationship, fooFields), new OracularTable("Bar", null, barbazRelationship, barFields), baz }; var specs = new List<OracularSpec> (); var config = new OracularConfig (tables, specs); var fooIsQuxNode = new Reference (new [] { "Foo", "IsQux" }); var anyFooNode = new MacroExpansion ( new Reference (new [] { "ANY" }), new [] { new Reference(new [] { "Foo" }), fooIsQuxNode } ); var anyBarNode = new MacroExpansion ( new Reference(new [] { "ANY" }), new AstNode[] { new Reference(new [] { "Bar" }), anyFooNode } ); var builder = new Sqlizer (baz, config); var whereClause = anyBarNode.Walk (builder); var expected = String.Format ("[AnnotatedBaz{0}].[AnyBar{0}] = 1", anyFooNode.Id); Assert.AreEqual (expected, whereClause); Assert.AreEqual (1, builder.JoinTables.Count ()); var join = builder.JoinTables.First (); expected = String.Format ("LEFT JOIN [AnnotatedBaz{0}] ON [AnnotatedBaz{0}].[Id] = [Baz].[Id]", anyFooNode.Id); Assert.AreEqual (expected, join); Assert.AreEqual (2, builder.CommonTableExpressions.Count ()); var annotatedBar = builder.CommonTableExpressions.First (); expected = String.Format (@"[AnnotatedBar{0}] AS ( SELECT DISTINCT [Bar].[Id], 1 [AnyFoo{0}] FROM [Bar] LEFT JOIN [Foo] ON [Foo].[BarId] = [Bar].[Id] WHERE [Foo].[IsQux] = 1 )", fooIsQuxNode.Id); Assert.AreEqual (expected, annotatedBar); var annotatedBaz = builder.CommonTableExpressions.Last (); expected = String.Format(@"[AnnotatedBaz{0}] AS ( SELECT DISTINCT [Baz].[Id], 1 [AnyBar{0}] FROM [Baz] LEFT JOIN [Bar] ON [Bar].[BazId] = [Baz].[Id] LEFT JOIN [AnnotatedBar{1}] ON [AnnotatedBar{1}].[Id] = [Bar].[Id] WHERE [AnnotatedBar{1}].[AnyFoo{1}] = 1 )", anyFooNode.Id, fooIsQuxNode.Id); Assert.AreEqual (expected, annotatedBaz); }
public void SerializeGrandparentTableOnAlias() { var justAnId = new List<FieldConfig> { new FieldConfig("Id", null) }; var idAndBazId = new List<FieldConfig> { new FieldConfig("Id", null), new FieldConfig("BazId", null) }; var bazRelationship = new List<ParentConfig> { new ParentConfig("Baz", null, null) }; var idAndBarId = new List<FieldConfig> { new FieldConfig("Id", null), new FieldConfig("BarId", null) }; var barRelationship = new List<ParentConfig> { new ParentConfig("Bar", null, null) }; var foo = new OracularTable ("Foo", null, barRelationship, idAndBarId); var tables = new List<OracularTable> { foo, new OracularTable("Bar", null, bazRelationship, idAndBazId), new OracularTable("Baz", null, null, justAnId) }; var specs = new List<OracularSpec> (); var config = new OracularConfig (tables, specs); var foobarReference = new Reference (new [] { "Foo", "Bar", "Baz" }); var builder = new Sqlizer (foo, config, "Alias"); var sql = foobarReference.Walk (builder); Assert.AreEqual ("[Alias.Bar.Baz]", sql); Assert.AreEqual (2, builder.JoinTables.Count ()); }
public void ReferenceTypesFromTable(FieldType fieldType, SpecType specType) { var fieldConfig = new List<FieldConfig> { new FieldConfig("Id", null), new FieldConfig("Test", fieldType) }; var tables = new List<OracularTable> { new OracularTable ("Foobar", null, null, fieldConfig) }; var config = new OracularConfig (tables, new List<OracularSpec> ()); var reference = new Reference (new string[]{ "Foobar", "Test" }); var refType = reference.Walk (new TypeChecker (config)); Assert.AreEqual (specType, refType.Type); }
public void ExpectSpecToExist() { var justAnId = new List<FieldConfig> { new FieldConfig("Id", null) }; var tables = new List<OracularTable> { new OracularTable("Foobar", null, null, justAnId) }; var config = new OracularConfig (tables, new List<OracularSpec> ()); var fnRef = new Reference (new string[]{ "isBaz" }); var foobarRef = new Reference (new string[]{ "Foobar" }); var isFoobarBaz = new MacroExpansion (fnRef, new AstNode[]{ foobarRef }); var ex = Assert.Throws<TypeCheckException> (() => isFoobarBaz.Walk (new TypeChecker (config))); Assert.That (ex.Message, Is.StringContaining ("name")); }
public void ExpectSpecTableToMatchInput() { var justAnId = new List<FieldConfig> { new FieldConfig("Id", null) }; var tables = new List<OracularTable> { new OracularTable("Foobar", null, null, justAnId), new OracularTable("SomethingElse", null, null, justAnId) }; var specs = new List<OracularSpec> { new OracularSpec("isBaz", "SomethingElse", "SomethingElse.Id = null") }; var config = new OracularConfig (tables, specs); var fnRef = new Reference (new string[]{ "isBaz" }); var foobarRef = new Reference (new string[]{ "Foobar" }); var isFoobarBaz = new MacroExpansion (fnRef, new AstNode[]{ foobarRef }); var ex = Assert.Throws<TypeCheckException> (() => isFoobarBaz.Walk (new TypeChecker (config))); Assert.That (ex.Message, Is.StringContaining ("mismatch")); }
public void ExpectReferenceRootToBeInTables() { var justAnId = new List<FieldConfig> { new FieldConfig("Id", null) }; var tables = new List<OracularTable> { new OracularTable("Other", null, null, justAnId), new OracularTable("Tables", null, null, justAnId) }; var config = new OracularConfig (tables, new List<OracularSpec> ()); var checker = new RefChecker (config); var reference = new Reference (new []{ "Foobar" }); var initial = new []{ "Other", "Tables" }; var ex = Assert.Throws<RefCheckException> (() => reference.Walk (checker, initial)); Assert.That (JOIN_RE.IsMatch (ex.Message)); }
public void SerializeParentOnAlias() { var justAnId = new List<FieldConfig> { new FieldConfig("Id", null) }; var idAndBarId = new List<FieldConfig> { new FieldConfig("Id", null), new FieldConfig("BarId", null) }; var barRelationship = new List<ParentConfig> { new ParentConfig("Bar", null, null) }; var foo = new OracularTable ("Foo", null, barRelationship, idAndBarId); var tables = new List<OracularTable> { foo, new OracularTable("Bar", null, null, justAnId) }; var specs = new List<OracularSpec> (); var config = new OracularConfig (tables, specs); var foobarReference = new Reference (new [] { "Foo", "Bar" }); var builder = new Sqlizer (foo, config, "Alias"); var sql = foobarReference.Walk (builder); Assert.AreEqual ("[Alias.Bar]", sql); Assert.AreEqual (1, builder.JoinTables.Count ()); var joinClause = builder.JoinTables.First(); Assert.AreEqual ("INNER JOIN [Bar] [Alias.Bar] ON [Alias.Bar].[Id] = [Alias].[BarId]", joinClause); }
public void SerializeReducer(string macro, string prefix, string suffix, string value, string equality) { var justAnId = new List<FieldConfig> { new FieldConfig("Id", null) }; var idAndBarId = new List<FieldConfig> { new FieldConfig("Id", null), new FieldConfig("BarId", null) }; var barRelationship = new List<ParentConfig> { new ParentConfig("Bar", null, null) }; var foo = new OracularTable ("Foo", null, barRelationship, idAndBarId); var bar = new OracularTable("Bar", null, null, justAnId); var tables = new List<OracularTable>{ foo, bar }; var specs = new List<OracularSpec> (); var config = new OracularConfig (tables, specs); var macroReference = new Reference (new[]{ macro }); var fooReference = new Reference (new[]{ "Foo" }); var idNotNull = new BinaryOperation ("!=", new Reference (new[]{ "Foo", "Id" }), new NullLiteral () ); var macroExpansion = new MacroExpansion (macroReference, new AstNode[] { fooReference, idNotNull }); var builder = new Sqlizer (bar, config); var sql = macroExpansion.Walk (builder); var expected = String.Format ("[AnnotatedBar{0}].[{1}Foo{0}]{2}", idNotNull.Id, prefix, suffix); Assert.AreEqual (expected, sql); Assert.AreEqual (1, builder.JoinTables.Count ()); var join = builder.JoinTables.First (); expected = String.Format ("LEFT JOIN [AnnotatedBar{0}] ON [AnnotatedBar{0}].[Id] = [Bar].[Id]", idNotNull.Id); Assert.AreEqual (expected, join); Assert.AreEqual (1, builder.CommonTableExpressions.Count ()); var annotated = builder.CommonTableExpressions.First (); expected = String.Format (@"[AnnotatedBar{0}] AS ( SELECT DISTINCT [Bar].[Id], {2} [{1}Foo{0}] FROM [Bar] LEFT JOIN [Foo] ON [Foo].[BarId] = [Bar].[Id] WHERE ([Foo].[Id] {3} NULL) )", idNotNull.Id, prefix, value, equality); Assert.AreEqual (expected, annotated); }
public void CheckSpecAgainstParentTable() { var parentConfig = new List<FieldConfig> { new FieldConfig("Id", null), new FieldConfig("IsBaz", FieldType.Boolean) }; var foobarConfig = new List<FieldConfig> { new FieldConfig("Id", null), new FieldConfig("ParentId", null) }; var relationshipConfig = new List<ParentConfig> { new ParentConfig("Parent", null, null) }; var tables = new List<OracularTable> { new OracularTable ("Parent", null, null, parentConfig), new OracularTable ("Foobar", null, relationshipConfig, foobarConfig) }; var isParentBaz = new OracularSpec ("isParentBaz", "Foobar", "isBaz(Foobar.Parent)"); var specs = new List<OracularSpec> { new OracularSpec("isBaz", "Parent", "Parent.IsBaz"), isParentBaz }; var config = new OracularConfig (tables, specs); var type = isParentBaz.Spec.Walk (new TypeChecker (config)); Assert.AreEqual (SpecType.Boolean, type.Type); }