Exemplo n.º 1
0
        public void Up_ShouldConvertIdToGuid()
        {
            var expected = "ABC";

            Migrate.From <ClassLibrary1.Migrations.Initial>()
            .Setup(
                conn => conn.ExecuteScalar <int>(cmd =>
            {
                cmd.CommandText = @"
                            INSERT INTO dbo.Students (Name) VALUES(@Name);
                            SELECT CAST(@@IDENTITY AS int);
                        ";
                cmd.Parameters.AddWithValue("@Name", expected);
            })
                )
            .To <ClassLibrary1.Migrations.ConvertPrimaryKeyToGuid>()
            .Assert((id, conn) =>
            {
                var convertedId = new Guid($"{id:00000000}-0000-0000-0000-000000000000");

                var actual = conn.ExecuteScalar <string>(cmd =>
                {
                    cmd.CommandText = @"
                            SELECT s.Name FROM dbo.Students s WHERE s.Id = @Id
                        ";
                    cmd.Parameters.AddWithValue("@Id", convertedId);
                });

                Assert.Equal(expected, actual);
            });
        }
        public void Up_ShouldMoveStudentNamesToPeople()
        {
            var id   = Guid.NewGuid();
            var name = "ABC";

            Migrate.From <ClassLibrary1.Migrations.ConvertPrimaryKeyToGuid>()
            .Setup(
                conn => conn.ExecuteNonQuery(cmd =>
            {
                cmd.CommandText = @"
                            INSERT INTO dbo.Students (Id, Name) VALUES(@Id, @Name);
                        ";
                cmd.Parameters.AddWithValue("@Id", id);
                cmd.Parameters.AddWithValue("@Name", name);
            })
                )
            .To <ClassLibrary1.Migrations.AddEmployeesTable>()
            .Assert((_, conn) =>
            {
                var result = conn.ExecuteReader(
                    cmd =>
                {
                    cmd.CommandText = @"
                                SELECT p.Id, p.Name
                                FROM dbo.Students s
                                INNER JOIN dbo.People p
                                ON p.Id = s.PersonId
                                WHERE s.Id = @Id
                            ";

                    cmd.Parameters.AddWithValue("@Id", id);
                },
                    reader => (reader.GetGuid(0), reader.GetString(1))
                    )
                             .First();

                Assert.NotEqual(id, result.Item1);
                Assert.Equal(name, result.Item2);
            });
        }