Exemplo n.º 1
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"]);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        public void RowToBean()
        {
            IBeanFactory factory = new BeanFactory();

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

            crud.AddObserver(observer);

            var bean = crud.RowToBean <Tracer>(new Dictionary <string, object> {
                { "s", "hello" }
            });

            Assert.Null(bean["id"]);
            Assert.Equal("hello", bean["s"]);
            Assert.Equal("ad: bl: al:", bean.TraceLog);
            Assert.Equal("ad: bl: al:", observer.TraceLog);

            observer.TraceLog = "";

            bean = crud.RowToBean <Tracer>(new Dictionary <string, object> {
                { "id", 123 },
                { "s", "see you" }
            });

            Assert.Equal(123, bean["id"]);
            Assert.Equal("see you", bean["s"]);
            Assert.Equal("ad: bl: al:123", bean.TraceLog);
            Assert.Equal("ad: bl: al:123", observer.TraceLog);

            Assert.Null(crud.Load("temp", null));
        }
Exemplo n.º 4
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));
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        public void HandlesObserver()
        {
            var crud         = new BeanCrud(new InMemoryStorage(), null, null, null);
            var auditorLight = new AuditorLight();
            var tracer       = new TracingObserver();

            crud.AddObserver(auditorLight);
            crud.AddObserver(tracer);
            Assert.True(crud.HasObservers());
            Assert.True(crud.IsObserverLoaded <AuditorLight>());
            Assert.True(crud.IsObserverLoaded <TracingObserver>());

            crud.AddObserver(auditorLight);
            Assert.Equal(auditorLight, crud.GetObserver <AuditorLight>());

            crud.RemoveObserver <TracingObserver>();
            Assert.False(crud.IsObserverLoaded <TracingObserver>());

            Assert.True(crud.HasObservers());
        }