public void UpdateSimple()
    {      
      using (testEntities context = new testEntities())
      {        
        MySqlTrace.Listeners.Clear();
        MySqlTrace.Switch.Level = SourceLevels.All;
        GenericListener listener = new GenericListener();
        MySqlTrace.Listeners.Add(listener);
        Product pc = null;
        try
        {
          pc = new Product();
          pc.Name= "Acme";
          context.AddToProducts(pc);
          context.SaveChanges();
          pc.Name = "Acme 2";
          context.SaveChanges();
        }
        finally
        {
#if CLR4
          context.Products.DeleteObject(pc);
#endif
        }
        // Check sql        
        Regex rx = new Regex(@"Query Opened: (?<item>UPDATE .*)", RegexOptions.Compiled | RegexOptions.Singleline);
        foreach( string s in listener.Strings )
        {
          Match m = rx.Match(s);
          if (m.Success)
          {
            CheckSql(m.Groups["item"].Value, SQLSyntax.UpdateWithSelect);
            Assert.Pass();
          }
        }
        Assert.Fail();
      }
    }
Esempio n. 2
0
        public void CanInsertRowWithDefaultTimeStamp()
        {
            using (testEntities context = new testEntities())
              {
            // The default timestamp is in the CreatedDate column.
            Product product = new Product();
            product.Name = "Coca Cola";

            context.AddToProducts(product);
            context.SaveChanges();

            Assert.AreEqual(DateTime.Today.Day, product.CreatedDate.Day);
              }
        }
    public void UpdateAllRows()
    {
      MySqlCommand cmd = new MySqlCommand("SELECT COUNT(*) FROM toys", conn);
      object count = cmd.ExecuteScalar();

      using (testEntities context = new testEntities())
      {
        foreach (Toy t in context.Toys)
          t.Name = "Top";
        context.SaveChanges();
      }

      cmd.CommandText = "SELECT COUNT(*) FROM Toys WHERE name='Top'";
      object newCount = cmd.ExecuteScalar();
      Assert.AreEqual(count, newCount);
    }
Esempio n. 4
0
        public void SimpleDeleteAllRows()
        {
            using (testEntities context = new testEntities())
            {
                foreach (Toy t in context.Toys)
                    context.DeleteObject(t);
                context.SaveChanges();

                EntityConnection ec = context.Connection as EntityConnection;
                MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM toys",
                    (MySqlConnection)ec.StoreConnection);
                DataTable dt = new DataTable();
                da.Fill(dt);
                Assert.AreEqual(0, dt.Rows.Count);
            }
        }
Esempio n. 5
0
        public void SimpleDeleteRowByParameter()
        {
            using (testEntities context = new testEntities())
            {
                MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM toys WHERE minage=3", conn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                Assert.IsTrue(dt.Rows.Count > 0);

                ObjectQuery<Toy> toys = context.Toys.Where("it.MinAge = @age", new ObjectParameter("age", 3));
                foreach (Toy t in toys)
                    context.DeleteObject(t);
                context.SaveChanges();

                dt.Clear();
                da.Fill(dt);
                Assert.AreEqual(0, dt.Rows.Count);
            }
        }
Esempio n. 6
0
        public void TimeType()
        {
            using (testEntities context = new testEntities())
            {
                TimeSpan birth = new TimeSpan(11,3,2);

                Child c = new Child();
                c.Id = 20;
                c.EmployeeID = 1;
                c.FirstName = "first";
                c.LastName = "last";
                c.BirthTime = birth;
                context.AddToChildren(c);
                context.SaveChanges();

                MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM EmployeeChildren WHERE id=20", conn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                Assert.AreEqual(birth, dt.Rows[0]["birthtime"]);
            }
        }
