def test_case_1(self, prototypical_showtimes):
        theatre = Theatre(name=prototypical_showtimes[0]['name'], showtimes=prototypical_showtimes[0]['showtimes'])
        theatre.calculate_double_dips(max_waiting_time=0, max_overlap_time=0)

        expected_dips = [DoubleDip(theatre.showtimes[0]), DoubleDip(theatre.showtimes[1])]

        assert theatre.double_dips == expected_dips
    def test_to_json(self):
        theatre_json = {
            "name": "Test Theatre 5",
            "description": "Test triplet when there's an unacceptable distance",
            "showtimes": [
                {
                    "name": "a",
                    "runtime": 60,
                    "times": ["16:00"]
                },
                {
                    "name": "b",
                    "runtime": 60,
                    "times": ["17:05"]
                },
                {
                    "name": "c",
                    "runtime": 60,
                    "times": ["19:05"]
                }
            ]
        }

        expected_output = {
            'name': theatre_json['name'],
            'info': theatre_json.get('info'),
            'doubleDips': [
                [
                    {
                        'movie': theatre_json['showtimes'][0]['name'],
                        'length': theatre_json['showtimes'][0]['runtime'],
                        'startTime': theatre_json['showtimes'][0]['times'][0],
                        'endTime': "17:00"
                    },
                    {
                        'movie': theatre_json['showtimes'][1]['name'],
                        'length': theatre_json['showtimes'][1]['runtime'],
                        'startTime': theatre_json['showtimes'][1]['times'][0],
                        'endTime': "18:05"
                    }
                ],
                [
                    {
                        'movie': theatre_json['showtimes'][2]['name'],
                        'length': theatre_json['showtimes'][2]['runtime'],
                        'startTime': theatre_json['showtimes'][2]['times'][0],
                        'endTime': "20:05"
                    }
                ]
            ]
        }

        theatre = Theatre(name=theatre_json.get('name'),
                          showtimes=theatre_json.get('showtimes'),
                          info=theatre_json.get('info'))

        assert theatre.to_json() == expected_output
def cinestar_berlin(self):
    cinestar_berlin = json.load(open('dairy_queen/tests/cinestar_berlin.json'))
    theatre = Theatre(name=cinestar_berlin['name'], showtimes=cinestar_berlin['showtimes'])

    # test the situation where all films should be singleton dips:
    # setting max_waiting_time=0 & max_overlap_time=-5 is an impossible condition
    theatre.calculate_double_dips(max_waiting_time=0, max_overlap_time=-5)
    expected_dips = [
        DoubleDip(movie) for movie in theatre.showtimes
        ]
    assert theatre.double_dips == expected_dips

    # test the situation where only films that are perfectly back-to-back are
    # double dips
    theatre.calculate_double_dips(max_waiting_time=0, max_overlap_time=0)
    non_trivial_dips = []
    for double_dip in theatre.double_dips:
        if len(double_dip) > 1:
            non_trivial_dips.append(double_dip)
    expected_dips = []

    assert non_trivial_dips == expected_dips
Example #4
0
def get_doubledips():
    location = request.args.get('location')
    days_from_now = request.args.get('days_from_now')
    max_waiting_time = request.args.get('max_wait_mins')
    max_overlap_time = request.args.get('max_overlap_mins')

    status = None
    msg = None
    mimetype = 'application/json'

    if location is None or not isinstance(location, str):
        status = 400
        msg = "'location' is mandatory and must be a string."
        resp = Response(dumps({'msg': msg}), status=status, mimetype=mimetype)
        resp.headers['Access-Control-Allow-Origin'] = '*'
        return resp

    if days_from_now is not None:
        try:
            days_from_now = int(days_from_now)
        except Exception:
            status = 400
            msg = "'days_from_now' must be a base-10 integer."
            resp = Response(dumps({'msg': msg}), status=status, mimetype=mimetype)
            resp.headers['Access-Control-Allow-Origin'] = '*'
            return resp
    else:
        days_from_now = 0

    if max_waiting_time is not None:
        try:
            max_waiting_time = int(max_waiting_time)
        except Exception:
            status = 400
            msg = "'max_waiting_time' must be a base-10 integer"
            resp = Response(dumps({'msg': msg}), status=status, mimetype=mimetype)
            resp.headers['Access-Control-Allow-Origin'] = '*'
            return resp
    else:
        max_waiting_time = 45

    if max_overlap_time is not None:
        try:
            max_overlap_time = int(max_overlap_time)
        except Exception:
            status = 400
            msg = "'max_overlap_time' must be a base-10 integer"
            resp = Response(dumps({'msg': msg}), status=status, mimetype=mimetype)
            resp.headers['Access-Control-Allow-Origin'] = '*'
            return resp
    else:
        max_overlap_time = 5

    gms_url = 'http://google-movies-scraper.herokuapp.com/v2/movies'
    gms_params = {
        'near': location,
        'date': days_from_now,
        'militaryTime': True
    }

    # should definitely build some logic to handle response code of r...
    r = requests.get(gms_url, params=gms_params)
    theatres_json = r.json()
    output = []
    for theatre in theatres_json:
        try:
            tmp_theatre = Theatre(name=theatre.get('name'),
                                  showtimes=theatre.get('showtimes'),
                                  info=theatre.get('info'))

            tmp_json = tmp_theatre.to_json(max_waiting_time=max_waiting_time,
                                           max_overlap_time=max_overlap_time)

            output.append(tmp_json)
        except TypeError as e:
            warn(str(e))

    status = 200
    resp = Response(dumps(output), status=status, mimetype=mimetype)
    resp.headers['Access-Control-Allow-Origin'] = '*'
    return(resp)
def create_theatre_and_calc_double_dips(theatre_json, max_waiting_mins=20, max_overlap_mins=6):
    theatre = Theatre(name=theatre_json['name'], showtimes=theatre_json['showtimes'])
    theatre.calculate_double_dips(max_waiting_mins, max_overlap_mins)
    theatre.showtimes.sort(key=attrgetter('name'))
    return theatre