public void Can_Insert_Update_record_across_multiple_threads()
        {
            using (var db = OpenDbConnection())
            {
                db.DropAndCreateTable<ModelWithIdAndName>();
            }

            int count = 0;

            20.Times(i =>
            {
                ThreadPool.QueueUserWorkItem(state =>
                {
                    40.Times(_ =>
                    {
                        using (var db = OpenDbConnection())
                        {
                            var objA = new ModelWithIdAndName { Name = "A" };
                            var objB = new ModelWithIdAndName { Name = "B" };

                            objA.Id = (int)db.Insert(objA, selectIdentity: true);

                            objB.Id = (int)db.Insert(objB, selectIdentity: true);
                            objB.Name = objA.Name;

                            db.Update(objB);
                            Interlocked.Increment(ref count);
                        }
                    });
                });
            });

            Thread.Sleep(5000);
            Assert.That(count, Is.EqualTo(20 * 40));
        }
        public void Can_serialize_Queue_Generic()
        {
            var queue = new Queue <ModelWithIdAndName>();

            queue.Enqueue(ModelWithIdAndName.Create(1));
            queue.Enqueue(ModelWithIdAndName.Create(2));
            queue.Enqueue(ModelWithIdAndName.Create(3));

            Serialize(queue);

            Assert.That(CsvSerializer.SerializeToString(queue),
                        Is.EqualTo("Id,Name\r\n1,Name1\r\n2,Name2\r\n3,Name3\r\n"));
        }
        public void Can_serialize_Stack_Generic()
        {
            var stack = new Stack <ModelWithIdAndName>();

            stack.Push(ModelWithIdAndName.Create(1));
            stack.Push(ModelWithIdAndName.Create(2));
            stack.Push(ModelWithIdAndName.Create(3));

            Serialize(stack);

            Assert.That(CsvSerializer.SerializeToString(stack),
                        Is.EqualTo("Id,Name\r\n3,Name3\r\n2,Name2\r\n1,Name1\r\n"));
        }
        public void Can_Store_and_GetById_ModelWithIdAndName()
        {
            using (var redis = new RedisClient(TestConfig.SingleHost))
            {
                const int modelId = 1;
                var       to      = ModelWithIdAndName.Create(modelId);
                redis.Store(to);

                var from = redis.GetById <ModelWithIdAndName>(modelId);

                ModelWithIdAndName.AssertIsEqual(to, from);
            }
        }
        public async Task Can_change_conflict_resolution_with_Insert_Async()
        {
            using (var db = OpenDbConnection())
            {
                db.DropAndCreateTable <ModelWithIdAndName>();

                var row = new ModelWithIdAndName(1);
                await db.InsertAsync(row, dbCmd => dbCmd.OnConflictIgnore());

                //Equivalent to:
                await db.InsertAsync(row, dbCmd => dbCmd.OnConflict(ConflictResolution.Ignore));
            }
        }
        public void Can_serialize_object_with_escaped_chars()
        {
            const string expected = "{\"Id\":1,\"Name\":\"Hi I'm\\r\\nnew line :)\"}";
            var          model    = new ModelWithIdAndName
            {
                Id   = 1,
                Name = "Hi I'm\r\nnew line :)"
            };

            var result = JsonSerializer.SerializeToString(model);

            Assert.AreEqual(expected, result);
        }
Example #7
0
        public void Can_deserialize_json_with_whitespace()
        {
            var model = new ModelWithIdAndName
            {
                Id   = 1,
                Name = @"Simple string"
            };

            const string json = "\t { \t \"Id\" \t : 1 , \t \"Name\" \t  : \t \"Simple string\" \t } \t ";

            var fromJson = JsonSerializer.DeserializeFromString <ModelWithIdAndName>(json);

            Assert.That(fromJson, Is.EqualTo(model));
        }
Example #8
0
        public void Can_deserialize_with_new_line()
        {
            var model = new ModelWithIdAndName
            {
                Id   = 1,
                Name = "Hi I'm" + Environment.NewLine + "new line :)"
            };

            const string json = "{\"Id\":1,\"Name\":\"Hi I'm\\r\\nnew line :)\"}";

            var fromJson = JsonSerializer.DeserializeFromString <ModelWithIdAndName>(json);

            Assert.That(fromJson, Is.EqualTo(model));
        }
