Пример #1
0
class Test_export_html(object):
    def setUp(self):
        self.database = Database(
            Connection_wrapper(
                sqlite.connect(":memory:", detect_types=sqlite.PARSE_DECLTYPES, check_same_thread=False)
            ),
            cache=Stub_cache(),
        )
        self.database.execute_script(file("model/schema.sqlite").read(), commit=True)

        self.username = u"mulder"
        self.password = u"trustno1"
        self.email_address = u"*****@*****.**"
        self.user = User.create(self.database.next_id(User), self.username, self.password, self.email_address)
        self.database.save(self.user, commit=False)

        self.trash = Notebook.create(self.database.next_id(Notebook), u"trash")
        self.database.save(self.trash, commit=False)
        self.notebook = Notebook.create(
            self.database.next_id(Notebook), u"notebook", self.trash.object_id, user_id=self.user.object_id
        )
        self.database.save(self.notebook, commit=False)

        note_id = self.database.next_id(Note)
        self.note1 = Note.create(
            note_id,
            u"<h3>my title</h3>blah",
            notebook_id=self.notebook.object_id,
            startup=True,
            user_id=self.user.object_id,
        )
        self.database.save(self.note1, commit=False)

        note_id = self.database.next_id(Note)
        self.note2 = Note.create(
            note_id, u"<h3>other title</h3>whee", notebook_id=self.notebook.object_id, user_id=self.user.object_id
        )
        self.database.save(self.note2, commit=False)

    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
