Skip to content

Commit 541397d

Browse files
committed
Add coverage for adding climb against existing climbs
Fix issue that popped up from coverage (new should be +1)
1 parent 0fe937f commit 541397d

2 files changed

Lines changed: 20 additions & 13 deletions

File tree

src/model/MutableClimbDataSource.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { GraphQLError } from 'graphql'
21
import { ApolloServerErrorCode } from '@apollo/server/errors'
2+
import { GraphQLError } from 'graphql'
33
import { ClientSession } from 'mongoose'
44
import muid, { MUUID } from 'uuid-mongodb'
55

@@ -10,12 +10,12 @@ import { ChangeRecordMetadataType } from '../db/ChangeLogType.js'
1010
import { getClimbModel } from '../db/ClimbSchema.js'
1111
import { ClimbChangeDocType, ClimbChangeInputType, ClimbEditOperationType, ClimbType, IPitch } from '../db/ClimbTypes.js'
1212
import { aggregateCragStats } from '../db/utils/Aggregate.js'
13+
import { withTransaction } from '../utils/helpers.js'
1314
import { sanitize, sanitizeStrict } from '../utils/sanitize.js'
1415
import ChangeLogDataSource from './ChangeLogDataSource.js'
1516
import ClimbDataSource from './ClimbDataSource.js'
1617
import ExperimentalUserDataSource from './ExperimentalUserDataSource.js'
1718
import MutableAreaDataSource from './MutableAreaDataSource.js'
18-
import { withTransaction } from '../utils/helpers.js'
1919

2020
export interface AddOrUpdateClimbsOptions {
2121
userId: MUUID
@@ -110,16 +110,17 @@ export default class MutableClimbDataSource extends ClimbDataSource {
110110
// when adding new climbs, ensure they are added in expected sort order, such that:
111111
// * multiple climbs keep the order they're input in.
112112
// * all newly added climbs come after existing climbs.
113-
let newClimbLeftRightIndex = (await this.climbModel
113+
const maxExisting = (await this.climbModel
114114
.find({ _id: { $in: parent.climbs } })
115115
.select('metadata.left_right_index')
116116
.sort({ 'metadata.left_right_index': -1 })
117-
.limit(1))[0]
118-
?.metadata.left_right_index ?? 1
117+
.limit(1))[0]?.metadata.left_right_index
118+
let newClimbLeftRightIndex = (maxExisting ?? 0) + 1
119+
119120
function resolveLeftRightIndex (i: number): { left_right_index: number } | null {
120121
// user input is always prioritized
121122
if (userInput[i].leftRightIndex !== undefined) {
122-
return { left_right_index: userInput[i].leftRightIndex! }
123+
return { left_right_index: userInput[i].leftRightIndex ?? 0 }
123124
}
124125
// otherwise, auto-order new climbs
125126
if (!idList[i].existed) {

src/model/__tests__/MutableClimbDataSource.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import muid from 'uuid-mongodb'
21
import { ChangeStream } from 'mongodb'
2+
import muid from 'uuid-mongodb'
33

4-
import MutableClimbDataSource from '../MutableClimbDataSource.js'
54
import MutableAreaDataSource from '../MutableAreaDataSource.js'
5+
import MutableClimbDataSource from '../MutableClimbDataSource.js'
66

7-
import { createIndexes, getAreaModel, getClimbModel } from '../../db/index.js'
8-
import { logger } from '../../logger.js'
97
import { ClimbChangeInputType, ClimbType } from '../../db/ClimbTypes.js'
10-
import { sanitizeDisciplines } from '../../GradeUtils.js'
118
import streamListener from '../../db/edit/streamListener.js'
12-
import ChangeLogDataSource from '../ChangeLogDataSource.js'
9+
import { createIndexes, getAreaModel, getClimbModel } from '../../db/index.js'
10+
import { sanitizeDisciplines } from '../../GradeUtils.js'
11+
import { logger } from '../../logger.js'
1312
import inMemoryDB from '../../utils/inMemoryDB.js'
13+
import ChangeLogDataSource from '../ChangeLogDataSource.js'
1414

1515
export const newSportClimb1: ClimbChangeInputType = {
1616
name: 'Cool route 1',
@@ -212,7 +212,13 @@ describe('Climb CRUD', () => {
212212
).rejects.toThrowError(/You can only add climbs to a crag/)
213213

214214
// Route-only area should accept new boulder problems
215-
await climbs.addOrUpdateClimbs(testUser, routesArea.metadata.area_id, [newBoulderProblem1])
215+
const [newBoulderID] = await climbs.addOrUpdateClimbs(testUser, routesArea.metadata.area_id, [newBoulderProblem1])
216+
// Should come after existing climbs
217+
expect(await climbs.findOneClimbByMUUID(muid.from(newBoulderID))).toMatchObject({
218+
metadata: {
219+
left_right_index: newClimbsToAdd.length + 1
220+
}
221+
})
216222
})
217223

218224
it('can add new boulder problems', async () => {

0 commit comments

Comments
 (0)