public void TestAlterEntities() { MSSQLProvider p = new MSSQLProvider(GetTestDB(), null); SourceView sv = p.GetSourceView(null, "ent1,ent2,1to2"); WXMLModel model = new WXMLModel(); SourceToModelConnector c = new SourceToModelConnector(sv, model); c.ApplySourceViewToModel(); Assert.AreEqual(3, model.GetSourceFragments().Count()); Assert.AreEqual(2, model.GetEntities().Count()); sv = p.GetSourceView(null, "ent1,ent2,1to2"); c = new SourceToModelConnector(sv, model); c.ApplySourceViewToModel(); Assert.AreEqual(3, model.GetSourceFragments().Count()); Assert.AreEqual(2, model.GetEntities().Count()); }
public void TestFillTables() { Worm_CodeGen_Core_OrmXmlParserAccessor parser = null; using (XmlReader rdr = XmlReader.Create(GetSampleFileStream())) { object privateParser = Worm_CodeGen_Core_OrmXmlParserAccessor.CreatePrivate(rdr); parser = new Worm_CodeGen_Core_OrmXmlParserAccessor(privateParser); parser.Read(); } parser.FillSourceFragments(); WXMLModel ormObjectDef = parser.Model; Assert.AreEqual <int>(6, ormObjectDef.GetSourceFragments().Count()); Assert.IsTrue(ormObjectDef.GetSourceFragments().Any( match => match.Identifier == "tblAlbums" && match.Name == "albums")); Assert.IsTrue(ormObjectDef.GetSourceFragments().Any( match => match.Identifier == "tblArtists" && match.Name == "artists")); Assert.IsTrue(ormObjectDef.GetSourceFragments().Any( match => match.Identifier == "tblAl2Ar" && match.Name == "al2ar")); Assert.IsTrue(ormObjectDef.GetSourceFragments().Any( match => match.Identifier == "tblSiteAccess" && match.Name == "sites_access")); }
public void TestGenerateScript() { var p = new MSSQLProvider(GetTestDB(), null); var sv = p.GetSourceView(); var model = new WXMLModel(); var smc = new SourceToModelConnector(sv, model); smc.ApplySourceViewToModel(); Assert.AreEqual(28, model.GetActiveEntities().Count()); Assert.AreEqual(32, model.GetSourceFragments().Count()); var msc = new ModelToSourceConnector(new SourceView(), model); string script = msc.GenerateSourceScript(p, false); Assert.IsFalse(string.IsNullOrEmpty(script)); Assert.AreEqual(32, new Regex("CREATE TABLE ").Matches(script).Count); Console.WriteLine(script); }
public void TestGenerateScriptDropConstraint() { var p = new MSSQLProvider(GetTestDB(), null); var sv = p.GetSourceView(); var model = new WXMLModel(); var smc = new SourceToModelConnector(sv, model); smc.ApplySourceViewToModel(false, relation1to1.Hierarchy, true, true, false); Assert.AreEqual(28, model.GetActiveEntities().Count()); Assert.AreEqual(32, model.GetSourceFragments().Count()); EntityPropertyDefinition prop = model.GetActiveEntities().SelectMany(item => item.GetProperties().OfType <EntityPropertyDefinition>()).First(); SourceConstraint c = new SourceConstraint(SourceConstraint.UniqueConstraintTypeName, "xxx"); c.SourceFields.AddRange(prop.SourceFields.Cast <SourceFieldDefinition>()); prop.SourceFragment.Constraints.Add(c); var msc = new ModelToSourceConnector(p.GetSourceView(), model); var tbl = msc.SourceView.GetSourceFragments().Single(item => item.Selector == prop.SourceFragment.Selector && item.Name == prop.SourceFragment.Name); tbl.Constraints.Add(new SourceConstraint(SourceConstraint.UniqueConstraintTypeName, "xxx")); string script = msc.GenerateSourceScript(p, false); Assert.IsFalse(string.IsNullOrEmpty(script), script); Assert.AreEqual(1, new Regex("DROP CONSTRAINT").Matches(script).Count); }
public void TestFillModel() { MSSQLProvider p = new MSSQLProvider(GetTestDB(), null); SourceView sv = p.GetSourceView(null, "ent1,ent2,1to2"); WXMLModel model = new WXMLModel(); SourceToModelConnector c = new SourceToModelConnector(sv, model); c.ApplySourceViewToModel(); Assert.AreEqual(3, model.GetSourceFragments().Count()); Assert.AreEqual(2, model.GetEntities().Count()); Assert.IsNotNull(model.GetEntity("e_dbo_ent1")); Assert.IsNotNull(model.GetEntity("e_dbo_ent2")); Assert.AreEqual(1, model.GetEntity("e_dbo_ent1").GetProperties().Count()); Assert.AreEqual(1, model.GetEntity("e_dbo_ent1").GetPkProperties().Count()); Assert.IsTrue(model.GetEntity("e_dbo_ent1").GetPkProperties().First().HasAttribute(Field2DbRelations.PrimaryKey)); Assert.AreEqual(2, model.GetEntity("e_dbo_ent2").GetProperties().Count()); Assert.AreEqual(1, model.GetRelations().Count()); }
public void TestFillModelRelations() { MSSQLProvider p = new MSSQLProvider(GetTestDB(), null); SourceView sv = p.GetSourceView(null, "aspnet_Applications,aspnet_Paths"); WXMLModel model = new WXMLModel(); SourceToModelConnector c = new SourceToModelConnector(sv, model); c.ApplySourceViewToModel(); Assert.AreEqual(2, model.GetSourceFragments().Count()); Assert.AreEqual(2, model.GetEntities().Count()); var aspnet_Applications = model.GetEntity("e_dbo_aspnet_Applications"); Assert.IsNotNull(aspnet_Applications); var aspnet_Paths = model.GetEntity("e_dbo_aspnet_Paths"); Assert.IsNotNull(aspnet_Paths); Assert.IsNotNull(aspnet_Paths.GetProperty("Application")); Assert.IsNotNull(aspnet_Paths.GetProperty("PathId")); Assert.IsNotNull(aspnet_Paths.GetProperty("Path")); Assert.IsNotNull(aspnet_Paths.GetProperty("LoweredPath")); Assert.AreEqual(1, aspnet_Applications.One2ManyRelations.Count()); Assert.AreEqual(aspnet_Paths, aspnet_Applications.One2ManyRelations.First().Entity); }
public void TestAlterEntity_ChangeTable() { using (Stream stream = Resources.GetXmlDocumentStream("suppressed")) { Assert.IsNotNull(stream); WXMLModel newModel = WXMLModel.LoadFromXml(new XmlTextReader(stream)); Assert.IsNotNull(newModel); Assert.AreEqual(2, newModel.GetActiveEntities().Count()); EntityDefinition e = newModel.GetActiveEntities().Single(item => item.Identifier == "e1"); Assert.AreEqual(1, e.GetSourceFragments().Count()); Assert.AreEqual(1, newModel.GetSourceFragments().Count()); Assert.AreEqual("tbl1", e.GetSourceFragments().First().Name); Assert.IsTrue(string.IsNullOrEmpty(e.GetSourceFragments().First().Selector)); SourceFragmentRefDefinition sf = new SourceFragmentRefDefinition( newModel.GetOrCreateSourceFragment("dbo", "table")); //foreach (SourceFragmentRefDefinition rsf in e.GetSourceFragments()) //{ // e.MarkAsDeleted(rsf); //} e.ClearSourceFragments(); e.AddSourceFragment(sf); foreach (PropertyDefinition property in e.GetProperties()) { property.SourceFragment = sf; } WXMLModel model = GetModel("suppressed"); model.Merge(Normalize(newModel)); e = model.GetActiveEntities().Single(item => item.Identifier == "e1"); Assert.AreEqual(1, e.GetSourceFragments().Count()); Assert.AreEqual(2, model.GetSourceFragments().Count()); Assert.AreEqual("table", e.GetSourceFragments().First().Name); Assert.AreEqual("dbo", e.GetSourceFragments().First().Selector); e = model.GetActiveEntities().Single(item => item.Identifier == "e11"); Assert.AreEqual(1, e.GetSourceFragments().Count()); Assert.AreEqual("table", e.GetSourceFragments().First().Name); Assert.AreEqual("dbo", e.GetSourceFragments().First().Selector); } }
public void TestAdventureWorks() { SourceView view; BinaryFormatter f = new BinaryFormatter { AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Simple }; ResolveEventHandler d = null; d = (sender, args) => { AppDomain.CurrentDomain.AssemblyResolve -= d; return(typeof(WXMLModel).Assembly); }; AppDomain.CurrentDomain.AssemblyResolve += d; Assembly assembly = Assembly.GetExecutingAssembly(); using (Stream fs = assembly.GetManifestResourceStream( string.Format("{0}.TestFiles.{1}", assembly.GetName().Name, "AdventureWorks.sourceview"))) { Assert.IsNotNull(fs); view = (SourceView)f.Deserialize(fs); } Assert.IsNotNull(view); WXMLModel model = new WXMLModel(); SourceToModelConnector c = new SourceToModelConnector(view, model); c.ApplySourceViewToModel(false, relation1to1.Default, true, true, false); Assert.AreEqual(70, model.GetActiveEntities().Count()); Assert.AreEqual(70, model.GetSourceFragments().Count()); model = new WXMLModel(); c = new SourceToModelConnector(view, model); c.ApplySourceViewToModel(false, relation1to1.Unify, true, true, false); Assert.AreEqual(67, model.GetActiveEntities().Count()); Assert.AreEqual(70, model.GetSourceFragments().Count()); model = new WXMLModel(); c = new SourceToModelConnector(view, model); c.ApplySourceViewToModel(false, relation1to1.Hierarchy, true, true, false); Assert.AreEqual(70, model.GetActiveEntities().Count()); Assert.AreEqual(70, model.GetSourceFragments().Count()); }
private void CheckSourceFragment(SourceFragmentDefinition sf) { if (Model != null && !Model.GetSourceFragments().Any(item => item.Identifier == sf.Identifier)) { throw new ArgumentException( string.Format("SourceFragment {0} not found in Model.SourceFragment collection", sf.Identifier)); } if (GetSourceFragments().Any(item => item.Identifier == sf.Identifier)) { throw new ArgumentException( string.Format("SourceFragment {0} already in SourceFragments collection", sf.Identifier)); } }
public void TestFillModel4() { MSSQLProvider p = new MSSQLProvider(GetTestDB(), null); SourceView sv = p.GetSourceView(null, "aspnet_Membership, 3to3"); WXMLModel model = new WXMLModel(); SourceToModelConnector c = new SourceToModelConnector(sv, model); c.ApplySourceViewToModel(); Assert.AreEqual(2, model.GetSourceFragments().Count()); Assert.AreEqual(2, model.GetEntities().Count()); }
public void TestFillModel3() { MSSQLProvider p = new MSSQLProvider(GetTestDB(), null); SourceView sv = p.GetSourceView(null, "complex_fk"); WXMLModel model = new WXMLModel(); SourceToModelConnector c = new SourceToModelConnector(sv, model); c.ApplySourceViewToModel(); Assert.AreEqual(1, model.GetSourceFragments().Count()); Assert.AreEqual(1, model.GetEntities().Count()); }
public void TestGenerateScriptHierachy() { var p = new MSSQLProvider(GetTestDB(), null); var sv = p.GetSourceView(); var model = new WXMLModel(); var smc = new SourceToModelConnector(sv, model); smc.ApplySourceViewToModel(false, relation1to1.Hierarchy, true, true, false); Assert.AreEqual(28, model.GetActiveEntities().Count()); Assert.AreEqual(32, model.GetSourceFragments().Count()); var msc = new ModelToSourceConnector(new SourceView(), model); string script = msc.GenerateSourceScript(p, false); Assert.IsFalse(string.IsNullOrEmpty(script)); Console.WriteLine(script); Assert.AreEqual(sv.GetSourceFragments().Count(), new Regex("CREATE TABLE ").Matches(script).Count); IEnumerable <SourceConstraint> pks = sv.GetSourceFragments().SelectMany(item => item.Constraints.Where(cns => cns.ConstraintType == SourceConstraint.PrimaryKeyConstraintTypeName)); Assert.AreEqual(pks.Count(), new Regex("PRIMARY KEY CLUSTERED").Matches(script).Count); Assert.AreEqual(2, new Regex("UNIQUE NONCLUSTERED").Matches(script).Count); Assert.AreEqual(1, new Regex("UNIQUE CLUSTERED").Matches(script).Count); Assert.AreEqual(sv.GetSourceFragments().SelectMany(item => item.Constraints.Where(cns => cns.ConstraintType == SourceConstraint.ForeignKeyConstraintTypeName)).Count(), new Regex("FOREIGN KEY").Matches(script).Count); msc = new ModelToSourceConnector(sv, model); script = msc.GenerateSourceScript(p, false); Assert.IsTrue(string.IsNullOrEmpty(script), script); RelationDefinitionBase r = model.GetActiveRelations().First(item => item.Constraint == RelationConstraint.PrimaryKey); r.Constraint = RelationConstraint.Unique; r.SourceFragment.Constraints.Single(item => item.ConstraintType == SourceConstraint.PrimaryKeyConstraintTypeName).ConstraintType = SourceConstraint.UniqueConstraintTypeName; msc = new ModelToSourceConnector(sv, model); script = msc.GenerateSourceScript(p, false); Assert.IsTrue(string.IsNullOrEmpty(script), script); }
public void TestFillUnify() { MSSQLProvider p = new MSSQLProvider(GetTestDB(), null); SourceView sv = p.GetSourceView(null, "aspnet_Membership, aspnet_Users"); WXMLModel model = new WXMLModel(); SourceToModelConnector c = new SourceToModelConnector(sv, model); c.ApplySourceViewToModel(false, relation1to1.Unify, true, true, false); Assert.AreEqual(2, model.GetSourceFragments().Count()); Assert.AreEqual(1, model.GetEntities().Count()); Assert.AreEqual(2, model.GetEntities().Single().GetSourceFragments().Count()); }
public void TestAlter() { var p = new MSSQLProvider(GetTestDB(), null); var sv = p.GetSourceView(); var model = new WXMLModel(); var smc = new SourceToModelConnector(sv, model); smc.ApplySourceViewToModel(false, relation1to1.Hierarchy, true, true, false); Assert.AreEqual(28, model.GetActiveEntities().Count()); Assert.AreEqual(32, model.GetSourceFragments().Count()); var msc = new ModelToSourceConnector(new SourceView(), model); foreach (SourceFragmentDefinition sf in sv.GetSourceFragments().ToArray()) { foreach (SourceFieldDefinition field in sv.GetSourceFields(sf).Where(item => !item.IsFK)) { msc.SourceView.SourceFields.Add(new SourceFieldDefinition(new SourceFragmentDefinition(field.SourceFragment.Identifier, field.SourceFragment.Name, field.SourceFragment.Selector), field.SourceFieldExpression, field.SourceType, field.SourceTypeSize, field.IsNullable, field.IsAutoIncrement, field.DefaultValue)); break; } } string script = msc.GenerateSourceScript(p, false); Assert.IsFalse(string.IsNullOrEmpty(script)); Console.WriteLine(script); Assert.AreEqual(4, new Regex("CREATE TABLE ").Matches(script).Count); Assert.AreEqual(26, new Regex("ALTER TABLE ").Matches(script.Remove(script.IndexOf("--Creating primary keys"))).Count); IEnumerable <SourceConstraint> pks = sv.GetSourceFragments().SelectMany(item => item.Constraints.Where(cns => cns.ConstraintType == SourceConstraint.PrimaryKeyConstraintTypeName)); Assert.AreEqual(pks.Count(), new Regex("PRIMARY KEY CLUSTERED").Matches(script).Count); Assert.AreEqual(2, new Regex("UNIQUE NONCLUSTERED").Matches(script).Count); Assert.AreEqual(1, new Regex("UNIQUE CLUSTERED").Matches(script).Count); Assert.AreEqual(sv.GetSourceFragments().SelectMany(item => item.Constraints.Where(cns => cns.ConstraintType == SourceConstraint.ForeignKeyConstraintTypeName)).Count(), new Regex("FOREIGN KEY").Matches(script).Count); }
public void TestFillHierarchy() { MSSQLProvider p = new MSSQLProvider(GetTestDB(), null); SourceView sv = p.GetSourceView(null, "aspnet_Membership, aspnet_Users"); WXMLModel model = new WXMLModel(); SourceToModelConnector c = new SourceToModelConnector(sv, model); c.ApplySourceViewToModel(); Assert.AreEqual(2, model.GetSourceFragments().Count()); Assert.AreEqual(2, model.GetEntities().Count()); EntityDefinition membership = model.GetEntity("e_dbo_aspnet_Membership"); Assert.IsNotNull(membership); EntityDefinition users = model.GetEntity("e_dbo_aspnet_Users"); Assert.IsNotNull(users); Assert.AreEqual(membership.BaseEntity, users); Assert.AreEqual(2, membership.GetSourceFragments().Count()); Assert.AreEqual(1, membership.OwnSourceFragments.Count()); Assert.AreEqual(1, users.GetSourceFragments().Count()); Assert.AreEqual(1, users.OwnSourceFragments.Count()); Assert.IsNull(users.BaseEntity); }
public void TestCompareLinqCtxMoreTables() { MSSQLProvider p = new MSSQLProvider(GetTestDB(), null); SourceView sv = p.GetSourceView(null, "ent1,ent2,1to2,ent3,3to3,complex_pk,complex_fk,aspnet_Applications,aspnet_Membership,aspnet_Users"); Assert.AreEqual(10, sv.GetSourceFragments().Count()); WXMLModel model = new WXMLModel(); SourceToModelConnector c = new SourceToModelConnector(sv, model); c.ApplySourceViewToModel(false, relation1to1.Default, false, false, false); Assert.AreEqual(10, model.GetSourceFragments().Count()); Assert.AreEqual(8, model.GetEntities().Count()); model.LinqSettings = new LinqSettingsDescriptor { ContextName = "TestCtxDataContext" }; model.Namespace = "LinqCodeGenTests"; LinqContextGenerator gen = new LinqContextGenerator(model, new WXML.CodeDom.WXMLCodeDomGeneratorSettings()); Console.WriteLine(gen.GenerateCode(LinqToCodedom.CodeDomGenerator.Language.CSharp)); Assembly assembly = gen.Compile(LinqToCodedom.CodeDomGenerator.Language.CSharp); Assert.IsNotNull(assembly); Type ctxType = assembly.GetType( string.IsNullOrEmpty(model.Namespace) ? model.LinqSettings.ContextName : model.Namespace + "." + model.LinqSettings.ContextName ); Assert.IsNotNull(ctxType); DataContext ctx = (DataContext)Activator.CreateInstance(ctxType, GetTestDBConnectionString()); Assert.IsNotNull(ctx); ctx.Log = Console.Out; TestCtxDataContext realCtx = new TestCtxDataContext(GetTestDBConnectionString()); Type rctxType = typeof(TestCtxDataContext); foreach (PropertyInfo pi in ctxType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)) { PropertyInfo rpi = rctxType.GetProperties().SingleOrDefault(item => item.Name == pi.Name); Assert.IsNotNull(rpi, "Cannot find property {0}", pi.Name); Assert.AreEqual(rpi.Attributes, pi.Attributes); Assert.AreEqual(rpi.CanRead, pi.CanRead); Assert.AreEqual(rpi.CanWrite, pi.CanWrite); Assert.AreEqual(rpi.GetGetMethod().Attributes, pi.GetGetMethod().Attributes); IListSource ent1s = (IListSource)pi.GetValue(ctx, null); Assert.IsNotNull(ent1s.GetList()); Assert.AreEqual(((IListSource)rpi.GetValue(realCtx, null)).GetList().Count, ent1s.GetList().Count); } }