public async Task BiExistsTest()
 {
     Assert.True(await OptionAsync <int> .Some(1).BiExists(i => i == 1, () => false));
     Assert.False(await OptionAsync <int> .Some(1).BiExists(i => i != 1, () => true));
     Assert.False(await OptionAsync <int> .None.BiExists(i => i == 1, () => false));
     Assert.True(await OptionAsync <int> .None.BiExists(i => i != 1, () => true));
 }
        public static async Task UnwrapSomeAsync_GivenSomeInput_ReturnsCorrectSomeValue()
        {
            const string expected = "expected";
            var          option   = OptionAsync <string> .Some(expected);

            var unwrapped = await option.UnwrapSomeAsync().ConfigureAwait(false);

            Assert.That(unwrapped, Is.EqualTo(expected));
        }
Exemple #3
0
        public async void TaskSomeIsSomeTask()
        {
            var ma = OptionAsync <int> .Some(123).AsTask();

            var mb = ma.Sequence();
            var mc = OptionAsync <Task <int> > .Some(123.AsTask());

            var mr = await(mb == mc);

            Assert.True(mr);
        }
        /// <summary>
        /// Retrieves a database view, if available.
        /// </summary>
        /// <param name="viewName">A view name.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A view definition, if available.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="viewName"/> is <c>null</c>.</exception>
        protected virtual OptionAsync <IDatabaseView> LoadView(Type viewType)
        {
            if (viewType == null)
            {
                throw new ArgumentNullException(nameof(viewType));
            }

            // TODO: check whether this even exists...
            var view = new ReflectionView(this, Dialect, viewType);

            return(OptionAsync <IDatabaseView> .Some(view));
        }
        protected virtual OptionAsync <IDatabaseSequence> LoadSequence(Type sequenceType)
        {
            if (sequenceType == null)
            {
                throw new ArgumentNullException(nameof(sequenceType));
            }

            // TODO: check whether this even exists...
            var sequence = new ReflectionSequence(this, Dialect, sequenceType);

            return(OptionAsync <IDatabaseSequence> .Some(sequence));
        }
        /// <summary>
        /// Retrieves a table from the database, if available.
        /// </summary>
        /// <param name="tableName">A table name.</param>
        /// <param name="queryCache">The query cache.</param>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <returns>A table, if available.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="tableName"/> is <c>null</c>.</exception>
        protected virtual OptionAsync <IRelationalDatabaseTable> LoadTable(Type tableType)
        {
            if (tableType == null)
            {
                throw new ArgumentNullException(nameof(tableType));
            }

            // TODO: check whether this even exists...
            var table = new ReflectionTable(this, Dialect, tableType);

            return(OptionAsync <IRelationalDatabaseTable> .Some(table));
        }
        public async Task MapTest()
        {
            var x = OptionAsync <int> .Some(1);

            Assert.True(await x.Select(i => i * 2).Exists(j => j == 2));
            Assert.True(await x.Map(i => i * 2).Exists(j => j == 2));

            var y = OptionAsync <int> .None;

            Assert.False(await y.Select(i => 2).Exists(j => j == 2));
            Assert.False(await y.Map(i => 2).Exists(j => j == 2));
        }
        public async Task BindTest()
        {
            var x = OptionAsync <int> .Some(1);

            Assert.True(await x.SelectMany(i => SomeAsync(i * 2), (i, j) => j).Exists(j => j == 2));
            Assert.True(await x.Bind(i => SomeAsync(i * 2)).Exists(j => j == 2));

            var y = OptionAsync <int> .None;

            Assert.False(await y.SelectMany(i => SomeAsync(2), (i, j) => j).Exists(j => true));
            Assert.False(await y.Bind(i => SomeAsync(2)).Exists(j => true));
        }
        protected virtual OptionAsync <IDatabaseSynonym> LoadSynonym(Type synonymType)
        {
            if (synonymType == null)
            {
                throw new ArgumentNullException(nameof(synonymType));
            }

            // TODO: check whether this even exists...
            var synonym = new ReflectionSynonym(this, Dialect, synonymType);

            return(OptionAsync <IDatabaseSynonym> .Some(synonym));
        }
        public OptionAsync <IDatabaseView> GetView(Identifier viewName, CancellationToken cancellationToken = default)
        {
            if (viewName == null)
            {
                throw new ArgumentNullException(nameof(viewName));
            }

            viewName = CreateQualifiedIdentifier(viewName);

            return(View.TryGetValue(viewName, out var view)
                ? OptionAsync <IDatabaseView> .Some(view)
                : OptionAsync <IDatabaseView> .None);
        }
        public OptionAsync <IDatabaseSequence> GetSequence(Identifier sequenceName, CancellationToken cancellationToken = default)
        {
            if (sequenceName == null)
            {
                throw new ArgumentNullException(nameof(sequenceName));
            }

            sequenceName = CreateQualifiedIdentifier(sequenceName);

            return(Sequence.TryGetValue(sequenceName, out var sequence)
                ? OptionAsync <IDatabaseSequence> .Some(sequence)
                : OptionAsync <IDatabaseSequence> .None);
        }
        public OptionAsync <IDatabaseSynonym> GetSynonym(Identifier synonymName, CancellationToken cancellationToken = default)
        {
            if (synonymName == null)
            {
                throw new ArgumentNullException(nameof(synonymName));
            }

            synonymName = CreateQualifiedIdentifier(synonymName);

            return(Synonym.TryGetValue(synonymName, out var synonym)
                ? OptionAsync <IDatabaseSynonym> .Some(synonym)
                : OptionAsync <IDatabaseSynonym> .None);
        }
        public OptionAsync <IRelationalDatabaseTable> GetTable(Identifier tableName, CancellationToken cancellationToken = default)
        {
            if (tableName == null)
            {
                throw new ArgumentNullException(nameof(tableName));
            }

            tableName = CreateQualifiedIdentifier(tableName);

            return(Table.TryGetValue(tableName, out var table)
                ? OptionAsync <IRelationalDatabaseTable> .Some(table)
                : OptionAsync <IRelationalDatabaseTable> .None);
        }
        public OptionAsync <IDatabaseRoutine> GetRoutine(Identifier routineName, CancellationToken cancellationToken = default)
        {
            if (routineName == null)
            {
                throw new ArgumentNullException(nameof(routineName));
            }

            routineName = CreateQualifiedIdentifier(routineName);

            return(Routine.TryGetValue(routineName, out var routine)
                ? OptionAsync <IDatabaseRoutine> .Some(routine)
                : OptionAsync <IDatabaseRoutine> .None);
        }
        public async Task FilterTest()
        {
            Assert.True(await OptionAsync <int> .Some(1).Filter(i => i == 1).IsSome);
            Assert.True(await OptionAsync <int> .Some(1).FilterAsync(i => Task.FromResult(i == 1)).IsSome);
            Assert.False(await OptionAsync <int> .Some(2).Filter(i => i == 1).IsSome);
            Assert.False(await OptionAsync <int> .Some(2).FilterAsync(i => Task.FromResult(i == 1)).IsSome);
            Assert.False(await OptionAsync <int> .None.Filter(i => true).IsSome);
            Assert.False(await OptionAsync <int> .None.FilterAsync(i => Task.FromResult(true)).IsSome);

            Assert.True(await OptionAsync <int> .Some(1).Where(i => i == 1).IsSome);
            Assert.False(await OptionAsync <int> .Some(2).Where(i => i == 1).IsSome);
            Assert.False(await OptionAsync <int> .None.Where(i => true).IsSome);
        }
 public async Task <OptionAsync <Payment> > GetByMerchantReferenceAsync(MerchantId merchantId, Option <MerchantReference> merchantReference)
 {
     return(await merchantReference.Match(async mr =>
     {
         Payment payment = Databag.Values.SingleOrDefault(p => p.MerchantReference == mr &&
                                                          p.MerchantId == merchantId);
         if (payment == null)
         {
             return await Task.FromResult(OptionAsync <Payment> .None);
         }
         return await Task.FromResult(OptionAsync <Payment> .Some(payment));
     },
                                          async() => await Task.FromResult(OptionAsync <Payment> .None)));
 }
        public async Task <OptionAsync <Payment> > GetAsync(MerchantId merchantId, PaymentId paymentId)
        {
            if (!Databag.ContainsKey(paymentId))
            {
                return(OptionAsync <Payment> .None);
            }
            Payment payment = Databag[paymentId];

            if (payment.MerchantId != merchantId)
            {
                return(OptionAsync <Payment> .None);
            }
            return(await Task.FromResult(OptionAsync <Payment> .Some(payment)));
        }
