Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F65040571
bibrecord_tests.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Fri, May 31, 07:37
Size
7 KB
Mime Type
text/x-python
Expires
Sun, Jun 2, 07:37 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
17993805
Attached To
R3600 invenio-infoscience
bibrecord_tests.py
View Options
## $Id$
## This file is part of the CERN Document Server Software (CDSware).
## Copyright (C) 2002 CERN.
##
## The CDSware is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License as
## published by the Free Software Foundation; either version 2 of the
## License, or (at your option) any later version.
##
## The CDSware is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with CDSware; if not, write to the Free Software Foundation, Inc.,
## 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
<
protect
>
# -*- coding: utf-8 -*-</protect>
from
config
import
tmpdir
import
bibrecord
import
unittest
from
string
import
expandtabs
,
replace
class
SanityTest
(
unittest
.
TestCase
):
### check for sanity -- xml -> create_record -> xml
def
test_for_sanity
(
self
):
""" bibrecord - checking for sanity """
f
=
open
(
tmpdir
+
'/demobibdata.xml'
,
'r'
)
xmltext
=
f
.
read
()
f
.
close
()
rs
=
bibrecord
.
create_records
(
xmltext
)
recs
=
map
((
lambda
x
:
x
[
0
]),
rs
)
xmlT
=
bibrecord
.
records_xml_output
(
recs
)
x
=
xmlT
.
replace
(
'
\n
'
,
''
)
y
=
xmltext
.
replace
(
'
\n
'
,
''
)
xx
=
expandtabs
(
x
)
yy
=
expandtabs
(
y
)
xxx
=
xx
.
replace
(
' '
,
''
)
yyy
=
yy
.
replace
(
' '
,
''
)
#self.assertEqual(xxx,yyy)
### testing for success
class
SuccessTest
(
unittest
.
TestCase
):
""" bibrecord - testing for success """
def
setUp
(
self
):
f
=
open
(
tmpdir
+
'/demobibdata.xml'
,
'r'
)
xmltext
=
f
.
read
()
f
.
close
()
self
.
recs
=
map
((
lambda
x
:
x
[
0
]),
bibrecord
.
create_records
(
xmltext
))
def
test_records_created
(
self
):
""" bibrecord - number of records created """
## check if it creates every records (the file demobibdata.xml has 75 records)
self
.
assertEqual
(
75
,
len
(
self
.
recs
))
def
test_tags_created
(
self
):
""" bibrecord - tags created """
## check if the tags are correct
tags
=
[
'020'
,
'037'
,
'041'
,
'080'
,
'088'
,
'100'
,
'245'
,
'246'
,
'250'
,
'260'
,
'270'
,
'300'
,
'340'
,
'490'
,
'500'
,
'502'
,
'520'
,
'590'
,
'595'
,
'650'
,
'653'
,
'690'
,
'700'
,
'710'
,
'856'
,
'909'
,
'980'
,
'999'
]
t
=
[]
for
rec
in
self
.
recs
:
t
.
extend
(
rec
.
keys
())
t
.
sort
()
#eliminate the elements repeated
tt
=
[]
for
x
in
t
:
if
not
x
in
tt
:
tt
.
append
(
x
)
self
.
assertEqual
(
tags
,
tt
)
def
test_fields_created
(
self
):
"""bibrecord - fields created"""
## check if the number of fields for each record is correct
fields
=
[
13
,
13
,
8
,
11
,
10
,
12
,
10
,
14
,
10
,
17
,
13
,
15
,
10
,
9
,
14
,
10
,
11
,
11
,
11
,
9
,
10
,
10
,
10
,
8
,
8
,
8
,
9
,
9
,
9
,
10
,
8
,
8
,
8
,
8
,
14
,
13
,
14
,
14
,
15
,
12
,
12
,
12
,
14
,
13
,
11
,
15
,
15
,
14
,
14
,
13
,
15
,
14
,
14
,
14
,
15
,
14
,
15
,
14
,
14
,
15
,
14
,
13
,
13
,
14
,
11
,
13
,
11
,
14
,
8
,
10
,
13
,
12
,
11
,
12
,
6
]
cr
=
[]
ret
=
[]
for
rec
in
self
.
recs
:
cr
.
append
(
len
(
rec
.
values
()))
ret
.
append
(
rec
)
self
.
assertEqual
(
fields
,
cr
)
class
BadInputTreatmentTest
(
unittest
.
TestCase
):
""" bibrecord - testing for bad input treatment """
### check bad input treatment ###
def
test_wrong_attribute
(
self
):
"""bibrecord - bad input : Has \'cde\' instead \'code\' in a subfield attribute"""
ws
=
bibrecord
.
cfg_bibrecord_warning_msgs
xml_error1
=
"""
<record>
<controlfield tag="001">33</controlfield>
<datafield tag="041" ind1="" ind2="">
<subfield code="a">eng</subfield>
</datafield>
<datafield tag="100" ind1="" ind2="">
<subfield code="a">Doe, John</subfield>
</datafield>
<datafield tag="245" ind1="" ind2="">
<subfield cde="a">On the foo and bar</subfield>
</datafield>
</record>
"""
(
rec
,
st
,
e
)
=
bibrecord
.
create_record
(
xml_error1
,
1
,
1
)
ee
=
''
for
i
in
e
:
if
type
(
i
)
.
__name__
==
'str'
:
if
i
.
count
(
ws
[
3
])
>
0
:
ee
=
i
self
.
assertEqual
(
bibrecord
.
warning
((
3
,
'(field number: 4)'
)),
ee
)
def
test_missing_attribute
(
self
):
""" bibrecord - bad input : Missing attribute \"tag\" """
ws
=
bibrecord
.
cfg_bibrecord_warning_msgs
xml_error2
=
"""
<record>
<controlfield tag="001">33</controlfield>
<datafield ind1="" ind2="">
<subfield code="a">eng</subfield>
</datafield>
<datafield tag="100" ind1="" ind2="">
<subfield code="a">Doe, John</subfield>
</datafield>
<datafield tag="245" ind1="" ind2="">
<subfield code="a">On the foo and bar</subfield>
</datafield>
</record>
"""
(
rec
,
st
,
e
)
=
bibrecord
.
create_record
(
xml_error2
,
1
,
1
)
ee
=
''
for
i
in
e
:
if
type
(
i
)
.
__name__
==
'str'
:
if
i
.
count
(
ws
[
1
])
>
0
:
ee
=
i
self
.
assertEqual
(
bibrecord
.
warning
((
1
,
'(field number(s): [2])'
)),
ee
)
def
test_empty_datafield
(
self
):
""" bibrecord - bad input : Datafield without any subfield """
ws
=
bibrecord
.
cfg_bibrecord_warning_msgs
xml_error3
=
"""
<record>
<controlfield tag="001">33</controlfield>
<datafield tag="041" ind1="" ind2="">
</datafield>
<datafield tag="100" ind1="" ind2="">
<subfield code="a">Doe, John</subfield>
</datafield>
<datafield tag="245" ind1="" ind2="">
<subfield code="a">On the foo and bar</subfield>
</datafield>
</record>
"""
(
rec
,
st
,
e
)
=
bibrecord
.
create_record
(
xml_error3
,
1
,
1
)
ee
=
''
for
i
in
e
:
if
type
(
i
)
.
__name__
==
'str'
:
if
i
.
count
(
ws
[
8
])
>
0
:
ee
=
i
self
.
assertEqual
(
bibrecord
.
warning
((
8
,
'(field number: 2)'
)),
ee
)
def
test_missing_tag
(
self
):
"""bibrecord - bad input : Missing end \"tag\"""
"
ws
=
bibrecord
.
cfg_bibrecord_warning_msgs
xml_error4
=
"""
<record>
<controlfield tag="001">33</controlfield>
<datafield tag="041" ind1="" ind2="">
<subfield code="a">eng</subfield>
</datafield>
<datafield tag="100" ind1="" ind2="">
<subfield code="a">Doe, John</subfield>
</datafield>
<datafield tag="245" ind1="" ind2="">
<subfield code="a">On the foo and bar</subfield>
</record>
"""
(
rec
,
st
,
e
)
=
bibrecord
.
create_record
(
xml_error4
,
1
,
1
)
ee
=
''
for
i
in
e
:
if
type
(
i
)
.
__name__
==
'str'
:
if
i
.
count
(
ws
[
99
])
>
0
:
ee
=
i
self
.
assertEqual
(
bibrecord
.
warning
((
99
,
'(Tagname : datafield)'
)),
ee
)
def
create_test_suite
():
"""Return test suite for the bibrecord module"""
return
unittest
.
TestSuite
((
unittest
.
makeSuite
(
SanityTest
,
'test'
),
unittest
.
makeSuite
(
SuccessTest
,
'test'
),
unittest
.
makeSuite
(
BadInputTreatmentTest
,
'test'
)))
if
__name__
==
'__main__'
:
unittest
.
TextTestRunner
(
verbosity
=
2
)
.
run
(
create_test_suite
())
Event Timeline
Log In to Comment