more thorough database testing
This commit is contained in:
parent
436608d69b
commit
1e38fd8ca3
1 changed files with 93 additions and 35 deletions
|
@ -7,7 +7,6 @@ from faker.providers import misc
|
||||||
import server.conf
|
import server.conf
|
||||||
import server.database as db
|
import server.database as db
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import json
|
|
||||||
import faker
|
import faker
|
||||||
from random import randint
|
from random import randint
|
||||||
|
|
||||||
|
@ -129,81 +128,124 @@ class TestCommentCreation(DatabaseTestCase):
|
||||||
self.assertEqual(reply['parent_id'], comment['comment_id'])
|
self.assertEqual(reply['parent_id'], comment['comment_id'])
|
||||||
self.assertEqual(reply['claim_id'], comment['claim_id'])
|
self.assertEqual(reply['claim_id'], comment['claim_id'])
|
||||||
|
|
||||||
def testInvalidUsername(self):
|
def testUsernameVariations(self):
|
||||||
self.assertRaises(
|
invalid_comment = self.db.create_comment(
|
||||||
AssertionError,
|
|
||||||
self.db.create_comment,
|
|
||||||
claim_id=self.claimId,
|
claim_id=self.claimId,
|
||||||
channel_name='$#(@#$@#$',
|
channel_name='$#(@#$@#$',
|
||||||
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
|
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
|
||||||
comment='this is an invalid username'
|
comment='this is an invalid username'
|
||||||
)
|
)
|
||||||
comment = self.db.create_comment(
|
self.assertIsNone(invalid_comment)
|
||||||
|
valid_username = self.db.create_comment(
|
||||||
claim_id=self.claimId,
|
claim_id=self.claimId,
|
||||||
channel_name='@' + 'a'*255,
|
channel_name='@' + 'a'*255,
|
||||||
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
|
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
|
||||||
comment='this is a valid username'
|
comment='this is a valid username'
|
||||||
)
|
)
|
||||||
self.assertIsNotNone(comment)
|
self.assertIsNotNone(valid_username)
|
||||||
self.assertRaises(
|
|
||||||
AssertionError,
|
lengthy_username = self.db.create_comment(
|
||||||
self.db.create_comment,
|
|
||||||
claim_id=self.claimId,
|
claim_id=self.claimId,
|
||||||
channel_name='@' + 'a'*256,
|
channel_name='@' + 'a'*256,
|
||||||
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
|
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
|
||||||
comment='this username is too long'
|
comment='this username is too long'
|
||||||
)
|
)
|
||||||
|
self.assertIsNone(lengthy_username)
|
||||||
comment = self.db.create_comment(
|
comment = self.db.create_comment(
|
||||||
claim_id=self.claimId,
|
claim_id=self.claimId,
|
||||||
channel_name='',
|
channel_name='',
|
||||||
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
|
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
|
||||||
comment='this username will default to anonymous'
|
comment='this username should not default to anonymous'
|
||||||
)
|
)
|
||||||
self.assertIsNotNone(comment)
|
self.assertIsNone(comment)
|
||||||
self.assertEqual(comment['channel_name'], server.conf.anonymous['channel_name'])
|
short_username = self.db.create_comment(
|
||||||
self.assertEqual(comment['channel_id'], server.conf.anonymous['channel_id'])
|
|
||||||
self.assertRaises(
|
|
||||||
AssertionError,
|
|
||||||
self.db.create_comment,
|
|
||||||
claim_id=self.claimId,
|
claim_id=self.claimId,
|
||||||
channel_name='@',
|
channel_name='@',
|
||||||
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
|
channel_id='529357c3422c6046d3fec76be2358001ba224b23',
|
||||||
comment='this username is too short'
|
comment='this username is too short'
|
||||||
)
|
)
|
||||||
|
self.assertIsNone(short_username)
|
||||||
|
|
||||||
|
|
||||||
class PopulatedDatabaseTest(DatabaseTestCase):
|
class PopulatedDatabaseTest(DatabaseTestCase):
|
||||||
|
def setUp(self) -> None:
|
||||||
|
super().setUp()
|
||||||
|
|
||||||
def testInsertComments(self):
|
def test01InsertRandomComments(self):
|
||||||
success, total = 0, 0
|
top_comments, claim_ids = generate_top_comments_random()
|
||||||
top_comments = generate_top_comments()
|
total = 0
|
||||||
|
success = 0
|
||||||
for _, comments in top_comments.items():
|
for _, comments in top_comments.items():
|
||||||
for i, comment in enumerate(comments):
|
for i, comment in enumerate(comments):
|
||||||
|
with self.subTest(comment=comment):
|
||||||
result = self.db.create_comment(**comment)
|
result = self.db.create_comment(**comment)
|
||||||
if result:
|
if result:
|
||||||
success += 1
|
success += 1
|
||||||
del comment
|
|
||||||
comments[i] = result
|
comments[i] = result
|
||||||
|
del comment
|
||||||
total += len(comments)
|
total += len(comments)
|
||||||
self.assertLessEqual(success, total)
|
self.assertLessEqual(success, total)
|
||||||
self.assertGreater(success, 0)
|
self.assertGreater(success, 0)
|
||||||
success = 0
|
success = 0
|
||||||
for reply in generate_replies(top_comments):
|
for reply in generate_replies_random(top_comments):
|
||||||
reply_id = self.db.create_comment(**reply)
|
reply_id = self.db.create_comment(**reply)
|
||||||
if reply_id:
|
if reply_id:
|
||||||
success += 1
|
success += 1
|
||||||
self.assertGreater(success, 0)
|
self.assertGreater(success, 0)
|
||||||
self.assertLess(success, total)
|
self.assertLess(success, total)
|
||||||
|
del top_comments
|
||||||
|
del claim_ids
|
||||||
|
|
||||||
|
def test02GenerateAndListComments(self):
|
||||||
|
top_comments, claim_ids = generate_top_comments()
|
||||||
|
total, success = 0, 0
|
||||||
|
for _, comments in top_comments.items():
|
||||||
|
for i, comment in enumerate(comments):
|
||||||
|
result = self.db.create_comment(**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):
|
||||||
|
self.db.create_comment(**reply)
|
||||||
|
self.assertEqual(success, total)
|
||||||
|
for claim_id in claim_ids:
|
||||||
|
comments_ids = self.db.get_comment_ids(claim_id)
|
||||||
|
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)
|
||||||
|
replies = self.db.get_comments_by_id(comments_ids)
|
||||||
|
self.assertLessEqual(len(replies), 50)
|
||||||
|
self.assertEqual(len(replies), len(comments_ids))
|
||||||
|
|
||||||
|
|
||||||
def generate_replies(top_comments):
|
def generate_replies(top_comments):
|
||||||
|
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 [{
|
return [{
|
||||||
'claim_id': comment['claim_id'],
|
'claim_id': comment['claim_id'],
|
||||||
'parent_id': comment['comment_id'],
|
'parent_id': comment['comment_id'],
|
||||||
'comment': ' '.join(fake.text(max_nb_chars=randint(50, 2500))),
|
'comment': ' '.join(fake.text(max_nb_chars=randint(50, 2500))),
|
||||||
'channel_name': '@' + fake.user_name(),
|
'channel_name': '@' + fake.user_name(),
|
||||||
'channel_id': fake.sha1() if hash(comment['comment_id']) % 11 == 0 else None,
|
'channel_id': fake.sha1() if hash(comment['comment_id']) % 5 == 0 else '',
|
||||||
'signature': fake.uuid4() if hash(comment['comment_id']) % 11 == 0 else None
|
'signature': fake.uuid4() if hash(comment['comment_id']) % 11 == 0 else None
|
||||||
}
|
}
|
||||||
for claim, comments in top_comments.items()
|
for claim, comments in top_comments.items()
|
||||||
|
@ -217,11 +259,27 @@ def generate_top_comments():
|
||||||
top_comments = {
|
top_comments = {
|
||||||
cid: [{
|
cid: [{
|
||||||
'claim_id': cid,
|
'claim_id': cid,
|
||||||
'comment': ''.join(fake.text(max_nb_chars=randint(50, 2500))),
|
'comment': ''.join(fake.text(max_nb_chars=randint(50, 500))),
|
||||||
'channel_name': '@' + fake.user_name() if (hash(cid) * i) % 29*i > 0 else None,
|
'channel_name': '@' + fake.user_name(),
|
||||||
'channel_id': fake.sha1() if (hash(cid) * i) % 29*i > 0 else None,
|
'channel_id': fake.sha1(),
|
||||||
'signature': fake.uuid4() if (hash(cid) * i) % 29*i > 0 > hash(cid) else None
|
'signature': fake.uuid4()
|
||||||
} for i in range(randint(0, hash(cid) % 91))]
|
} for _ in range(100)]
|
||||||
for cid in claim_ids
|
for cid in claim_ids
|
||||||
}
|
}
|
||||||
return top_comments
|
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))),
|
||||||
|
'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
|
||||||
|
}
|
||||||
|
return top_comments, claim_ids
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue