2019-03-15 06:33:41 +01:00
|
|
|
from unittest import TestCase
|
|
|
|
from decimal import Decimal
|
|
|
|
|
2019-06-21 03:02:58 +02:00
|
|
|
from lbry.schema.claim import Claim, Stream
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestClaimContainerAwareness(TestCase):
|
|
|
|
|
|
|
|
def test_stream_claim(self):
|
|
|
|
stream = Stream()
|
|
|
|
claim = stream.claim
|
2019-04-21 05:54:34 +02:00
|
|
|
self.assertEqual(claim.claim_type, Claim.STREAM)
|
2019-03-15 06:33:41 +01:00
|
|
|
claim = Claim.from_bytes(claim.to_bytes())
|
2019-04-21 05:54:34 +02:00
|
|
|
self.assertEqual(claim.claim_type, Claim.STREAM)
|
2019-03-15 06:33:41 +01:00
|
|
|
self.assertIsNotNone(claim.stream)
|
|
|
|
with self.assertRaisesRegex(ValueError, 'Claim is not a channel.'):
|
|
|
|
print(claim.channel)
|
|
|
|
|
|
|
|
|
|
|
|
class TestFee(TestCase):
|
|
|
|
|
|
|
|
def test_amount_setters(self):
|
|
|
|
stream = Stream()
|
|
|
|
|
|
|
|
stream.fee.lbc = Decimal('1.01')
|
|
|
|
self.assertEqual(stream.fee.lbc, Decimal('1.01'))
|
|
|
|
self.assertEqual(stream.fee.dewies, 101000000)
|
|
|
|
self.assertEqual(stream.fee.currency, 'LBC')
|
2019-03-15 14:05:14 +01:00
|
|
|
stream.fee.dewies = 203000000
|
|
|
|
self.assertEqual(stream.fee.lbc, Decimal('2.03'))
|
|
|
|
self.assertEqual(stream.fee.dewies, 203000000)
|
|
|
|
self.assertEqual(stream.fee.currency, 'LBC')
|
2019-03-15 06:33:41 +01:00
|
|
|
with self.assertRaisesRegex(ValueError, 'USD can only be returned for USD fees.'):
|
|
|
|
print(stream.fee.usd)
|
2019-03-15 14:05:14 +01:00
|
|
|
with self.assertRaisesRegex(ValueError, 'Pennies can only be returned for USD fees.'):
|
|
|
|
print(stream.fee.pennies)
|
2019-03-15 06:33:41 +01:00
|
|
|
|
|
|
|
stream.fee.usd = Decimal('1.01')
|
|
|
|
self.assertEqual(stream.fee.usd, Decimal('1.01'))
|
2019-03-15 14:05:14 +01:00
|
|
|
self.assertEqual(stream.fee.pennies, 101)
|
|
|
|
self.assertEqual(stream.fee.currency, 'USD')
|
|
|
|
stream.fee.pennies = 203
|
|
|
|
self.assertEqual(stream.fee.usd, Decimal('2.03'))
|
|
|
|
self.assertEqual(stream.fee.pennies, 203)
|
2019-03-15 06:33:41 +01:00
|
|
|
self.assertEqual(stream.fee.currency, 'USD')
|
|
|
|
with self.assertRaisesRegex(ValueError, 'LBC can only be returned for LBC fees.'):
|
|
|
|
print(stream.fee.lbc)
|
|
|
|
with self.assertRaisesRegex(ValueError, 'Dewies can only be returned for LBC fees.'):
|
|
|
|
print(stream.fee.dewies)
|
2019-03-30 01:26:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestLanguages(TestCase):
|
|
|
|
|
|
|
|
def test_language_successful_parsing(self):
|
|
|
|
stream = Stream()
|
|
|
|
|
|
|
|
stream.languages.append('en')
|
|
|
|
self.assertEqual(stream.languages[0].langtag, 'en')
|
|
|
|
self.assertEqual(stream.languages[0].language, 'en')
|
|
|
|
self.assertEqual(stream.langtags, ['en'])
|
|
|
|
|
|
|
|
stream.languages.append('en-US')
|
|
|
|
self.assertEqual(stream.languages[1].langtag, 'en-US')
|
|
|
|
self.assertEqual(stream.languages[1].language, 'en')
|
|
|
|
self.assertEqual(stream.languages[1].region, 'US')
|
|
|
|
self.assertEqual(stream.langtags, ['en', 'en-US'])
|
|
|
|
|
|
|
|
stream.languages.append('en-Latn-US')
|
|
|
|
self.assertEqual(stream.languages[2].langtag, 'en-Latn-US')
|
|
|
|
self.assertEqual(stream.languages[2].language, 'en')
|
|
|
|
self.assertEqual(stream.languages[2].script, 'Latn')
|
|
|
|
self.assertEqual(stream.languages[2].region, 'US')
|
|
|
|
self.assertEqual(stream.langtags, ['en', 'en-US', 'en-Latn-US'])
|
|
|
|
|
2019-07-15 22:12:55 +02:00
|
|
|
stream.languages.append('es-419')
|
|
|
|
self.assertEqual(stream.languages[3].langtag, 'es-419')
|
|
|
|
self.assertEqual(stream.languages[3].language, 'es')
|
2019-10-02 18:58:51 +02:00
|
|
|
self.assertIsNone(stream.languages[3].script)
|
2019-07-15 22:12:55 +02:00
|
|
|
self.assertEqual(stream.languages[3].region, '419')
|
|
|
|
self.assertEqual(stream.langtags, ['en', 'en-US', 'en-Latn-US', 'es-419'])
|
|
|
|
|
2019-03-30 01:26:10 +01:00
|
|
|
stream = Stream()
|
|
|
|
stream.languages.extend(['en-Latn-US', 'es-ES', 'de-DE'])
|
|
|
|
self.assertEqual(stream.languages[0].language, 'en')
|
|
|
|
self.assertEqual(stream.languages[1].language, 'es')
|
|
|
|
self.assertEqual(stream.languages[2].language, 'de')
|
|
|
|
|
|
|
|
def test_language_error_parsing(self):
|
|
|
|
stream = Stream()
|
|
|
|
with self.assertRaisesRegex(ValueError, 'Language has no value defined for name zz'):
|
|
|
|
stream.languages.append('zz')
|
|
|
|
with self.assertRaisesRegex(ValueError, 'Script has no value defined for name Zabc'):
|
|
|
|
stream.languages.append('en-Zabc')
|
|
|
|
with self.assertRaisesRegex(ValueError, 'Country has no value defined for name ZZ'):
|
|
|
|
stream.languages.append('en-Zzzz-ZZ')
|
|
|
|
with self.assertRaisesRegex(AssertionError, 'Failed to parse language tag: en-Zzz-US'):
|
|
|
|
stream.languages.append('en-Zzz-US')
|
|
|
|
|
|
|
|
|
2019-09-25 12:28:02 +02:00
|
|
|
class TestTags(TestCase):
|
|
|
|
|
|
|
|
def test_normalize_tags(self):
|
|
|
|
claim = Claim()
|
|
|
|
|
|
|
|
claim.channel.update(tags=['Anime', 'anime', ' aNiMe', 'maNGA '])
|
|
|
|
self.assertCountEqual(claim.channel.tags, ['anime', 'manga'])
|
|
|
|
|
|
|
|
claim.channel.update(tags=['Juri', 'juRi'])
|
|
|
|
self.assertCountEqual(claim.channel.tags, ['anime', 'manga', 'juri'])
|
|
|
|
|
|
|
|
claim.channel.update(tags='Anime')
|
|
|
|
self.assertCountEqual(claim.channel.tags, ['anime', 'manga', 'juri'])
|
|
|
|
|
|
|
|
claim.channel.update(clear_tags=True)
|
|
|
|
self.assertEqual(len(claim.channel.tags), 0)
|
|
|
|
|
|
|
|
claim.channel.update(tags='Anime')
|
|
|
|
self.assertEqual(claim.channel.tags, ['anime'])
|
|
|
|
|
|
|
|
|
2019-03-30 01:26:10 +01:00
|
|
|
class TestLocations(TestCase):
|
|
|
|
|
|
|
|
def test_location_successful_parsing(self):
|
|
|
|
# from simple string
|
|
|
|
stream = Stream()
|
|
|
|
stream.locations.append('US')
|
|
|
|
self.assertEqual(stream.locations[0].country, 'US')
|
|
|
|
|
|
|
|
# from full string
|
|
|
|
stream = Stream()
|
|
|
|
stream.locations.append('US:NH:Manchester:03101:42.990605:-71.460989')
|
|
|
|
self.assertEqual(stream.locations[0].country, 'US')
|
|
|
|
self.assertEqual(stream.locations[0].state, 'NH')
|
|
|
|
self.assertEqual(stream.locations[0].city, 'Manchester')
|
|
|
|
self.assertEqual(stream.locations[0].code, '03101')
|
|
|
|
self.assertEqual(stream.locations[0].latitude, '42.990605')
|
|
|
|
self.assertEqual(stream.locations[0].longitude, '-71.460989')
|
|
|
|
|
|
|
|
# from partial string
|
|
|
|
stream = Stream()
|
|
|
|
stream.locations.append('::Manchester:03101:')
|
2019-10-02 18:58:51 +02:00
|
|
|
self.assertIsNone(stream.locations[0].country)
|
2019-03-30 01:26:10 +01:00
|
|
|
self.assertEqual(stream.locations[0].state, '')
|
|
|
|
self.assertEqual(stream.locations[0].city, 'Manchester')
|
|
|
|
self.assertEqual(stream.locations[0].code, '03101')
|
2019-10-02 18:58:51 +02:00
|
|
|
self.assertIsNone(stream.locations[0].latitude)
|
|
|
|
self.assertIsNone(stream.locations[0].longitude)
|
2019-03-30 01:26:10 +01:00
|
|
|
|
|
|
|
# from partial string lat/long
|
|
|
|
stream = Stream()
|
|
|
|
stream.locations.append('::::42.990605:-71.460989')
|
2019-10-02 18:58:51 +02:00
|
|
|
self.assertIsNone(stream.locations[0].country)
|
2019-03-30 01:26:10 +01:00
|
|
|
self.assertEqual(stream.locations[0].state, '')
|
|
|
|
self.assertEqual(stream.locations[0].city, '')
|
|
|
|
self.assertEqual(stream.locations[0].code, '')
|
|
|
|
self.assertEqual(stream.locations[0].latitude, '42.990605')
|
|
|
|
self.assertEqual(stream.locations[0].longitude, '-71.460989')
|
|
|
|
|
|
|
|
# from short circuit lat/long
|
|
|
|
stream = Stream()
|
|
|
|
stream.locations.append('42.990605:-71.460989')
|
2019-10-02 18:58:51 +02:00
|
|
|
self.assertIsNone(stream.locations[0].country)
|
2019-03-30 01:26:10 +01:00
|
|
|
self.assertEqual(stream.locations[0].state, '')
|
|
|
|
self.assertEqual(stream.locations[0].city, '')
|
|
|
|
self.assertEqual(stream.locations[0].code, '')
|
|
|
|
self.assertEqual(stream.locations[0].latitude, '42.990605')
|
|
|
|
self.assertEqual(stream.locations[0].longitude, '-71.460989')
|
|
|
|
|
|
|
|
# from json string
|
|
|
|
stream = Stream()
|
|
|
|
stream.locations.append('{"country": "ES"}')
|
|
|
|
self.assertEqual(stream.locations[0].country, 'ES')
|
|
|
|
|
|
|
|
# from dict
|
|
|
|
stream = Stream()
|
|
|
|
stream.locations.append({"country": "UA"})
|
|
|
|
self.assertEqual(stream.locations[0].country, 'UA')
|