Beispiel #1
0
        public void DefineUpdateEntity()
        {
            MsSqlEntityPipeline insertPipeline = new MsSqlEntityPipeline(ConnectString, PipelineMode.Update);

            try
            {
                insertPipeline.InitiEntity(EntitySpider.ParseEntityMetaData(typeof(UpdateEntity1).GetTypeInfo()));
                throw new SpiderException("TEST FAILED.");
            }
            catch (SpiderException e)
            {
                Assert.Equal("Columns set as primary is not a property of your entity.", e.Message);
            }

            try
            {
                insertPipeline.InitiEntity(EntitySpider.ParseEntityMetaData(typeof(UpdateEntity2).GetTypeInfo()));
                throw new SpiderException("TEST FAILED.");
            }
            catch (SpiderException e)
            {
                Assert.Equal("Columns set as update is not a property of your entity.", e.Message);
            }

            try
            {
                insertPipeline.InitiEntity(EntitySpider.ParseEntityMetaData(typeof(UpdateEntity3).GetTypeInfo()));
                throw new SpiderException("TEST FAILED.");
            }
            catch (SpiderException e)
            {
                Assert.Equal("There is no column need update.", e.Message);
            }

            insertPipeline.InitiEntity(EntitySpider.ParseEntityMetaData(typeof(UpdateEntity4).GetTypeInfo()));
            Assert.Equal(1, insertPipeline.GetUpdateColumns().Count);
            Assert.Equal("value", insertPipeline.GetUpdateColumns().First().Name);

            MsSqlEntityPipeline insertPipeline2 = new MsSqlEntityPipeline(ConnectString, PipelineMode.Update);

            insertPipeline2.InitiEntity(EntitySpider.ParseEntityMetaData(typeof(UpdateEntity5).GetTypeInfo()));
            Assert.Equal(1, insertPipeline2.GetUpdateColumns().Count);
            Assert.Equal("value", insertPipeline2.GetUpdateColumns().First().Name);
        }
Beispiel #2
0
        public void UpdateWhenUnionPrimary()
        {
            ClearDb();

            using (SqlConnection conn = new SqlConnection(ConnectString))
            {
                ISpider spider = new DefaultSpider("test", new Site());

                MsSqlEntityPipeline insertPipeline = new MsSqlEntityPipeline(ConnectString);
                insertPipeline.InitiEntity(EntitySpider.ParseEntityMetaData(typeof(Product2).GetTypeInfo()));
                insertPipeline.InitPipeline(spider);

                JObject data1 = new JObject {
                    { "sku", "110" }, { "category1", "4C" }, { "category", "3C" }, { "url", "http://jd.com/110" }, { "cdate", "2016-08-13" }
                };
                JObject data2 = new JObject {
                    { "sku", "111" }, { "category1", "4C" }, { "category", "3C" }, { "url", "http://jd.com/111" }, { "cdate", "2016-08-13" }
                };
                insertPipeline.Process(new List <JObject> {
                    data1, data2
                });

                MsSqlEntityPipeline updatePipeline = new MsSqlEntityPipeline(ConnectString, PipelineMode.Update);
                updatePipeline.InitiEntity(EntitySpider.ParseEntityMetaData(typeof(Product2).GetTypeInfo()));
                updatePipeline.InitPipeline(spider);

                JObject data3 = new JObject {
                    { "sku", "110" }, { "category1", "4C" }, { "category", "AAAA" }, { "url", "http://jd.com/110" }, { "cdate", "2016-08-13" }
                };
                updatePipeline.Process(new List <JObject> {
                    data3
                });

                var list = conn.Query <Product2>($"use test;select * from sku2_{DateTime.Now.ToString("yyyy_MM_dd")}").ToList();
                Assert.Equal(2, list.Count);
                Assert.Equal("110", list[0].Sku);
                Assert.Equal("AAAA", list[0].Category);
            }

            ClearDb();
        }
Beispiel #3
0
        public void Insert()
        {
            ClearDb();

            using (SqlConnection conn = new SqlConnection(ConnectString))
            {
                ISpider spider = new DefaultSpider("test", new Site());

                MsSqlEntityPipeline insertPipeline = new MsSqlEntityPipeline(ConnectString);
                insertPipeline.InitiEntity(EntitySpider.ParseEntityMetaData(typeof(Product).GetTypeInfo()));
                insertPipeline.InitPipeline(spider);

                // Common data
                JObject data1 = new JObject {
                    { "sku", "110" }, { "category", "3C" }, { "url", "http://jd.com/110" }, { "cdate", "2016-08-13" }
                };
                JObject data2 = new JObject {
                    { "sku", "111" }, { "category", "3C" }, { "url", "http://jd.com/111" }, { "cdate", "2016-08-13" }
                };
                // Value is null
                JObject data3 = new JObject {
                    { "sku", "112" }, { "category", null }, { "url", "http://jd.com/111" }, { "cdate", "2016-08-13" }
                };
                insertPipeline.Process(new List <JObject> {
                    data1, data2, data3
                });

                var list = conn.Query <Product>($"use test;select * from sku_{DateTime.Now.ToString("yyyy_MM_dd")}").ToList();
                Assert.Equal(3, list.Count);
                Assert.Equal("110", list[0].Sku);
                Assert.Equal("111", list[1].Sku);
                Assert.Equal(null, list[2].Category);
            }

            ClearDb();
        }