first working paper test + bugfix

main
Fabien Benureau 11 years ago
parent 9243859294
commit dfd16c029d

@ -28,9 +28,11 @@ class Paper(object):
if self.metadata is None: if self.metadata is None:
self.metadata = copy.deepcopy(DEFAULT_META) self.metadata = copy.deepcopy(DEFAULT_META)
if self.citekey is None: if self.citekey is None:
self.citekey = bibstruct.extract_citekey(self.bibdata) self.citekey = bibstruct.extract_citekey(self.bibdata)
bibstruct.check_citekey(self.citekey) bibstruct.check_citekey(self.citekey)
self.metadata['tags'] = set(self.metadata.get('tags', []))
def __eq__(self, other): def __eq__(self, other):
return (isinstance(self, Paper) and type(other) is type(self) return (isinstance(self, Paper) and type(other) is type(self)
and self.bibdata == other.bibdata and self.bibdata == other.bibdata
@ -61,7 +63,7 @@ class Paper(object):
@property @property
def tags(self): def tags(self):
return self.metadata.setdefault('tags', set()) return self.metadata['tags']
@tags.setter @tags.setter
def tags(self, value): def tags(self, value):

@ -23,5 +23,6 @@ doe_bib = """
franny_bibdata = coder.decode_bibdata(franny_bib) franny_bibdata = coder.decode_bibdata(franny_bib)
doe_bibdata = coder.decode_bibdata(doe_bib) doe_bibdata = coder.decode_bibdata(doe_bib)
page_bibdata = coder.decode_bibdata(str_fixtures.bibtex_raw0)
turing_bibdata = coder.decode_bibdata(str_fixtures.turing_bib) turing_bibdata = coder.decode_bibdata(str_fixtures.turing_bib)
page_bibdata = coder.decode_bibdata(str_fixtures.bibtex_raw0)
page_metadata = coder.decode_metadata(str_fixtures.metadata_raw0)

@ -99,7 +99,7 @@ bibtex_raw0 = """@techreport{
""" """
metadata_raw0 = """docfile: null metadata_raw0 = """docfile: docsdir://Page99.pdf
tags: [search, network] tags: [search, network]
added: '2013-11-14 13:14:20' added: '2013-11-14 13:14:20'
""" """

@ -1,106 +1,16 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os import os
import unittest import unittest
import tempfile
import shutil
import yaml
from pybtex.database import Person
import testenv import testenv
import fixtures import fixtures
from papers.paper import Paper from pubs.paper import Paper
BIB = """
entries:
Turing1950:
author:
- first: 'Alan'
last: 'Turing'
title: 'Computing machinery and intelligence.'
type: article
year: '1950'
"""
META = """
external-document: null
notes: []
tags: ['AI', 'computer']
"""
class TestCreateCitekey(unittest.TestCase):
def test_fails_on_empty_paper(self):
paper = Paper()
with self.assertRaises(ValueError):
paper.generate_citekey()
class TestSaveLoad(unittest.TestCase):
def setUp(self):
self.tmpdir = tempfile.mkdtemp()
os.makedirs(os.path.join(self.tmpdir, 'bibdata'))
os.makedirs(os.path.join(self.tmpdir, 'meta'))
self.bibfile = os.path.join(self.tmpdir, 'bib.bibyaml')
with open(self.bibfile, 'w') as f:
f.write(BIB)
self.metafile = os.path.join(self.tmpdir, 'meta.meta')
with open(self.metafile, 'w') as f:
f.write(META)
self.dest_bibfile = os.path.join(self.tmpdir, 'written_bib.yaml')
self.dest_metafile = os.path.join(self.tmpdir, 'written_meta.yaml')
def test_load_valid(self):
p = Paper.load(self.bibfile, metapath=self.metafile)
self.assertEqual(fixtures.turing1950, p)
def test_save_fails_with_no_citekey(self):
p = Paper()
with self.assertRaises(ValueError):
p.save(self.dest_bibfile, self.dest_metafile)
def test_save_creates_bib(self):
fixtures.turing1950.save(self.dest_bibfile, self.dest_metafile)
self.assertTrue(os.path.exists(self.dest_bibfile))
def test_save_creates_meta(self):
fixtures.turing1950.save(self.dest_bibfile, self.dest_metafile)
self.assertTrue(os.path.exists(self.dest_metafile))
def test_save_right_bib(self):
fixtures.turing1950.save(self.dest_bibfile, self.dest_metafile)
with open(self.dest_bibfile, 'r') as f:
written = yaml.load(f)
ok = yaml.load(BIB)
self.assertEqual(written, ok)
def test_save_right_meta(self):
fixtures.turing1950.save(self.dest_bibfile, self.dest_metafile)
with open(self.dest_metafile, 'r') as f:
written = yaml.load(f)
ok = yaml.load(META)
self.assertEqual(written, ok)
def tearDown(self):
shutil.rmtree(self.tmpdir)
class TestCopy(unittest.TestCase): class TestAttributes(unittest.TestCase):
def setUp(self): def test_tags(self):
self.orig = Paper() p = Paper(fixtures.page_bibdata, metadata=fixtures.page_metadata)
self.orig.bibentry.fields['title'] = u'Nice title.' self.assertEqual(p.tags, set(['search', 'network']))
self.orig.bibentry.fields['year'] = u'2013'
self.orig.bibentry.persons['author'] = [Person(u'John Doe')]
self.orig.citekey = self.orig.generate_citekey()
def test_copy_equal(self):
copy = self.orig.copy()
self.assertEqual(copy, self.orig)
def test_copy_can_be_changed(self):
copy = self.orig.copy()
copy.bibentry.fields['year'] = 2014
self.assertEqual(self.orig.bibentry.fields['year'], u'2013')

Loading…
Cancel
Save