Sunday 21 May 2017

Developing Windows Azure and Web Services Lab @ 2 Ans 3 ~ NIIT POST

CREATE TESTS FOR THE ENTITY FRAMEWORK DATA MODEL BY ISSUING QUERIES, MANIPULATING DATA, AND USING TRANSACTIONS TO TEST MULTIPLE REPOSITORIES AT ONCE.
THE MAIN TASKS FOR THIS EXERCISE ARE:
1. ADD EXISTING TEST PROJECT TO SOLUTION.
2. EXPLORE THE EXISTING INTEGRATION TEST PROJECT.
3. CREATE QUERIES BY USING LINQ TO ENTITIES.
4. CREATE QUERIES BY USING ENTITY SQL AND RAW SQL.
5. CREATE A TEST FOR MANIPULATING DATA.
6. CREATE CROSS-REPOSITORIES INTEGRATION TESTS WITH SYSTEM.TRANSACTIONS.
7. RUN THE TESTS, AND EXPLORE THE DATABASE CREATED BY ENTITY FRAMEWORK.






To implement the required functionality, you need to perform the following tasks:
1. Add existing test project to solution.
2. Explore the existing integration test project.
3. Create queries by using LINQ to entities.
4. Create queries by using entity SQL and raw SQL.
5. Create a test for manipulating data.
6. Create cross-repositories integration tests with System.Transactions.
7. Run the tests, and explore the database created by entity framework.
Task 1: Adding Existing Test Project to Solution
1. Browse to the location where Exercise 02.zip file is saved.
2. Extract the files.
3. Double-click the Exercise 02 folder.
4. Double-click the BlueYonder.Companion folder.
5. Double-click the BlueYonder.Companion.sin file. The BlueYonder.Companion - Microsoft Visual Studio window displayed.
6. Ensure that the Solution Explorer window is opened.
7. Right-click the Solution 'BlueYonder.Companion' (3 projects) node, and then select Add—Existing Project. The Add Existing Project dialog box is displayed.
8. Double-click the BlueYonder.Companion folder.
9. Double-click the BlueYonder.IntegrationTests folder.
10. Select the BlueYonder.IntegrationTests.csproj file.
11. Click the Open button.

Task 2: Exploring the Existing Integration Test Project
1. Ensure that the Solution Explorer window is opened.
2. Ensure that the BlueYonder.IntegrationTests node is expanded.
3. Double-click the TravelCompanionDatabaselnitializer.es file. The TravelCompanionDatabaselnitializer.es file is displayed.
4. Explore the database initialization code in the Seed() method within the TravelCompanicnDatabaselnitializer class.
5. Double-click the FlightQueries.es file in the Solution Explorer window.
6. Explore the query test methods in the FlightQueries class. The Testlnitialize() static method is responsible for initializing the database and the test data, and all the other methods are intended to test various queries with lazy load and eager load.
7. Double-click the FlightActions.es file.The FlightActions.es file is displayed.
8. Explore the insert, update, and delete test methods in the FlightActions class. Observe the use of the Assert static class to verify the results of the test.

Task 3: Creating Queries by Using LINQ to Entities
1. Ensure that the Solution Explorer window is opened.
2. Ensure that the BlueYonder.IntegrationT ests node is expanded.
3. Double-click the ReservationQueries.es file. The ReservationQueries.es file is displayed.
4. Type the highlighted portions of the following code snippet inside the GetSingleReservaticn() test method:

4. Type the highlighted portions of the following code snippet inside the GetSingleReservation() test method:
[TestMethod]
public void GetSingleReservation()
{
using (var repository * new ReservationRepository())
{
var query = from r in repository.GetAll()
where r.ConfirmationCode == "1234” select r;
var reservation = query. FirstOrDefault();
Assert.IsNotNull(reservation);
>
5. Type the highlighted portions of the following code snippet inside the GetReservationWithFlightsEagerLoad() test method:
[TestMethod]
public void GetReservationWithFlightsEagerLoad()
{
Reservation reservation;
using (var repository = new ReservationRepository())
{
var query = from r in repository.GetAll()
where r.ConfirmationCode == "1234" select r;
query = query.Include(r => r.OepartureFlight).Include(r =>
r.ReturnFlight);
reservation = query.FirstOrDefauIt();
}
Assert.IsNotNul1(reservation);
Assert.IsNotNull(reservation.DepartureFlight);
Assert. IsNotNull(reservation.RetumFlight);
}

6. Type the highlighted portions of the following code snippet inside the GetReservationWithFlights LazyLoad() test method:
[TestMethod]
public void GetReservationWithFlightsLazyLoad()
{
Reservation reservation}
using (var repository = new ReservationRepository())
{
var query = from r in repository.GetAll()
where r.ConfirmationCode == "1234" select r;
reservation = query.FirstOrDefault();
Assert.IsNotNull(reservation)}
Assert.IsNotNull(reservation.DepartureFlight)}
Assert.IsNotNull(reservation.ReturnFlight)}
}
}

