public async Task WhenInsertFailsBecauseOtherError_ItShouldRaiseError()
        {
            await this.DatabaseTestAsync(async testDatabase =>
            {
                this.target = new RequestFreePostDbStatement(testDatabase);
                await this.CreateDataAsync(testDatabase);

                await testDatabase.TakeSnapshotAsync();

                await ExpectedException.AssertExceptionAsync <Exception>(
                    () => this.target.ExecuteAsync(RequestorId, PostId.Random(), Now, MaximumPosts));

                return(ExpectedSideEffects.None);
            });
        }
        public async Task WhenInsertSucceeds_ItShouldReturnTrue()
        {
            await this.DatabaseTestAsync(async testDatabase =>
            {
                this.target = new RequestFreePostDbStatement(testDatabase);
                await this.CreateDataAsync(testDatabase);
                await testDatabase.TakeSnapshotAsync();

                var result = await this.target.ExecuteAsync(RequestorId, PostId1, Now, MaximumPosts);
                Assert.IsTrue(result);

                return(new ExpectedSideEffects
                {
                    Insert = new FreePost(RequestorId.Value, PostId1.Value, null, Now),
                });
            });
        }
        public async Task WhenInsertFailsWithDuplicateKey_ItShouldReturnTrue()
        {
            await this.DatabaseTestAsync(async testDatabase =>
            {
                this.target = new RequestFreePostDbStatement(testDatabase);
                await this.CreateDataAsync(testDatabase);

                using (var connection = testDatabase.CreateConnection())
                {
                    await connection.InsertAsync(new FreePost(RequestorId.Value, PostId1.Value, null, Now));
                }

                await testDatabase.TakeSnapshotAsync();

                var result = await this.target.ExecuteAsync(RequestorId, PostId1, Now, MaximumPosts);
                Assert.IsTrue(result);

                return(ExpectedSideEffects.None);
            });
        }
        public async Task WhenInsertFailsBecauseMaximumPostsReached_ItShouldReturnFalse()
        {
            await this.DatabaseTestAsync(async testDatabase =>
            {
                this.target = new RequestFreePostDbStatement(testDatabase);
                await this.CreateDataAsync(testDatabase);

                await testDatabase.TakeSnapshotAsync();

                Assert.IsTrue(await this.target.ExecuteAsync(RequestorId, PostId1, Now, MaximumPosts));
                Assert.IsTrue(await this.target.ExecuteAsync(RequestorId, PostId2, Now, MaximumPosts));
                Assert.IsFalse(await this.target.ExecuteAsync(RequestorId, PostId3, Now, MaximumPosts));

                return(new ExpectedSideEffects
                {
                    Inserts = new List <IIdentityEquatable>
                    {
                        new FreePost(RequestorId.Value, PostId1.Value, null, Now),
                        new FreePost(RequestorId.Value, PostId2.Value, null, Now),
                    },
                });
            });
        }
 public void Initialize()
 {
     // Required for non-database tests.
     this.target = new RequestFreePostDbStatement(new Mock <IFifthweekDbConnectionFactory>(MockBehavior.Strict).Object);
 }