Пример #2
0
class Test_database( object ):
  def setUp( self ):
    # make an in-memory sqlite database to use during testing
    self.connection = Connection_wrapper( sqlite.connect( ":memory:", detect_types = sqlite.PARSE_DECLTYPES, check_same_thread = False ) )
    self.cache = Stub_cache()
    cursor = self.connection.cursor()
    cursor.execute( Stub_object.sql_create_table() )

    self.database = Database( self.connection, self.cache )

  def tearDown( self ):
    self.database.close()

  def test_save_and_load( self ):
    basic_obj = Stub_object( object_id = "5", value = 1 )
    original_revision = basic_obj.revision

    self.database.save( basic_obj )
    obj = self.database.load( Stub_object, basic_obj.object_id )

    assert obj.object_id == basic_obj.object_id
    assert obj.revision.replace( tzinfo = utc ) == original_revision
    assert obj.value == basic_obj.value

  def test_save_and_load_without_commit( self ):
    basic_obj = Stub_object( object_id = "5", value = 1 )
    original_revision = basic_obj.revision

    self.database.save( basic_obj, commit = False )
    self.connection.rollback() # if commit wasn't called, this should back out the save
    obj = self.database.load( Stub_object, basic_obj.object_id )

    assert obj == None

  def test_save_and_load_with_explicit_commit( self ):
    basic_obj = Stub_object( object_id = "5", value = 1 )
    original_revision = basic_obj.revision

    self.database.save( basic_obj, commit = False )
    self.database.commit()
    self.connection.rollback() # should have no effect because of the call to commit
    obj = self.database.load( Stub_object, basic_obj.object_id )

    assert obj.object_id == basic_obj.object_id
    assert obj.revision.replace( tzinfo = utc ) == original_revision
    assert obj.value == basic_obj.value

  def test_select_one( self ):
    basic_obj = Stub_object( object_id = "5", value = 1 )
    original_revision = basic_obj.revision

    self.database.save( basic_obj )
    obj = self.database.select_one( Stub_object, Stub_object.sql_load( basic_obj.object_id ) )

    assert obj.object_id == basic_obj.object_id
    assert obj.revision.replace( tzinfo = utc ) == original_revision
    assert obj.value == basic_obj.value

  def test_select_datetime( self ):
    # this revision (with .504099) happens to test for a bug caused by floating point rounding errors
    original_revision = "2008-01-01 01:00:42.504099+00:00"
    basic_obj = Stub_object( object_id = "5", revision = original_revision, value = 1 )

    self.database.save( basic_obj )
    obj = self.database.select_one( Stub_object, Stub_object.sql_load( basic_obj.object_id ) )

    assert obj.object_id == basic_obj.object_id
    assert str( obj.revision.replace( tzinfo = utc ) ) == original_revision
    assert obj.value == basic_obj.value

  def test_select_datetime_with_many_fractional_digits( self ):
    original_revision = "2008-01-01 01:00:42.5032429489284+00:00"
    basic_obj = Stub_object( object_id = "5", revision = original_revision, value = 1 )

    self.database.save( basic_obj )
    obj = self.database.select_one( Stub_object, Stub_object.sql_load( basic_obj.object_id ) )

    assert obj.object_id == basic_obj.object_id
    assert str( obj.revision.replace( tzinfo = utc ) ) == "2008-01-01 01:00:42.503242+00:00"
    assert obj.value == basic_obj.value

  def test_select_datetime_with_zero_fractional_seconds( self ):
    original_revision = "2008-01-01 01:00:42.0+00:00"
    basic_obj = Stub_object( object_id = "5", revision = original_revision, value = 1 )

    self.database.save( basic_obj )
    obj = self.database.select_one( Stub_object, Stub_object.sql_load( basic_obj.object_id ) )

    assert obj.object_id == basic_obj.object_id
    assert str( obj.revision.replace( tzinfo = utc ) ) == "2008-01-01 01:00:42+00:00"
    assert obj.value == basic_obj.value

  def test_select_one_tuple( self ):
    obj = self.database.select_one( tuple, Stub_object.sql_tuple() )

    assert len( obj ) == 2
    assert obj[ 0 ] == 1
    assert obj[ 1 ] == 2

  def test_select_many( self ):
    basic_obj = Stub_object( object_id = "5", value = 1 )
    original_revision = basic_obj.revision
    basic_obj2 = Stub_object( object_id = "6", value = 2 )
    original_revision2 = basic_obj2.revision

    self.database.save( basic_obj )
    self.database.save( basic_obj2 )
    objs = self.database.select_many( Stub_object, Stub_object.sql_load_em_all() )

    assert len( objs ) == 2
    assert objs[ 0 ].object_id == basic_obj.object_id
    assert objs[ 0 ].revision.replace( tzinfo = utc ) == original_revision
    assert objs[ 0 ].value == basic_obj.value
    assert objs[ 1 ].object_id == basic_obj2.object_id
    assert objs[ 1 ].revision.replace( tzinfo = utc ) == original_revision2
    assert objs[ 1 ].value == basic_obj2.value

  def test_select_many_tuples( self ):
    objs = self.database.select_many( tuple, Stub_object.sql_tuple() )

    assert len( objs ) == 1
    assert len( objs[ 0 ] ) == 2
    assert objs[ 0 ][ 0 ] == 1
    assert objs[ 0 ][ 1 ] == 2

  def test_select_many_with_no_matches( self ):
    objs = self.database.select_many( Stub_object, Stub_object.sql_load_em_all() )

    assert len( objs ) == 0

  def test_save_and_load_revision( self ):
    basic_obj = Stub_object( object_id = "5", value = 1 )
    original_revision = basic_obj.revision

    self.database.save( basic_obj )
    basic_obj.value = 2

    self.database.save( basic_obj )
    obj = self.database.load( Stub_object, basic_obj.object_id )

    assert obj.object_id == basic_obj.object_id
    assert obj.revision.replace( tzinfo = utc ) == basic_obj.revision
    assert obj.value == basic_obj.value

    revised = self.database.load( Stub_object, basic_obj.object_id, revision = original_revision )

    assert revised.object_id == basic_obj.object_id
    assert revised.value == 1
    assert revised.revision.replace( tzinfo = utc ) == original_revision

  def test_execute( self ):
    basic_obj = Stub_object( object_id = "5", value = 1 )
    original_revision = basic_obj.revision

    self.database.execute( basic_obj.sql_create() )
    obj = self.database.load( Stub_object, basic_obj.object_id )

    assert obj.object_id == basic_obj.object_id
    assert obj.revision.replace( tzinfo = utc ) == original_revision
    assert obj.value == basic_obj.value

  def test_execute_without_commit( self ):
    basic_obj = Stub_object( object_id = "5", value = 1 )
    original_revision = basic_obj.revision

    self.database.execute( basic_obj.sql_create(), commit = False )
    self.connection.rollback()
    obj = self.database.load( Stub_object, basic_obj.object_id )

    assert obj == None

  def test_execute_with_explicit_commit( self ):
    basic_obj = Stub_object( object_id = "5", value = 1 )
    original_revision = basic_obj.revision

    self.database.execute( basic_obj.sql_create(), commit = False )
    self.database.commit()
    obj = self.database.load( Stub_object, basic_obj.object_id )

    assert obj.object_id == basic_obj.object_id
    assert obj.revision.replace( tzinfo = utc ) == original_revision
    assert obj.value == basic_obj.value

  def test_load_unknown( self ):
    basic_obj = Stub_object( object_id = "5", value = 1 )
    obj = self.database.load( Stub_object, basic_obj.object_id )

    assert obj == None

  def test_next_id( self ):
    next_id = self.database.next_id( Stub_object )
    assert next_id
    assert self.database.load( Stub_object, next_id )
    prev_ids = [ next_id ]

    next_id = self.database.next_id( Stub_object )
    assert next_id
    assert next_id not in prev_ids
    assert self.database.load( Stub_object, next_id )
    prev_ids.append( next_id )

    next_id = self.database.next_id( Stub_object )
    assert next_id
    assert next_id not in prev_ids
    assert self.database.load( Stub_object, next_id )

  def test_next_id_without_commit( self ):
    next_id = self.database.next_id( Stub_object, commit = False )
    self.connection.rollback()
    assert self.database.load( Stub_object, next_id ) == None

  def test_next_id_with_explicit_commit( self ):
    next_id = self.database.next_id( Stub_object, commit = False )
    self.database.commit()
    assert next_id
    assert self.database.load( Stub_object, next_id )

  def test_synchronize( self ):
    def make_objects():
      for i in range( 50 ):
        object_id = self.database.next_id( Stub_object )
        basic_obj = Stub_object( object_id, value = 1 )
        original_revision = basic_obj.revision

        self.database.execute( basic_obj.sql_create() )
        obj = self.database.load( Stub_object, basic_obj.object_id )

        assert obj.object_id == basic_obj.object_id
        delta = abs( obj.revision.replace( tzinfo = utc ) - original_revision )
        assert delta <= timedelta( seconds = 0.000001 )
        assert obj.value == basic_obj.value

        object_id = self.database.next_id( Stub_object )

    # if synchronization (locking) is working properly, then these two threads should be able to run
    # simultaneously without error. without locking, SQLite will raise
    thread1 = Thread( target = make_objects )
    thread2 = Thread( target = make_objects )
    thread1.start()
    thread2.start()

    thread1.join()
    thread2.join()

  def test_backend( self ):
    assert self.database.backend == Persistent.SQLITE_BACKEND
