CodeSmith Community
Your Code. Your Way. Faster!

Non-identity, fixed-length PK fields and Unit Tests

Latest post 10-25-2007 11:53 PM by bslatner. 0 replies.
  • 10-25-2007 11:53 PM

    • bslatner
    • Top 500 Contributor
    • Joined on 10-26-2007
    • Charlotte, NC
    • Posts 19
    • Points 485

    Non-identity, fixed-length PK fields and Unit Tests

    I have a table "Country" with this schema:

    CREATE TABLE [dbo].[Country](
        [Country] nchar(2) NOT NULL,
        [Name] nvarchar(50) NOT NULL,
     CONSTRAINT [PK_Country] PRIMARY KEY CLUSTERED
    (
        [Country] ASC
    )) ON [PRIMARY]

    When the Step 4 (update) unit test is generated for this table, it tries to put a 3-character value in the "Country" field. It does this because the RandomString function in CommonSqlCode.cs has this near the end:

    if (column.IsPrimaryKeyMember && !IsIdentityColumn(column) && !IsComputed(column))
        return string.Concat(result, Guid.NewGuid().ToString("N").Substring(0,2));

    Thus, it's always returning a 2-character string. The UpdateMockInstance function generated by the unit test creation code appends a "2" to whatever comes back from RandomString, thus creating a 3-character string, which causes the unit test to always fail.

    On a side note, the RandomString function generates a string of length ((size / 2) - 1), where size is the # of characters in the column. In the case of a nchar(2) field that is NOT the primary key, RandomString will always return an empty string.

    Filed under: ,
    • Post Points: 5
Page 1 of 1 (1 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems