urma/tests/test_models.py
2023-01-15 20:50:57 +00:00

118 lines
2.7 KiB
Python

from app.models import (
Accounts,
Hashtags,
Posts,
)
def test_accounts_creation(session):
"""Account creation"""
account_id = "109568725613662482"
acct = Accounts(session, account_id)
assert acct
assert acct.account_id == account_id
assert acct.username is None
def test_create_or_add_account(session):
"""Check we can retrieve existing account"""
account_id = "109568725613662482"
acct = Accounts.get_or_create(session, account_id)
acct2 = Accounts.get_or_create(session, account_id)
assert acct is acct2
def test_get_followed_accounts(session):
"""Test retrieval of followed accounts"""
account1_id = "109568725613662482"
account2_id = "109568725613662483"
acct1 = Accounts.get_or_create(session, account1_id)
acct2 = Accounts.get_or_create(session, account2_id)
acct2.followed = True
session.flush()
accts_followed = Accounts.get_followed(session)
assert acct1 not in accts_followed
assert acct2 in accts_followed
def test_hashtags_access(session):
"""Test we can access hashtags table"""
result = Hashtags.get_all(session)
assert result == []
def test_create_hashtag(session):
"""Create a hashtag"""
h_name = "MyHashtag"
h_url = "https://example.com"
ht = Hashtags.get_or_create(session, h_name, h_url)
assert ht
assert ht.name == h_name
assert ht.url == h_url
def test_create_or_add_hashtag(session):
"""Check we can retrieve existing hashtag"""
h_name = "MyHashtag"
h_url = "https://example.com"
ht = Hashtags.get_or_create(session, h_name, h_url)
ht2 = Hashtags.get_or_create(session, h_name, h_url)
assert ht is ht2
def test_get_followed_hashtags(session):
"""Test retrieval of followed hashtags"""
ht1 = "HashTagOne"
ht1_url = "https://one.example.com"
ht2 = "HashTagTwo"
ht2_url = "https://two.example.com"
hashtag1 = Hashtags.get_or_create(session, ht1, ht1_url)
hashtag2 = Hashtags.get_or_create(session, ht2, ht2_url)
hashtag2.followed = True
session.flush()
hashtags_followed = Hashtags.get_followed(session)
assert hashtag1 not in hashtags_followed
assert hashtag2 in hashtags_followed
def test_create_posts(session):
"""Test we can create posts"""
post_id = "109666763623624320"
post = Posts(session, post_id)
assert post.post_id == post_id
assert post.account_id is None
def test_get_by_post_id(session):
"""Retrieve by post ID"""
post1_id = "109666763623624320"
post2_id = "109666763623624321"
post1 = Posts(session, post1_id)
post2 = Posts(session, post2_id)
post = Posts.get_by_post_id(session, post1_id)
assert post is post1
assert post is not post2