comment-server/tests/database_test.py

423 lines
16 KiB
Python
Raw Normal View History

from random import randint
2019-05-21 13:54:52 +02:00
import faker
from faker.providers import internet
from faker.providers import lorem
from faker.providers import misc
2019-05-19 08:23:20 +02:00
2019-05-21 14:58:43 +02:00
from lbry_comment_server.database import get_comments_by_id, create_comment, get_comment_ids, create_comment_async, \
get_claim_comments
from schema.db_helpers import setup_database, teardown_database
from lbry_comment_server.settings import config
from tests.testcase import DatabaseTestCase, AsyncioTestCase
fake = faker.Faker()
fake.add_provider(internet)
fake.add_provider(lorem)
fake.add_provider(misc)
2019-05-19 08:23:20 +02:00
class TestCommentCreation(DatabaseTestCase):
def setUp(self) -> None:
super().setUp()
self.claimId = '529357c3422c6046d3fec76be2358004ba22e340'
2019-05-20 11:07:39 +02:00
def test01NamedComments(self):
2019-05-21 14:58:43 +02:00
comment = create_comment(
2019-05-20 11:07:39 +02:00
conn=self.conn,
2019-05-19 08:23:20 +02:00
claim_id=self.claimId,
comment='This is a named comment',
2019-05-19 09:00:18 +02:00
channel_name='@username',
2019-05-19 08:23:20 +02:00
channel_id='529357c3422c6046d3fec76be2358004ba22abcd',
)
self.assertIsNotNone(comment)
self.assertIsNone(comment['parent_id'])
previous_id = comment['comment_id']
2019-05-21 14:58:43 +02:00
reply = create_comment(
2019-05-20 11:07:39 +02:00
conn=self.conn,
2019-05-19 08:23:20 +02:00
claim_id=self.claimId,
comment='This is a named response',
2019-05-19 09:00:18 +02:00
channel_name='@another_username',
2019-05-19 08:23:20 +02:00
channel_id='529357c3422c6046d3fec76be2358004ba224bcd',
parent_id=previous_id
)
self.assertIsNotNone(reply)
self.assertEqual(reply['parent_id'], comment['comment_id'])
self.assertEqual(reply['claim_id'], comment['claim_id'])
2019-05-20 11:07:39 +02:00
def test02AnonymousComments(self):
2019-05-21 14:58:43 +02:00
comment = create_comment(
2019-05-20 11:07:39 +02:00
conn=self.conn,
2019-05-19 08:23:20 +02:00
claim_id=self.claimId,
comment='This is an ANONYMOUS comment'
2019-05-19 08:23:20 +02:00
)
self.assertIsNotNone(comment)
self.assertIsNone(comment['parent_id'])
previous_id = comment['comment_id']
2019-05-21 14:58:43 +02:00
reply = create_comment(
2019-05-20 11:07:39 +02:00
conn=self.conn,
2019-05-19 08:23:20 +02:00
claim_id=self.claimId,
comment='This is an unnamed response',
parent_id=previous_id
)
self.assertIsNotNone(reply)
self.assertEqual(reply['parent_id'], comment['comment_id'])
self.assertEqual(reply['claim_id'], comment['claim_id'])
2019-05-20 11:07:39 +02:00
def test03SignedComments(self):
2019-05-21 14:58:43 +02:00
comment = create_comment(
2019-05-20 11:07:39 +02:00
conn=self.conn,
2019-05-19 08:23:20 +02:00
claim_id=self.claimId,
comment='I like big butts and i cannot lie',
2019-05-19 09:00:18 +02:00
channel_name='@sirmixalot',
2019-05-19 08:23:20 +02:00
channel_id='529357c3422c6046d3fec76be2358005ba22abcd',
2019-05-20 11:07:39 +02:00
signature='siggy'
2019-05-19 08:23:20 +02:00
)
self.assertIsNotNone(comment)
self.assertIsNone(comment['parent_id'])
previous_id = comment['comment_id']
2019-05-21 14:58:43 +02:00
reply = create_comment(
2019-05-20 11:07:39 +02:00
conn=self.conn,
2019-05-19 08:23:20 +02:00
claim_id=self.claimId,
comment='This is a LBRY verified response',
2019-05-19 09:00:18 +02:00
channel_name='@LBRY',
2019-05-19 08:23:20 +02:00
channel_id='529357c3422c6046d3fec76be2358001ba224bcd',
parent_id=previous_id,
2019-05-20 11:07:39 +02:00
signature='Cursive Font Goes Here'
2019-05-19 08:23:20 +02:00
)
self.assertIsNotNone(reply)
self.assertEqual(reply['parent_id'], comment['comment_id'])
self.assertEqual(reply['claim_id'], comment['claim_id'])
2019-05-20 11:07:39 +02:00
def test04UsernameVariations(self):
2019-05-21 14:58:43 +02:00
invalid_comment = create_comment(
2019-05-20 11:07:39 +02:00
conn=self.conn,
2019-05-19 09:00:18 +02:00
claim_id=self.claimId,
channel_name='$#(@#$@#$',
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
comment='this is an invalid username'
)
2019-05-19 23:16:07 +02:00
self.assertIsNone(invalid_comment)
2019-05-21 14:58:43 +02:00
valid_username = create_comment(
2019-05-20 11:07:39 +02:00
conn=self.conn,
2019-05-19 09:00:18 +02:00
claim_id=self.claimId,
channel_name='@' + 'a'*255,
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
comment='this is a valid username'
)
2019-05-19 23:16:07 +02:00
self.assertIsNotNone(valid_username)
2019-05-21 14:58:43 +02:00
lengthy_username = create_comment(
2019-05-20 11:07:39 +02:00
conn=self.conn,
2019-05-19 09:00:18 +02:00
claim_id=self.claimId,
channel_name='@' + 'a'*256,
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
comment='this username is too long'
)
2019-05-19 23:16:07 +02:00
self.assertIsNone(lengthy_username)
2019-05-21 14:58:43 +02:00
comment = create_comment(
2019-05-20 11:07:39 +02:00
conn=self.conn,
2019-05-19 09:00:18 +02:00
claim_id=self.claimId,
channel_name='',
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
comment='this username should not default to ANONYMOUS'
2019-05-19 09:00:18 +02:00
)
2019-05-19 23:16:07 +02:00
self.assertIsNone(comment)
2019-05-21 14:58:43 +02:00
short_username = create_comment(
2019-05-20 11:07:39 +02:00
conn=self.conn,
2019-05-19 09:00:18 +02:00
claim_id=self.claimId,
channel_name='@',
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
comment='this username is too short'
)
2019-05-19 23:16:07 +02:00
self.assertIsNone(short_username)
2019-05-19 09:00:18 +02:00
2019-05-20 11:07:39 +02:00
def test05InsertRandomComments(self):
2019-05-19 23:16:07 +02:00
top_comments, claim_ids = generate_top_comments_random()
total = 0
success = 0
for _, comments in top_comments.items():
for i, comment in enumerate(comments):
2019-05-19 23:16:07 +02:00
with self.subTest(comment=comment):
2019-05-21 14:58:43 +02:00
result = create_comment(self.conn, **comment)
2019-05-19 23:16:07 +02:00
if result:
success += 1
comments[i] = result
del comment
total += len(comments)
self.assertLessEqual(success, total)
self.assertGreater(success, 0)
success = 0
2019-05-19 23:16:07 +02:00
for reply in generate_replies_random(top_comments):
2019-05-21 14:58:43 +02:00
reply_id = create_comment(self.conn, **reply)
if reply_id:
success += 1
self.assertGreater(success, 0)
self.assertLess(success, total)
2019-05-19 23:16:07 +02:00
del top_comments
del claim_ids
2019-05-20 11:07:39 +02:00
def test06GenerateAndListComments(self):
2019-05-19 23:16:07 +02:00
top_comments, claim_ids = generate_top_comments()
total, success = 0, 0
for _, comments in top_comments.items():
for i, comment in enumerate(comments):
2019-05-21 14:58:43 +02:00
result = create_comment(self.conn, **comment)
2019-05-19 23:16:07 +02:00
if result:
success += 1
comments[i] = result
del comment
total += len(comments)
self.assertEqual(total, success)
self.assertGreater(total, 0)
for reply in generate_replies(top_comments):
2019-05-21 14:58:43 +02:00
create_comment(self.conn, **reply)
2019-05-19 23:16:07 +02:00
for claim_id in claim_ids:
2019-05-21 14:58:43 +02:00
comments_ids = get_comment_ids(self.conn, claim_id)
2019-05-19 23:16:07 +02:00
with self.subTest(comments_ids=comments_ids):
self.assertIs(type(comments_ids), list)
self.assertGreaterEqual(len(comments_ids), 0)
self.assertLessEqual(len(comments_ids), 50)
2019-05-21 14:58:43 +02:00
replies = get_comments_by_id(self.conn, comments_ids)
2019-05-19 23:16:07 +02:00
self.assertLessEqual(len(replies), 50)
self.assertEqual(len(replies), len(comments_ids))
2019-05-20 07:18:56 +02:00
class ListDatabaseTest(DatabaseTestCase):
def setUp(self) -> None:
super().setUp()
top_coms, self.claim_ids = generate_top_comments(5, 75)
self.top_comments = {
2019-05-21 14:58:43 +02:00
commie_id: [create_comment(self.conn, **commie) for commie in commie_list]
2019-05-20 07:18:56 +02:00
for commie_id, commie_list in top_coms.items()
}
self.replies = [
2019-05-21 14:58:43 +02:00
create_comment(self.conn, **reply)
2019-05-20 07:18:56 +02:00
for reply in generate_replies(self.top_comments)
]
def testLists(self):
for claim_id in self.claim_ids:
with self.subTest(claim_id=claim_id):
2019-05-21 14:58:43 +02:00
comments = get_claim_comments(self.conn, claim_id)
2019-05-20 07:18:56 +02:00
self.assertIsNotNone(comments)
self.assertLessEqual(len(comments), 50)
2019-05-21 14:58:43 +02:00
top_comments = get_claim_comments(self.conn, claim_id, top_level=True, page=1, page_size=50)
2019-05-20 07:18:56 +02:00
self.assertIsNotNone(top_comments)
self.assertLessEqual(len(top_comments), 50)
2019-05-21 14:58:43 +02:00
comment_ids = get_comment_ids(self.conn, claim_id, page_size=50, page=1)
2019-05-20 07:18:56 +02:00
with self.subTest(comment_ids=comment_ids):
self.assertIsNotNone(comment_ids)
self.assertLessEqual(len(comment_ids), 50)
2019-05-21 14:58:43 +02:00
matching_comments = get_comments_by_id(self.conn, comment_ids)
2019-05-20 07:18:56 +02:00
self.assertIsNotNone(matching_comments)
self.assertEqual(len(matching_comments), len(comment_ids))
class AsyncWriteTest(AsyncioTestCase):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.db_path = config['PATH']['TEST']
self.claimId = '529357c3422c6046d3fec76be2358004ba22e340'
async def asyncSetUp(self):
await super().asyncSetUp()
2019-05-21 14:58:43 +02:00
setup_database(self.db_path)
async def asyncTearDown(self):
await super().asyncTearDown()
2019-05-21 14:58:43 +02:00
teardown_database(self.db_path)
async def test01NamedComments(self):
2019-05-21 14:58:43 +02:00
comment = await create_comment_async(
self.db_path,
claim_id=self.claimId,
comment='This is a named comment',
channel_name='@username',
channel_id='529357c3422c6046d3fec76be2358004ba22abcd',
)
self.assertIsNotNone(comment)
self.assertIsNone(comment['parent_id'])
previous_id = comment['comment_id']
2019-05-21 14:58:43 +02:00
reply = await create_comment_async(
self.db_path,
claim_id=self.claimId,
comment='This is a named response',
channel_name='@another_username',
channel_id='529357c3422c6046d3fec76be2358004ba224bcd',
parent_id=previous_id
)
self.assertIsNotNone(reply)
self.assertEqual(reply['parent_id'], comment['comment_id'])
self.assertEqual(reply['claim_id'], comment['claim_id'])
async def test02AnonymousComments(self):
2019-05-21 14:58:43 +02:00
comment = await create_comment_async(
self.db_path,
claim_id=self.claimId,
comment='This is an ANONYMOUS comment'
)
self.assertIsNotNone(comment)
self.assertIsNone(comment['parent_id'])
previous_id = comment['comment_id']
2019-05-21 14:58:43 +02:00
reply = await create_comment_async(
self.db_path,
claim_id=self.claimId,
comment='This is an unnamed response',
parent_id=previous_id
)
self.assertIsNotNone(reply)
self.assertEqual(reply['parent_id'], comment['comment_id'])
self.assertEqual(reply['claim_id'], comment['claim_id'])
async def test03SignedComments(self):
2019-05-21 14:58:43 +02:00
comment = await create_comment_async(
self.db_path,
claim_id=self.claimId,
comment='I like big butts and i cannot lie',
channel_name='@sirmixalot',
channel_id='529357c3422c6046d3fec76be2358005ba22abcd',
signature='siggy'
)
self.assertIsNotNone(comment)
self.assertIsNone(comment['parent_id'])
previous_id = comment['comment_id']
2019-05-21 14:58:43 +02:00
reply = await create_comment_async(
self.db_path,
claim_id=self.claimId,
comment='This is a LBRY verified response',
channel_name='@LBRY',
channel_id='529357c3422c6046d3fec76be2358001ba224bcd',
parent_id=previous_id,
signature='Cursive Font Goes Here'
)
self.assertIsNotNone(reply)
self.assertEqual(reply['parent_id'], comment['comment_id'])
self.assertEqual(reply['claim_id'], comment['claim_id'])
async def test04UsernameVariations(self):
2019-05-21 14:58:43 +02:00
invalid_comment = await create_comment_async(
self.db_path,
claim_id=self.claimId,
channel_name='$#(@#$@#$',
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
comment='this is an invalid username'
)
self.assertIsNone(invalid_comment)
2019-05-21 14:58:43 +02:00
valid_username = await create_comment_async(
self.db_path,
claim_id=self.claimId,
channel_name='@' + 'a'*255,
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
comment='this is a valid username'
)
self.assertIsNotNone(valid_username)
2019-05-21 14:58:43 +02:00
lengthy_username = await create_comment_async(
self.db_path,
claim_id=self.claimId,
channel_name='@' + 'a'*256,
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
comment='this username is too long'
)
self.assertIsNone(lengthy_username)
2019-05-21 14:58:43 +02:00
comment = await create_comment_async(
self.db_path,
claim_id=self.claimId,
channel_name='',
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
comment='this username should not default to ANONYMOUS'
)
self.assertIsNone(comment)
2019-05-21 14:58:43 +02:00
short_username = await create_comment_async(
self.db_path,
claim_id=self.claimId,
channel_name='@',
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
comment='this username is too short'
)
self.assertIsNone(short_username)
async def test06GenerateAndListComments(self):
top_comments, claim_ids = generate_top_comments()
total, success = 0, 0
for _, comments in top_comments.items():
for i, comment in enumerate(comments):
2019-05-21 14:58:43 +02:00
result = await create_comment_async(self.db_path, **comment)
if result:
success += 1
comments[i] = result
del comment
total += len(comments)
self.assertEqual(total, success)
self.assertGreater(total, 0)
success, total = 0, 0
for reply in generate_replies(top_comments):
2019-05-21 14:58:43 +02:00
inserted_reply = await create_comment_async(self.db_path, **reply)
if inserted_reply:
success += 1
total += 1
self.assertEqual(success, total)
self.assertGreater(success, 0)
2019-05-21 01:09:07 +02:00
def generate_replies(top_comments):
2019-05-19 23:16:07 +02:00
return [{
'claim_id': comment['claim_id'],
'parent_id': comment['comment_id'],
'comment': ' '.join(fake.text(max_nb_chars=randint(50, 500))),
'channel_name': '@' + fake.user_name(),
'channel_id': fake.sha1(),
'signature': fake.uuid4()
}
for claim, comments in top_comments.items()
for i, comment in enumerate(comments)
if comment # ensures comment is non-null
]
def generate_replies_random(top_comments):
return [{
'claim_id': comment['claim_id'],
'parent_id': comment['comment_id'],
'comment': ' '.join(fake.text(max_nb_chars=randint(50, 2500))),
'channel_name': '@' + fake.user_name(),
2019-05-19 23:16:07 +02:00
'channel_id': fake.sha1() if hash(comment['comment_id']) % 5 == 0 else '',
'signature': fake.uuid4() if hash(comment['comment_id']) % 11 == 0 else None
}
for claim, comments in top_comments.items()
for i, comment in enumerate(comments)
if comment
]
2019-05-20 07:18:56 +02:00
def generate_top_comments(ncid=15, ncomm=100, minchar=50, maxchar=500):
claim_ids = [fake.sha1() for _ in range(ncid)]
2019-05-19 23:16:07 +02:00
top_comments = {
cid: [{
'claim_id': cid,
2019-05-20 07:18:56 +02:00
'comment': ''.join(fake.text(max_nb_chars=randint(minchar, maxchar))),
2019-05-19 23:16:07 +02:00
'channel_name': '@' + fake.user_name(),
'channel_id': fake.sha1(),
'signature': fake.uuid4()
2019-05-20 07:18:56 +02:00
} for _ in range(ncomm)]
2019-05-19 23:16:07 +02:00
for cid in claim_ids
}
return top_comments, claim_ids
def generate_top_comments_random():
claim_ids = [fake.sha1() for _ in range(15)]
top_comments = {
cid: [{
'claim_id': cid,
'comment': ''.join(fake.text(max_nb_chars=randint(50, 2500))),
2019-05-19 23:16:07 +02:00
'channel_name': '@' + fake.user_name() if (hash(cid) * i) % 7 > 0 else '',
'channel_id': fake.sha1() if (hash(cid) * i) % 7 > 0 else '',
'signature': fake.uuid4() if (hash(cid) * i) % 7 > 0 > hash(cid) else None
} for i in range(randint(60, 200))]
for cid in claim_ids
}
2019-05-19 23:16:07 +02:00
return top_comments, claim_ids