Пример #3
0
class Test_export_html(object):
    def setUp(self):
        self.database = Database(
            Connection_wrapper(
                sqlite.connect(":memory:",
                               detect_types=sqlite.PARSE_DECLTYPES,
                               check_same_thread=False)),
            cache=Stub_cache(),
        )
        self.database.execute_script(file("model/schema.sqlite").read(),
                                     commit=True)

        self.username = u"mulder"
        self.password = u"trustno1"
        self.email_address = u"*****@*****.**"
        self.user = User.create(self.database.next_id(User), self.username,
                                self.password, self.email_address)
        self.database.save(self.user, commit=False)

        self.trash = Notebook.create(self.database.next_id(Notebook), u"trash")
        self.database.save(self.trash, commit=False)
        self.notebook = Notebook.create(self.database.next_id(Notebook),
                                        u"notebook",
                                        self.trash.object_id,
                                        user_id=self.user.object_id)
        self.database.save(self.notebook, commit=False)

        note_id = self.database.next_id(Note)
        self.note1 = Note.create(note_id,
                                 u"<h3>my title</h3>blah",
                                 notebook_id=self.notebook.object_id,
                                 startup=True,
                                 user_id=self.user.object_id)
        self.database.save(self.note1, commit=False)

        note_id = self.database.next_id(Note)
        self.note2 = Note.create(note_id,
                                 u"<h3>other title</h3>whee",
                                 notebook_id=self.notebook.object_id,
                                 user_id=self.user.object_id)
        self.database.save(self.note2, commit=False)

    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
