Skip to content

Commit

Permalink
tests and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
matijakolaric committed Jun 17, 2020
1 parent b9607f3 commit 0d0bf67
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 45 deletions.
34 changes: 12 additions & 22 deletions music_publisher/royalty_calculation.py
Expand Up @@ -62,20 +62,20 @@ def get_right_types():
help_text='Select the column containing work IDs.'
)
work_id_source = forms.ChoiceField(
choices=get_id_sources, label='Work ID Source',
choices=get_id_sources, initial=settings.PUBLISHER_CODE,
label='Work ID Source',
help_text='You or CWR acknowledgement source.',
initial=settings.PUBLISHER_CODE
)
amount_column = forms.ChoiceField(
choices=[], label='Amount', initial=-1,
help_text='Select the column containing received amount.'
)
right_type_column = forms.ChoiceField(
choices=get_right_types, label='Right type',
choices=get_right_types, initial='p', label='Right type',
help_text='Select the right type or the column specifying it.'
)
algo = forms.ChoiceField(
choices=ALGOS, label='Algorithm type',
choices=ALGOS, initial='fee', label='Algorithm type',
help_text='Choose the algorithm type, see user manual for details.'
)
default_fee = forms.DecimalField(
Expand All @@ -96,8 +96,6 @@ def is_valid(self):
self.fields['amount_column'].choices.append((str(i), field))
valid = super().is_valid()
except Exception as e:
if settings.DEBUG:
raise
return False
return valid

Expand All @@ -117,15 +115,10 @@ def __init__(self, form):
self.rc = int(self.right)
self.right = None
self.ac = int(form.cleaned_data.get('amount_column'))
self._fieldnames = []
self.writer_ids = set()
self.writers = {}
self.works = defaultdict(list)


def __str__(self):
return self.filename

@property
def filename(self):
"""Return the filename of the output file."""
Expand All @@ -135,30 +128,28 @@ def filename(self):
@property
def fieldnames(self):
"""Return the list of field names in the output file."""
if self._fieldnames:
return self._fieldnames
self.file.seek(0)
csv_reader = csv.DictReader(self.file)
self._fieldnames = csv_reader.fieldnames
fieldnames = csv_reader.fieldnames
if self.algo == 'share':
self._fieldnames.append('Right Type')
self._fieldnames += [
fieldnames.append('Right Type')
fieldnames += [
'Controlled by publisher (%)',
'Interested party',
'Role']
if self.algo == 'fee':
self._fieldnames += [
fieldnames += [
'Manuscript share (%)',
'Share in amount received (%)',
'Amount before fee',
'Fee (%)',
'Fee amount']
elif self.algo == 'share':
self._fieldnames += [
fieldnames += [
'Owned Share (%)',
'Share in amount received (%)']
self._fieldnames.append('Net amount')
return self._fieldnames
fieldnames.append('Net amount')
return fieldnames

def get_work_ids(self):
self.file.seek(0)
Expand Down Expand Up @@ -280,7 +271,7 @@ def process_row(self, row):
fee = (line['fee'] or writer['fee'] or self.default_fee) / 100
out_row.append('{}'.format(fee))
fee_amount = amount_before_fee * fee
out_row.append('{}'.format(fee_amount))
out_row.append('{}'.format(fee_amount or '0'))
net_amount = amount_before_fee - fee_amount
out_row.append('{}'.format(net_amount))

Expand Down Expand Up @@ -326,7 +317,6 @@ def out_file_path(self):
csv_writer.writerow(self.fieldnames)
for row in csv_reader:
for out_row in self.process_row(row):
print(out_row)
csv_writer.writerow(out_row)
f.filename = self.filename
f.close()
Expand Down
5 changes: 2 additions & 3 deletions music_publisher/templatetags/dmp_dashboard.py
Expand Up @@ -27,12 +27,11 @@ def dmp_model_groups(model_list):
model_dict = OrderedDict([(el['object_name'], el) for el in model_list])
sections = {
'Musical Works': [
'Work', 'Publisher', 'Writer', 'CWRExport', 'ACKImport', 'DataImport', 'RoyaltyCalculation'],
'Work', 'Publisher', 'Writer', 'CWRExport', 'ACKImport',
'DataImport', 'RoyaltyCalculation'],
'Recordings': ['Recording', 'Artist', 'Label'],
'Releases': [
'CommercialRelease', 'LibraryRelease', 'Library']
}
# Works
yield from yield_sections(model_dict, sections)
if model_dict:
yield {'name': 'Other', 'models': model_dict.values()}
6 changes: 3 additions & 3 deletions music_publisher/tests/dataimport.csv
@@ -1,4 +1,4 @@
Work ID,Work Title,ISWC,Original Title,Alt Title 1,Alt Title 2,Alt Title 3,Alt Title 4,Alt Title 5,Library,CD Identifier,Writer 1 Last,Writer 1 First,Writer 1 IPI,Writer 1 PRO,Writer 1 Role,Writer 1 Share,Writer 1 Controlled,Writer 1 SAAN,Writer 2 Last,Writer 2 First,Writer 2 IPI,Writer 2 PRO,Writer 2 Role,Writer 2 Share,Writer 2 Controlled,Writer 2 SAAN,Writer 3 Last,Writer 3 First,Writer 3 IPI,Writer 3 PRO,Writer 3 Role,Writer 3 Share,Writer 3 Controlled,Writer 3 SAAN,Writer 4 Last,Writer 4 First,Writer 4 IPI,Writer 4 PRO,Writer 4 Role,Writer 4 Share,Writer 4 Controlled,Writer 4 SAAN,Writer 5 Last,Writer 5 First,Writer 5 IPI,Writer 5 PRO,Writer 5 Role,Writer 5 Share,Writer 5 Controlled,Writer 5 SAAN,Artist 1 Last,Artist 1 First,Artist 1 ISNI,Artist 2 Last,Artist 2 First,Artist 2 ISNI,Artist 3 Last,Artist 3 First,Artist 3 ISNI,Artist 4 Last,Artist 4 First,Artist 4 ISNI,Artist 5 Last,Artist 5 First,Artist 5 ISNI
X123,Simple Original,T-123.456.789-4,,Simply Original,,,,,Library,Internet,Writer,Test,199,10 – ASCAP,CA – Composer&Lyricist,100%,Yes,GGGGGG,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
X125,Simple With Artist,,,,,,,,,,Writer,Test,199,10 – ASCAP,CA – Composer&Lyricist,0.6667,General,AAAAAA,Author,Test,591,10 – ASCAP,CA – Composer&Lyricist,33.33%,No,,,,,,,,,,,,,,,,,,,,,,,,,,Artist,Test,,,,,,,,,,,,,
,Simple With Unknown Artist,,,,,,,,,,Writer,Test,199,10 – ASCAP,CA – Composer&Lyricist,0.6667,General,AAAAAA,,,,,CA – Composer&Lyricist,23.33%,No,,,,,,CA,0.1,False,,,,,,,,,,,,,,,,,,Artist,Test,,,,,,,,,,,,,
X123,Simple Original,T-123.456.789-4,,Simply Original,,,,,Library,Internet,Writer,,199,10 – ASCAP,CA – Composer&Lyricist,100%,Yes,GGGGGG,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
X125,Simple With Artist,,,,,,,,,,Writer,,199,10 – ASCAP,CA – Composer&Lyricist,0.6667,General,AAAAAA,Author,Test,591,10 – ASCAP,CA – Composer&Lyricist,33.33%,No,,,,,,,,,,,,,,,,,,,,,,,,,,Artist,Test,,,,,,,,,,,,,
,Simple With Unknown Artist,,,,,,,,,,Writer,,199,10 – ASCAP,CA – Composer&Lyricist,0.6667,General,AAAAAA,,,,,CA – Composer&Lyricist,23.33%,No,,,,,,CA,0.1,False,,,,,,,,,,,,,,,,,,Artist,Test,,,,,,,,,,,,,
7 changes: 5 additions & 2 deletions music_publisher/tests/royaltystatement.csv
@@ -1,3 +1,6 @@
Something,Work ID,Something Else,Right,"Something, Again",Amount,Nonsense
"A title, maybe, must be long, really long",X126,Who knows?,PR,100.51Really?,100.51,Really
"A title, maybe, must be long, really long",X125,Who knows?,PR,100.51Really?,100.51,Really
"A title, maybe, must be long, really long",X123,Who knows?,PR,100.51Really?,100.51,Really
"A title, maybe, must be long, really long",X125,Who knows?,PR,99.49Really?,99.49,Really
"A title, maybe, must be long, really long",X125,Who knows?,PR,,100.51,Really
"A title, maybe, must be long, really long",337739ES,Who knows?,PR,,100.00,Really
"A title, maybe, must be long, really long",T-123.456.789-4,Who knows?,PR,,100,Really
107 changes: 97 additions & 10 deletions music_publisher/tests/tests.py
Expand Up @@ -13,16 +13,14 @@
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.template import Context
from django.test import (
SimpleTestCase, TestCase, TransactionTestCase, override_settings,
)
override_settings, SimpleTestCase, TestCase, TransactionTestCase)
from django.urls import reverse

import music_publisher.models
from music_publisher.models import (
AlternateTitle, Artist, CWRExport, CommercialRelease, Label, Library,
LibraryRelease, Recording, Release, Work, Writer, WriterInWork, Track
)
from music_publisher import cwr_templates, validators, dataimport
from music_publisher import cwr_templates, dataimport, validators
from music_publisher.models import (AlternateTitle, Artist, CommercialRelease,
CWRExport, Label, Library, LibraryRelease, Recording, Release, Track, Work,
Writer, WriterInWork)


def get_data_from_response(response):
Expand Down Expand Up @@ -234,7 +232,7 @@ def test_unknown_key_exceptions(self):
REQUIRE_PUBLISHER_FEE=True,
PUBLISHING_AGREEMENT_PUBLISHER_PR=Decimal('0.333333'),
PUBLISHING_AGREEMENT_PUBLISHER_MR=Decimal('0.5'),
PUBLISHING_AGREEMENT_PUBLISHER_SR=Decimal('0.75'))
PUBLISHING_AGREEMENT_PUBLISHER_SR=Decimal('1.0'))
class AdminTest(TestCase):
"""Functional tests on the interface, and several related unit tests."""
fixtures = ['publishing_staff.json']
Expand Down Expand Up @@ -455,6 +453,9 @@ def test_unknown_user(self):
args=(1,))
response = self.client.get(url, follow=False)
self.assertEqual(response.status_code, 302)
url = reverse('royalty_calculation')
response = self.client.get(url, follow=False)
self.assertEqual(response.status_code, 302)

