Example #1
0
    public virtual void AddDatabaseApi(string key, IDatabaseApi api)
    {
        Check.NotNull(key, nameof(key));
        Check.NotNull(api, nameof(api));

        if (_databaseApis.ContainsKey(key))
        {
            throw new AbpException("There is already a database API in this unit of work with given key: " + key);
        }

        _databaseApis.Add(key, api);
    }
        public void ShouldCreateDatabaseApi()
        {
            // Arrange
            HttpHeaders     headers = new HttpHeaders();
            IServiceFactory factory = new ServiceFactory(Mock.Of <IHttpAddress>(), Mock.Of <IHttpFacade>(), Mock.Of <IContentSerializer>(), headers);

            // Act
            IDatabaseApi api = factory.CreateDatabaseApi("mysql");

            // Assert
            api.ShouldNotBe(null);
        }
Example #3
0
        public void ShouldGetAccessComponentsAsync()
        {
            // Arrange
            IDatabaseApi databaseApi = CreateDatabaseApi();

            // Act
            List <TableInfo> result = databaseApi.GetAccessComponentsAsync().Result.ToList();

            // Assert
            result.Count.ShouldBe(4);
            result.Any(x => x.Name == "_table").ShouldBe(true);
        }
Example #4
0
        public void ShouldCallStoredProcWithoutParametersAsync()
        {
            // Arrange
            IDatabaseApi databaseApi = CreateDatabaseApi();

            // Act & Assert
            databaseApi.CallStoredProcAsync <ProcResponse>("foo", "dataset").Wait();

            Should.Throw <ArgumentNullException>(() => databaseApi.CallStoredProcAsync(null));
            Should.Throw <ArgumentNullException>(() => databaseApi.CallStoredProcAsync <ProcResponse>(null));
            Should.Throw <ArgumentNullException>(() => databaseApi.CallStoredProcAsync <ProcResponse>(null, "dataset"));
            Should.Throw <ArgumentNullException>(() => databaseApi.CallStoredProcAsync <ProcResponse>("foo", (string)null));
        }
Example #5
0
        public void ShouldDeleteTableAsync()
        {
            // Arrange
            IDatabaseApi databaseApi = CreateDatabaseApi();

            // Act
            bool success = databaseApi.DeleteTableAsync("staff").Result;

            // Assert
            success.ShouldBe(true);

            Should.Throw <ArgumentNullException>(() => databaseApi.DeleteTableAsync(null));
        }
Example #6
0
        public void ShouldGetLastestDocumentRevisionCorrectly()
        {
            var response = ConstructOkResponse("1-1a517022a0c2d4814d51abfedf9bfee7");

            var          handler     = new MockMessageHandler(response);
            IDatabaseApi databaseApi = GetDatabaseApi(handler);
            var          revision    = databaseApi.Synchronously.RequestLastestDocumentRevision("doc1");

            Assert.Equal("http://example.com:5984/testdb/doc1", handler.Request.RequestUri.ToString());
            Assert.Equal(HttpMethod.Head, handler.Request.Method);
            Assert.Equal(null, handler.Request.Content);
            Assert.Equal("1-1a517022a0c2d4814d51abfedf9bfee7", revision);
        }
Example #7
0
        public void ShouldGetTableNamesAsync()
        {
            // Arrange
            IDatabaseApi databaseApi = CreateDatabaseApi("alt");

            // Act
            List <string> names = databaseApi.GetTableNamesAsync(true).Result.ToList();

            // Assert
            names.Count.ShouldBe(7);
            names.ShouldContain("todo");
            names.ShouldContain("_schema/todo");
        }
Example #8
0
        public void ShouldDescribeTableAsync()
        {
            // Arrange
            IDatabaseApi databaseApi = CreateDatabaseApi();

            // Act
            TableSchema schema = databaseApi.DescribeTableAsync("staff").Result;

            // Assert
            schema.Name.ShouldBe("staff");

            Should.Throw <ArgumentNullException>(() => databaseApi.DescribeTableAsync(null));
        }
