def setUp(self):
        uploaduser = User.objects.create_user('temporary', '*****@*****.**', 'temporary')
        self.client.login(username='******', password='******')

        # Need a supported track in the system.
        trackname_obj = TrackName(trackname="TACOMA R/C RACEWAY")
        trackname_obj.save()
        self.trackname_obj = trackname_obj
        
        sup_trackname_obj = SupportedTrackName(trackkey=trackname_obj)
        sup_trackname_obj.save()
        
                
        # Need to fake the file upload.
        #     Make sure its is created in the right location
        #     Make sure there is a record of its upload in the logs.
        for upload in self.racelist_to_upload:
        
            filename = upload['filename']
            with open(os.path.join(settings.MEDIA_USER_UPLOAD, filename), "wb") as f:
                f.write(upload['filecontent'])
                    
            log_entry = models.UploadRecord(origfilename="NO_origional_filename",
                                            ip="1.1.1.1",
                                            user=uploaduser,
                                            filesize="56",
                                            filename=filename,
                                            uploaddate=utils.formated_local_time_for_orm(),
                                            processed=False)
            log_entry.save()
            
            response = self.client.get("/upload_start/" + str(log_entry.id) + "/")
            self.assertEqual(response.status_code, 200)
            
            response = self.client.post('/upload_start/' + str(log_entry.id) + '/', {'track_id': sup_trackname_obj.id})
            self.assertEqual(response.status_code, 200)
    def test_singlerace_upload(self):
        
        # Setup
        self.singlerace_db_objs = []

        uploaduser = User.objects.create_user('temporary', '*****@*****.**', 'temporary')
        self.singlerace_db_objs.append(uploaduser)
        self.client.login(username='******', password='******')
        
        
        self.assertEqual(1 + 1, 2)
        
        # Need to fake the file upload.
        #     Make sure its is created in the right location
        #     Make sure there is a record of its upload in the logs.
        filename = "singlerace_testfile"
        with open(os.path.join(settings.MEDIA_USER_UPLOAD, filename), "wb") as f:
            f.write(self.singlerace_testfile)
                
        log_entry = models.UploadRecord(origfilename="NO_origional_filename",
                                        ip="1.1.1.1",
                                        user=uploaduser,
                                        filesize="56",
                                        filename=filename,
                                        uploaddate=utils.formated_local_time_for_orm(),
                                        processed=False)
        log_entry.save()
        self.singlerace_db_objs.append(log_entry)
                        
        # Need a supported track in the system.
        trackname_obj = TrackName(trackname="TACOMA R/C RACEWAY")
        trackname_obj.save()
        self.singlerace_db_objs.append(trackname_obj)
        
        sup_trackname_obj = SupportedTrackName(trackkey=trackname_obj)
        sup_trackname_obj.save()
        self.singlerace_db_objs.append(sup_trackname_obj)
        
        response = self.client.get("/upload_start/2/")
        self.assertEqual(response.status_code, 404)
        
        response = self.client.get("/upload_start/" + str(log_entry.id) + "/")
        self.assertEqual(response.status_code, 200)
        
        response = self.client.post('/upload_start/' + str(log_entry.id) + '/', {'track_id': sup_trackname_obj.id})
        #print "Content:", response.content
        self.assertEqual(response.status_code, 200)
        
        #====================================================
        # Validate Racers
        #====================================================
        # The race should now be uploaded, we want to validate it is in the system.
        car2 = RacerId.objects.get(racerpreferredname="RacerFirstCar2, Jon")
        car5 = RacerId.objects.get(racerpreferredname="RacerThirdCar5, Jon")
        car1 = RacerId.objects.get(racerpreferredname="RacerFifthCar1, Jon")
        
        #====================================================
        # Validate Race Details
        #====================================================
        # Validate the race details have been uploaded.
        raceobj = SingleRaceDetails.objects.get(trackkey=trackname_obj,
                                                 racedata="MODIFIED BUGGY",
                                                 racenumber=2,
                                                 roundnumber=3,
                                                 racelength=8,
                                                 winninglapcount=28,
                                                 mainevent=1)
        
        #====================================================
        # Validate Race Laps
        #====================================================        
        # Validate the corner cases for the lap times and positions
        LapTimes.objects.get(raceid=raceobj,
                             racerid=car1,                             
                             racelap=0,
                             raceposition=5,
                             racelaptime='35.95')
        
        LapTimes.objects.get(raceid=raceobj,
                             racerid=car2,
                             racelap=0,
                             raceposition=1,
                             racelaptime='26.24')
        
        LapTimes.objects.get(raceid=raceobj,
                             racerid=car2,
                             racelap=27,
                             raceposition=1,
                             racelaptime='20.71')
        
        LapTimes.objects.get(raceid=raceobj,
                             racerid=car5,
                             racelap=0,
                             raceposition=3,
                             racelaptime='29.63')
        
        LapTimes.objects.get(raceid=raceobj,
                             racerid=car5,
                             racelap=25,
                             raceposition=3,
                             racelaptime='19.69')
        
        LapTimes.objects.get(raceid=raceobj,
                             racerid=car5,
                             racelap=26)
        
        
        #====================================================
        # Validate Race Results
        #====================================================
        #________________________Driver___Car#____Laps____RaceTime____Fast Lap___Behind_
        #RacerFirstCar2, Jon            #2         28         8:18.588         17.042                  
        #RacerSecondCar4, Jon            #4         27         8:08.928         17.116                  
        #RacerThirdCar5, Jon            #5         26         8:00.995         17.274                  
        #RacerFourthCar3, Jon            #3         25         8:02.680         17.714                  
        #RacerFifthCar1, Jon            #1          1           35.952         35.952 
        SingleRaceResults.objects.get(racerid=car1,
                                      raceid=raceobj,
                                      carnum=1,
                                      lapcount=1)
        
        results = SingleRaceResults.objects.get(racerid=car2,
                                      raceid=raceobj,
                                      carnum=2,
                                      lapcount=28)
        
        # Test to verify the all of the time data is uploaded.
        self.assertEqual(results.racetime, datetime.time(minute=8, second=18, microsecond=588000))
        self.assertEqual(results.fastlap, decimal.Decimal("17.042"))
        
        SingleRaceResults.objects.get(racerid=car5,
                                      raceid=raceobj,
                                      carnum=5,
                                      lapcount=26)
        
        # Quick end to end test, verify we now have a results page for the new race.
        #response = self.client.get("/displayresults/singleracedetailed/1/")
        #self.assertEqual(response.status_code, 200)
        
        # Teardown
        for obj in self.singlerace_db_objs:
            obj.delete()