def test_auto_primary_key(sqldb): """Ensure the primary key for a row is updated when it is added to the db.""" cursor = sqldb.cursor() row1 = rules_db.TargetDevicesRow(RuleID=12) row2 = rules_db.TargetDevicesRow(RuleID=34) row1.update_db(cursor) row2.update_db(cursor) assert row1.TargetDevicesPK + 1 == row2.TargetDevicesPK
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
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
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])