Example #9
0
            /// <summary>
            ///     Determines whether the specified <see cref="T:System.Object" /> is equal to the current
            ///     <see cref="T:System.Object" />.
            /// </summary>
            /// <param name="other">The model with identifier and name to compare to this object.</param>
            /// <returns>
            ///     true if the specified <see cref="T:System.Object" /> is equal to the current
            ///     <see cref="T:System.Object" />; otherwise, false.
            /// </returns>
            public bool Equals(ModelWithIdAndName other)
            {
                if (ReferenceEquals(null, other))
                {
                    return(false);
                }

                if (ReferenceEquals(this, other))
                {
                    return(true);
                }

                return(other.Id == Id && Equals(other.Name, Name));
            }
        public void Can_StoreAll_and_GetByIds_ModelWithIdAndName()
        {
            using (var redis = new RedisClient(TestConfig.SingleHost))
            {
                var ids = new[] { 1, 2, 3, 4, 5 };
                var tos = ids.ConvertAll(x => ModelWithIdAndName.Create(x));

                redis.StoreAll(tos);

                var froms   = redis.GetByIds <ModelWithIdAndName>(ids);
                var fromIds = froms.ConvertAll(x => x.Id);

                Assert.That(fromIds, Is.EquivalentTo(ids));
            }
        }
Example #11
0
        public void Can_select_scalar_value()
        {
            const int n = 5;

            using (var db = OpenDbConnection())
            {
                db.CreateTable <ModelWithIdAndName>(true);

                n.Times(x => db.Insert(ModelWithIdAndName.Create(x)));

                var count = db.GetScalar <long>("SELECT COUNT(*) FROM \"ModelWithIdAndName\"");

                Assert.That(count, Is.EqualTo(n));
            }
        }
        public void Can_GetFirstColumnDistinct()
        {
            const int n = 5;

            using (var db = OpenDbConnection())
            {
                db.CreateTable <ModelWithIdAndName>(true);

                n.Times(x => db.Insert(ModelWithIdAndName.Create(x)));

                var ids = db.ColumnDistinct <int>("SELECT \"id\" FROM " + "ModelWithIdAndName".SqlTable(DialectProvider));

                Assert.That(ids.Count, Is.EqualTo(n));
            }
        }
Example #13
0
        public void Can_GetFirstColumn()
        {
            const int n = 5;

            using (var db = OpenDbConnection())
            {
                db.CreateTable <ModelWithIdAndName>(true);

                n.Times(x => db.Insert(ModelWithIdAndName.Create(x)));

                var ids = db.GetFirstColumn <int>("SELECT \"Id\" FROM \"ModelWithIdAndName\"");

                Assert.That(ids.Count, Is.EqualTo(n));
            }
        }
        public void Can_select_scalar_value()
        {
            const int n = 5;

            using (var db = OpenDbConnection())
            {
                db.DropAndCreateTable <ModelWithIdAndName>();

                n.Times(x => db.Insert(ModelWithIdAndName.Create(x)));

                var count = db.ScalarFmt <int>("SELECT COUNT(*) FROM ModelWithIdAndName");

                Assert.That(count, Is.EqualTo(n));
            }
        }
        public void Can_GetFirstColumnDistinct()
        {
            const int n = 5;

            using (var db = OpenDbConnection())
            {
                db.DropAndCreateTable <ModelWithIdAndName>();

                n.Times(x => db.Insert(ModelWithIdAndName.Create(x)));

                var ids = db.ColumnDistinctFmt <int>("SELECT Id FROM ModelWithIdAndName");

                Assert.That(ids.Count, Is.EqualTo(n));
            }
        }
Example #16
0
        public void Can_select_scalar_value()
        {
            const int n = 5;

            using (var db = new OrmLiteConnectionFactory(ConnectionString, FirebirdDialect.Provider).Open())
            {
                db.CreateTable <ModelWithIdAndName>(true);
                db.DeleteAll <ModelWithIdAndName>();

                n.Times(x => db.Insert(ModelWithIdAndName.Create(0)));

                var count = db.Scalar <int>("SELECT COUNT(*) FROM ModelWIN");

                Assert.That(count, Is.EqualTo(n));
            }
        }
