I did some head-banging this morning while writing a unit test for Azure table storage CRUD operations. My WebRole application uses the PersonDataSource (line 8) as an ObjectDataSource and works without issue so I was perplexed as to why my unit test kept crashing on line 40. The UpdateItem(testdata) was successfully executing but the value was not being updated to "TestOrganization" as expected.
1 /// <summary>
2 /// Determines whether this instance can create read update delete
3 /// azure table records.
4 /// </summary>
5 [TestMethod]
6 public void CanCreateReadUpdateDeleteAzureTableRecords()
7 {
8 PersonDataSource mockData = new PersonDataSource();
9 EHRPerson testData = null;
10
11 string uniqueId = Guid.NewGuid().ToString();
12
13 //:::::[ CREATE ]:::::::
14 bool isSuccessful = mockData.CreateItem(new EHRPerson
15 {
16 ID = uniqueId,
17 Name = "EHRTest",
18 Organization = "EHROrganization",
19 PersonType = "EHRPersonType",
20 ProfessionalTraining = "EHRProfessionalTraining"
21 });
22 Assert.IsTrue(isSuccessful);
23
24 //:::::[ READ ]:::::::
25 testData = new List<EHRPerson>(mockData.Read())
26 .FirstOrDefault<EHRPerson>(p => p.ID.Equals(uniqueId));
27
28 Assert.IsNotNull(testData);
29 Assert.AreEqual("EHRTest", testData.Name);
30 Assert.AreEqual("EHROrganization", testData.Organization);
31 Assert.AreEqual("EHRPersonType", testData.PersonType);
32 Assert.AreEqual("EHRProfessionalTraining", testData.ProfessionalTraining);
33
34 //:::::[ UPDATE ]:::::::
35 testData.Organization = "TestOrganization";
36
37 mockData.UpdateItem(testData);
38 testData = new List<EHRPerson>(mockData.Read())
39 .FirstOrDefault<EHRPerson>(p => p.ID.Equals(uniqueId));
40 Assert.AreEqual("TestOrganization", testData.Organization);
41
42 //:::::[ DELETE ]:::::::
43 mockData.DeleteItem(testData);
44
45 testData = new List<EHRPerson>(mockData.Read())
46 .FirstOrDefault<EHRPerson>(p => p.ID.Equals(uniqueId));
47 Assert.IsNull(testData);
48
49 }
What I found was that my item (TItem) parameter was being reset to default items on line 330 (image below) effectively overwriting my changes with original values. However, if I ran my Cloud application it successfully updated the data (PersonDataSource as ObjectDataSource).
I discovered that I was being returned the reference of the item that was read previously (line 25 above) which meant I was passing around a reference so line 330 below effectively replaced it with the original contents thus entry = item. This wasn't a problem with the ObjectDataSource because I trust that ASP.NET creates a new instance and populates it with form values prior to sending it to the ObjectDataSource.
I resolved the issue by updating my TableServiceEntity base class to implement ICloneable so that I don't have to manage it at higher levels. All of my table entities, i.e., EHRPerson will be POCO so this should work well.
Tags:
azure,
crud,
unit test
Categories:
Azure |
Visual Studio 2010