Exemple #1
0
        public void Store()
        {
            IBeanFactory factory = new BeanFactory();

            factory.Options.ValidateGetColumns = false;
            var crud     = new BeanCrud(new InMemoryStorage(), null, new KeyUtil(), factory);
            var observer = new TracingObserver();

            crud.AddObserver(observer);

            var bean = crud.Dispense <Tracer>();

            // ----- Insert
            var id = crud.Store(bean);

            Assert.Equal(0L, id);
            Assert.Equal(0L, bean["id"]);
            Assert.Equal($"ad: bs: bi: ai:{id} as:{id}", bean.TraceLog);
            Assert.Equal($"ad: bs: bi: ai:{id} as:{id}", observer.TraceLog);

            // ------ Update
            bean.Put("p1", "test");
            observer.TraceLog = "";
            bean.TraceLog     = "";
            crud.Store(bean);
            Assert.Equal(0L, bean["id"]);
            Assert.Equal($"bs:{id} bu:{id} au:{id} as:{id}", bean.TraceLog);
            Assert.Equal($"bs:{id} bu:{id} au:{id} as:{id}", observer.TraceLog);
        }
        public void ImplicitTransactionsOnStoreAndTrash()
        {
            using (var conn = SQLitePortability.CreateConnection()) {
                conn.Open();

                IDatabaseDetails details = new SQLiteDetails();
                IDatabaseAccess  db      = new DatabaseAccess(conn, details);
                IKeyAccess       keys    = new KeyUtil();
                DatabaseStorage  storage = new DatabaseStorage(details, db, keys);
                IBeanFactory     factory = new BeanFactory();
                IBeanCrud        crud    = new BeanCrud(storage, db, keys, factory);

                storage.EnterFluidMode();

                var bean = crud.Dispense <ThrowingBean>();
                bean["foo"] = "ok";
                var id = crud.Store(bean);

                bean.Throw  = true;
                bean["foo"] = "fail";

                try { crud.Store(bean); } catch { }
                Assert.Equal("ok", db.Cell <string>(true, "select foo from test where id = {0}", id));

                try { crud.Trash(bean); } catch { }
                Assert.True(db.Cell <int>(true, "select count(*) from test") > 0);
            }
        }
        public void ImplicitTransactionsOnStoreAndTrash()
        {
            using(var conn = SQLitePortability.CreateConnection()) {
                conn.Open();

                IDatabaseDetails details = new SQLiteDetails();
                IDatabaseAccess db = new DatabaseAccess(conn, details);
                IKeyAccess keys = new KeyUtil();
                DatabaseStorage storage = new DatabaseStorage(details, db, keys);
                IBeanCrud crud = new BeanCrud(storage, db, keys);

                storage.EnterFluidMode();

                var bean = crud.Dispense<ThrowingBean>();
                bean["foo"] = "ok";
                var id = crud.Store(bean);

                bean.Throw = true;
                bean["foo"] = "fail";

                try { crud.Store(bean); } catch { }
                Assert.Equal("ok", db.Cell<string>(true, "select foo from test where id = {0}", id));

                try { crud.Trash(bean); } catch { }
                Assert.True(db.Cell<int>(true, "select count(*) from test") > 0);
            }
        }
Exemple #4
0
        public void Load()
        {
            IBeanFactory factory = new BeanFactory();

            factory.Options.ValidateGetColumns = false;
            var crud     = new BeanCrud(new InMemoryStorage(), null, new KeyUtil(), factory);
            var observer = new TracingObserver();

            crud.AddObserver(observer);

            // Load non-existing bean
            Assert.Null(crud.Load("any", 123));
            Assert.Empty(observer.TraceLog);

            var bean = crud.Dispense <Tracer>();

            bean.Put("p1", "test");

            var id = crud.Store(bean);

            observer.TraceLog = "";

            bean = crud.Load <Tracer>(id);
            Assert.Equal("ad: bl: al:" + id, bean.TraceLog);
            Assert.Equal("ad: bl: al:" + id, observer.TraceLog);
            Assert.NotNull(bean["id"]);
            Assert.Equal(id, bean["id"]);
            Assert.Equal("test", bean["p1"]);
        }
Exemple #5
0
 public void Dispense_Default()
 {
     var crud = new BeanCrud(null, null, null);
     var bean = crud.Dispense("test");
     Assert.Equal("test", bean.GetKind());
     Assert.Equal(typeof(Bean), bean.GetType());
 }
