Esempio n. 1
0
        public void StartStorageService()
        {
            var testSchema = DataSetStorageContext.CreateTestSchema();
            Func <DataSchema> getTestSchema = () => { return(testSchema); };

            objPersisterContext = new DataSetStorageContext(getTestSchema);
            storageDalc         = new StorageDalc(
                objPersisterContext.StorageDbMgr.Dalc,
                objPersisterContext.ObjectContainerStorage,
                getTestSchema);

            DataSetStorageContext.AddTestData(testSchema, objPersisterContext.ObjectContainerStorage);

            var storageService = new StorageService(
                objPersisterContext.ObjectContainerStorage,
                storageDalc,
                getTestSchema);

            serviceHost = new WebServiceHost(storageService, new[] { new Uri(baseUrl) });

            serviceHost.Description.Behaviors.Find <ServiceDebugBehavior>().IncludeExceptionDetailInFaults = true;
            serviceHost.Description.Behaviors.Find <ServiceBehaviorAttribute>().InstanceContextMode        = InstanceContextMode.Single;

            serviceHost.Open();

            var endPoint = (WebHttpEndpoint)serviceHost.Description.Endpoints.First();

            endPoint.AutomaticFormatSelectionEnabled = true;
            ((WebHttpBehavior)endPoint.EndpointBehaviors.First()).AutomaticFormatSelectionEnabled = true;
            ((WebHttpBehavior)endPoint.EndpointBehaviors.First()).DefaultOutgoingResponseFormat   = WebMessageFormat.Json;
        }
Esempio n. 2
0
        public void Delete()
        {
            DataSetStorageContext.AddTestData(testSchema, objContext.ObjectContainerStorage);

            Assert.AreEqual(3, storageDalc.RecordsCount(new Query("contacts")));

            var ds          = new DataSet();
            var contactsTbl = testSchema.FindClassByID("contacts").CreateDataTable();

            ds.Tables.Add(contactsTbl);

            storageDalc.Load(new Query("contacts", (QField)"id" == (QConst)3), ds);
            Assert.AreEqual(1, ds.Tables["contacts"].Rows.Count);
            ds.Tables["contacts"].Rows[0].Delete();
            storageDalc.Update(ds.Tables["contacts"]);

            Assert.AreEqual(2, storageDalc.RecordsCount(new Query("contacts")));

            Assert.AreEqual(2, storageDalc.Delete(new Query("contacts")));
            Assert.AreEqual(0, storageDalc.RecordsCount(new Query("contacts")));
            Assert.AreEqual(2, storageDalc.RecordsCount(new Query("companies")));

            Assert.AreEqual(1, storageDalc.Delete(new Query("companies", (QField)"title" == (QConst)"Microsoft")));
            Assert.AreEqual(1, storageDalc.RecordsCount(new Query("companies")));
        }
Esempio n. 3
0
        protected DataSet CreateStorageSchemaDS()
        {
            var ds = new DataSet();

            ds.Tables.Add(DataSetStorageContext.CreateValueTable("object_datetime_values", typeof(DateTime)));
            ds.Tables.Add(DataSetStorageContext.CreateValueLogTable("object_datetime_values_log", typeof(DateTime)));

            ds.Tables.Add(DataSetStorageContext.CreateValueTable("object_decimal_values", typeof(decimal)));
            ds.Tables.Add(DataSetStorageContext.CreateValueLogTable("object_decimal_values_log", typeof(decimal)));

            ds.Tables.Add(DataSetStorageContext.CreateValueTable("object_integer_values", typeof(long)));
            ds.Tables.Add(DataSetStorageContext.CreateValueLogTable("object_integer_values_log", typeof(long)));

            ds.Tables.Add(DataSetStorageContext.CreateValueTable("object_string_values", typeof(string)));
            ds.Tables.Add(DataSetStorageContext.CreateValueLogTable("object_string_values_log", typeof(string)));

            ds.Tables.Add(DataSetStorageContext.CreateRelationsTable());
            ds.Tables.Add(DataSetStorageContext.CreateRelationsLogTable());

            ds.Tables.Add(DataSetStorageContext.CreateObjectTable());
            ds.Tables.Add(DataSetStorageContext.CreateObjectLogTable());

            ds.Tables.Add(DataSetStorageContext.CreateMetadataClassTable());
            ds.Tables.Add(DataSetStorageContext.CreateMetadataPropertyTable());
            ds.Tables.Add(DataSetStorageContext.CreateMetadataPropertyToClassTable());

            ds.Tables.Add(DataSetStorageContext.CreateMetadataRelationshipTable());
            return(ds);
        }
