def test_export_html(self): note3 = Note.create("55", u"<h3>blah</h3>foo", notebook_id=self.notebook.object_id) self.database.save(note3) response_headers = {} expected_notes = (self.note1, self.note2, note3) result = invoke("export", "html", self.database, self.notebook, expected_notes, response_headers) # response headers should be unchanged assert response_headers == {} notes = result.get("notes") assert len(notes) == len(expected_notes) # assert that the notes are in the expected order for (note, expected_note) in zip(notes, expected_notes): assert note.object_id == expected_note.object_id assert note.revision == expected_note.revision assert note.title == expected_note.title assert note.contents == expected_note.contents assert note.notebook_id == expected_note.notebook_id assert note.startup == expected_note.startup assert note.deleted_from_id == expected_note.deleted_from_id assert note.rank == expected_note.rank assert note.user_id == expected_note.user_id assert note.creation == expected_note.creation
def test_export_html(self): note3 = Note.create("55", u"<h3>blah</h3>foo", notebook_id=self.notebook.object_id) self.database.save(note3) response_headers = {} expected_notes = (self.note1, self.note2, note3) result = invoke( "export", "html", self.database, self.notebook, expected_notes, response_headers, ) # response headers should be unchanged assert response_headers == {} notes = result.get("notes") assert len(notes) == len(expected_notes) # assert that the notes are in the expected order for (note, expected_note) in zip(notes, expected_notes): assert note.object_id == expected_note.object_id assert note.revision == expected_note.revision assert note.title == expected_note.title assert note.contents == expected_note.contents assert note.notebook_id == expected_note.notebook_id assert note.startup == expected_note.startup assert note.deleted_from_id == expected_note.deleted_from_id assert note.rank == expected_note.rank assert note.user_id == expected_note.user_id assert note.creation == expected_note.creation
def test_export_csv( self, note_contents = None, expected_contents = None ): if not note_contents: note_contents = u"<h3>blah</h3>foo" note3 = Note.create( self.database.next_id( Note ), note_contents, notebook_id = self.notebook.object_id, user_id = self.user.object_id ) self.database.save( note3 ) response_headers = {} expected_notes = ( self.note1, self.note2, note3 ) result = invoke( "export", "csv", self.database, self.notebook, expected_notes, response_headers, ) assert response_headers assert response_headers[ u"Content-Type" ] == u"text/csv;charset=utf-8" assert response_headers[ u"Content-Disposition" ] == 'attachment; filename=%s.csv' % self.notebook.friendly_id assert isinstance( result, types.GeneratorType ) pieces = [] for piece in result: pieces.append( piece ) csv_data = "".join( pieces ) reader = csv.reader( StringIO( csv_data ) ) row = reader.next() expected_header = [ u"contents", u"title", u"note_id", u"startup", u"username", u"revision_date" ] assert row == expected_header note_count = 0 # assert that startup notes come first, then normal notes in descending revision order for row in reader: assert len( row ) == len( expected_header ) ( contents, title, note_id, startup, username, revision_date ) = row assert note_count < len( expected_notes ) expected_note = expected_notes[ note_count ] assert expected_note if expected_contents and note_id == note3.object_id: assert contents.decode( "utf8" ) == expected_contents.replace( "\n", " " ).strip() else: assert contents.decode( "utf8" ) == expected_note.contents.replace( "\n", " " ).strip() if expected_note.title: assert title.decode( "utf8" ) == expected_note.title.strip() else: assert not title assert note_id.decode( "utf8" ) == expected_note.object_id assert startup.decode( "utf8" ) == expected_note.startup and u"1" or "0" assert username.decode( "utf8" ) == ( expected_note.user_id and self.user.username or u"" ) assert revision_date.decode( "utf8" ) == unicode( expected_note.revision ) note_count += 1 assert note_count == len( expected_notes )
def test_export_csv(self, note_contents=None, expected_contents=None): if not note_contents: note_contents = u"<h3>blah</h3>foo" note3 = Note.create(self.database.next_id(Note), note_contents, notebook_id=self.notebook.object_id, user_id=self.user.object_id) self.database.save(note3) response_headers = {} expected_notes = (self.note1, self.note2, note3) result = invoke( "export", "csv", self.database, self.notebook, expected_notes, response_headers, ) assert response_headers assert response_headers[u"Content-Type"] == u"text/csv;charset=utf-8" assert response_headers[ u"Content-Disposition"] == 'attachment; filename=%s.csv' % self.notebook.friendly_id assert isinstance(result, types.GeneratorType) pieces = [] for piece in result: pieces.append(piece) csv_data = "".join(pieces) reader = csv.reader(StringIO(csv_data)) row = reader.next() expected_header = [ u"contents", u"title", u"note_id", u"startup", u"username", u"revision_date" ] assert row == expected_header note_count = 0 # assert that startup notes come first, then normal notes in descending revision order for row in reader: assert len(row) == len(expected_header) (contents, title, note_id, startup, username, revision_date) = row assert note_count < len(expected_notes) expected_note = expected_notes[note_count] assert expected_note if expected_contents and note_id == note3.object_id: assert contents.decode("utf8") == expected_contents.replace( "\n", " ").strip() else: assert contents.decode( "utf8") == expected_note.contents.replace("\n", " ").strip() if expected_note.title: assert title.decode("utf8") == expected_note.title.strip() else: assert not title assert note_id.decode("utf8") == expected_note.object_id assert startup.decode( "utf8") == expected_note.startup and u"1" or "0" assert username.decode("utf8") == (expected_note.user_id and self.user.username or u"") assert revision_date.decode("utf8") == unicode( expected_note.revision) note_count += 1 assert note_count == len(expected_notes)