Skip to content

Commit

Permalink
Fix tests for new TextStyle abstraction.
Browse files Browse the repository at this point in the history
  • Loading branch information
danizen committed Mar 12, 2021
1 parent 341b7d8 commit 54e21d5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
4 changes: 2 additions & 2 deletions pymarcspec/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def search(self, record, style=None):
results = [style.field_text(value) for value in results]
elif isinstance(self.filter, IndicatorFilter):
results = [
field.indicators[self.filter.indicator]
field.indicator1 if self.filter.indicator == 1 else field.indicator2
for field in results
]
elif isinstance(self.filter, list):
Expand All @@ -163,5 +163,5 @@ def search(self, record, style=None):
if style:
results = [style.subfield_text(values) for values in results]
if style:
return style.join(results)
return style.fields_join(results)
return results
8 changes: 7 additions & 1 deletion pymarcspec/textstyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def field_text(self, field_or_text):
def subfield_text(self, values):
return values

def join(self, results):
def fields_join(self, results):
return results


Expand All @@ -33,3 +33,9 @@ def subfield_text(self, values):
return self.subfield_delimiter.join(values)
else:
return values

def fields_join(self, results):
if self.join_fields:
return self.field_delimiter.join(results)
else:
return results
28 changes: 16 additions & 12 deletions test/test_search.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from pymarc import Field
from pymarcspec.model import (
MarcSpec,
IndicatorFilter,
Expand All @@ -11,18 +12,21 @@
def test_fieldspec_leader(jama_record):
spec = MarcSpec(tag='LDR', filter=FieldFilter())
leader = spec.search(jama_record)
assert leader == '03244cas a2200721 4500'
assert leader == ['03244cas a2200721 4500']


def test_fieldspec_subject(jama_record):
spec = MarcSpec(tag='650', filter=FieldFilter())
subject = spec.search(jama_record)
assert subject == 'Medicine https://id.nlm.nih.gov/mesh/D008511'
subjects = spec.search(jama_record)
assert len(subjects) == 1
assert isinstance(subjects[0], Field)
assert subjects[0].tag == '650'
assert subjects[0].get_subfields('a') == ['Medicine']


def test_fieldspec_subjects_dot(jama_record):
spec = MarcSpec(tag='65.', filter=FieldFilter())
subjects = spec.search(jama_record, totext=False)
subjects = spec.search(jama_record)
assert len(subjects) == 2
assert subjects[0].tag == '650'
assert subjects[1].tag == '655'
Expand All @@ -31,25 +35,25 @@ def test_fieldspec_subjects_dot(jama_record):
def test_subfield_subjects_a(jama_record):
spec = MarcSpec(tag='65.', filter=[SubfieldFilter(start='a')])
subjects = spec.search(jama_record)
assert subjects == 'Medicine:Periodical'
assert subjects == [['Medicine'], ['Periodical']]


def test_indicators_two(jama_record):
spec = MarcSpec(tag='65.', filter=IndicatorFilter(indicator=2))
indicator2_values = spec.search(jama_record)
assert indicator2_values == '2:2'
assert indicator2_values == ['2', '2']


def test_fieldspec_index_first(jama_record):
spec = MarcSpec(tag='65.', filter=FieldFilter(index=IndexSpec(start=0)))
subjects = spec.search(jama_record, totext=False)
subjects = spec.search(jama_record)
assert len(subjects) == 1
assert subjects[0].tag == '650'


def test_fieldspec_index_last(jama_record):
spec = MarcSpec(tag='65.', filter=FieldFilter(index=IndexSpec(start='#', end=1)))
subjects = spec.search(jama_record, totext=False)
subjects = spec.search(jama_record)
assert len(subjects) == 1
assert subjects[0].tag == '655'

Expand All @@ -59,24 +63,24 @@ def test_subfield_index_first(jama_record):
SubfieldFilter(start='a', index=IndexSpec(start=0)),
])
subject = spec.search(jama_record)
assert subject == 'Medicine'
assert subject == [['Medicine']]


def test_subfield_index_first(jama_record):
spec = MarcSpec(tag='65.', filter=[
SubfieldFilter(start='a', index=IndexSpec(start='#', end=1)),
])
subject = spec.search(jama_record)
assert subject == 'Periodical'
assert subject == [['Periodical']]


def test_field5_range(jama_record):
spec = MarcSpec(tag='005', filter=FieldFilter(cspec=CharSpec(start=0, end=8)))
update_date = spec.search(jama_record)
assert update_date == '20201127'
assert update_date == ['20201127']


def test_leader_range(jama_record):
spec = MarcSpec(tag='LDR', filter=FieldFilter(cspec=CharSpec(start=9)))
encoding = spec.search(jama_record)
assert encoding == 'a'
assert encoding == ['a']

0 comments on commit 54e21d5

Please sign in to comment.