Esempio n. 4
0
        public void createTestStorageDalc()
        {
            testSchema = DataSetStorageContext.CreateTestSchema();
            Func <DataSchema> getTestSchema = () => { return(testSchema); };

            objContext  = new DataSetStorageContext(getTestSchema);
            storageDalc = new StorageDalc(objContext.StorageDbMgr.Dalc, objContext.ObjectContainerStorage, getTestSchema);
        }
Esempio n. 5
0
        public void RelationAddRemoveConstraint()
        {
            var o = DataSetStorageContext.CreateTestSchema();
            var objPersisterContext = new DataSetStorageContext(() => { return(o); });

            // add one company and one contact
            var googCompany = new ObjectContainer(o.FindClassByID("companies"));

            googCompany["title"] = "Google";
            objPersisterContext.ObjectContainerStorage.Insert(googCompany);

            var johnContact = new ObjectContainer(o.FindClassByID("contacts"));

            johnContact["name"] = "John";
            objPersisterContext.ObjectContainerStorage.Insert(johnContact);

            // set relation
            var rel = o.FindClassByID("contacts").FindRelationship(o.FindClassByID("contactCompany"), o.FindClassByID("companies"));

            objPersisterContext.ObjectContainerStorage.AddRelation(
                new ObjectRelation(johnContact.ID.Value, rel, googCompany.ID.Value));

            // test relation
            var johnRelations = objPersisterContext.ObjectContainerStorage.LoadRelations(johnContact,
                                                                                         new[] { rel });

            Assert.AreEqual(1, johnRelations.Count());
            Assert.AreEqual(false, johnRelations.First().Relation.Reversed);
            Assert.AreEqual(johnContact.ID.Value, johnRelations.First().SubjectID);
            Assert.AreEqual(googCompany.ID.Value, johnRelations.First().ObjectID);

            var googRelations = objPersisterContext.ObjectContainerStorage.LoadRelations(googCompany,
                                                                                         new[] { o.FindClassByID("companies").FindRelationship(o.FindClassByID("contactCompany"), o.FindClassByID("contacts")) });

            Assert.AreEqual(1, googRelations.Count());
            Assert.AreEqual(true, googRelations.First().Relation.Reversed);
            Assert.AreEqual(johnContact.ID.Value, googRelations.First().ObjectID);
            Assert.AreEqual(googCompany.ID.Value, googRelations.First().SubjectID);

            // constraint
            var msCompany = new ObjectContainer(o.FindClassByID("companies"));

            msCompany["title"] = "Microsoft";
            objPersisterContext.ObjectContainerStorage.Insert(msCompany);
            // try to add one more company for John
            Assert.Throws <ConstraintException>(() => {
                objPersisterContext.ObjectContainerStorage.AddRelation(
                    new ObjectRelation(johnContact.ID.Value, rel, msCompany.ID.Value));
            });
        }
Esempio n. 6
0
        public void Update()
        {
            DataSetStorageContext.AddTestData(testSchema, objContext.ObjectContainerStorage);

            var ds          = new DataSet();
            var contactsTbl = testSchema.FindClassByID("contacts").CreateDataTable();

            ds.Tables.Add(contactsTbl);

            storageDalc.Load(new Query("contacts", (QField)"name" == (QConst)"Bob"), ds);
            Assert.AreEqual(1, contactsTbl.Rows.Count);

            contactsTbl.Rows[0]["name"]     = "Bob Marley";
            contactsTbl.Rows[0]["birthday"] = new DateTime(1945, 2, 6);

            storageDalc.Update(contactsTbl);

            Assert.AreEqual(0, storageDalc.RecordsCount(new Query("contacts", (QField)"name" == (QConst)"Bob")));
            Assert.AreEqual(1, storageDalc.RecordsCount(new Query("contacts", (QField)"name" == (QConst)"Bob Marley")));
        }