Example #9
0
        public void ShouldSendDeleteRequestOnDeletion()
        {
            var handler = new MockMessageHandler(
                new { ok = true, id = "doc1", rev = "1-1a517022a0c2d4814d51abfedf9bfee7" }.ToJsonObject());
            IDatabaseApi databaseApi = ((ICouchApi) new CouchApi(new CouchApiSettings(new Uri("http://example.com:5984/")), handler)).Db("testdb");

            var resultObject = databaseApi.Synchronously.DeleteDocument(docId: "doc1", revision: "1-1a517022a0c2d4814d51abfedf9bfee7");

            Assert.Equal(
                "http://example.com:5984/testdb/doc1?rev=1-1a517022a0c2d4814d51abfedf9bfee7",
                handler.Request.RequestUri.ToString());
            Assert.Equal("DELETE", handler.Request.Method.ToString());
            Assert.Equal(new DocumentInfo(id: "doc1", revision: "1-1a517022a0c2d4814d51abfedf9bfee7"), resultObject);
        }
        /// <summary>
        /// Adds the color attribute type.
        /// </summary>
        /// <returns>The type ID.</returns>
        private int AddColorAttributeType(IDatabaseApi uut, Guid databaseId)
        {
            AssetTypeBuilder       typeBuilder   = new AssetTypeBuilder(colorTypeName, databaseId);
            AssetNameAttributeType assetNameType = new AssetNameAttributeType
            {
                Key      = nameKey,
                Required = true
            };

            typeBuilder.AttributeTypes.Add(assetNameType);

            IntegerAttributeType redValue = new IntegerAttributeType
            {
                Key          = redKey,
                Required     = true,
                MinValue     = 0,
                MaxValue     = 255,
                DefaultValue = 0
            };

            typeBuilder.AttributeTypes.Add(redValue);

            IntegerAttributeType greenValue = new IntegerAttributeType
            {
                Key          = greenKey,
                Required     = true,
                MinValue     = 0,
                MaxValue     = 255,
                DefaultValue = 0
            };

            typeBuilder.AttributeTypes.Add(greenValue);

            IntegerAttributeType blueValue = new IntegerAttributeType
            {
                Key          = blueKey,
                Required     = true,
                MinValue     = 0,
                MaxValue     = 255,
                DefaultValue = 0
            };

            typeBuilder.AttributeTypes.Add(blueValue);

            int colorAssetTypeID = uut.AddAssetType(typeBuilder);

            return(colorAssetTypeID);
        }
Example #11
0
        public void ShouldThrowCouchCommunicationExceptionOn400StatusCode()
        {
            var handler =
                new MockMessageHandler(new HttpResponseMessage(HttpStatusCode.BadRequest)
            {
                Content = new JsonContent(new { error = "bad_request", reason = "Mock reason" }.ToJsonObject())
            });

            IDatabaseApi databaseApi = CreateCouchApi(handler).Db("testdb");

            var exception = Assert.Throws <CouchCommunicationException>(() =>
                                                                        databaseApi.Synchronously.BulkUpdate(x => x.Create(new { _id = "doc1", name = "John", age = 42 }.ToDocument()))
                                                                        );

            Assert.Contains("bad_request: Mock reason", exception.Message);
        }
Example #12
0
        public void ShouldReturnMetadataWhenQueryingRecordsAsync()
        {
            // Arrange
            IDatabaseApi databaseApi = CreateDatabaseApi();
            SqlQuery     query       = new SqlQuery {
                Fields = "*", IncludeCount = true, IncludeSchema = true
            };

            // Act
            DatabaseResourceWrapper <StaffRecord> result = databaseApi.GetRecordsAsync <StaffRecord>("staff", query).Result;

            // Assert
            result.Meta.ShouldNotBe(null);
            result.Meta.Count.ShouldBe(3);
            result.Meta.Schema.ShouldNotBe(null);
        }
Example #13
0
        public void ShouldGetRecordsAsync()
        {
            // Arrange
            IDatabaseApi databaseApi = CreateDatabaseApi();
            SqlQuery     query       = new SqlQuery {
                fields = "*"
            };

            // Act
            List <StaffRecord> records = databaseApi.GetRecordsAsync <StaffRecord>("staff", query).Result.ToList();

            // Assert
            records.Count.ShouldBe(3);
            records.First().uid.ShouldBe(1);
            records.Last().uid.ShouldBe(3);
        }
Example #14
0
        public void ShouldThrowCouchCommunicationExceptionOn400StatusCode()
        {
            var handler =
                new MockMessageHandler(new HttpResponseMessage(HttpStatusCode.BadRequest)
            {
                Content = new JsonContent(new { error = "bad_request", reason = "Mock reason" }.ToJsonObject())
            });

            IDatabaseApi databaseApi = GetDatabaseApi(handler);

            var exception = Assert.Throws <CouchCommunicationException>(
                () => databaseApi.Synchronously.RequestLastestDocumentRevision("doc1")
                );

            Assert.Contains("bad_request: Mock reason", exception.Message);
        }