Exemple #18
0
        public static async Task FirstSome_GivenAsyncInputWithSomes_ReturnsFirstSome()
        {
            const string expected = "test_1";
            var          input    = new[]
            {
                OptionAsync <string> .None,
                OptionAsync <string> .Some("test_1"),
                OptionAsync <string> .Some("test_2"),
                OptionAsync <string> .None
            };
            var result    = input.FirstSome();
            var unwrapped = await result.UnwrapSomeAsync().ConfigureAwait(false);

            Assert.That(unwrapped, Is.EqualTo(expected));
        }
Exemple #19
0
        private static async Task <IIdentifierDefaults> GetIdentifierDefaultsAsyncCore(ISchematicConnection connection, CancellationToken cancellationToken)
        {
            var hostInfoOption      = connection.DbConnection.QueryFirstOrNone <DatabaseHost>(IdentifierDefaultsQuerySql, cancellationToken);
            var qualifiedServerName = await hostInfoOption
                                      .Bind(dbHost => dbHost.ServerHost != null && dbHost.ServerSid != null
                                            ?OptionAsync <DatabaseHost> .Some(dbHost)
                                            : OptionAsync <DatabaseHost> .None
                                            )
                                      .MatchUnsafe(
                dbHost => dbHost.ServerHost + "/" + dbHost.ServerSid,
                () => (string?)null
                ).ConfigureAwait(false);

            var dbName = await hostInfoOption.MatchUnsafe(h => h.DatabaseName, () => null).ConfigureAwait(false);

            var defaultSchema = await hostInfoOption.MatchUnsafe(h => h.DefaultSchema, () => null).ConfigureAwait(false);

            return(new IdentifierDefaults(qualifiedServerName, dbName, defaultSchema));
        }
 public async Task ExistsAsyncTest()
 {
     Assert.True(await OptionAsync <int> .Some(1).ExistsAsync(i => Task.FromResult(i == 1)));
     Assert.False(await OptionAsync <int> .None.ExistsAsync(i => Task.FromResult(true)));
 }
