Пример #1
0
def test_configure_post_updates_and_creates_buttons(client, app):
    submitted_data = {'zone': zone_4}
    expected_playlists = generate_expected_actions("added_" + zone_4)
    with app.app_context():
        button_accessor = ButtonAccessor()
        orig_buttons = button_accessor.get_buttons_for_zone(zone_4)
        submitted_data["button_id_{}".format(orig_buttons[0].id)] = orig_buttons[0].action + "_v2"
        submitted_data["button_id_{}".format(orig_buttons[1].id)] = orig_buttons[1].action + "_v2"
        submitted_data["button_id_{}".format(orig_buttons[3].id)] = orig_buttons[3].action + "_v2"
        modified_ids = [orig_buttons[0].id, orig_buttons[1].id, orig_buttons[3].id]

        for i, expected_playlist in enumerate(expected_playlists[4:]):
            submitted_data["new_{}".format(i)] = expected_playlist

        response = client.post(
            '/configuration',
            data=submitted_data
        )

        buttons = button_accessor.get_buttons_for_zone(zone_4)
        buttons_by_id = {button.id: button for button in buttons}
        assert len(buttons) == 9
        for i, orig_button in enumerate(orig_buttons):
            button = buttons_by_id[orig_button.id]
            if orig_button.id in modified_ids:
                assert_button_properties(button, orig_button.action + "_v2", orig_button.zone, orig_button.position)
            else:
                assert_button_properties(button, orig_button.action, orig_button.zone, orig_button.position)
        assert b"updated action for 3 buttons" in response.data
        assert b"created 5 new buttons" in response.data
Пример #2
0
def test_get_buttons_for_zone(app):
    with app.app_context():
        button_accessor = ButtonAccessor()
        buttons = button_accessor.get_buttons_for_zone(zone_4)
        assert len(buttons) == 4
        assert_buttons_for_zone(buttons, generate_expected_actions(zone_4),
                                zone_4)
Пример #3
0
def test_play_playlist_in_zone(client, app):
    with app.app_context():
        response = client.get('/play?zone={}&button_position={}'.format(zone_9, 3))
        button_accessor = ButtonAccessor()
        button = button_accessor.get_button_by_zone_and_position(zone_9, 3)
        soco_queue = get_soco_queue_fake()
        assert button.action in soco_queue[0]
        assert len(soco_queue) == 1
        assert b'Playing "three_zone_9" from button at position 3' in response.data
Пример #4
0
def test_new(app):
    with app.app_context():
        button_accessor = ButtonAccessor()
        new_action = 'something_new'
        new_position = 1
        button = button_accessor.new(new_zone, new_position, new_action)
        assert_button_properties(button, new_action, new_zone, new_position)

        button = button_accessor.get_button_by_zone_and_position(
            new_zone, new_position)
        assert_button_properties(button, new_action, new_zone, new_position)
Пример #5
0
def test_get_all_buttons_by_zone(app):
    with app.app_context():
        button_accessor = ButtonAccessor()
        buttons = button_accessor.get_all_buttons_by_zone()
        assert len(buttons[zone_4]) == 4
        assert len(buttons[zone_9]) == 9
        assert set(buttons.keys()) == {zone_4, zone_9}

        assert_buttons_for_zone(buttons[zone_4],
                                generate_expected_actions(zone_4), zone_4)
        assert_buttons_for_zone(buttons[zone_9],
                                generate_expected_actions(zone_9), zone_9)
Пример #6
0
def test_update_action_does_not_update_when_action_same(app):
    with app.app_context():
        button_accessor = ButtonAccessor()

        button = button_accessor.get_button_by_zone_and_position(zone_4, 3)
        original_action = 'three_zone_4'
        assert_button_properties(button, original_action, zone_4, 3)

        is_updated = button_accessor.update_action(button, original_action)
        assert not is_updated
        button = button_accessor.get_button_by_zone_and_position(zone_4, 3)
        assert_button_properties(button, original_action, zone_4, 3)
