urma/tests/test_models.py
2023-01-15 21:19:18 +00:00

216 lines
5.1 KiB
Python

from app.models import (
Accounts,
Hashtags,
Posts,
)
# Accounts
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
# Hashtags
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_get_or_create_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
# Posts
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
def test_get_or_create_post(session):
"""Check we can retrieve existing post"""
post_id = "109666763623624576"
post = Posts.get_or_create(session, post_id)
post2 = Posts.get_or_create(session, post_id)
assert post is post2
def test_max_post_id_empty(session):
"""Test max_post_id with empty table"""
max_post_id = Posts.max_post_id(session)
assert max_post_id is None
def test_max_post_id_one(session):
"""Test max_post_id with one entry"""
post_id = "109666763623624576"
post = Posts(session, post_id)
max_post_id = Posts.max_post_id(session)
assert max_post_id == post_id
def test_max_post_id_three(session):
"""Test max_post_id with three entries"""
post1_id = "109666763623624576"
post2_id = "209666763623624576" # highest ID
post3_id = "109666763623624577"
post1 = Posts(session, post1_id)
post2 = Posts(session, post2_id)
post3 = Posts(session, post3_id)
max_post_id = Posts.max_post_id(session)
assert max_post_id == post2_id
# PostTags
def test_add_hashtag_to_post(session):
"""Test adding a hashtag to a post"""
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)
post1_id = "109666763623624576"
post2_id = "209666763623624576"
post3_id = "109666763623624577"
post1 = Posts(session, post1_id)
post2 = Posts(session, post2_id)
post3 = Posts(session, post3_id)
post1.hashtags.append(hashtag2)
assert len(post1.hashtags) == 1
assert post1.hashtags[0] is hashtag2
def test_add_two_hashtags_to_post(session):
"""Test adding two hashtags to a post"""
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)
post1_id = "109666763623624576"
post2_id = "209666763623624576"
post3_id = "109666763623624577"
post1 = Posts(session, post1_id)
post2 = Posts(session, post2_id)
post3 = Posts(session, post3_id)
post1.hashtags.append(hashtag2)
post1.hashtags.append(hashtag1)
assert len(post1.hashtags) == 2
assert hashtag1 in post1.hashtags
assert hashtag2 in post1.hashtags