Esempio n. 7
0
        public void InsertSingleRow()
        {
            MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM companies", conn);
              DataTable dt = new DataTable();
              da.Fill(dt);
              DataRow lastRow = dt.Rows[dt.Rows.Count - 1];
              int lastId = (int)lastRow["id"];
              DateTime dateBegan = DateTime.Now;

              using (testEntities context = new testEntities())
              {
            Company c = new Company();
            c.Id = 23;
            c.Name = "Yoyo";
            c.NumEmployees = 486;
            c.DateBegan = dateBegan;
            c.Address.Address = "212 My Street.";
            c.Address.City = "Helena";
            c.Address.State = "MT";
            c.Address.ZipCode = "44558";

            context.AddToCompanies(c);
            int result = context.SaveChanges();

            DataTable afterInsert = new DataTable();
            da.Fill(afterInsert);
            lastRow = afterInsert.Rows[afterInsert.Rows.Count - 1];

            Assert.AreEqual(dt.Rows.Count + 1, afterInsert.Rows.Count);
            Assert.AreEqual(lastId + 1, lastRow["id"]);
            Assert.AreEqual("Yoyo", lastRow["name"]);
            Assert.AreEqual(486, lastRow["numemployees"]);
            DateTime insertedDT = (DateTime)lastRow["dateBegan"];
            Assert.AreEqual(dateBegan.Date, insertedDT.Date);
            Assert.AreEqual("212 My Street.", lastRow["address"]);
            Assert.AreEqual("Helena", lastRow["city"]);
            Assert.AreEqual("MT", lastRow["state"]);
            Assert.AreEqual("44558", lastRow["zipcode"]);
              }
        }
    public void Update()
    {
      MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM Authors", conn);
      DataTable dt = new DataTable();
      da.Fill(dt);
      int count = dt.Rows.Count;

      using (testEntities context = new testEntities())
      {
        var q = from a in context.Authors
                where a.Name == "Don Box"
                select a;
        foreach (Author a in q)
          a.Name = "Dummy";
        context.SaveChanges();
      }

      da.SelectCommand.CommandText = "SELECT * FROM Authors WHERE name='Dummy'";
      dt.Clear();
      da.Fill(dt);
      Assert.AreEqual(1, dt.Rows.Count);
    }
    public void Insert()
    {
      MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM Authors", conn);
      DataTable dt = new DataTable();
      da.Fill(dt);
      int count = dt.Rows.Count;

      using (testEntities context = new testEntities())
      {
        Author a = new Author();
        a.Id = 23;
        a.Name = "Test name";
        a.Age = 44;
        context.AddToAuthors(a);
        context.SaveChanges();
      }

      dt.Clear();
      da.Fill(dt);
      Assert.AreEqual(count + 1, dt.Rows.Count);
      Assert.AreEqual(23, dt.Rows[count]["id"]);
    }
        public void CommandTimeout()
        {
            string connectionString = String.Format(
              "metadata=res://*/TestModel.csdl|res://*/TestModel.ssdl|res://*/TestModel.msl;provider=MySql.Data.MySqlClient; provider connection string=\"{0};default command timeout=5\"", GetConnectionString(true));
              EntityConnection connection = new EntityConnection(connectionString);

              using (testEntities context = new testEntities(connection))
              {
            Author a = new Author();
            a.Id = 66;  // special value to indicate the routine should take 30 seconds
            a.Name = "Test name";
            a.Age = 44;
            context.AddToAuthors(a);
            try
            {
              context.SaveChanges();
              Assert.Fail("This should have timed out");
            }
            catch (Exception ex)
            {
              string s = ex.Message;
            }
              }
        }
    public void Delete()
    {
      using (testEntities context = new testEntities())
      {
        foreach (Book b in context.Books)
          context.DeleteObject(b);
        context.SaveChanges();
      }

      MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM Books", conn);
      DataTable dt = new DataTable();
      da.Fill(dt);
      Assert.AreEqual(0, dt.Rows.Count);
    }
        public void DefaultCommandTimeOutNotWorking()
        {
            string connectionString = String.Format(
              "metadata=res://*/TestModel.csdl|res://*/TestModel.ssdl|res://*/TestModel.msl;provider=MySql.Data.MySqlClient; provider connection string=\"{0};Use Default Command Timeout For EF=true; Default Command Timeout=5;\"", GetConnectionString(true));
              //EntityConnection connection = new EntityConnection(connectionString);

              using (testEntities context = new testEntities(connectionString))
              {
            Author a = new Author();
            a.Id = 66;  // special value to indicate the routine should take 30 seconds
            a.Name = "Test name";
            a.Age = 44;
            context.AddToAuthors(a);
            try
            {
              context.SaveChanges();
              Assert.Fail("This should have timed out");
            }
            catch (Exception ex)
            {
              Exception innerException = ex.InnerException;
              Assert.AreEqual("Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.", innerException.Message);
            }
              }
        }
Esempio n. 13
0
        public void DoubleValuesNonEnglish()
        {
            CultureInfo curCulture = Thread.CurrentThread.CurrentCulture;
            CultureInfo curUICulture = Thread.CurrentThread.CurrentUICulture;
            CultureInfo newCulture = new CultureInfo("da-DK");
            Thread.CurrentThread.CurrentCulture = newCulture;
            Thread.CurrentThread.CurrentUICulture = newCulture;

            try
            {
                using (testEntities context = new testEntities())
                {
                    Child c = new Child();
                    c.Id = 20;
                    c.EmployeeID = 1;
                    c.FirstName = "Bam bam";
                    c.LastName = "Rubble";
                    c.BirthWeight = 8.65;
                    context.AddToChildren(c);
                    context.SaveChanges();
                }
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture = curCulture;
                Thread.CurrentThread.CurrentUICulture = curUICulture;
            }
        }
Esempio n. 14
0
        public void TimestampColumn()
        {
            DateTime now = DateTime.Now;

            using (testEntities context = new testEntities())
            {
                Child c = context.Children.First();
                DateTime dt = c.Modified.DateTime;
                c.Modified = now;
                context.SaveChanges();

                c = context.Children.First();
                dt = c.Modified.DateTime;
                Assert.AreEqual(now, dt);
            }
        }