Пример #4
0
class Test_database(object):
    def setUp(self):
        # make an in-memory sqlite database to use during testing
        self.connection = Connection_wrapper(
            sqlite.connect(":memory:",
                           detect_types=sqlite.PARSE_DECLTYPES,
                           check_same_thread=False))
        self.cache = Stub_cache()
        cursor = self.connection.cursor()
        cursor.execute(Stub_object.sql_create_table())

        self.database = Database(self.connection, self.cache)

    def tearDown(self):
        self.database.close()

    def test_save_and_load(self):
        basic_obj = Stub_object(object_id="5", value=1)
        original_revision = basic_obj.revision

        self.database.save(basic_obj)
        obj = self.database.load(Stub_object, basic_obj.object_id)

        assert obj.object_id == basic_obj.object_id
        assert obj.revision.replace(tzinfo=utc) == original_revision
        assert obj.value == basic_obj.value

    def test_save_and_load_without_commit(self):
        basic_obj = Stub_object(object_id="5", value=1)
        original_revision = basic_obj.revision

        self.database.save(basic_obj, commit=False)
        self.connection.rollback(
        )  # if commit wasn't called, this should back out the save
        obj = self.database.load(Stub_object, basic_obj.object_id)

        assert obj == None

    def test_save_and_load_with_explicit_commit(self):
        basic_obj = Stub_object(object_id="5", value=1)
        original_revision = basic_obj.revision

        self.database.save(basic_obj, commit=False)
        self.database.commit()
        self.connection.rollback(
        )  # should have no effect because of the call to commit
        obj = self.database.load(Stub_object, basic_obj.object_id)

        assert obj.object_id == basic_obj.object_id
        assert obj.revision.replace(tzinfo=utc) == original_revision
        assert obj.value == basic_obj.value

    def test_select_one(self):
        basic_obj = Stub_object(object_id="5", value=1)
        original_revision = basic_obj.revision

        self.database.save(basic_obj)
        obj = self.database.select_one(
            Stub_object, Stub_object.sql_load(basic_obj.object_id))

        assert obj.object_id == basic_obj.object_id
        assert obj.revision.replace(tzinfo=utc) == original_revision
        assert obj.value == basic_obj.value

    def test_select_datetime(self):
        # this revision (with .504099) happens to test for a bug caused by floating point rounding errors
        original_revision = "2008-01-01 01:00:42.504099+00:00"
        basic_obj = Stub_object(object_id="5",
                                revision=original_revision,
                                value=1)

        self.database.save(basic_obj)
        obj = self.database.select_one(
            Stub_object, Stub_object.sql_load(basic_obj.object_id))

        assert obj.object_id == basic_obj.object_id
        assert str(obj.revision.replace(tzinfo=utc)) == original_revision
        assert obj.value == basic_obj.value

    def test_select_datetime_with_many_fractional_digits(self):
        original_revision = "2008-01-01 01:00:42.5032429489284+00:00"
        basic_obj = Stub_object(object_id="5",
                                revision=original_revision,
                                value=1)

        self.database.save(basic_obj)
        obj = self.database.select_one(
            Stub_object, Stub_object.sql_load(basic_obj.object_id))

        assert obj.object_id == basic_obj.object_id
        assert str(obj.revision.replace(
            tzinfo=utc)) == "2008-01-01 01:00:42.503242+00:00"
        assert obj.value == basic_obj.value

    def test_select_datetime_with_zero_fractional_seconds(self):
        original_revision = "2008-01-01 01:00:42.0+00:00"
        basic_obj = Stub_object(object_id="5",
                                revision=original_revision,
                                value=1)

        self.database.save(basic_obj)
        obj = self.database.select_one(
            Stub_object, Stub_object.sql_load(basic_obj.object_id))

        assert obj.object_id == basic_obj.object_id
        assert str(
            obj.revision.replace(tzinfo=utc)) == "2008-01-01 01:00:42+00:00"
        assert obj.value == basic_obj.value

    def test_select_one_tuple(self):
        obj = self.database.select_one(tuple, Stub_object.sql_tuple())

        assert len(obj) == 2
        assert obj[0] == 1
        assert obj[1] == 2

    def test_select_many(self):
        basic_obj = Stub_object(object_id="5", value=1)
        original_revision = basic_obj.revision
        basic_obj2 = Stub_object(object_id="6", value=2)
        original_revision2 = basic_obj2.revision

        self.database.save(basic_obj)
        self.database.save(basic_obj2)
        objs = self.database.select_many(Stub_object,
                                         Stub_object.sql_load_em_all())

        assert len(objs) == 2
        assert objs[0].object_id == basic_obj.object_id
        assert objs[0].revision.replace(tzinfo=utc) == original_revision
        assert objs[0].value == basic_obj.value
        assert objs[1].object_id == basic_obj2.object_id
        assert objs[1].revision.replace(tzinfo=utc) == original_revision2
        assert objs[1].value == basic_obj2.value

    def test_select_many_tuples(self):
        objs = self.database.select_many(tuple, Stub_object.sql_tuple())

        assert len(objs) == 1
        assert len(objs[0]) == 2
        assert objs[0][0] == 1
        assert objs[0][1] == 2

    def test_select_many_with_no_matches(self):
        objs = self.database.select_many(Stub_object,
                                         Stub_object.sql_load_em_all())

        assert len(objs) == 0

    def test_save_and_load_revision(self):
        basic_obj = Stub_object(object_id="5", value=1)
        original_revision = basic_obj.revision

        self.database.save(basic_obj)
        basic_obj.value = 2

        self.database.save(basic_obj)
        obj = self.database.load(Stub_object, basic_obj.object_id)

        assert obj.object_id == basic_obj.object_id
        assert obj.revision.replace(tzinfo=utc) == basic_obj.revision
        assert obj.value == basic_obj.value

        revised = self.database.load(Stub_object,
                                     basic_obj.object_id,
                                     revision=original_revision)

        assert revised.object_id == basic_obj.object_id
        assert revised.value == 1
        assert revised.revision.replace(tzinfo=utc) == original_revision

    def test_execute(self):
        basic_obj = Stub_object(object_id="5", value=1)
        original_revision = basic_obj.revision

        self.database.execute(basic_obj.sql_create())
        obj = self.database.load(Stub_object, basic_obj.object_id)

        assert obj.object_id == basic_obj.object_id
        assert obj.revision.replace(tzinfo=utc) == original_revision
        assert obj.value == basic_obj.value

    def test_execute_without_commit(self):
        basic_obj = Stub_object(object_id="5", value=1)
        original_revision = basic_obj.revision

        self.database.execute(basic_obj.sql_create(), commit=False)
        self.connection.rollback()
        obj = self.database.load(Stub_object, basic_obj.object_id)

        assert obj == None

    def test_execute_with_explicit_commit(self):
        basic_obj = Stub_object(object_id="5", value=1)
        original_revision = basic_obj.revision

        self.database.execute(basic_obj.sql_create(), commit=False)
        self.database.commit()
        obj = self.database.load(Stub_object, basic_obj.object_id)

        assert obj.object_id == basic_obj.object_id
        assert obj.revision.replace(tzinfo=utc) == original_revision
        assert obj.value == basic_obj.value

    def test_load_unknown(self):
        basic_obj = Stub_object(object_id="5", value=1)
        obj = self.database.load(Stub_object, basic_obj.object_id)

        assert obj == None

    def test_next_id(self):
        next_id = self.database.next_id(Stub_object)
        assert next_id
        assert self.database.load(Stub_object, next_id)
        prev_ids = [next_id]

        next_id = self.database.next_id(Stub_object)
        assert next_id
        assert next_id not in prev_ids
        assert self.database.load(Stub_object, next_id)
        prev_ids.append(next_id)

        next_id = self.database.next_id(Stub_object)
        assert next_id
        assert next_id not in prev_ids
        assert self.database.load(Stub_object, next_id)

    def test_next_id_without_commit(self):
        next_id = self.database.next_id(Stub_object, commit=False)
        self.connection.rollback()
        assert self.database.load(Stub_object, next_id) == None

    def test_next_id_with_explicit_commit(self):
        next_id = self.database.next_id(Stub_object, commit=False)
        self.database.commit()
        assert next_id
        assert self.database.load(Stub_object, next_id)

    def test_synchronize(self):
        def make_objects():
            for i in range(50):
                object_id = self.database.next_id(Stub_object)
                basic_obj = Stub_object(object_id, value=1)
                original_revision = basic_obj.revision

                self.database.execute(basic_obj.sql_create())
                obj = self.database.load(Stub_object, basic_obj.object_id)

                assert obj.object_id == basic_obj.object_id
                delta = abs(
                    obj.revision.replace(tzinfo=utc) - original_revision)
                assert delta <= timedelta(seconds=0.000001)
                assert obj.value == basic_obj.value

                object_id = self.database.next_id(Stub_object)

        # if synchronization (locking) is working properly, then these two threads should be able to run
        # simultaneously without error. without locking, SQLite will raise
        thread1 = Thread(target=make_objects)
        thread2 = Thread(target=make_objects)
        thread1.start()
        thread2.start()

        thread1.join()
        thread2.join()

    def test_backend(self):
        assert self.database.backend == Persistent.SQLITE_BACKEND