Exemple #6
0
        public void Trash()
        {
            IBeanFactory factory = new BeanFactory();

            factory.Options.ValidateGetColumns = false;
            var crud     = new BeanCrud(new InMemoryStorage(), null, new KeyUtil(), factory);
            var observer = new TracingObserver();

            crud.AddObserver(observer);

            var bean = crud.Dispense <Tracer>();

            observer.TraceLog = bean.TraceLog = "";
            crud.Trash(bean);
            Assert.Empty(bean.TraceLog);
            Assert.Empty(observer.TraceLog);

            var id = crud.Store(bean);

            observer.TraceLog = bean.TraceLog = "";
            crud.Trash(bean);
            Assert.Equal("bt:" + id + " at:" + id, bean.TraceLog);
            Assert.Equal("bt:" + id + " at:" + id, observer.TraceLog);
            Assert.Equal(id, bean["id"]);

            Assert.Null(crud.Load <Tracer>(id));
        }
Exemple #7
0
        public void Dispense_Default()
        {
            var crud = new BeanCrud(null, null, null);
            var bean = crud.Dispense("test");

            Assert.Equal("test", bean.GetKind());
            Assert.Equal(typeof(Bean), bean.GetType());
        }
Exemple #8
0
        public void Dispense_Hooks()
        {
            var crud = new BeanCrud(null, null, null);
            var observer = new TracingObserver();
            crud.AddObserver(observer);

            var bean = crud.Dispense<Tracer>();
            Assert.Equal("tracer", bean.GetKind());

            Assert.Equal("ad:", bean.TraceLog);
            Assert.Equal("ad:", observer.TraceLog);
            Assert.Same(bean, observer.LastBean);
        }
Exemple #9
0
        public void Dispense_Hooks()
        {
            var crud     = new BeanCrud(null, null, null);
            var observer = new TracingObserver();

            crud.AddObserver(observer);

            var bean = crud.Dispense <Tracer>();

            Assert.Equal("tracer", bean.GetKind());

            Assert.Equal("ad:", bean.TraceLog);
            Assert.Equal("ad:", observer.TraceLog);
            Assert.Same(bean, observer.LastBean);
        }
Exemple #10
0
        public void Store()
        {
            var crud     = new BeanCrud(new InMemoryStorage(), null, new KeyUtil());
            var observer = new TracingObserver();

            crud.AddObserver(observer);

            var bean = crud.Dispense <Tracer>();

            var id = crud.Store(bean);

            Assert.Equal(0L, id);
            Assert.Equal(0L, bean["id"]);
            Assert.Equal("ad: bs: as:" + id, bean.TraceLog);
            Assert.Equal("ad: bs: as:" + id, observer.TraceLog);
        }
Exemple #11
0
        public void Dispense_Hooks()
        {
            IBeanFactory factory = new BeanFactory();

            factory.Options.ValidateGetColumns = false;
            var crud     = new BeanCrud(null, null, null, factory);
            var observer = new TracingObserver();

            crud.AddObserver(observer);

            var bean = crud.Dispense <Tracer>();

            Assert.Equal("tracer", bean.GetKind());

            Assert.Equal("ad:", bean.TraceLog);
            Assert.Equal("ad:", observer.TraceLog);
            Assert.Same(bean, observer.LastBean);
        }
Exemple #12
0
        public void ConvertsValueToString()
        {
            IBeanFactory factory = new BeanFactory();
            var          crud    = new BeanCrud(null, null, null, factory);

            var bean = crud.Dispense("foo");

            bean.Import(
                new Dictionary <string, object>()
            {
                { "null", null },
                { "bool", true },
                { "sbyte", sbyte.Parse("123") },
                { "ssbyte", sbyte.Parse("-123") },
                { "byte", byte.Parse("123") },
                { "int", 123 },
                { "long", 123456789L },
                { "double", 123.4567 },
                { "decimal", 123.45m },
                { "string", "Hello!" },
                { "datetime", new DateTime(2000, 1, 1) },
                { "guid", Guid.Parse("6161ADAD-72F0-48D1-ACE2-CD98315C9D5B") },
                { "byte[]", Encoding.UTF8.GetBytes("Hello!") }
            }
                );

            AssertExtensions.WithCulture("de-DE", () =>
            {
                Assert.Equal("#NULL#", bean["null"].FormatValueToString());
                Assert.Equal("true", bean["bool"].FormatValueToString());
                Assert.Equal("123", bean["sbyte"].FormatValueToString());
                Assert.Equal("-123", bean["ssbyte"].FormatValueToString());
                Assert.Equal("123", bean["byte"].FormatValueToString());
                Assert.Equal("123", bean["int"].FormatValueToString());
                Assert.Equal("123456789", bean["long"].FormatValueToString());
                Assert.Equal("123,4567", bean["double"].FormatValueToString());
                Assert.Equal("123,45", bean["decimal"].FormatValueToString());
                Assert.Equal("Hello!", bean["string"].FormatValueToString());
                Assert.Equal("2000-01-01T00:00:00", bean["datetime"].FormatValueToString());
                Assert.Equal("6161ADAD-72F0-48D1-ACE2-CD98315C9D5B", bean["guid"].FormatValueToString());
                Assert.Equal("Hello!", bean["byte[]"].FormatValueToString());
            });
        }
