def test_get_ferries(self):
        ''' get all ferries associated with a player's game '''
        self.simulate_login()
        
        # Bob and George are red herrings for testing
        bob = database.User(name = 'ferrycapn', email = '*****@*****.**')
        george = database.User(name = 'bestsysmgr', email = '*****@*****.**')
        ferry_class_props = { 'name': 'Jumbo Mark II' }
        ferry_class = database.Ferry_Class(name = ferry_class_props['name'])
        gameA = database.Game(player = self.user)
        gameB = database.Game(player = george)

        ferryA = database.Ferry(name = 'Puget Rider', ferry_class = ferry_class, game = gameA)
        ferryB = database.Ferry(ferry_class = ferry_class, game = gameB)

        session.add_all([bob, george, ferry_class, gameA, gameB, ferryA, ferryB])
        session.commit()
        
        response = self.client.get('/api/games/' + str(gameA.id) + '/ferries',
            headers = [('Accept', 'application/json')]
        )
        
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.mimetype, 'application/json')
        
        data = json.loads(response.data.decode('ascii'))
        self.assertEqual(len(data), 1)
        
        ferryA = data[0]
        self.assertEqual(ferryA['ferry_class']['name'], ferry_class_props['name'])
        self.assertEqual(ferryA['name'], 'Puget Rider')
 def test_get_routes(self):
     ''' get a new game for the current user '''
     self.simulate_login()
     
     game = database.Game(player = self.user)
     
     seattle = database.Terminal(name = 'Seattle', lat = 47.6025001, lon = -122.33857590000002)
     bainbridge_island = database.Terminal(name = 'Bainbridge Island', lat = 47.623089, lon = -122.511171)
     
     route = database.Route(game = game, first_terminal = seattle, second_terminal = bainbridge_island)
     
     session.add_all([game, seattle, bainbridge_island])
     session.commit()
     
     response = self.client.get('/api/games/' + str(game.id) + '/routes',
         headers = [('Accept', 'application/json')]
     )
     
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.mimetype, 'application/json')
     
     data = json.loads(response.data.decode('ascii'))
     self.assertEqual(len(data), 1)
     
     route = data[0]
     print(route)
     self.assertEqual(route['game']['player']['id'], self.user.id)
     self.assertAlmostEqual(route['route_distance'], 7.47, 2)
 def test_get_new_game(self):
     ''' get a new game for the current user '''
     self.simulate_login()
     
     game = database.Game(player = self.user)
     
     session.add_all([game])
     session.commit()
     
     response = self.client.get('/api/games',
         headers = [('Accept', 'application/json')]
     )
     
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.mimetype, 'application/json')
     
     data = json.loads(response.data.decode('ascii'))
     self.assertEqual(len(data), 1)
     
     game = data[0]
     self.assertEqual(game['player']['id'], self.user.id)
     self.assertLessEqual(game['created_date'], unix_timestamp(datetime.now()))
     self.assertEqual(game['cash_available'], 0)
     self.assertEqual(game['current_week'], 1)
     self.assertEqual(game['current_year'], 2000)
 def test_get_empty_routes(self):
     ''' try to get routes for a game where none exist '''
     
     pw = 'notsecret'
     bob = database.User(name = 'ferrycapn', email = '*****@*****.**', password = generate_password_hash(pw))
     
     game = database.Game(player = bob)
     
     session.add_all([bob, game])
     session.commit()
     
     token = self.get_jwt(bob.name, pw)
     
     response = self.client.get('/api/games/' + str(game.id) + '/routes',
         headers = [
             ('Accept', 'application/json'),
             ('Authorization', 'JWT ' + token)
         ]
     )
     
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.mimetype, 'application/json')
     
     data = json.loads(response.data.decode('ascii'))
     self.assertEqual(len(data), 0)
 def test_get_terminals(self):
     ''' test getting a couple of terminals '''
     
     terminalA = database.Terminal(name = 'Timbuktu', lat = 111.1111, lon = -122.2222)
     terminalB = database.Terminal(name = 'Never Never Land', lat = 33.3333, lon = -160.9999)
     
     session.add_all([terminalA, terminalB])
     session.commit()
     
     response = self.client.get('/api/terminals',
         headers = [('Accept', 'application/json')]
     )
     
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.mimetype, 'application/json')
     
     data = json.loads(response.data.decode('ascii'))
     self.assertEqual(len(data), 2)
     
     # we have to switch the indexes because the API returns results sorted by name
     terminalA = data[1]
     terminalB = data[0]
     self.assertEqual(terminalA['name'], 'Timbuktu')
     self.assertEqual(terminalA['lat'], 111.1111)
     self.assertEqual(terminalA['lon'], -122.2222)
     self.assertEqual(terminalB['name'], 'Never Never Land')
     self.assertEqual(terminalB['lat'], 33.3333)
     self.assertEqual(terminalB['lon'], -160.9999)
 def test_get_routes(self):
     ''' get all the routes for a player's game '''
     
     pw = 'notsecret'
     bob = database.User(name = 'ferrycapn', email = '*****@*****.**', password = generate_password_hash(pw))
     
     game = database.Game(player = bob)
     
     seattle = database.Terminal(name = 'Seattle', lat = 47.6025001, lon = -122.33857590000002)
     bainbridge_island = database.Terminal(name = 'Bainbridge Island', lat = 47.623089, lon = -122.511171)
     
     route = database.Route(game = game, first_terminal = seattle, second_terminal = bainbridge_island)
     
     session.add_all([bob, game, seattle, bainbridge_island])
     session.commit()
     
     token = self.get_jwt(bob.name, pw)
     
     response = self.client.get('/api/games/' + str(game.id) + '/routes',
         headers = [
             ('Accept', 'application/json'),
             ('Authorization', 'JWT ' + token)
         ]
     )
     
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.mimetype, 'application/json')
     
     data = json.loads(response.data.decode('ascii'))
     self.assertEqual(len(data), 1)
     
     route = data[0]
     print(route)
     # self.assertEqual(route['game']['player']['id'], bob.id)
     self.assertAlmostEqual(route['route_distance'], 7.47, 2)
 def test_ferry_post(self):
     ''' test the purchasing of a ferry '''
     
     pw = 'notsecret'
     bob = database.User(name = 'ferrycapn', email = '*****@*****.**', password = generate_password_hash(pw))
     
     ferry_class_props = { 
         'name': 'Jumbo Mark II',
         'passengers': 2500,
         'cars': 202,
         'trucks': 60,
         'speed': 21,
         'burn_rate': 350,
         'turnover_time': 0.2,
         'cost': 50000,
         'residual_value': 4000,
         'usable_life': 60
     }
     
     ferry_class_A = database.Ferry_Class(
         name = ferry_class_props['name'],
         passengers = ferry_class_props['passengers'],
         cars = ferry_class_props['cars'],
         trucks = ferry_class_props['trucks'],
         speed = ferry_class_props['speed'],
         burn_rate = ferry_class_props['burn_rate'],
         turnover_time = ferry_class_props['turnover_time'],
         cost = ferry_class_props['cost'],
         residual_value = ferry_class_props['residual_value'],
         usable_life = ferry_class_props['usable_life']
     )
     
     game = database.Game(player = bob)
     
     session.add_all([bob, game, ferry_class_A])
     session.commit()
     
     token = self.get_jwt(bob.name, pw)
     
     data = {
         "classId": ferry_class_A.id,
         "name": "M/V Wenatchee"
     }
     
     response = self.client.post('/api/games/' + str(game.id) + '/ferries',
         data = json.dumps(data),
         # TODO all PUT and POST requests should have content_type
         content_type = 'application/json',
         headers = [
             ('Accept', 'application/json'),
             ('Authorization', 'JWT ' + token)
         ],
     )
     
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.mimetype, 'application/json')
     
     data = json.loads(response.data.decode('ascii'))
     self.assertEqual(len(data), 8)
 def test_get_empty_routes(self):
     ''' try to get routes for a game where none exist '''
     self.simulate_login()
     
     game = database.Game(player = self.user)
     
     session.add_all([game])
     session.commit()
     
     response = self.client.get('/api/games/' + str(game.id) + '/routes',
         headers = [('Accept', 'application/json')]
     )
     
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.mimetype, 'application/json')
     
     data = json.loads(response.data.decode('ascii'))
     self.assertEqual(len(data), 0)
 def test_delete_game(self):
     ''' delete a game for a user '''
     pw = 'notsecret'
     bob = database.User(name = 'ferrycapn', email = '*****@*****.**', password = generate_password_hash(pw))
     
     game = database.Game(player = bob)
     
     session.add_all([bob, game])
     session.commit()
     
     token = self.get_jwt(bob.name, pw)
     
     response = self.client.delete('/api/games/' + str(game.id),
          headers = [
             ('Accept', 'application/json'),
             ('Authorization', 'JWT ' + token)
         ]
     )
     
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.mimetype, 'application/json')
     
     data = json.loads(response.data.decode('ascii'))
     self.assertEqual(len(data), 8)
     
     self.assertEqual(data['player']['id'], bob.id)
     self.assertLessEqual(data['created_date'], unix_timestamp(datetime.now()))
     self.assertEqual(data['cash_available'], 1000000)
     self.assertEqual(data['current_week'], 1)
     self.assertEqual(data['current_year'], 2000)
     
     # now, make sure that there are precisely zero games
     response = self.client.get('/api/games',
          headers = [
             ('Accept', 'application/json'),
             ('Authorization', 'JWT ' + token)
         ]
     )
     
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.mimetype, 'application/json')
     
     data = json.loads(response.data.decode('ascii'))
     self.assertEqual(len(data), 0)
    def test_get_ferries(self):
        ''' get all ferries associated with a player's game '''
        
        # George is a red herring for testing
        pw = 'notsecret'
        bob = database.User(name = 'ferrycapn', email = '*****@*****.**', password = generate_password_hash(pw))
        george = database.User(name = 'bestsysmgr', email = '*****@*****.**', password = generate_password_hash(pw))
        ferry_class_props = { 'name': 'Jumbo Mark II', 'usable_life': 60, 'residual_value': 1000000, 'cost': 1000000 }
        ferry_class = database.Ferry_Class(
            name = ferry_class_props['name'], usable_life = ferry_class_props['usable_life'], 
            residual_value = ferry_class_props['residual_value'], cost = ferry_class_props['cost']
        )
        gameA = database.Game(player = bob)
        gameB = database.Game(player = george)

        ferryA = database.Ferry(name = 'Puget Rider', ferry_class = ferry_class, game = gameA, launched = 2000)
        ferryB = database.Ferry(ferry_class = ferry_class, game = gameB, launched = 2000)

        session.add_all([bob, george, ferry_class, gameA, gameB, ferryA, ferryB])
        session.commit()
        
        token = self.get_jwt(bob.name, pw)
        
        response = self.client.get('/api/games/' + str(gameA.id) + '/ferries',
            headers = [
                ('Accept', 'application/json'),
                ('Authorization', 'JWT ' + token)
            ]
        )
        
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.mimetype, 'application/json')
        
        data = json.loads(response.data.decode('ascii'))
        self.assertEqual(len(data), 1)
        
        ferryA = data[0]
        self.assertEqual(ferryA['ferry_class']['name'], ferry_class_props['name'])
        self.assertEqual(ferryA['name'], 'Puget Rider')
 def test_delete_game_failure(self):
     ''' try to delete someone else's game '''
     pw = 'notsecret'
     bob = database.User(name = 'ferrycapn', email = '*****@*****.**', password = generate_password_hash(pw))
     george = database.User(name = 'bestsysmgr', email = '*****@*****.**', password = generate_password_hash(pw))
     
     game = database.Game(player = bob)
     
     session.add_all([bob, george, game])
     session.commit()
     
     token = self.get_jwt(george.name, pw)
     
     response = self.client.delete('/api/games/' + str(game.id),
          headers = [
             ('Accept', 'application/json'),
             ('Authorization', 'JWT ' + token)
         ]
     )
     
     self.assertEqual(response.status_code, 403)
     self.assertEqual(response.mimetype, 'application/json')
     
     # and now make sure that Bob still has his game
     token = self.get_jwt(bob.name, pw)
     
     response = self.client.get('/api/games/' + str(game.id),
          headers = [
             ('Accept', 'application/json'),
             ('Authorization', 'JWT ' + token)
         ]
     )
     
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.mimetype, 'application/json')
     
     data = json.loads(response.data.decode('ascii'))
     self.assertEqual(len(data), 8)