Пример #7
0
def test_update_action_updates_when_action_different(app):
    with app.app_context():
        button_accessor = ButtonAccessor()

        button = button_accessor.get_button_by_zone_and_position(zone_4, 3)
        original_action = 'three_zone_4'
        assert_button_properties(button, original_action, zone_4, 3)

        new_action = 'something_new'
        is_updated = button_accessor.update_action(button, new_action)
        assert is_updated
        button = button_accessor.get_button_by_zone_and_position(zone_4, 3)
        assert_button_properties(button, new_action, zone_4, 3)
Пример #8
0
def play():
    button_accessor = ButtonAccessor()
    zone = request.args.get('zone', type=str)
    button_position = request.args.get('button_position', type=int)

    button = button_accessor.get_button_by_zone_and_position(zone, button_position)
    if not button:
        abort(404, "button at position {} for zone {} not configured.".format(button_position, zone))
    sonoser = SonosAccessor()
    try:
        sonoser.play_playlist_at_zone(button.action, button.zone)
    except SonoserException as e:
        abort(404, "Could not play '{}' from button at position {} for zone {}. {}".format(
            button.action, button_position, zone, str(e)))
    return render_template('button/play.html', button=button)
Пример #9
0
def test_configure_get(client, app):
    response = client.get('/configuration')

    set_playlists = "playlists = " + json.dumps(all_playlist_titles)
    assert set_playlists.encode() in response.data
    set_zones = "zones = " + json.dumps(all_zones_names)
    assert set_zones.encode() in response.data

    with app.app_context():
        button_accessor = ButtonAccessor()
        # noinspection PyPep8Naming
        set_buttonsByZone = "buttonsByZone = " + json.dumps(button_accessor.get_all_buttons_by_zone())
        assert set_buttonsByZone.encode() in response.data
        # noinspection PyPep8Naming
        set_lastModifiedZone = "lastModifiedZone = " + json.dumps(button_accessor.last_modified_button().zone)
        assert set_lastModifiedZone.encode() in response.data
Пример #10
0
def test_play_playlist_in_non_existent_zone(client, app):
    with app.app_context():
        button_accessor = ButtonAccessor()
        button = button_accessor.get_button_by_zone_and_position(zone_9, 3)

        button_accessor.db.execute(
            "UPDATE button "
            "SET zone = ?, last_modified=datetime('now') "
            "WHERE id = ?", ("does_not_exist", button.id))
        button_accessor.db.commit()

        response = client.get('/play?zone={}&button_position={}'.format("does_not_exist", 3))

        assert len(get_soco_queue_fake()) == 0
        assert b"player name 'does_not_exist' not found" in response.data
        assert b"Could not play 'three_zone_9' from button at position 3 for zone does_not_exist" in response.data
Пример #11
0
def test_configure_post_new_buttons(client, app):
    expected_playlists = generate_expected_actions(new_zone)
    submitted_data = {'zone': new_zone}
    for i, expected_playlist in enumerate(expected_playlists):
        submitted_data["new_{}".format(i+1)] = expected_playlist
    with app.app_context():
        response = client.post(
            '/configuration',
            data=submitted_data
        )
        button_accessor = ButtonAccessor()
        buttons = button_accessor.get_buttons_for_zone(new_zone)
        assert len(buttons) == 9
        assert_buttons_for_zone(buttons,
                                generate_expected_actions(new_zone),
                                new_zone)
        assert b"updated action for 0 buttons" in response.data
        assert b"created 9 new buttons" in response.data