Example #15
0
        public void ShouldCreateRecordsAsync()
        {
            // Arrange
            IDatabaseApi databaseApi          = CreateDatabaseApi();
            IEnumerable <StaffRecord> records = CreateStaffRecords();
            SqlQuery query = new SqlQuery {
                fields = "*"
            };

            // Act
            List <StaffRecord> created = databaseApi.CreateRecordsAsync("staff", records, query).Result.ToList();

            // Assert
            created.Count.ShouldBe(3);
            created.First().uid.ShouldBe(1);
            created.Last().uid.ShouldBe(3);
        }
        public async Task <IActionResult> CurrentRecords([FromServices] IDatabaseApi database, [FromServices] QuizStore quizStore)
        {
            var memberStr = User.FindFirst("memberId").Value;

            if (!Guid.TryParse(memberStr, out Guid memberId))
            {
                return(Forbid());
            }

            var records = await database.ListMemberRequiredTraining(memberId);

            var interesting = records.GroupJoin(
                quizStore.Quizzes,
                f => f.Course.Id,
                g => string.IsNullOrWhiteSpace(g.RecordsId) ? Guid.Empty : new Guid(g.RecordsId),
                (l, r) => l);

            return(Json(interesting));
        }
Example #17
0
        public void ShouldGetRecordsAsync()
        {
            // Arrange
            IDatabaseApi databaseApi = CreateDatabaseApi();
            SqlQuery     query       = new SqlQuery {
                Fields = "*"
            };

            // Act
            List <StaffRecord> records = databaseApi.GetRecordsAsync <StaffRecord>("staff", query).Result.Records.ToList();

            // Assert
            records.Count.ShouldBe(3);
            records.First().Uid.ShouldBe(1);
            records.Last().Uid.ShouldBe(3);

            Should.Throw <ArgumentNullException>(() => databaseApi.GetRecordsAsync <StaffRecord>(null, query));
            Should.Throw <ArgumentNullException>(() => databaseApi.GetRecordsAsync <StaffRecord>("staff", null));
        }
        private int AddBlueAsset(IDatabaseApi uut, Guid databaseId, int assetTypeId)
        {
            Asset blueAsset = uut.GenerateEmptyAsset(databaseId, assetTypeId);

            blueAsset.SetAttribute(nameKey, new AssetNameAttribute {
                Value = "Blue"
            });
            blueAsset.SetAttribute(redKey, new IntegerAttribute {
                Value = 0
            });
            blueAsset.SetAttribute(greenKey, new IntegerAttribute {
                Value = 0
            });
            blueAsset.SetAttribute(blueKey, new IntegerAttribute {
                Value = 255
            });

            return(uut.AddAsset(blueAsset));
        }
Example #19
0
        public static void Reset()
        {
            _databaseApi = null;

            _accessTokenDao           = null;
            _administratorDao         = null;
            _administratorsInRolesDao = null;
            _areaDao               = null;
            _channelDao            = null;
            _channelGroupDao       = null;
            _configDao             = null;
            _contentCheckDao       = null;
            _contentDao            = null;
            _contentGroupDao       = null;
            _contentTagDao         = null;
            _databaseDao           = null;
            _dbCacheDao            = null;
            _departmentDao         = null;
            _errorLogDao           = null;
            _keywordDao            = null;
            _logDao                = null;
            _permissionsInRolesDao = null;
            _pluginConfigDao       = null;
            _pluginDao             = null;
            _recordDao             = null;
            _relatedFieldDao       = null;
            _relatedFieldItemDao   = null;
            _roleDao               = null;
            _siteDao               = null;
            _siteLogDao            = null;
            _sitePermissionsDao    = null;
            _specialDao            = null;
            _tableStyleDao         = null;
            _tableStyleItemDao     = null;
            _tagDao                = null;
            _templateDao           = null;
            _templateLogDao        = null;
            _templateMatchDao      = null;
            _userDao               = null;
            _userGroupDao          = null;
            _userLogDao            = null;
            _userMenuDao           = null;
        }
