Retrieve a Recording


We need to write a test to retrieve a Recording with all the relationships filled in. We will call this method FindByRecordingId , and we will place this method in a class named Catalog . Just as in the previous tests, we need to insert a known recording and its associated entities into the database and then make the call to retrieve the recording and verify whether it is correct. After the tests are done, we then have to delete the known entity. We will use the RecordingBuilder as we did previously to insert or delete a known recording. Here are the tests for the Catalog:

 using System; using DataAccessLayer; using NUnit.Framework; [TestFixture] public class CatalogFixture : ConnectionFixture { // member variables     [SetUp]    public void SetUp()    {       // code to insert the Recording entity        RecordingDataSet loadedDataSet = new RecordingDataSet();       loadedRecording =       Catalog.FindByRecordingId(loadedDataSet,recordingId);    }    [TearDown]    public void TearDown()    { /* code to delete the Recording entity  */ }    [Test]    public void NotNull()    { Assert.IsNotNull(loadedRecording); }    [Test]    public void CountTracks()    {       RecordingDataSet.Track[] loadedTracks =           loadedRecording.GetTracks();       Assert.AreEqual(1, loadedTracks.Length);    }    [Test]    public void CountReviews()    {       RecordingDataSet.Review[] loadedReviews =           loadedRecording.GetReviews();       Assert.AreEqual(1, loadedReviews.Length);    }    [Test]    public void ArtistOfTheRecording()    {       RecordingDataSet.Artist loadedArtist = loadedRecording.Artist;       Assert.AreEqual(artistId,loadedArtist.Id);    }    [Test]    public void LabelOfTheRecording()    {       RecordingDataSet.Label loadedLabel = loadedRecording.Label;       Assert.AreEqual(labelId,loadedLabel.Id);    }    [Test]    public void ArtistOfTheTrack()    {       RecordingDataSet.Artist loadedArtist =           loadedRecording.GetTracks()[0].Artist;       Assert.AreEqual(artistId,loadedArtist.Id);    }    [Test]    public void GenreOfTheTrack()    {       RecordingDataSet.Genre loadedGenre =           loadedRecording.GetTracks()[0].Genre;       Assert.AreEqual(genreId,loadedGenre.Id);    }    [Test]    public void ReviewerOfTheReview()    {       RecordingDataSet.Reviewer loadedReviewer =           loadedRecording.GetReviews()[0].Reviewer;       Assert.AreEqual(reviewerId, loadedReviewer.Id);    } } 

These tests verify that we loaded the Recording from the database correctly. Given these tests, here is the implementation of Catalog that satisfies these tests:

 using System; using System.Configuration; using System.Data; using System.Data.SqlClient; using DataAccessLayer; public class Catalog {    public static RecordingDataSet.Recording FindByRecordingId(RecordingDataSet recordingDataSet, long recordingId)    {       SqlConnection connection = null;       RecordingDataSet.Recording recording = null;       try       {          connection = new SqlConnection(ConfigurationSettings.AppSettings.Get("Catalog.Connection"));          connection.Open();          RecordingGateway recordingGateway = new RecordingGateway(connection);          recording = recordingGateway.FindById(recordingId, recordingDataSet);          if(recording != null)          {             long artistId = recording.ArtistId;             ArtistGateway artistGateway = new ArtistGateway(connection);             RecordingDataSet.Artist artist =                 artistGateway.FindById(artistId, recordingDataSet);             long labelId = recording.LabelId;             LabelGateway labelGateway = new LabelGateway(connection);             RecordingDataSet.Label label =                 labelGateway.FindById(labelId, recordingDataSet);             GenreGateway genreGateway = new GenreGateway(connection);             TrackGateway trackGateway = new TrackGateway(connection);             foreach(RecordingDataSet.Track track in                 trackGateway.FindByRecordingId(recordingId, recordingDataSet))             {                artistId = track.ArtistId;                long genreId = track.GenreId;                artist = artistGateway.FindById(artistId, recordingDataSet);                RecordingDataSet.Genre genre =                    genreGateway.FindById(genreId, recordingDataSet);             }             ReviewGateway reviewGateway = new ReviewGateway(connection);             ReviewerGateway reviewerGateway = new ReviewerGateway(connection);             foreach(RecordingDataSet.Review review in                 reviewGateway.FindByRecordingId(recordingId, recordingDataSet))             {                long reviewerId = review.ReviewerId;                RecordingDataSet.Reviewer reviewer =                    reviewerGateway.FindById(reviewerId, recordingDataSet);             }          }       }       finally       {          if(connection != null)             connection.Close();       }       return recording;    } } 

When you look at the implementation, it is clear that the Catalog code uses the gateway classes that we wrote earlier. If you look at the tests, you notice that the scope of the tests is somewhat different from the previous tests. They test the integration of all the gateways to load a Recording from the database. This would cause a problem if there were a failure in an underlying gateway that would cause these tests to fail as well. What you should strive for in your tests is to have a failure cause one and only one test to fail. This is often not possible, but it is a goal. If you can achieve this, your time in the debugger will be diminished because the failing test points you to the problem. In this case, we did not do that because we are testing the ability to load an object from the database. If we were to provide stub implementations of the gateway classes, we would not be using the database, so it does not seem like a worthwhile investment. The code compiles and all 45 tests pass and there are no tests left on the list, so let s do some housekeeping before we move on.




Test-Driven Development in Microsoft .NET
Test-Driven Development in Microsoft .NET (Microsoft Professional)
ISBN: 0735619484
EAN: 2147483647
Year: 2004
Pages: 85

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net