Beispiel #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;
        }
        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")));
        }
        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")));
        }
        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]);
        }