Example #20
0
        public void ShouldThrowInvalidDocumentExceptionOnErrorResponse()
        {
            var handler = new MockMessageHandler(new object[] {
                new { id = "doc1", rev = "1-1a517022a0c2d4814d51abfedf9bfee7" },
                new { id = "doc2", error = "forbidden", reason = "message" }
            }.ToJsonValue());

            IDatabaseApi databaseApi = CreateCouchApi(handler).Db("testdb");

            var exception = Assert.Throws <InvalidDocumentException>(() =>
                                                                     databaseApi.Synchronously.BulkUpdate(
                                                                         x => {
                x.Create(new { _id = "doc1", name = "John", age = 42 }.ToDocument());
                x.Update(new { _id = "doc2", _rev = "1-1a517022a0c2d4814d51abfedf9bfee8", name = "John", age = 42 }.ToDocument());
            })
                                                                     );

            Assert.Contains("doc2", exception.Message);
        }
Example #21
0
        public void ShouldCallStoredFuncAsync()
        {
            // Arrange
            IDatabaseApi            databaseApi = CreateDatabaseApi();
            IStoreProcParamsBuilder builder     =
                new StoreProcParamsBuilder()
                .WithOutParam <string>("bar")
                .WithOutParam <int>("foo");

            StoredProcParam[] parameters = builder.Build();

            // Act
            ProcResponse result = databaseApi.CallStoredFuncAsync <ProcResponse>("foo", "dataset", parameters).Result;

            // Assert
            result.foo.ShouldBe(123);
            result.bar.ShouldBe("test");
            result.dataset.Count.ShouldBe(2);
            result.dataset.Any(x => x.first_name == "Selena").ShouldBe(true);
        }
Example #22
0
        public void ShouldDeleteRecordsAsync()
        {
            // Arrange
            IDatabaseApi databaseApi          = CreateDatabaseApi();
            IEnumerable <StaffRecord> records = CreateStaffRecords().Take(1).ToList();
            SqlQuery query = new SqlQuery {
                Fields = "*"
            };

            // Act
            List <StaffRecord> deleted = databaseApi.DeleteRecordsAsync("staff", records, query).Result.Records;

            // Assert
            deleted.Count.ShouldBe(1);
            deleted.First().Uid.ShouldBe(0);

            Should.Throw <ArgumentNullException>(() => databaseApi.DeleteRecordsAsync(null, records, query));
            Should.Throw <ArgumentNullException>(() => databaseApi.DeleteRecordsAsync("staff", (List <StaffRecord>)null, query));
            Should.Throw <ArgumentNullException>(() => databaseApi.DeleteRecordsAsync("staff", records, null));
        }
Example #23
0
        public void ShouldCreateUpdateCreateAndDeleteRecordsInBulkUpdateRequest()
        {
            var handler = new MockMessageHandler(
                new[] {
                new { id = "doc1", rev = "2-1a517022a0c2d4814d51abfedf9bfee7" },
                new { id = "doc2", rev = "2-1a517022a0c2d4814d51abfedf9bfee8" },
                new { id = "doc3", rev = "1-1a517022a0c2d4814d51abfedf9bfee9" },
                new { id = "doc4", rev = "1-1a517022a0c2d4814d51abfedf9bfee0" }
            }.ToJsonValue());
            IDatabaseApi databaseApi = CreateCouchApi(handler).Db("testdb");

            var result = databaseApi.Synchronously.BulkUpdate(
                x => {
                x.Create(new { _id = "doc1", name = "John", age = 42 }.ToDocument());
                x.Update(
                    new { _id = "doc2", _rev = "1-1a517022a0c2d4814d51abfedf9bfee8", name = "John", age = 42 }.
                    ToDocument());
                x.Delete(
                    new { _id = "doc3", _rev = "1-1a517022a0c2d4814d51abfedf9bfee9", name = "John", age = 42 }.
                    ToDocument());
                x.Delete("doc4", "1-1a517022a0c2d4814d51abfedf9bfee0");
            });

            var expectedDescriptor = new {
                docs = new object[] {
                    new { _id = "doc1", name = "John", age = 42 },
                    new { _id = "doc2", _rev = "1-1a517022a0c2d4814d51abfedf9bfee8", name = "John", age = 42 },
                    new { _id = "doc3", _rev = "1-1a517022a0c2d4814d51abfedf9bfee9", _deleted = true },
                    new { _id = "doc4", _rev = "1-1a517022a0c2d4814d51abfedf9bfee0", _deleted = true }
                }
            }.ToJsonString();

            var sendDescriptor = handler.RequestBodyString;

            Assert.Equal(expectedDescriptor, sendDescriptor);

            Assert.Equal("2-1a517022a0c2d4814d51abfedf9bfee7", result["doc1"].Revision);
            Assert.Equal("2-1a517022a0c2d4814d51abfedf9bfee8", result["doc2"].Revision);
            Assert.Equal("1-1a517022a0c2d4814d51abfedf9bfee9", result["doc3"].Revision);
            Assert.Equal("1-1a517022a0c2d4814d51abfedf9bfee0", result["doc4"].Revision);
        }