Esempio n. 7
0
        public void InsertLoadUpdateDelete()
        {
            var o = DataSetStorageContext.CreateTestSchema();

            var objPersisterContext = new DataSetStorageContext(() => { return(o); });

            var googCompany = new ObjectContainer(o.FindClassByID("companies"));

            googCompany["title"] = "Google";
            Assert.Catch <ArgumentException>(new TestDelegate(() => {
                googCompany["name"] = "ZZZ";
            }));
            googCompany["net_income"] = 999;

            var yahooCompany = new ObjectContainer(o.FindClassByID("companies"));

            yahooCompany["title"] = "Yahoo Inc";
            var googleChildCompany = new ObjectContainer(o.FindClassByID("companies"));

            googleChildCompany["title"] = "YouTube";


            var johnContact = new ObjectContainer(o.FindClassByID("contacts"));

            johnContact["name"]       = "John";
            johnContact["is_primary"] = true;
            var maryContact = new ObjectContainer(o.FindClassByID("contacts"));

            maryContact["name"]       = "Mary";
            maryContact["is_primary"] = false;
            maryContact["birthday"]   = new DateTime(1999, 5, 20);
            var bobContact = new ObjectContainer(o.FindClassByID("contacts"));

            bobContact["name"]       = "Bob";
            bobContact["is_primary"] = true;

            var usaCountry = new ObjectContainer(o.FindClassByID("countries"));

            usaCountry["title"] = "USA";
            var canadaCountry = new ObjectContainer(o.FindClassByID("countries"));

            canadaCountry["title"] = "Canada";


            objPersisterContext.ObjectContainerStorage.Insert(googCompany);
            Assert.True(googCompany.ID.HasValue);
            AssertObjectLog(objPersisterContext.StorageDS, googCompany.ID.Value, "insert");
            objPersisterContext.ObjectContainerStorage.Insert(yahooCompany);
            objPersisterContext.ObjectContainerStorage.Insert(googleChildCompany);

            objPersisterContext.ObjectContainerStorage.Insert(johnContact);
            objPersisterContext.ObjectContainerStorage.Insert(maryContact);
            objPersisterContext.ObjectContainerStorage.Insert(bobContact);

            objPersisterContext.ObjectContainerStorage.Insert(usaCountry);
            objPersisterContext.ObjectContainerStorage.Insert(canadaCountry);


            // load test
            var maryCopy = objPersisterContext.ObjectContainerStorage.Load(new[] { maryContact.ID.Value }).Values.FirstOrDefault();

            Assert.NotNull(maryCopy, "Object Load failed");
            Assert.AreEqual((string)maryContact["name"], (string)maryCopy["name"]);
            Assert.AreEqual((bool)maryContact["is_primary"], (bool)maryCopy["is_primary"]);
            Assert.AreEqual((DateTime)maryContact["birthday"], (DateTime)maryCopy["birthday"]);

            var googCopy = objPersisterContext.ObjectContainerStorage.Load(new[] { googCompany.ID.Value }).Values.FirstOrDefault();

            Assert.NotNull(googCopy, "Object Load failed");
            Assert.AreEqual((string)googCompany["title"], (string)googCopy["title"]);
            Assert.AreEqual((decimal)googCompany["net_income"], (decimal)googCopy["net_income"]);

            // update test
            maryCopy["name"]       = "Mary Second";
            maryCopy["birthday"]   = new DateTime(1988, 2, 10);
            maryCopy["is_primary"] = true;
            objPersisterContext.ObjectContainerStorage.Update(maryCopy);
            AssertObjectLog(objPersisterContext.StorageDS, maryCopy.ID.Value, "update");

            // reload mary contact
            maryContact = objPersisterContext.ObjectContainerStorage.Load(new[] { maryContact.ID.Value }).Values.FirstOrDefault();
            Assert.AreEqual((string)maryContact["name"], "Mary Second");
            Assert.AreEqual((bool)maryContact["is_primary"], true);
            Assert.AreEqual((DateTime)maryContact["birthday"], (DateTime)maryCopy["birthday"]);

            // test relations
            var contactToCompanyRel = maryContact.GetClass().FindRelationship(
                o.FindClassByID("contactCompany"), googCompany.GetClass()
                );
            var companyToParentCompanyRel = googleChildCompany.GetClass().FindRelationship(
                o.FindClassByID("parentCompany"), googCompany.GetClass(), false
                );
            var companyToCountryRel = googCompany.GetClass().FindRelationship(
                o.FindClassByID("companyCountry"), usaCountry.GetClass(), false
                );

            objPersisterContext.ObjectContainerStorage.AddRelation(
                new ObjectRelation(maryContact.ID.Value, contactToCompanyRel, googCompany.ID.Value),
                new ObjectRelation(johnContact.ID.Value, contactToCompanyRel, googCompany.ID.Value),
                new ObjectRelation(bobContact.ID.Value, contactToCompanyRel, yahooCompany.ID.Value),
                new ObjectRelation(googleChildCompany.ID.Value, companyToParentCompanyRel, googCompany.ID.Value),
                new ObjectRelation(googCompany.ID.Value, companyToCountryRel, usaCountry.ID.Value),
                new ObjectRelation(yahooCompany.ID.Value, companyToCountryRel, usaCountry.ID.Value),
                new ObjectRelation(googleChildCompany.ID.Value, companyToCountryRel, canadaCountry.ID.Value)
                );

            var googCompanyRels = objPersisterContext.ObjectContainerStorage.LoadRelations(googCompany, null);

            Assert.AreEqual(4, googCompanyRels.Count(), "Expected 3 relations for Google company");

            var yahooCompanyRels = objPersisterContext.ObjectContainerStorage.LoadRelations(yahooCompany, null);

            Assert.AreEqual(2, yahooCompanyRels.Count(), "Expected 1 relation for Yahoo company");
            Assert.AreEqual(bobContact.ID.Value,
                            yahooCompanyRels.Where(r => r.Relation.Predicate.ID == "contactCompany").First().ObjectID,
                            "Bob should be a only contact of Yahoo");

            // remove rel
            var maryRel = googCompanyRels.Where(r => r.ObjectID == maryContact.ID.Value).First();

            objPersisterContext.ObjectContainerStorage.RemoveRelation(
                new ObjectRelation(
                    googCompany.ID.Value,
                    googCompany.GetClass().FindRelationship(o.FindClassByID("contactCompany"), maryContact.GetClass()),
                    maryContact.ID.Value)
                );

            Assert.AreEqual(1, objPersisterContext.ObjectContainerStorage.LoadRelations(googCompany,
                                                                                        new[] {
                googCompany.GetClass().FindRelationship(o.FindClassByID("contactCompany"), maryContact.GetClass())
            }).Count(),
                            "Expected 1 relation for Google company after Mary removal");

            // test for inferred relation
            var johnToCountryRels = objPersisterContext.ObjectContainerStorage.LoadRelations(johnContact, new[] {
                new Relationship(
                    johnContact.GetClass(),
                    new [] { contactToCompanyRel, companyToCountryRel },
                    o.FindClassByID("countries"))
            });

            Assert.AreEqual(1, johnToCountryRels.Count());
            Assert.AreEqual(usaCountry.ID.Value, johnToCountryRels.First().ObjectID);

            //Console.WriteLine("DataSet after test:\n" + objPersisterContext.StorageDS.GetXml());
        }
