Skip to content

Add back (rx)session.lastBookmark methods #853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions packages/core/src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,17 @@ class Session {
this._hasTx = false
}

/**
* Return the bookmarks received following the last completed {@link Transaction}.
*
* @deprecated This method will be removed in version 6.0. Please, use {@link Session#lastBookmarks} instead.
*
* @return {string[]} A reference to a previous transaction.
*/
lastBookmark(): string[] {
return this.lastBookmarks()
}

/**
* Return the bookmarks received following the last completed {@link Transaction}.
*
Expand Down
13 changes: 13 additions & 0 deletions packages/core/test/driver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ describe('Driver', () => {
expect(session).not.toBeUndefined()
expect(createSession).toHaveBeenCalledWith(expectedSessionParams())
})

it.each([
[undefined, Bookmarks.empty()],
[null, Bookmarks.empty()],
['bookmark', new Bookmarks('bookmark')],
[['bookmark'], new Bookmarks(['bookmark'])],
[['bookmark1', 'bookmark2'], new Bookmarks(['bookmark1', 'bookmark2'])],
])('should create session using param bookmarks', (bookmarks, expectedBookmarks) => {
// @ts-ignore
const session = driver!.session({ bookmarks })

expect(session.lastBookmarks()).toEqual(expectedBookmarks.values())
})
})

it.each([
Expand Down
36 changes: 34 additions & 2 deletions packages/core/test/session.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* limitations under the License.
*/
import { ConnectionProvider, Session, Connection } from '../src'
import { bookmarks } from '../src/internal'
import { ACCESS_MODE_READ, FETCH_ALL } from '../src/internal/constants'
import FakeConnection from './utils/connection.fake'

Expand Down Expand Up @@ -202,9 +203,39 @@ describe('session', () => {
done()
})
}, 70000)

describe('.lastBookmark()', () => {
it.each([
[bookmarks.Bookmarks.empty()],
[new bookmarks.Bookmarks('bookmark1')],
[new bookmarks.Bookmarks(['bookmark1', 'bookmark2'])]
])('should return the bookmark informed in the object creation', (bookmarks) => {
const session = newSessionWithConnection(newFakeConnection(), false, 1000, bookmarks)

expect(session.lastBookmark()).toEqual(bookmarks.values())
})
})

describe('.lastBookmark()', () => {
it.each([
[bookmarks.Bookmarks.empty()],
[new bookmarks.Bookmarks('bookmark1')],
[new bookmarks.Bookmarks(['bookmark1', 'bookmark2'])]
])('should return the bookmark informed in the object creation', (bookmarks) => {
const session = newSessionWithConnection(newFakeConnection(), false, 1000, bookmarks)

expect(session.lastBookmarks()).toEqual(bookmarks.values())
})
})
})