Example #17
0
        public void Can_GetFirstColumnDistinct()
        {
            const int n = 5;

            using (var db = new OrmLiteConnectionFactory(ConnectionString, FirebirdDialect.Provider).Open())
            {
                db.CreateTable <ModelWithIdAndName>(true);
                db.DeleteAll <ModelWithIdAndName>();

                n.Times(x => db.Insert(ModelWithIdAndName.Create(0)));

                var ids = db.ColumnDistinct <int>("SELECT Id FROM ModelWIN");

                Assert.That(ids.Count, Is.EqualTo(n));
            }
        }
        public void Can_ExecAs_ModelWithIdAndName_Func_IList()
        {
            ModelWithIdAndName[] expected =
            {
                ModelWithIdAndName.Create(1),
                ModelWithIdAndName.Create(2),
                ModelWithIdAndName.Create(3)
            };
            var actual = this._redisManager.ExecAs <ModelWithIdAndName>(m => {
                var list = m.Lists["typed-list"];
                list.AddRange(expected);
                return((IList <ModelWithIdAndName>)list.GetAll());
            });

            Assert.That(actual.EquivalentTo(expected));
        }
Example #19
0
        public void Can_GetFirstColumn()
        {
            const int n = 5;

            using (var db = ConnectionString.OpenDbConnection())
            {
                db.CreateTable <ModelWithIdAndName>(true);
                db.DeleteAll <ModelWithIdAndName>();

                n.Times(x => db.Insert(ModelWithIdAndName.Create(0)));

                var ids = db.GetFirstColumn <int>("SELECT Id FROM ModelWIN");

                Assert.That(ids.Count, Is.EqualTo(n));
            }
        }
        public void Can_GetFirstColumnDistinct()
        {
            const int n = 5;

            using (var db = ConnectionString.OpenDbConnection())
                using (var dbCmd = db.CreateCommand())
                {
                    dbCmd.CreateTable <ModelWithIdAndName>(true);

                    n.Times(x => dbCmd.Insert(ModelWithIdAndName.Create(x)));

                    var ids = dbCmd.GetFirstColumnDistinct <int>("SELECT Id FROM ModelWithIdAndName");

                    Assert.That(ids.Count, Is.EqualTo(n));
                }
        }
        public void Can_ExecAs_ModelWithIdAndName_Func_List()
        {
            var expected = new[] {
                ModelWithIdAndName.Create(1),
                ModelWithIdAndName.Create(2),
                ModelWithIdAndName.Create(3),
            };
            List <ModelWithIdAndName> actual = redisManager.ExecAs <ModelWithIdAndName>(m =>
            {
                var list = m.Lists["typed-list"];
                list.AddRange(expected);
                return(list.GetAll());
            });

            Assert.That(actual.EquivalentTo(expected));
        }
        public void Can_Select_In_for_string_value()
        {
            const int n = 5;

            using (var db = OpenDbConnection())
            {
                db.DropAndCreateTable <ModelWithIdAndName>();

                n.Times(x => db.Insert(ModelWithIdAndName.Create(x)));

                var selectInNames = new[] { "Name1", "Name2" };
                var rows          = db.SelectFmt <ModelWithIdAndName>("Name IN ({0})", selectInNames.SqlInValues());

                Assert.That(rows.Count, Is.EqualTo(selectInNames.Length));
            }
        }
        public void Can_select_scalar_value()
        {
            const int n = 5;

            using (var db = ConnectionString.OpenDbConnection())
                using (var dbCmd = db.CreateCommand())
                {
                    dbCmd.CreateTable <ModelWithIdAndName>(true);

                    n.Times(x => dbCmd.Insert(ModelWithIdAndName.Create(x)));

                    var count = dbCmd.GetScalar <int>("SELECT COUNT(*) FROM ModelWithIdAndName");

                    Assert.That(count, Is.EqualTo(n));
                }
        }
        public void Can_convert_model_with_ListChar()
        {
            var model = new ModelWithIdAndName {
                Id = 1, Name = "in [ valid"
            };
            var toModel = Serialize(model);

            ModelWithIdAndName.AssertIsEqual(toModel, model);

            var model2 = new ModelWithIdAndName {
                Id = 1, Name = "in valid]"
            };
            var toModel2 = Serialize(model2);

            ModelWithIdAndName.AssertIsEqual(toModel2, model2);
        }
        public void Can_GetDictionary()
        {
            const int n = 5;

            using (var db = OpenDbConnection())
            {
                db.DropAndCreateTable <ModelWithIdAndName>();

                n.Times(x => db.Insert(ModelWithIdAndName.Create(x)));

                var dictionary = db.Dictionary <int, string>("SELECT Id, Name FROM ModelWithIdAndName");

                Assert.That(dictionary, Has.Count.EqualTo(5));

                //Console.Write(dictionary.Dump());
            }
        }
        public void Can_Select_In_for_string_value()
        {
            const int n = 5;

            using (var db = ConnectionString.OpenDbConnection())
                using (var dbCmd = db.CreateCommand())
                {
                    dbCmd.CreateTable <ModelWithIdAndName>(true);

                    n.Times(x => dbCmd.Insert(ModelWithIdAndName.Create(x)));

                    var selectInNames = new[] { "Name1", "Name2" };
                    var rows          = dbCmd.Select <ModelWithIdAndName>("\"Name\" IN ({0})", selectInNames.SqlInValues());

                    Assert.That(rows.Count, Is.EqualTo(selectInNames.Length));
                }
        }
        public void Can_Save_table_with_null_fields()
        {
            using var db = OpenDbConnection();
            db.CreateTable <ModelWithIdAndName>(true);

            var row = ModelWithIdAndName.Create(1);

            row.Name = null;

            db.Save(row);

            var rows = db.Select <ModelWithIdAndName>();

            Assert.That(rows, Has.Count.EqualTo(1));

            ModelWithIdAndName.AssertIsEqual(rows[0], row);
        }