def test_super_user(self):
"""Testing index for superuser covers all the cases."""
Expand Down Expand Up @@ -500,6 +501,9 @@ def test_staff_user(self):
response = self.client.post(
url, data=data, follow=False)
self.assertEqual(response.status_code, 302)
url = reverse('royalty_calculation')
response = self.client.get(url, follow=False)
self.assertEqual(response.status_code, 200)

def test_cwr_previews(self):
"""Test that CWR preview works."""
Expand Down Expand Up @@ -688,6 +692,7 @@ def test_generally_controlled_missing_capacity(self):
response = self.client.get(url, follow=False)
data = get_data_from_response(response)
data['writerinwork_set-0-capacity'] = ''
data['writerinwork_set-0-capacity'] = ''
response = self.client.post(url, data)
self.assertEqual(response.status_code, 200)
self.assertIn(
Expand Down Expand Up @@ -1084,9 +1089,22 @@ def test_ack_import_and_work_filters(self):
ackimport = music_publisher.models.ACKImport.objects.first()
self.assertIsNotNone(ackimport)

# required after acknowledgement imports because it lists
# ack sources
url = reverse('royalty_calculation')
response = self.client.get(url, follow=False)
self.assertEqual(response.status_code, 200)
data = get_data_from_response(response)



@override_settings(REQUIRE_SAAN=False, REQUIRE_PUBLISHER_FEE=False)
def test_data_import(self):
"""Test data import."""
def test_data_import_and_royalty_calculations(self):
"""Test data import, ack import and royalty calculations.
This is the normal process, work data is entered, then the registration
follows and then it can be processed in royalty statements.
"""

self.client.force_login(self.superuser)
with open(TEST_DATA_IMPORT_FILENAME) as csvfile:
Expand All @@ -1110,6 +1128,74 @@ def test_data_import(self):
response = self.client.get(url, follow=False)
self.assertEqual(response.status_code, 200)

self.client.force_login(self.staffuser)

mock = StringIO()
mock.write(ACK_CONTENT_21_EXT)
mock.seek(0)
mockfile = InMemoryUploadedFile(
mock, 'acknowledgement_file', 'CW180001000_FOO.V21',
'text', 0, None)
url = reverse('admin:music_publisher_ackimport_add')
response = self.client.get(url)
data = get_data_from_response(response)
data.update({'acknowledgement_file': mockfile})
response = self.client.post(url, data, follow=False)
self.assertEqual(response.status_code, 302)

with open(TEST_ROYALTY_PROCESSING_FILENAME) as csvfile:
mock = StringIO()
mock.write(csvfile.read())
mock.seek(0)
mockfile = InMemoryUploadedFile(
mock, 'statement_file', 'statement.csv',
'text', 0, None)
url = reverse('royalty_calculation')
response = self.client.get(url)
data = get_data_from_response(response)
data.update({'in_file': mockfile})
# Not enough data
response = self.client.post(url, data, follow=False)
self.assertFalse(hasattr(response, 'streaming_content'))

# Enough data, fee, p
mock.seek(0)
data.update({
'work_id_column': '1',
'work_id_source': '52',
'amount_column': '5',
})
response = self.client.post(url, data, follow=False)
self.assertTrue(hasattr(response, 'streaming_content'))

# Enough data, share, with rights column, ISWC
mock.seek(0)
data.update({
'algo': 'share',
'right_type_column': '3',
'work_id_source': 'ISWC',
})
response = self.client.post(url, data, follow=False)
self.assertTrue(hasattr(response, 'streaming_content'))

# Enough data, fee, sync
mock.seek(0)
data.update({
'right_type_column': 's',
'work_id_source': 'MK',
})
response = self.client.post(url, data, follow=False)
self.assertTrue(hasattr(response, 'streaming_content'))

# Enough data, fee, sync
mock.seek(0)
data.update({
'right_type_column': 'm',
'work_id_source': 'MK',
})
response = self.client.post(url, data, follow=False)
self.assertTrue(hasattr(response, 'streaming_content'))

@override_settings(REQUIRE_SAAN=False, REQUIRE_PUBLISHER_FEE=False)
def test_bad_data_import(self):
"""Test bad data import."""
Expand Down Expand Up @@ -1677,6 +1763,7 @@ def test_work(self):
TRL000010000001000000005"""

TEST_DATA_IMPORT_FILENAME = 'music_publisher/tests/dataimport.csv'
TEST_ROYALTY_PROCESSING_FILENAME = 'music_publisher/tests/royaltystatement.csv'
TEST_CWR2_FILENAME = 'music_publisher/tests/CW200001DMP_000.V21'
TEST_CWR3_FILENAME = 'music_publisher/tests/CW200002DMP_0000_V3-0-0.SUB'
TEST_ISR_FILENAME = 'music_publisher/tests/CW200003DMP_0000_V3-0-0.ISR'
10 changes: 5 additions & 5 deletions publishing_staff.json
Expand Up @@ -219,11 +219,6 @@
"music_publisher",
"release"
],
[
"add_royaltycalculation",
"music_publisher",
"royaltycalculation"
],
[
"add_track",
"music_publisher",
Expand All @@ -249,6 +244,11 @@
"music_publisher",
"work"
],
[
"can_process_royalties",
"music_publisher",
"work"
],
[
"change_work",
"music_publisher",
Expand Down

0 comments on commit 0d0bf67

Please sign in to comment.