Exemple #13
0
        public void Store()
        {
            IBeanFactory factory = new BeanFactory();

            factory.Options.ValidateGetColumns = false;
            var crud     = new BeanCrud(new InMemoryStorage(), null, new KeyUtil(), factory);
            var observer = new TracingObserver();

            crud.AddObserver(observer);

            var bean = crud.Dispense <Tracer>();

            var id = crud.Store(bean);

            Assert.Equal(0L, id);
            Assert.Equal(0L, bean["id"]);
            Assert.Equal("ad: bs: as:" + id, bean.TraceLog);
            Assert.Equal("ad: bs: as:" + id, observer.TraceLog);
        }
Exemple #14
0
        public void Load()
        {
            var crud = new BeanCrud(new InMemoryStorage(), null, new KeyUtil());
            var observer = new TracingObserver();
            crud.AddObserver(observer);

            // Load non-existing bean
            Assert.Null(crud.Load("any", 123));
            Assert.Empty(observer.TraceLog);

            var bean = crud.Dispense<Tracer>();
            bean.Put("p1", "test");

            var id = crud.Store(bean);
            observer.TraceLog = "";

            bean = crud.Load<Tracer>(id);
            Assert.Equal("ad: bl: al:" + id, bean.TraceLog);
            Assert.Equal("ad: bl: al:" + id, observer.TraceLog);
            Assert.NotNull(bean["id"]);
            Assert.Equal(id, bean["id"]);
            Assert.Equal("test", bean["p1"]);
        }
Exemple #15
0
        public void Trash()
        {
            var crud     = new BeanCrud(new InMemoryStorage(), null, new KeyUtil());
            var observer = new TracingObserver();

            crud.AddObserver(observer);

            var bean = crud.Dispense <Tracer>();

            observer.TraceLog = bean.TraceLog = "";
            crud.Trash(bean);
            Assert.Empty(bean.TraceLog);
            Assert.Empty(observer.TraceLog);

            var id = crud.Store(bean);

            observer.TraceLog = bean.TraceLog = "";
            crud.Trash(bean);
            Assert.Equal("bt:" + id + " at:" + id, bean.TraceLog);
            Assert.Equal("bt:" + id + " at:" + id, observer.TraceLog);
            Assert.Equal(id, bean["id"]);

            Assert.Null(crud.Load <Tracer>(id));
        }
Exemple #16
0
        public void Trash()
        {
            var crud = new BeanCrud(new InMemoryStorage(), null, new KeyUtil());
            var observer = new TracingObserver();
            crud.AddObserver(observer);

            var bean = crud.Dispense<Tracer>();

            observer.TraceLog = bean.TraceLog = "";
            crud.Trash(bean);
            Assert.Empty(bean.TraceLog);
            Assert.Empty(observer.TraceLog);

            var id = crud.Store(bean);

            observer.TraceLog = bean.TraceLog = "";
            crud.Trash(bean);
            Assert.Equal("bt:" + id + " at:" + id, bean.TraceLog);
            Assert.Equal("bt:" + id + " at:" + id, observer.TraceLog);
            Assert.Equal(id, bean["id"]);

            Assert.Null(crud.Load<Tracer>(id));
        }
Exemple #17
0
        public void Store()
        {
            var crud = new BeanCrud(new InMemoryStorage(), null, new KeyUtil());
            var observer = new TracingObserver();
            crud.AddObserver(observer);

            var bean = crud.Dispense<Tracer>();

            var id = crud.Store(bean);
            Assert.Equal(0L, id);
            Assert.Equal(0L, bean["id"]);
            Assert.Equal("ad: bs: as:" + id, bean.TraceLog);
            Assert.Equal("ad: bs: as:" + id, observer.TraceLog);
        }