Пример #12
0
def test_configure_post_update_all_buttons(client, app):
    submitted_data = {'zone': zone_9}
    with app.app_context():
        button_accessor = ButtonAccessor()
        buttons = button_accessor.get_buttons_for_zone(zone_9)
        for button in buttons:
            submitted_data["button_id_{}".format(button.id)] = button.action + "_v2"
        response = client.post(
            '/configuration',
            data=submitted_data
        )

        buttons = button_accessor.get_buttons_for_zone(zone_9)
        assert len(buttons) == 9
        assert_buttons_for_zone(buttons,
                                generate_expected_actions(zone_9 + "_v2"),
                                zone_9)
        assert b"updated action for 9 buttons" in response.data
        assert b"created 0 new buttons" in response.data
Пример #13
0
def test_configure_post_update_some_buttons(client, app):
    submitted_data = {'zone': zone_9}
    with app.app_context():
        button_accessor = ButtonAccessor()
        orig_buttons = button_accessor.get_buttons_for_zone(zone_9)
        submitted_data["button_id_{}".format(orig_buttons[0].id)] = orig_buttons[0].action + "_v2"
        submitted_data["button_id_{}".format(orig_buttons[1].id)] = orig_buttons[1].action + "_v2"
        submitted_data["button_id_{}".format(orig_buttons[7].id)] = orig_buttons[7].action + "_v2"
        response = client.post(
            '/configuration',
            data=submitted_data
        )

        buttons = button_accessor.get_buttons_for_zone(zone_9)
        assert len(buttons) == 9
        for i, (button, orig_button) in enumerate(zip(buttons, orig_buttons)):
            if i in [0, 1, 7]:
                assert_button_properties(button, orig_button.action + "_v2", orig_button.zone, orig_button.position)
            else:
                assert_button_properties(button, orig_button.action, orig_button.zone, orig_button.position)
        assert b"updated action for 3 buttons" in response.data
        assert b"created 0 new buttons" in response.data
Пример #14
0
def test_get_button_by_zone_and_position(app):
    with app.app_context():
        button_accessor = ButtonAccessor()
        button = button_accessor.get_button_by_zone_and_position(zone_4, 2)
        assert_button_properties(button, 'two_zone_4', zone_4, 2)

        button_accessor = ButtonAccessor()
        button = button_accessor.get_button_by_zone_and_position(zone_9, 6)
        assert_button_properties(button, 'six_zone_9', zone_9, 6)
Пример #15
0
def configure():
    button_accessor = ButtonAccessor()
    if request.method == 'POST':
        updated = 0
        zone = request.form['zone']
        buttons = button_accessor.get_buttons_for_zone(zone)
        for button in buttons:
            button_id_form_name = "button_id_{}".format(button.id)
            if button_id_form_name in request.form:
                action = request.form[button_id_form_name]
                updated += button_accessor.update_action(button, action)
            else:
                flash("missing action for button id {}".format(button.id))

        new_buttons = []
        for key in request.form.keys():
            if key.startswith("new_"):
                button_index = key[len("new_"):]
                if not button_index.isdigit():
                    flash("button index must be a number {}".format(key))
                else:
                    new_buttons.append((int(button_index), request.form[key]))

        new_buttons.sort()
        for new_button_num, action in new_buttons:
            button_accessor.new(zone, new_button_num, action)
        flash("updated action for {} buttons. created {} new buttons".format(updated, len(new_buttons)))

    buttons_by_zone = button_accessor.get_all_buttons_by_zone()
    #
    sonoser = SonosAccessor()
    playlist_titles = [p.title for p in sonoser.playlists]
    playlist_titles.sort()
    zone_names = [z.player_name for z in sonoser.zones]
    zone_names.sort()

    last_modified_button = button_accessor.last_modified_button()
    last_modified_zone = last_modified_button.zone if last_modified_button else zone_names[0]

    return render_template(
        'button/configure.html',
        buttons_by_zone=buttons_by_zone,
        playlists=playlist_titles,
        zones=zone_names,
        selected_zone=last_modified_zone
    )
Пример #16
0
def test_last_modified_button(app):
    with app.app_context():
        button_accessor = ButtonAccessor()
        button = button_accessor.last_modified_button()
        assert_button_properties(button, 'four_zone_4', zone_4, 4)