public void TestNonSupportedInput()
        {
            var echoModel = new EchoModel();

            echoModel.RegisterModel();

            var model      = nameof(EchoModel);
            var input      = Expression.Value("string");
            var prediction = Function.Prediction(model, input);

            using (var q = QueryBuilder.Select(SelectResult.Expression(prediction))
                           .From(DataSource.Database(Db))) {
                q.Invoking(x => x.Execute()).Should().Throw <CouchbaseSQLiteException>()
                .Where(x => x.BaseError == SQLiteStatus.Error);
            }

            var dict = new Dictionary <string, object> {
                ["key"] = this
            };

            input      = Expression.Dictionary(dict);
            prediction = Function.Prediction(model, input);

            using (var q = QueryBuilder.Select(SelectResult.Expression(prediction))
                           .From(DataSource.Database(Db))) {
                q.Invoking(x => x.Execute()).Should().Throw <ArgumentException>();
            }
        }
Exemplo n.º 2
0
        public void TestNonSupportedInput()
        {
            var echoModel = new EchoModel();

            echoModel.RegisterModel();

            var model      = nameof(EchoModel);
            var input      = Expression.Value("string");
            var prediction = Function.Prediction(model, input);

            //Due to possibility of optimization on new SQLite, SQLite skips these expensive query calls if there is no document before running a query.
            using (var doc = new MutableDocument()) {
                doc.SetInt("number", 1);
                SaveDocument(doc);
            }

            using (var q = QueryBuilder.Select(SelectResult.Expression(prediction))
                           .From(DataSource.Database(Db))) {
                q.Invoking(x => x.Execute()).Should().Throw <CouchbaseSQLiteException>()
                .Where(x => x.BaseError == SQLiteStatus.Error);
            }

            var dict = new Dictionary <string, object> {
                ["key"] = this
            };

            input      = Expression.Dictionary(dict);
            prediction = Function.Prediction(model, input);

            using (var q = QueryBuilder.Select(SelectResult.Expression(prediction))
                           .From(DataSource.Database(Db))) {
                q.Invoking(x => x.Execute()).Should().Throw <ArgumentException>();
            }
        }
        public void TestRegisterMultipleModelsWithSameName()
        {
            CreateDocument(1, 2, 3, 4, 5);

            var model          = "TheModel";
            var aggregateModel = new AggregateModel();

            Database.Prediction.RegisterModel(model, aggregateModel);

            try {
                var input      = AggregateModel.CreateInput("numbers");
                var prediction = Function.Prediction(model, input);
                using (var q = QueryBuilder.Select(SelectResult.Expression(prediction))
                               .From(DataSource.Database(Db))) {
                    var rows = VerifyQuery(q, (n, result) =>
                    {
                        var pred = result.GetDictionary(0);
                        pred.GetInt("sum").Should().Be(15);
                    });
                    rows.Should().Be(1);

                    var echoModel = new EchoModel();
                    Database.Prediction.RegisterModel(model, echoModel);

                    rows = VerifyQuery(q, (n, result) =>
                    {
                        var pred = result.GetDictionary(0);
                        pred.GetValue("sum").Should().BeNull("because the model should have been replaced");
                        pred.GetArray("numbers").Should().ContainInOrder(new[] { 1L, 2L, 3L, 4L, 5L },
                                                                         "because the document should simply be echoed back");
                    });
                    rows.Should().Be(1);
                }
            } finally {
                Database.Prediction.UnregisterModel(model);
            }
        }
        public void TestPredictionInputOutput()
        {
            var echoModel = new EchoModel();

            echoModel.RegisterModel();

            using (var doc = new MutableDocument()) {
                doc.SetString("name", "Daniel");
                doc.SetInt("number", 2);
                doc.SetDouble("max", Double.MaxValue);
                Db.Save(doc);
            }

            var date  = DateTimeOffset.Now;
            var power = Function.Power(Expression.Property("number"), Expression.Int(2));
            var map   = new Dictionary <string, object>
            {
                ["null"]               = null,
                ["number1"]            = 10,
                ["number2"]            = 10.1,
                ["int_min"]            = Int32.MinValue,
                ["int_max"]            = Int32.MaxValue,
                ["int64_min"]          = Int64.MinValue,
                ["int64_max"]          = Int64.MaxValue,
                ["float_min"]          = Single.MinValue,
                ["float_max"]          = Single.MaxValue, // NOTE: Double limits are not guaranteed
                ["boolean_true"]       = true,
                ["boolean_false"]      = false,
                ["string"]             = "hello",
                ["date"]               = date,
                ["expr_property"]      = Expression.Property("name"),
                ["expr_value_number1"] = Expression.Value(20),
                ["expr_value_number2"] = Expression.Value(20.1),
                ["expr_value_boolean"] = Expression.Value(true),
                ["expr_value_string"]  = Expression.Value("hi"),
                ["expr_value_date"]    = Expression.Value(date),
                ["expr_value_null"]    = Expression.Value(null),
                ["expr_power"]         = power
            };

            var submap = new Dictionary <string, object> {
                ["foo"] = "bar"
            };

            map["dict"] = submap;
            var subList = new[] { "1", "2", "3" };

            map["array"] = subList;

            var subExprMap = new Dictionary <string, object> {
                ["ping"] = "pong"
            };

            map["expr_value_dict"] = Expression.Value(subExprMap);
            var subExprList = new[] { "4", "5", "6" };

            map["expr_value_array"] = Expression.Value(subExprList);

            var input      = Expression.Value(map);
            var model      = nameof(EchoModel);
            var prediction = Function.Prediction(model, input);

            using (var q = QueryBuilder.Select(SelectResult.Expression(prediction))
                           .From(DataSource.Database(Db)))
            {
                var rows = VerifyQuery(q, (n, result) =>
                {
                    var pred = result.GetDictionary(0);
                    pred.Count.Should().Be(map.Count,
                                           "because all properties should be serialized and recovered correctly");
                    pred.GetInt("number1").Should().Be(10);
                    pred.GetDouble("number2").Should().Be(10.1);
                    pred.GetInt("int_min").Should().Be(Int32.MinValue);
                    pred.GetInt("int_max").Should().Be(Int32.MaxValue);
                    pred.GetLong("int64_min").Should().Be(Int64.MinValue);
                    pred.GetLong("int64_max").Should().Be(Int64.MaxValue);
                    pred.GetFloat("float_min").Should().Be(Single.MinValue);
                    pred.GetFloat("float_max").Should().Be(Single.MaxValue);
                    pred.GetBoolean("boolean_true").Should().BeTrue();
                    pred.GetBoolean("boolean_false").Should().BeFalse();
                    pred.GetString("string").Should().Be("hello");
                    pred.GetDate("date").Should().Be(date);
                    pred.GetString("null").Should().BeNull();
                    pred.GetDictionary("dict").Should().Contain(submap);
                    pred.GetArray("array").Should().ContainInOrder(subList);

                    pred.GetString("expr_property").Should().Be("Daniel");
                    pred.GetInt("expr_value_number1").Should().Be(20);
                    pred.GetDouble("expr_value_number2").Should().Be(20.1);
                    pred.GetBoolean("expr_value_boolean").Should().BeTrue();
                    pred.GetString("expr_value_string").Should().Be("hi");
                    pred.GetDate("expr_value_date").Should().Be(date);
                    pred.GetString("expr_value_null").Should().BeNull();
                    pred.GetDictionary("expr_value_dict").Should().Contain(subExprMap);
                    pred.GetArray("expr_value_array").Should().ContainInOrder(subExprList);
                    pred.GetInt("expr_power").Should().Be(4);
                });

                rows.Should().Be(1);
            }
        }