Пример #5
0
class Test_export_csv( object ):
  def setUp( self ):
    self.database = Database(
      Connection_wrapper( sqlite.connect( ":memory:", detect_types = sqlite.PARSE_DECLTYPES, check_same_thread = False ) ),
      cache = Stub_cache(),
    )
    self.database.execute_script( file( "model/schema.sqlite" ).read(), commit = True )

    self.username = u"mulder"
    self.password = u"trustno1"
    self.email_address = u"*****@*****.**"
    self.user = User.create( self.database.next_id( User ), self.username, self.password, self.email_address )
    self.database.save( self.user, commit = False )

    self.trash = Notebook.create( self.database.next_id( Notebook ), u"trash" )
    self.database.save( self.trash, commit = False )
    self.notebook = Notebook.create( self.database.next_id( Notebook ), u"notebook", self.trash.object_id, user_id = self.user.object_id )
    self.database.save( self.notebook, commit = False )

    note_id = self.database.next_id( Note )
    self.note1 = Note.create( note_id, u"<h3>my title</h3>blah", notebook_id = self.notebook.object_id, startup = True, user_id = self.user.object_id )
    self.database.save( self.note1, commit = False )

    note_id = self.database.next_id( Note )
    self.note2 = Note.create( note_id, u"<h3>other title</h3>whee", notebook_id = self.notebook.object_id, user_id = self.user.object_id )
    self.database.save( self.note2, commit = False )

  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_with_unicode( self ):
    self.test_export_csv( note_contents = u"<h3>blah</h3>ümlaut.png" )

  def test_export_csv_without_note_title( self ):
    self.test_export_csv( note_contents = u"there's no title" )

  def test_export_csv_with_trailing_newline_in_title( self ):
    self.test_export_csv( note_contents = u"<h3>blah\n</h3>foo" )

  def test_export_csv_with_trailing_newline_in_contents( self ):
    self.test_export_csv( note_contents = u"<h3>blah</h3>foo\n" )

  def test_export_csv_with_file_attachment_in_contents( self ):
    self.test_export_csv(
      note_contents = u"<h3>blah</h3>foo<a href=\"/files/download?file_id=blah&quote_filename=False\">file</a>",
      expected_contents = "<h3>blah</h3>foo<a>file</a>",
    )

  def test_export_csv_with_image_in_contents( self ):
    self.test_export_csv(
      note_contents = u"<h3>blah</h3>foo<a href=\"/files/download?file_id=blah&quote_filename=False\"><img src=\"whee.png\" /></a>",
      expected_contents = "<h3>blah</h3>foo<a></a>",
    )

  def test_export_csv_with_blank_username( self ):
    self.user._User__username = None
    self.database.save( self.user )

    self.test_export_csv( note_contents = u"<h3>blah</h3>foo" )