Example #28
0
        public void Can_access_ModelWithIdAndName()
        {
            var accessor = TypeProperties <ModelWithIdAndName> .Instance;

            var obj = new ModelWithIdAndName {
                Id = 1, Name = "A"
            };

            Assert.That(accessor.GetPublicGetter("Id")(obj), Is.EqualTo(1));
            Assert.That(accessor.GetPublicGetter("Name")(obj), Is.EqualTo("A"));

            accessor.GetPublicSetter("Id")(obj, 2);
            accessor.GetPublicSetter("Name")(obj, "B");

            Assert.That(obj.Id, Is.EqualTo(2));
            Assert.That(obj.Name, Is.EqualTo("B"));
        }
Example #29
0
        public void Can_GetDictionary()
        {
            const int n = 5;

            using (var db = new OrmLiteConnectionFactory(ConnectionString, FirebirdDialect.Provider).Open())
            {
                db.CreateTable <ModelWithIdAndName>(true);
                db.DeleteAll <ModelWithIdAndName>();
                n.Times(x => db.Insert(ModelWithIdAndName.Create(0)));

                var dictionary = db.Dictionary <int, string>("SELECT Id, Name FROM ModelWIN");

                Assert.That(dictionary, Has.Count.EqualTo(5));

                //Console.Write(dictionary.Dump());
            }
        }
Example #30
0
        public void Can_GetDictionary()
        {
            const int n = 5;

            using (var db = OpenDbConnection())
            {
                db.CreateTable <ModelWithIdAndName>(true);

                n.Times(x => db.Insert(ModelWithIdAndName.Create(x)));

                var dictionary = db.Dictionary <int, string>("SELECT \"id\", \"name\" FROM {0}".Fmt("ModelWithIdAndName".SqlTable()));

                Assert.That(dictionary, Has.Count.EqualTo(5));

                //Console.Write(dictionary.Dump());
            }
        }
        public void Can_PrettyFormat_generic_type()
        {
            var model = new ModelWithIdAndName {
                Id = 1, Name = "Name"
            };
            var modelStr = model.Dump();

            Assert.That(modelStr,
                        Is.EqualTo(
                            "{"
                            + Environment.NewLine
                            + "\tId: 1,"
                            + Environment.NewLine
                            + "\tName: Name"
                            + Environment.NewLine
                            + "}"
                            ));
        }