Exemple #21
0
 public async void SomeAsync_Pass() =>
 await Assert.SomeAsync(OptionAsync <string> .Some("1234"));
Exemple #22
0
 public async void SomeAsync_WithExpectation_Pass() =>
 await Assert.SomeAsync("1234", OptionAsync <string> .Some("1234"));
Exemple #23
0
 public async void Some_WithIncorrectExpectation_Fail() =>
 await Assert.ThrowsAsync <EqualException>(
     async() => await Assert.SomeAsync("1234", OptionAsync <string> .Some("5678")));
Exemple #24
0
 public OptionAsync <A> Some(A value) =>
 isnull(value)
         ? throw new ArgumentNullException(nameof(value))
         : OptionAsync <A> .Some(value);
Exemple #25
0
 public async void SomeAsync_WithCorrectAsserts_Pass() =>
 await Assert.SomeAsync(OptionAsync <int> .Some(1234), x => Assert.InRange(x, 1, 9999));
Exemple #26
0
 public async void NoneAsync_Fail() =>
 await Assert.ThrowsAsync <NoneException>(
     async() => await Assert.NoneAsync(OptionAsync <string> .Some("1234")));
Exemple #27
0
 public async void SomeAsync_WithIncorrectAsserts_Fail() =>
 await Assert.ThrowsAsync <InRangeException>(
     async() => await Assert.SomeAsync(OptionAsync <int> .Some(1234), x => Assert.InRange(x, 1, 999)));
Exemple #28
0
 public static OptionAsync <A> ToAsync <A>(this Option <A> self) =>
 self.IsSome ? OptionAsync <A> .Some(self.Value) : default;
        // Not valuable any more
        //[Fact]
        //public async void SequenceFlip()
        //{
        //    Task<Option<int>> taskOpt = Task.Run(() => Some(10));

        //    Option<Task<int>> optTask = taskOpt.Sequence();
        //    var res = await optTask.IfNone(0.AsTask());
        //    Assert.True(res == 10);

        //    taskOpt = optTask.Sequence();
        //}

        private static OptionAsync <int> GetValue(bool select) =>
        select
                ? OptionAsync <int> .Some(1000)
                : OptionAsync <int> .None;