Esempio n. 8
0
        public void Load()
        {
            DataSetStorageContext.AddTestData(testSchema, objContext.ObjectContainerStorage);

            var primaryContacts = storageDalc.LoadAllRecords(new Query("contacts", (QField)"is_primary" == new QConst(true)));

            Assert.AreEqual(2, primaryContacts.Length);
            Assert.True(primaryContacts.Where(r => r["name"].ToString() == "John").Any());
            Assert.True(primaryContacts.Where(r => r["name"].ToString() == "Bob").Any());

            // load only some fields
            var ds          = new DataSet();
            var contactsTbl = storageDalc.Load(new Query("contacts")
            {
                Fields = new[] { (QField)"name" }
            }, ds);

            Assert.AreEqual(1, contactsTbl.Columns.Count);
            Assert.AreEqual("name", contactsTbl.Columns[0].ColumnName);

            Assert.AreEqual(new DateTime(1999, 5, 20),
                            storageDalc.LoadValue(new Query("contacts", (QField)"name" == (QConst)"Mary")
            {
                Fields = new[] { (QField)"birthday" }
            }));

            // sort
            var companies = storageDalc.LoadAllRecords(new Query("companies")
            {
                Sort = new[] { new QSort("title", System.ComponentModel.ListSortDirection.Descending) }
            });

            Assert.AreEqual(2, companies.Length);
            Assert.AreEqual("Microsoft", companies[0]["title"]);
            Assert.AreEqual("Google", companies[1]["title"]);

            var sortedContactsQuery = new Query("contacts")
            {
                Fields = new [] { (QField)"id" },
                Sort   = new[] {
                    new QSort("birthday", System.ComponentModel.ListSortDirection.Ascending),
                    new QSort("is_primary", System.ComponentModel.ListSortDirection.Descending),
                    new QSort("name", System.ComponentModel.ListSortDirection.Descending)
                }
            };
            var sortedContactIds = storageDalc.LoadAllValues(sortedContactsQuery);

            Assert.AreEqual(3, sortedContactIds[0]);
            Assert.AreEqual(5, sortedContactIds[1]);
            Assert.AreEqual(4, sortedContactIds[2]);

            sortedContactsQuery.StartRecord = 1;
            sortedContactsQuery.RecordCount = 1;
            var pagedContactIds = storageDalc.LoadAllValues(sortedContactsQuery);

            Assert.AreEqual(1, pagedContactIds.Length);
            Assert.AreEqual(5, pagedContactIds[0]);

            // load relation
            var googContactIds = storageDalc.LoadAllValues(new Query("contacts_contactCompany_companies",
                                                                     (QField)"object_id" == new QConst(1))
            {
                Fields = new[] { (QField)"subject_id" }
            });

            Assert.AreEqual(1, googContactIds.Length);
            Assert.AreEqual(3, googContactIds[0]);
        }