Пример #1
0
def test_update_user_notifications(db, user, service_factory):
    service1 = service_factory()
    service2 = service_factory()
    user.subscribe(service1)
    user.subscribe(service2)
    db.commit()
    notification1 = crud.create_service_notification(
        db, schemas.NotificationCreate(title="First message"), service1
    )
    notification2 = crud.create_service_notification(
        db, schemas.NotificationCreate(title="Second message"), service1
    )
    notification3 = crud.create_service_notification(
        db, schemas.NotificationCreate(title="Second message"), service2
    )
    assert user.notifications == [notification1, notification2, notification3]
    unknown_id = max(notification1.id, notification2.id, notification3.id) + 1
    updated_notifications = [
        schemas.UserUpdateNotification(id=notification1.id, status="read"),
        schemas.UserUpdateNotification(id=notification2.id, status="deleted"),
        schemas.UserUpdateNotification(id=unknown_id, status="read"),
        schemas.UserUpdateNotification(id=notification3.id, status="unread"),
    ]
    crud.update_user_notifications(db, updated_notifications, user)
    #  Notifications are updated
    # Unknown notification is ignored
    db.refresh(user)
    assert user.notifications == [notification1, notification3]
    # notification2 is still in the service notifications
    # it was only deleted from the user notifications
    db_notification2 = db.query(models.Notification).get(notification2.id)
    assert db_notification2 == notification2
    assert db_notification2 in notification2.service.notifications
Пример #2
0
def test_get_user_notifications(db, user, service):
    user.subscribe(service)
    db.commit()
    notification1 = crud.create_service_notification(
        db, schemas.NotificationCreate(title="First message"), service
    )
    notification2 = crud.create_service_notification(
        db, schemas.NotificationCreate(title="Second message"), service
    )
    user_notifications = crud.get_user_notifications(db, user)
    user_notification1 = schemas.UserNotification(
        id=notification1.id,
        timestamp=notification1.timestamp,
        title=notification1.title,
        subtitle=notification1.subtitle,
        url=notification1.url,
        service_id=service.id,
        is_read=False,
    )
    user_notification2 = schemas.UserNotification(
        id=notification2.id,
        timestamp=notification2.timestamp,
        title=notification2.title,
        subtitle=notification2.subtitle,
        url=notification2.url,
        service_id=service.id,
        is_read=False,
    )
    assert user_notifications == [user_notification2, user_notification1]
Пример #3
0
def test_create_service_notification(db, user_factory, service):
    title = "My message"
    subtitle = "This is a test"
    url = "https://google.com"
    user1 = user_factory()
    user2 = user_factory()
    user1.subscribe(service)
    db.commit()
    assert service.notifications == []
    assert user1.notifications == []
    assert user2.notifications == []
    notification = crud.create_service_notification(
        db,
        schemas.NotificationCreate(title=title, subtitle=subtitle, url=url),
        service=service,
    )
    assert isinstance(notification.id, int)
    assert notification.title == title
    assert notification.subtitle == subtitle
    assert notification.url == url
    # The notification was added both to the service and service subscribers
    assert service.notifications == [notification]
    assert user1.notifications == [notification]
    assert user2.notifications == []
    assert notification.users_notification == [user1.user_notifications[0]]
Пример #4
0
def test_delete_notifications_with_user(db, user, service_factory, notification_date):
    service1 = service_factory()
    service2 = service_factory()
    user.subscribe(service1)
    db.commit()
    notification1 = crud.create_service_notification(
        db, schemas.NotificationCreate(title="message1"), service1
    )
    notification1.timestamp = notification_date(40)
    notification2 = crud.create_service_notification(
        db, schemas.NotificationCreate(title="message2"), service1
    )
    notification2.timestamp = notification_date(40)
    notification3 = crud.create_service_notification(
        db, schemas.NotificationCreate(title="message3"), service2
    )
    notification3.timestamp = notification_date(35)
    notification4 = crud.create_service_notification(
        db, schemas.NotificationCreate(title="message4"), service1
    )
    notification4.timestamp = notification_date(1)
    notification5 = crud.create_service_notification(
        db, schemas.NotificationCreate(title="message5"), service2
    )
    assert user.notifications == [notification1, notification2, notification4]
    assert db.query(models.Notification).order_by(
        models.Notification.timestamp
    ).all() == [
        notification1,
        notification2,
        notification3,
        notification4,
        notification5,
    ]
    crud.delete_notifications(db, 30)
    # Check that old notifications have been deleted
    assert db.query(models.Notification).order_by(
        models.Notification.timestamp
    ).all() == [
        notification4,
        notification5,
    ]
    # Check that old UserNotifications have also been deleted
    assert user.notifications == [notification4]
