public ActionResult Update(Bam.Net.Data.Repositories.Tests.MainObject dao)
 {
     try
     {
         dao.Save();
         return(Json(new { Success = true, Message = "", Dao = dao.ToJsonSafe() }));
     }
     catch (Exception ex)
     {
         return(GetErrorResult(ex));
     }
 }
Example #2
0
        private static MainObject CreateFromFilter(IQueryFilter filter, Database database = null)
        {
            Database db  = database ?? Db.For <MainObject>();
            var      dao = new MainObject();

            filter.Parameters.Each(p =>
            {
                dao.Property(p.ColumnName, p.Value);
            });
            dao.Save(db);
            return(dao);
        }
        private static void FillDatabaseWithTestData(SQLiteDatabase toBackup)
        {
            3.Times(i =>
            {
                OutLineFormat("Creating main...", ConsoleColor.DarkCyan);
                MainObject m = new MainObject();
                m.Created    = DateTime.UtcNow;
                m.Name       = "MainObject: {0}"._Format(i);
                m.Save(toBackup);
                OutLine(m.PropertiesToString(), ConsoleColor.Blue);

                OutLineFormat("\tAdding secondary...", ConsoleColor.Yellow);
                RandomNumber.Between(1, 3).Times(n =>
                {
                    SecondaryObject s = m.SecondaryObjectsByMainId.AddNew();
                    s.Created         = DateTime.UtcNow;
                    s.Name            = "\tSecondaryObject: {0}"._Format(n + 1);
                });
                m.Save(toBackup);
                m.SecondaryObjectsByMainId.Each(s =>
                {
                    OutLineFormat("\t\tAdding ternary...", ConsoleColor.Blue);
                    OutLine(s.PropertiesToString(), ConsoleColor.Cyan);
                    RandomNumber.Between(1, 3).Times(n =>
                    {
                        TernaryObject t = s.TernaryObjects.AddNew();
                        t.Created       = DateTime.UtcNow;
                        t.Name          = "\t\tTernaryObject: {0}"._Format(n + 1);
                    });
                    s.Save(toBackup);
                    s.TernaryObjects.Each(t =>
                    {
                        OutLineFormat(t.PropertiesToString(), ConsoleColor.DarkGray);
                    });
                });
            });
        }
        public void ShouldNotHaveSameId()
        {
            string sourceName = "{0}_{1}"._Format(MethodBase.GetCurrentMethod().Name, "Source");
            string destName   = "{0}_{1}"._Format(MethodBase.GetCurrentMethod().Name, "Destintation");

            SQLiteDatabase source;
            SQLiteDatabase dest;

            CreateTestDatabases(sourceName, destName, out source, out dest);

            MainObject one = new MainObject();

            one.Name    = "One: ".RandomLetters(8);
            one.Created = DateTime.UtcNow;
            one.Save(source);
            Expect.IsTrue(one.Id > 0);
            3.Times(i =>
            {
                MainObject existing = new MainObject();
                existing.Name       = "Existing in destintation: ".RandomLetters(6);
                existing.Created    = DateTime.UtcNow;
                existing.Save(dest);
            });
            MainObject check = MainObject.OneWhere(c => c.Name == one.Name, source);

            Expect.IsNotNull(check);
            check = MainObject.OneWhere(c => c.Name == one.Name, dest);
            Expect.IsNull(check, "Should have been null");

            string methodName = MethodBase.GetCurrentMethod().Name;

            List <IRepository> repos = new List <IRepository>();

            repos.Add(new DaoRepository(new SQLiteDatabase("BackupRepo_{0}"._Format(methodName), "BackupRepoDb")));
            repos.Add(new ObjectRepository("ObjectRepo_{0}"._Format(methodName)));

            foreach (IRepository repo in repos)
            {
                DaoBackup backup = new DaoBackup(typeof(MainObject).Assembly, source, repo);
                backup.Backup();
                HashSet <OldToNewIdMapping> wasIs = backup.Restore(dest);
                OutputWasIs(wasIs);
                check = MainObject.OneWhere(c => c.Uuid == one.Uuid, dest);
                Expect.IsNotNull(check);
                Expect.IsFalse(one.Id == check.Id);
                Expect.IsFalse(one.Id.Equals(check.Id));
                OutLineFormat(check.PropertiesToString());
            }
        }
        public void ShouldBeAbleToCopyDaoAsGeneratedDtoType()
        {
            SQLiteDatabase saveTo = GetTestDatabase(false);
            MainObject     toAdd  = new MainObject();

            toAdd.Name = 4.RandomLetters().Plus(":: The Name");
            toAdd.Save(saveTo);

            object dtoInstance = Dto.Copy(toAdd);

            OutLine("Dto info", ConsoleColor.Cyan);
            OutLineFormat("Type: {0}.{1}", dtoInstance.GetType().Namespace, dtoInstance.GetType().Name);
            OutLine(dtoInstance.TryPropertiesToString());

            MainObject daoInstance = MainObject.FirstOneWhere(c => c.Name == dtoInstance.Property("Name"), saveTo);

            Expect.IsNotNull(daoInstance);
            Expect.AreSame(saveTo, daoInstance.Database);
            OutLine("Dao info", ConsoleColor.Cyan);
            OutLineFormat("Type: {0}.{1}", daoInstance.GetType().Namespace, daoInstance.GetType().Name);
            OutLine(daoInstance.TryPropertiesToString());
        }
        public void ShouldBeAbleToSetValuesFromQueryFilter()
        {
            SQLiteDatabase testDatabase = GetTestDatabase();
            string         testName     = 8.RandomLetters();
            QueryFilter    filter       = Query.Where("Name") == testName;
            MainObject     one          = MainObject.OneWhere(filter, testDatabase);

            Expect.IsNull(one);
            if (one == null)
            {
                one = new MainObject();
                filter.Parameters.Each(p =>
                {
                    one.Property(p.ColumnName, p.Value);
                });
                one.Save(testDatabase);
            }

            MainObject check = MainObject.OneWhere(filter, testDatabase);

            Expect.IsNotNull(check);
            OutLine(check.TryPropertiesToString());
        }
 private static void SetupTestData(SQLiteDatabase testDatabase)
 {
     10.Times(i =>
     {
         MainObject o = new MainObject();
         o.Name       = 8.RandomLetters().Plus("::Main_Object {0}", i);
         o.Created    = DateTime.UtcNow;
         o.Save(testDatabase);
     });
     10.Times(i =>
     {
         SecondaryObject o = new SecondaryObject();
         o.Name            = 4.RandomLetters().Plus("::Secondary_object {0}", i);
         o.Created         = DateTime.UtcNow;
         o.Save(testDatabase);
     });
     10.Times(i =>
     {
         TernaryObject o = new TernaryObject();
         o.Name          = 3.RandomLetters().Plus("::Ternary_object ", i);
         o.Created       = DateTime.UtcNow;
         o.Save(testDatabase);
     });
 }
        public void ChildCollectionsShouldRestoreProperly()
        {
            string sourceName = "{0}_{1}"._Format(MethodBase.GetCurrentMethod().Name, "Source");
            string destName   = "{0}_{1}"._Format(MethodBase.GetCurrentMethod().Name, "Destintation");

            Dao.GlobalInitializer = (dao) =>
            {
                dao.Property("Created", DateTime.UtcNow, false);
            };
            Dao.BeforeCommitAny += (db, dao) =>
            {
                dao.Property("Modified", DateTime.UtcNow, false);
            };

            SQLiteDatabase source;
            SQLiteDatabase dest;

            CreateTestDatabases(sourceName, destName, out source, out dest);

            MainObject main = new MainObject();

            main.Name = "The Main Parent";
            main.Save(source);
            SecondaryObject secondary        = main.SecondaryObjectsByMainId.AddNew();
            string          secondaryOneName = 8.RandomLetters();

            secondary.Name = secondaryOneName;
            SecondaryObject secondary2       = main.SecondaryObjectsByMainId.AddNew();
            string          secondaryTwoName = 6.RandomLetters();

            secondary2.Name = secondaryTwoName;
            main.Save(source);
            Expect.IsNotNullOrEmpty(main.Uuid);
            TernaryObject ternary = secondary2.TernaryObjects.AddNew();

            ternary.Name = 4.RandomLetters();
            ternary.Save(source);
            secondary2.Save(source);
            ternary = TernaryObject.OneWhere(c => c.Uuid == ternary.Uuid, source);
            Expect.AreEqual(1, ternary.SecondaryObjects.Count);
            string     uuid  = main.Uuid;
            MainObject check = MainObject.OneWhere(c => c.Id == main.Id, source);

            Expect.IsTrue(check.SecondaryObjectsByMainId.Count == 2);

            string             methodName = MethodBase.GetCurrentMethod().Name;
            List <IRepository> repos      = new List <IRepository>();

            repos.Add(new DaoRepository(new SQLiteDatabase("BackupRepo_{0}"._Format(methodName), "BackupRepoDb")));
            //repos.Add(new ObjectRepository("ObjectRepo_{0}"._Format(methodName)));

            foreach (IRepository repo in repos)
            {
                DaoBackup backup = new DaoBackup(typeof(MainObject).Assembly, source, repo);
                backup.Backup();
                HashSet <OldToNewIdMapping> wasIs = backup.Restore(dest);
                OutputWasIs(wasIs);
                MainObject toValidate = MainObject.OneWhere(c => c.Uuid == uuid, dest);
                Expect.IsTrue(toValidate.SecondaryObjectsByMainId.Count == 2);
                List <string> names = toValidate.SecondaryObjectsByMainId.Select(s => s.Name).ToList();
                Expect.IsTrue(names.Contains(secondaryOneName));
                Expect.IsTrue(names.Contains(secondaryTwoName));
                SecondaryObject secondary2Check = SecondaryObject.OneWhere(c => c.Uuid == secondary2.Uuid, dest);
                Expect.IsNotNull(secondary2Check);
                Expect.AreEqual(1, secondary2Check.TernaryObjects.Count);
                Expect.IsTrue(secondary2Check.TernaryObjects[0].Name.Equals(ternary.Name));
                TernaryObject ternaryCheck = TernaryObject.OneWhere(c => c.Uuid == secondary2.TernaryObjects[0].Uuid, dest);
                Expect.IsTrue(ternaryCheck.SecondaryObjects.Count == 1);
            }
        }