function newSessionWithConnection(connection: Connection, beginTx: boolean = true, fetchSize: number = 1000): Session {
function newSessionWithConnection(
connection: Connection,
beginTx: boolean = true,
fetchSize: number = 1000,
lastBookmarks: bookmarks.Bookmarks = bookmarks.Bookmarks.empty()
): Session {

const connectionProvider = new ConnectionProvider()
connectionProvider.acquireConnection = () => Promise.resolve(connection)
connectionProvider.close = () => Promise.resolve()
Expand All @@ -215,7 +246,8 @@ function newSessionWithConnection(connection: Connection, beginTx: boolean = tru
database: "",
fetchSize,
config: {},
reactive: false
reactive: false,
bookmarks: lastBookmarks
})

if (beginTx) {
Expand Down
2 changes: 1 addition & 1 deletion packages/neo4j-driver/src/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Driver extends CoreDriver {
return new RxSession({
session: this._newSession({
defaultAccessMode,
bookmarks,
bookmarkOrBookmarks: bookmarks,
database,
impersonatedUser,
reactive: true,
Expand Down
19 changes: 18 additions & 1 deletion packages/neo4j-driver/src/session-rx.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,23 @@ export default class RxSession {
})
}

/**
* Returns the bookmarks received following the last successfully completed query, which is executed
* either in an {@link RxTransaction} obtained from this session instance or directly through one of
* the {@link RxSession#run} method of this session instance.
*
* If no bookmarks were received or if this transaction was rolled back, the bookmarks value will not be
* changed.
*
* @deprecated This method will be removed in 6.0 version. Please, use {@link RxSession#lastBookmarks} instead.
*
* @public
* @returns {string[]}
*/
lastBookmark () {
return this.lastBookmarks()
}

/**
* Returns the bookmarks received following the last successfully completed query, which is executed
* either in an {@link RxTransaction} obtained from this session instance or directly through one of
Expand All @@ -130,7 +147,7 @@ export default class RxSession {
* changed.
*
* @public
* @returns {string}
* @returns {string[]}
*/
lastBookmarks () {
return this._session.lastBookmarks()
Expand Down
28 changes: 26 additions & 2 deletions packages/neo4j-driver/test/driver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ import {
DEFAULT_ACQUISITION_TIMEOUT,
DEFAULT_MAX_SIZE
} from '../../bolt-connection/lib/pool/pool-config'
import { ServerVersion, VERSION_4_0_0 } from '../src/internal/server-version'
import testUtils from './internal/test-utils'
import { json } from 'neo4j-driver-core'
import { json, internal } from 'neo4j-driver-core'

const {
bookmarks: { Bookmarks }
} = internal

// As long as driver creation doesn't touch the network it's fine to run
// this as a unit test.
Expand Down Expand Up @@ -114,6 +117,27 @@ describe('#unit driver', () => {
})
).toThrow()
})

describe('.rxSession()', () => {
;[
[undefined, Bookmarks.empty()],
[null, Bookmarks.empty()],
['bookmark', new Bookmarks('bookmark')],
[['bookmark'], new Bookmarks(['bookmark'])],
[['bookmark1', 'bookmark2'], new Bookmarks(['bookmark1', 'bookmark2'])]
].forEach(([bookmarks, expectedBookmarks]) => {
it(`should create session using param bookmarks=${bookmarks}`, () => {
driver = neo4j.driver(
`neo4j+ssc://${sharedNeo4j.hostname}`,
sharedNeo4j.authToken
)

const session = driver.rxSession({ bookmarks })

expect(session.lastBookmarks()).toEqual(expectedBookmarks.values())
})
})
})
})

describe('#integration driver', () => {
Expand Down
55 changes: 54 additions & 1 deletion packages/neo4j-driver/test/rx/session.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@
import { Notification, throwError } from 'rxjs'
import { map, materialize, toArray, concat } from 'rxjs/operators'
import neo4j from '../../src'
import RxSession from '../../src/session-rx'
import sharedNeo4j from '../internal/shared-neo4j'
import { newError, error } from 'neo4j-driver-core'
import {
newError,
error,
internal,
Session,
ConnectionProvider
} from 'neo4j-driver-core'

const { SERVICE_UNAVAILABLE, SESSION_EXPIRED } = error
const { bookmarks } = internal

describe('#integration rx-session', () => {
let driver
Expand Down Expand Up @@ -274,3 +282,48 @@ describe('#integration rx-session', () => {
}
}
})

describe('#unit rx-session', () => {
describe('lastBookmark', () => {
;[
bookmarks.Bookmarks.empty(),
new bookmarks.Bookmarks('bookmark1'),
new bookmarks.Bookmarks(['bookmark1', 'bookmark2'])
].forEach(bookmarks => {
it(`should return ${bookmarks}`, () => {
const session = newSession(bookmarks)
expect(session.lastBookmark()).toBe(bookmarks.values())
})
})
})

describe('lastBookmarks', () => {
;[
bookmarks.Bookmarks.empty(),
new bookmarks.Bookmarks('bookmark1'),
new bookmarks.Bookmarks(['bookmark1', 'bookmark2'])
].forEach(bookmarks => {
it(`should return ${bookmarks}`, () => {
const session = newSession(bookmarks)
expect(session.lastBookmarks()).toBe(bookmarks.values())
})
})
})

function newSession (lastBookmarks = bookmarks.Bookmarks.empty()) {
const connectionProvider = new ConnectionProvider()
connectionProvider.acquireConnection = () => Promise.resolve(null)
connectionProvider.close = () => Promise.resolve()

const session = new Session({
mode: 'READ',
connectionProvider,
database: '',
config: {},
reactive: true,
bookmarks: lastBookmarks
})

return new RxSession({ session })
}
})
3 changes: 2 additions & 1 deletion packages/neo4j-driver/test/types/session-rx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,8 @@ const txConfig7: TransactionConfig = {
}

const tx1: Observable<RxTransaction> = rxSession.beginTransaction()
const bookmarks: null | string = <null>rxSession.lastBookmarks()
const bookmarks: string[] = rxSession.lastBookmarks()
const bookmark: string[] = rxSession.lastBookmark()

const observable1: Observable<number> = rxSession.readTransaction(
(tx: RxTransaction) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/neo4j-driver/types/session-rx.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ declare interface RxSession {

beginTransaction(config?: TransactionConfig): Observable<RxTransaction>

lastBookmarks(): string | null
lastBookmarks(): string[]
lastBookmark(): string[]

readTransaction<T>(
work: RxTransactionWork<T>,
Expand Down