Beispiel #12
0
 def test_get_ferry_classes(self):
     ''' get all ferry classes '''
     ferry_class_props = { 
         'name': 'Jumbo Mark II',
         'passengers': 2500,
         'cars': 202,
         'trucks': 60,
         'speed': 21,
         'burn_rate': 350
     }
     ferry_class_A = database.Ferry_Class(
         name = ferry_class_props['name'],
         passengers = ferry_class_props['passengers'],
         cars = ferry_class_props['cars'],
         trucks = ferry_class_props['trucks'],
         speed = ferry_class_props['speed'],
         burn_rate = ferry_class_props['burn_rate'],
     )
     
     # ferry_class_B is a red herring for testing
     ferry_class_B = database.Ferry_Class()
     session.add_all([ferry_class_A, ferry_class_B])
     session.commit()
     
     response = self.client.get('/api/ferry_classes',
         headers = [('Accept', 'application/json')]
     )
     
     self.assertEqual(response.status_code, 200)
     self.assertEqual(response.mimetype, 'application/json')
     
     data = json.loads(response.data.decode('ascii'))
     self.assertEqual(len(data), 2)
     
     ferry_class_A = data[0]
     for key, value in ferry_class_props.items():
         self.assertEqual(ferry_class_A[key], value)
Beispiel #13
0
    def test_game_endturn(self):
        ''' end the current turn, run the simulation cycle, and test the response '''
        self.simulate_login()
        
        # create a new game
        game = database.Game(player = self.user)
        
        # create terminals
        seattle = database.Terminal(name = 'Seattle', lat = 47.6025001, lon = -122.33857590000002, passenger_pool = 13000, car_pool = 2000, truck_pool = 300)
        bainbridge_island = database.Terminal(name = 'Bainbridge Island', lat = 47.623089, lon = -122.511171, passenger_pool = 13000, car_pool = 2000, truck_pool = 300)
        
        # create route
        route = database.Route(
            game = game, first_terminal = seattle, second_terminal = bainbridge_island,
            passenger_fare = 8, car_fare = 18, truck_fare = 50
        )
        
        # TODO ferry_class_props should be moved outside of this function as it duplicates an object for another test
        # create a ferry class
        ferry_class_props = { 
            'name': 'Jumbo Mark II',
            'passengers': 2500,
            'cars': 202,
            'trucks': 60,
            'speed': 21,
            'burn_rate': 350,
            'turnover_time': 0.2,
        }
        ferry_class_A = database.Ferry_Class(
            name = ferry_class_props['name'],
            passengers = ferry_class_props['passengers'],
            cars = ferry_class_props['cars'],
            trucks = ferry_class_props['trucks'],
            speed = ferry_class_props['speed'],
            burn_rate = ferry_class_props['burn_rate'],
            turnover_time = ferry_class_props['turnover_time'],
        )
        
        ferry = database.Ferry(game = game, ferry_class = ferry_class_A, route = route)
        
        session.add_all([game, seattle, bainbridge_island, route, ferry_class_A, ferry])
        session.commit()
        
        response = self.client.get('/api/games/' + str(game.id) + '/endturn',
            headers = [('Accept', 'application/json')]
        )
        
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.mimetype, 'application/json')
        
        data = json.loads(response.data.decode('ascii'))
        self.assertEqual(data['message'], 'success')

        response = self.client.get('/api/games/' + str(game.id),
            headers = [('Accept', 'application/json')]
        )
        
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.mimetype, 'application/json')
        
        data = json.loads(response.data.decode('ascii'))
        # self.assertEqual(len(data), 1)
        
        self.assertEqual(data['current_week'], 2)
        
        response = self.client.get('/api/games/' + str(game.id) + '/turn-results/' + str(game.current_week), 
            headers = [('Accept', 'application/json')]
        )
        
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.mimetype, 'application/json')
        
        data = json.loads(response.data.decode('ascii'))
        print(data)
        
        self.assertEqual(data['week'], 1)
        self.assertEqual(data['year'], 2000)
        self.assertEqual(data['route_results'][0]['id'], 1)
    def test_game_endturn(self):
        ''' end the current turn, run the simulation cycle, and test the response '''
        pw = 'notsecret'
        bob = database.User(name = 'ferrycapn', email = '*****@*****.**', password = generate_password_hash(pw))
        
        # create a new game
        game = database.Game(player = bob)
        
        # create terminals
        seattle = database.Terminal(name = 'Seattle', lat = 47.6025001, lon = -122.33857590000002, passenger_pool = 13000, car_pool = 2000, truck_pool = 300)
        bainbridge_island = database.Terminal(name = 'Bainbridge Island', lat = 47.623089, lon = -122.511171, passenger_pool = 13000, car_pool = 2000, truck_pool = 300)
        
        # create route
        route = database.Route(
            game = game, first_terminal = seattle, second_terminal = bainbridge_island,
            passenger_fare = 8, car_fare = 18, truck_fare = 50
        )
        
        # TODO ferry_class_props should be moved outside of this function as it duplicates an object for another test
        # create a ferry class
        ferry_class_props = { 
            'name': 'Jumbo Mark II',
            'passengers': 2500,
            'cars': 202,
            'trucks': 60,
            'speed': 21,
            'burn_rate': 350,
            'turnover_time': 0.2,
            'cost': 50000,
            'residual_value': 10000,
            'usable_life': 60
        }
        ferry_class_A = database.Ferry_Class(
            name = ferry_class_props['name'],
            passengers = ferry_class_props['passengers'],
            cars = ferry_class_props['cars'],
            trucks = ferry_class_props['trucks'],
            speed = ferry_class_props['speed'],
            burn_rate = ferry_class_props['burn_rate'],
            turnover_time = ferry_class_props['turnover_time'],
            cost = ferry_class_props['cost'],
            residual_value = ferry_class_props['residual_value'],
            usable_life = ferry_class_props['usable_life'],
        )
        
        ferry = database.Ferry(
            game = game, ferry_class = ferry_class_A, route = route, 
            name = 'M/V Wenatchee',
            launched = 2000
        )
        
        session.add_all([bob, game, seattle, bainbridge_island, route, ferry_class_A, ferry])
        session.commit()
        
        token = self.get_jwt(bob.name, pw)
        
        response = self.client.get('/api/games/' + str(game.id) + '/endturn',
            headers = [
                ('Accept', 'application/json'),
                ('Authorization', 'JWT ' + token)
            ]
        )
        
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.mimetype, 'application/json')
        
        data = json.loads(response.data.decode('ascii'))
        self.assertEqual(len(data), 8)
        
        data = json.loads(response.data.decode('ascii'))
        self.assertEqual(data['current_week'], 2)
        self.assertEqual(data['current_year'], 2000)

        response = self.client.get('/api/games/' + str(game.id),
            headers = [
                ('Accept', 'application/json'),
                ('Authorization', 'JWT ' + token)
            ]
        )
        
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.mimetype, 'application/json')
        
        data = json.loads(response.data.decode('ascii'))
        # self.assertEqual(len(data), 1)
        
        self.assertEqual(data['current_week'], 2)
        
        response = self.client.get('/api/games/' + str(game.id) + '/turn_results/' + str(game.current_year) + '/week/' + str(game.current_week - 1), 
            headers = [
                ('Accept', 'application/json'),
                ('Authorization', 'JWT ' + token)
            ]
        )
        
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.mimetype, 'application/json')
        
        data = json.loads(response.data.decode('ascii'))
        
        self.assertEqual(data['week'], 1)
        self.assertEqual(data['year'], 2000)
        self.assertEqual(data['route_results'][0]['id'], 1)
        self.assertEqual(data['route_results'][0]['ferry_results'][0]['ferry']['name'], 'M/V Wenatchee')
 def test_post_route(self):
     ''' create a new route for the current game '''
     
     # create terminals
     seattle = database.Terminal(name = 'Seattle', lat = 47.6025001, lon = -122.33857590000002, passenger_pool = 13000, car_pool = 2000, truck_pool = 300)
     bainbridge_island = database.Terminal(name = 'Bainbridge Island', lat = 47.623089, lon = -122.511171, passenger_pool = 13000, car_pool = 2000, truck_pool = 300)
     
     pw = 'notsecret'
     bob = database.User(name = 'ferrycapn', email = '*****@*****.**', password = generate_password_hash(pw))
     
     ferry_class_props = { 
         'name': 'Jumbo Mark II',
         'passengers': 2500,
         'cars': 202,
         'trucks': 60,
         'speed': 21,
         'burn_rate': 350,
         'turnover_time': 0.2,
         'cost': 50000000
     }
     
     ferry_class_A = database.Ferry_Class(
         name = ferry_class_props['name'],
         passengers = ferry_class_props['passengers'],
         cars = ferry_class_props['cars'],
         trucks = ferry_class_props['trucks'],
         speed = ferry_class_props['speed'],
         burn_rate = ferry_class_props['burn_rate'],
         turnover_time = ferry_class_props['turnover_time'],
         cost = ferry_class_props['cost'],
     )
     
     game = database.Game(player = bob)
     
     ferry = database.Ferry(
         ferry_class = ferry_class_A,
         game = game,
         name = 'M/V Minnow',
         launched = 2000,
     )
     
     session.add_all([seattle, bainbridge_island, bob, game, ferry_class_A, ferry])
     session.commit()
     
     data = {
         "terminal1Id": seattle.id,
         "terminal2Id": bainbridge_island.id,
         "passenger_fare": 8.50,
         "car_fare": 18,
         "truck_fare": 48,
         "ferries": [
             ferry.id
         ]
     }
     
     token = self.get_jwt(bob.name, pw)
     
     response = self.client.post('/api/games/' + str(game.id) + '/routes',
     data = json.dumps(data),
         content_type = 'application/json',
         headers = [
             ('Accept', 'application/json'),
             ('Authorization', 'JWT ' + token)
         ]
     )
     
     self.assertEqual(response.status_code, 201)
     self.assertEqual(response.mimetype, 'application/json')
     
     data = json.loads(response.data.decode('ascii'))
     self.assertEqual(len(data), 8)