Пример #1
0
def test_add_remove(sqldb):
    db = rules_db.RulesDb(sqldb, MOCK_UDN, MOCK_NAME)

    # Rules
    assert len(db._rules) == 0
    rule = db.add_rule(
        rules_db.RulesRow(
            RuleID=501,
            Name="Long Press Rule",
            Type=MOCK_RULE_TYPE,
            State=1,
        ))
    assert len(db._rules) == 1
    db.remove_rule(rule)
    assert len(db._rules) == 0

    # RuleDevices
    assert len(db._rule_devices) == 0
    device = db.add_rule_devices(
        rules_db.RuleDevicesRow(RuleDevicePK=1, RuleID=501, DeviceID=MOCK_UDN))
    assert len(db._rule_devices) == 1
    db.remove_rule_devices(device)
    assert len(db._rule_devices) == 0

    # TargetDevices
    assert len(db._target_devices) == 0
    target = db.add_target_devices(
        rules_db.TargetDevicesRow(RuleID=501, DeviceID=MOCK_TARGET_UDN))
    assert len(db._target_devices) == 1
    db.remove_target_devices(target)
    assert len(db._target_devices) == 0
Пример #2
0
def test_clear_all(sqldb):
    db = rules_db.RulesDb(sqldb, MOCK_UDN, MOCK_NAME)
    rule = db.add_rule(
        rules_db.RulesRow(
            RuleID=501,
            Name="Long Press Rule",
            Type=MOCK_RULE_TYPE,
            State=1,
        ))
    assert len(db._rules) == 1

    # RuleDevices
    assert len(db._rule_devices) == 0
    device = db.add_rule_devices(
        rules_db.RuleDevicesRow(RuleDevicePK=1, RuleID=501, DeviceID=MOCK_UDN))
    assert len(db._rule_devices) == 1

    # TargetDevices
    assert len(db._target_devices) == 0
    target = db.add_target_devices(
        rules_db.TargetDevicesRow(RuleID=501, DeviceID=MOCK_TARGET_UDN))
    assert len(db._target_devices) == 1

    db.clear_all()
    assert len(db._rules) == 0
    assert len(db._rule_devices) == 0
    assert len(db._target_devices) == 0
Пример #3
0
def test_update_if_modified_new_entry(sqldb):
    rule = rules_db.RulesRow(RuleID=501)
    db = rules_db.RulesDb(sqldb, MOCK_UDN, MOCK_NAME)
    assert db.update_if_modified() is False

    # Adding a new entry in the db should cause update_if_modified() to be True
    db.add_target_device_to_rule(rule, MOCK_TARGET_UDN)
    assert db.update_if_modified() is True
Пример #4
0
def test_get_target_devices_for_rule(sqldb):
    cursor = sqldb.cursor()
    rule = rules_db.RulesRow(RuleID=501)
    rules_db.TargetDevicesRow(
        RuleID=rule.RuleID,
        DeviceID=MOCK_TARGET_UDN,
    ).update_db(cursor)
    db = rules_db.RulesDb(sqldb, MOCK_UDN, MOCK_NAME)

    assert db.get_target_devices_for_rule(rule) == frozenset([MOCK_TARGET_UDN])
Пример #5
0
def test_add_remove_target_device_to_rule(sqldb):
    rule = rules_db.RulesRow(RuleID=501)
    db = rules_db.RulesDb(sqldb, MOCK_UDN, MOCK_NAME)
    assert MOCK_TARGET_UDN not in db.get_target_devices_for_rule(rule)

    db.add_target_device_to_rule(rule, MOCK_TARGET_UDN)
    assert MOCK_TARGET_UDN in db.get_target_devices_for_rule(rule)

    db.remove_target_device_from_rule(rule, MOCK_TARGET_UDN)
    assert MOCK_TARGET_UDN not in db.get_target_devices_for_rule(rule)
Пример #6
0
def test_ensure_long_press_rule_exists(test_input, expected):
    with tempfile.NamedTemporaryFile(prefix="wemorules",
                                     suffix=".db") as temp_file:
        rules_db._create_empty_db(temp_file.name)
        try:
            conn = sqlite3.connect(temp_file.name)
            conn.row_factory = sqlite3.Row
            cursor = conn.cursor()
            for row in test_input:
                row.update_db(cursor)

            db = rules_db.RulesDb(conn, MOCK_UDN, MOCK_NAME)
            long_press.ensure_long_press_rule_exists(db, MOCK_NAME, MOCK_UDN)

            assert (db.rules_for_device(
                rule_type=long_press.RULE_TYPE_LONG_PRESS) == expected)
        finally:
            conn.close()
Пример #7
0
    def rules_db_from_device(self, device):
        with tempfile.NamedTemporaryFile(
            prefix="wemorules", suffix=".db"
        ) as temp_file:
            rules_db._create_empty_db(temp_file.name)
            try:
                conn = sqlite3.connect(temp_file.name)
                conn.row_factory = sqlite3.Row
                rdb = rules_db.RulesDb(conn, device.udn, device.name)

                @contextlib.contextmanager
                def yield_rdb(*_):
                    yield rdb

                with patch(TestLongPress.method, side_effect=yield_rdb):
                    yield rdb
            finally:
                conn.close()
Пример #8
0
def test_update_if_modified_field_changed(sqldb):
    cursor = sqldb.cursor()
    rules_db.RulesRow(
        RuleID=501,
        Name="Long Press Rule",
        Type=MOCK_RULE_TYPE,
        State=1,
    ).update_db(cursor)

    rules_db.RuleDevicesRow(RuleDevicePK=1, RuleID=501,
                            DeviceID=MOCK_UDN).update_db(cursor)

    db = rules_db.RulesDb(sqldb, MOCK_UDN, MOCK_NAME)
    rule, device = db.rules_for_device()[0]
    assert db.update_if_modified() is False

    # Modifying an entry in the db should cause update_if_modified() to be True
    rule.State = 0
    assert db.update_if_modified() is True