Пример #5
0
def test_get_user_notifications_filter_services_id(db, user, service_factory):
    # Create some notifications
    service1 = service_factory()
    service2 = service_factory()
    service3 = service_factory()
    user.subscribe(service1)
    user.subscribe(service2)
    user.subscribe(service3)
    db.commit()
    notifications = []
    now = datetime.datetime.now()
    for service_nb, service in enumerate((service1, service2, service3), start=1):
        for nb in range(10):
            notification = crud.create_service_notification(
                db,
                schemas.NotificationCreate(title=f"Service{service_nb} - message{nb}"),
                service,
            )
            notification.timestamp = now - datetime.timedelta(minutes=nb + service_nb)
            notifications.append(notification)
    # Get the user notifications with filtering on the services id
    user_notifications = crud.get_user_notifications(db, user)
    assert len(user_notifications) == 30
    # Filter on service1 only
    service1_user_notifications = crud.get_user_notifications(
        db, user, filter_services_id=[service1.id]
    )
    assert len(service1_user_notifications) == 10
    for notification in service1_user_notifications:
        assert notification.title.startswith("Service1")
    # Filter on service1 and service3
    service1_3_user_notifications = crud.get_user_notifications(
        db, user, filter_services_id=[service1.id, service3.id]
    )
    assert len(service1_3_user_notifications) == 20
    for notification in service1_3_user_notifications:
        assert not notification.title.startswith("Service2")
    # Filter on all services is the same as no filtering
    services_user_notifications = crud.get_user_notifications(
        db, user, filter_services_id=[service1.id, service2.id, service3.id]
    )
    assert user_notifications == services_user_notifications
    # Filter on empty list
    assert crud.get_user_notifications(db, user, filter_services_id=[]) == []
    # Filter on unknown service id
    assert (
        crud.get_user_notifications(
            db, user, filter_services_id=["2cf81604-514a-45e1-9a34-f4368834db99"]
        )
        == []
    )
Пример #6
0
def test_delete_service_with_user(db, user, service_factory):
    service1 = service_factory(category="category1")
    service2 = service_factory(category="category2")
    service3 = service_factory(category="category3")
    user.subscribe(service1)
    user.subscribe(service3)
    db.commit()
    notification1 = crud.create_service_notification(
        db, schemas.NotificationCreate(title="message1"), service1
    )
    notification2 = crud.create_service_notification(
        db, schemas.NotificationCreate(title="message2"), service1
    )
    notification3 = crud.create_service_notification(
        db, schemas.NotificationCreate(title="message3"), service2
    )
    notification4 = crud.create_service_notification(
        db, schemas.NotificationCreate(title="message4"), service1
    )
    notification5 = crud.create_service_notification(
        db, schemas.NotificationCreate(title="message5"), service2
    )
    notification6 = crud.create_service_notification(
        db, schemas.NotificationCreate(title="message6"), service3
    )
    assert user.notifications == [
        notification1,
        notification2,
        notification4,
        notification6,
    ]
    assert db.query(models.Notification).order_by(models.Notification.title).all() == [
        notification1,
        notification2,
        notification3,
        notification4,
        notification5,
        notification6,
    ]
    crud.delete_service(db, service1)
    # Check that service1 UserNotifications have been deleted
    assert user.notifications == [notification6]
    # Check that service1 notifications have been deleted
    assert db.query(models.Notification).order_by(models.Notification.title).all() == [
        notification3,
        notification5,
        notification6,
    ]
    # Check that service1 has been deleted
    assert db.query(models.Service).order_by(models.Service.category).all() == [
        service2,
        service3,
    ]
Пример #7
0
def test_get_user_notifications_limit(db, user, service, limit, sort):
    # Create some notifications
    user.subscribe(service)
    db.commit()
    notifications = []
    now = datetime.datetime.now()
    for nb in range(20):
        notification = crud.create_service_notification(
            db, schemas.NotificationCreate(title=f"message{nb}"), service
        )
        notification.timestamp = now - datetime.timedelta(minutes=nb)
        notifications.append(notification)
    # Get the user notifications
    user_notifications = crud.get_user_notifications(db, user, limit=limit, sort=sort)
    # Check the number of notifications returned
    if limit > 0 and limit <= 20:
        assert len(user_notifications) == limit
        oldest_timestamp = notifications[limit - 1].timestamp
    else:
        assert len(user_notifications) == 20
        oldest_timestamp = notifications[-1].timestamp
    # Check they are properly sorted
    # In both cases, we have the same list, but sorted in reverse order
    newest_timestamp = notifications[0].timestamp
    if sort == "asc":
        assert user_notifications[0].timestamp == oldest_timestamp
        assert user_notifications[-1].timestamp == newest_timestamp
        is_reverse = False
    elif sort == "desc":
        assert user_notifications[0].timestamp == newest_timestamp
        assert user_notifications[-1].timestamp == oldest_timestamp
        is_reverse = True
    sorted_user_notifications = sorted(
        user_notifications, key=attrgetter("timestamp"), reverse=is_reverse
    )
    assert user_notifications == sorted_user_notifications