Example #24
0
        public void ShouldThrowAggregateExceptionOnMultiplyErrorResponse()
        {
            var handler = new MockMessageHandler(new object[] {
                new { id = "doc1", error = "forbidden", reason = "message" },
                new { id = "doc2", error = "conflict", reason = "message" }
            }.ToJsonValue());

            IDatabaseApi databaseApi = CreateCouchApi(handler).Db("testdb");

            var exception = Assert.Throws <AggregateException>(() =>
                                                               databaseApi.Synchronously.BulkUpdate(
                                                                   x => {
                x.Create(new { _id = "doc1", name = "John", age = 42 }.ToDocument());
                x.Create(new { _id = "doc2" }.ToDocument());
            })
                                                               );

            Assert.Equal(2, exception.InnerExceptions.Count);
            Assert.IsType <InvalidDocumentException>(exception.InnerExceptions[0]);
            Assert.IsType <StaleObjectStateException>(exception.InnerExceptions[1]);
        }
Example #25
0
        public void ShouldCallStoredFuncAsync()
        {
            // Arrange
            IDatabaseApi            databaseApi = CreateDatabaseApi();
            IStoreProcParamsBuilder builder     =
                new StoreProcParamsBuilder()
                .WithOutParam <string>("bar")
                .WithOutParam <int>("foo");

            StoredProcParam[] parameters = builder.Build();

            // Act
            ProcResponse result = databaseApi.CallStoredFuncAsync <ProcResponse>("foo", "dataset", parameters).Result;

            // Assert
            result.Foo.ShouldBe(123);
            result.Bar.ShouldBe("test");
            result.Dataset.Count.ShouldBe(2);
            result.Dataset.Any(x => x.FirstName == "Selena").ShouldBe(true);

            Should.Throw <ArgumentNullException>(() => databaseApi.CallStoredFuncAsync <ProcResponse>(null, parameters));
            Should.Throw <ArgumentNullException>(() => databaseApi.CallStoredFuncAsync <ProcResponse>(null, "dataset", parameters));
            Should.Throw <ArgumentNullException>(() => databaseApi.CallStoredFuncAsync <ProcResponse>("foo", null, parameters));
        }
Example #26
0
 private static ICouchApi MockCouchApi(IDatabaseApi databaseApi)
 {
     return Mock.Of<ICouchApi>(ca => ca.Db("testdb") == databaseApi);
 }
Example #27
0
 public ReplicatorApi(CouchApi parent)
 {
     this.parent = parent;
     synchronousReplicatorApi = new SynchronousReplicatorApi(this);
     replicatorDbApi          = parent.Db(parent.Settings.ReplicatorDatabase);
 }
Example #28
0
 /// <constructor />
 public DatabaseApiReference(IDatabaseApi dbApi = null)
 {
     dbApiWeakReference = dbApi == null? null: new WeakReference(dbApi, trackResurrection: false);
 }
Example #29
0
 public void AddDatabaseApi(string key, IDatabaseApi api)
 {
     _parent.AddDatabaseApi(key, api);
 }
Example #30
0
 /// <constructor />
 public DatabaseApiReference(IDatabaseApi dbApi = null)
 {
     dbApiWeakReference = dbApi == null? null: new WeakReference(dbApi, trackResurrection: false);
 }
 public SynchronousDatabaseApi(IDatabaseApi databaseApi)
 {
     this.databaseApi = databaseApi;
 }
 public ContactInfoController(IDatabaseApi databaseApi)
 {
     this.databaseApi = databaseApi;
 }
 public ContactGroupController(IDatabaseApi databaseApi)
 {
     this.databaseApi = databaseApi;
 }
 public ContactGroupController(IDatabaseApi databaseApi)
 {
     this.databaseApi = databaseApi;
 }
Example #35
0
 public SynchronousDatabaseApi(IDatabaseApi databaseApi)
 {
     this.databaseApi = databaseApi;
 }
 public ContactController(IDatabaseApi databaseApi, IFilesApi filesApi)
 {
     this.databaseApi = databaseApi;
     this.filesApi = filesApi;
 }