7. Type the highlighted portions of the following code snippet inside the Get ReservationWithSingleF light Failed LazyLoad () test method:
[TestMethod]
[ExpectedException(typeof(ObjectDisposedException))] public void GetReservationsWithSingleFlightFailedl_azyLoad() {
Reservation reservation;
using (var repository = new ReservationRepository())
{
var query = from r in repository.GetAll()
where r.ConfirmationCode == "1234" select r;
reservation = query.FirstOrDefault();
}
Assert.IsNotNull(reservation);
// We haven't eager loaded the departure flights,
// and the context is disposed, so lazy load will fail Assert.IsNotNull(reservation.DepartureFlight);
}
8. Select FILE—Save All to save the changes
Task 4: Creating Queries by Using Entity SQL and Raw SQL
1. Ensure that the Solution Explorer window is opened 2 Ensure that the ReservationQueries.es file is opened
3. Type the highlighted portions of the following code snippet inside theGetOrderedReservations() method:
[TestMethod]
public void GetOrderedReservations()
{
using (TravelCompanionContext context = new TravelCompanionContext())
{
ObjectQuery<Reservation> query =
((IObjectContextAdapter)context).ObjectContext.CreateQuery<Reservation>(
^"SELECT value r
FROM reservations as r
ORDER BY r.con-firmationCode DESC”);
List<Reservation> reservations = query.ToListQj Assert.AreEqual(reservations.Count, 2);
Assert.AreEqual(reservations[0]-ConfirmationCode, "4321");
Assert.AreEqual(reservations[l].ConfirmationCode, "1234");
}
}

4. Type the highlighted portions of the following code snippet inside theGetReservationsForDepartFlightsBySeatClass() method:
[TestMethod]
public void GetReservationsForDepartFlightsBySeatClass()
{
using (TravelCompanionContext context = new TravelCompanionContext())
{
IEnumerable<Reservation> query = context.Database.SqlQuery<Reservation>( @"select r.* from Reservations r
inner join Trips t on r.DepartFlightSchedulelD = t.TripId where t.Class = 2");
List<Reservation> flights = query.ToList();
Assert.AreEqual(flights.Count, 1)5
Assert.AreEqual(flights[0].ConfirmationCode, "1234");
}
}
5. Select FILE—Save All to save the changes

Task 5: Creating a Test for Manipulating Data
1. Ensure that the Solution Explorer window is opened
2 Ensure that the BlueYonder.IntegrationTests node is expanded
3. Double-click the FlightActions.es file. The FlightAetions.es file is displayed
4 Type the highlighted portions of the following code snippet inside the UpdateFlightQ method
[TestMethod]
public void UpdateFlight()
{
FlightRepository repository;
using (repository = new FlightRepositoryO)
{
Flight flight = repository.FindBy(f => f.FlightNumber ”BY002").Single();
flight.FlightNumber = "BY002_updated"; repository.Edit(flight); repository.Save();
}
using (repository = new FlightRepositoryO)
{
Flight flight = repository.FindBy(f => f.FlightNumber "BY002_updated").FirstOrDefault();
Assert.IsNotNull(flight);
}
}
5. Select FILE—Save All to save the changes
Task 6: Creating Cross-repositories Integration Tests with System.Transactions
1. Ensure that the Solution Explorer window is opened 2 Ensure that the FlightActions.es file is opened
3. Type the highlighted portions of the following code snippet inside the UpdateUsingTwoRepositories() method [TestMethod]
public void UpdateUsingTwoRepositories()
{
LocationRepository locationRepository = new LocationRepository(); FlightRepository flight Repository = new FlightRepository();
Flight flight, flightFromDbj Location location;
using (TransactionScope scope = nav TransactionScope())
{
// Update flight and location
flight = flightRepository.FindBy(f => f.FlightNuraber ==
"BY001") .Single();
flight.FlightNumber = "BY001_updated”i flightRepository.Edit(flight); flightRepository.Save();
location - locationRepository.FindBy(l => l.City == "Rome").SingleQj location.City = ”Rome_updated";
 locationRepository.Edit(location)j locationRepository.Save();
// Verify the flight and location were updated in the current transaction flightFromDb = (from f in flightRepository.GetAllQ
where f.Source.City == "Rome_updated'' select f).FirstOrDefault();
Assert.IsNotNull(flightFromDb);
Assert.AreEqual(flightFromDb.FlightNumber, "BY001_updated")j // Do not commit the transaction //scope.Complete();
}
// Transaction was rollbacked, so the flight
// and location did not get updated
flightFromDb = (from f in flightRepository.GetAll()
where f.Source.City == "Rome_updated"
select f).FirstOrDefault();
 Assert.IsNull(flightFromDb);
 locationRepository.Dispose();
 flightRepository.Dispose();

}
4. Select FILE—Save All to save the changes.

Task 7: Running the Tests and Exploring the Database Created by Entity Framework
1. Select Test—Windows—Test Explorer. The Test Explorer window is displayed.
2. Click Run All, and then wait for all the tests to complete.
3. Open the SQL Server Management Studio window.The Connect to Server dialog box is displayed.
4. Select and replace the text in the Server name text box with ASQLExpress
5. Select Windows Authentication from the Authentictaion drop-down list.
6. Click the Connect button.The Microsoft SQL Server Management Studio window is displayed.
7. Expand the Databases node in the Object Explorer window, and then expand the BlueYonder.Companion.Lab02 database.
8. Expand the Tables nodes
9. Notice that the Reservations and Trips tables are created.
10. Close the Microsoft SQL Server Management Studio window.
11. Switch to Microsoft Visual Studio 2012, and close it.

No comments:

Post a Comment