Пример #6
0
class Test_export_csv(object):
    def setUp(self):
        self.database = Database(
            Connection_wrapper(
                sqlite.connect(":memory:",
                               detect_types=sqlite.PARSE_DECLTYPES,
                               check_same_thread=False)),
            cache=Stub_cache(),
        )
        self.database.execute_script(file("model/schema.sqlite").read(),
                                     commit=True)

        self.username = u"mulder"
        self.password = u"trustno1"
        self.email_address = u"*****@*****.**"
        self.user = User.create(self.database.next_id(User), self.username,
                                self.password, self.email_address)
        self.database.save(self.user, commit=False)

        self.trash = Notebook.create(self.database.next_id(Notebook), u"trash")
        self.database.save(self.trash, commit=False)
        self.notebook = Notebook.create(self.database.next_id(Notebook),
                                        u"notebook",
                                        self.trash.object_id,
                                        user_id=self.user.object_id)
        self.database.save(self.notebook, commit=False)

        note_id = self.database.next_id(Note)
        self.note1 = Note.create(note_id,
                                 u"<h3>my title</h3>blah",
                                 notebook_id=self.notebook.object_id,
                                 startup=True,
                                 user_id=self.user.object_id)
        self.database.save(self.note1, commit=False)

        note_id = self.database.next_id(Note)
        self.note2 = Note.create(note_id,
                                 u"<h3>other title</h3>whee",
                                 notebook_id=self.notebook.object_id,
                                 user_id=self.user.object_id)
        self.database.save(self.note2, commit=False)

    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_with_unicode(self):
        self.test_export_csv(note_contents=u"<h3>blah</h3>ümlaut.png")

    def test_export_csv_without_note_title(self):
        self.test_export_csv(note_contents=u"there's no title")

    def test_export_csv_with_trailing_newline_in_title(self):
        self.test_export_csv(note_contents=u"<h3>blah\n</h3>foo")

    def test_export_csv_with_trailing_newline_in_contents(self):
        self.test_export_csv(note_contents=u"<h3>blah</h3>foo\n")

    def test_export_csv_with_file_attachment_in_contents(self):
        self.test_export_csv(
            note_contents=
            u"<h3>blah</h3>foo<a href=\"/files/download?file_id=blah&quote_filename=False\">file</a>",
            expected_contents="<h3>blah</h3>foo<a>file</a>",
        )

    def test_export_csv_with_image_in_contents(self):
        self.test_export_csv(
            note_contents=
            u"<h3>blah</h3>foo<a href=\"/files/download?file_id=blah&quote_filename=False\"><img src=\"whee.png\" /></a>",
            expected_contents="<h3>blah</h3>foo<a></a>",
        )

    def test_export_csv_with_blank_username(self):
        self.user._User__username = None
        self.database.save(self.user)

        self.test_export_csv(note_contents=u"<h3>blah</h3>foo")