Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
aelassas committed Jan 8, 2025
2 parents be5c8c1 + e3a0147 commit 01fe49e
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 85 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,3 @@ You can also install the Android App by downloading the APK and installing it on
## License

Movin' In is [MIT licensed](https://github.com/aelassas/movinin/blob/main/LICENSE).

13 changes: 5 additions & 8 deletions api/__tests__/agency.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import url from 'url'
import path from 'path'
import fs from 'node:fs/promises'
import { nanoid } from 'nanoid'
import mongoose from 'mongoose'
import * as movininTypes from ':movinin-types'
import * as databaseHelper from '../src/common/databaseHelper'
import * as testHelper from './testHelper'
Expand Down Expand Up @@ -43,15 +42,13 @@ beforeAll(async () => {
// Closing and cleaning the database connection after running the test suite
//
afterAll(async () => {
if (mongoose.connection.readyState) {
await testHelper.close()
await testHelper.close()

// delete agencies
await testHelper.deleteAgency(AGENCY1_ID)
await testHelper.deleteAgency(AGENCY2_ID)
// delete agencies
await testHelper.deleteAgency(AGENCY1_ID)
await testHelper.deleteAgency(AGENCY2_ID)

await databaseHelper.close()
}
await databaseHelper.close()
})

//
Expand Down
27 changes: 12 additions & 15 deletions api/__tests__/booking.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dotenv/config'
import request from 'supertest'
import { nanoid } from 'nanoid'
import mongoose from 'mongoose'
import * as movininTypes from ':movinin-types'
import app from '../src/app'
import * as databaseHelper from '../src/common/databaseHelper'
Expand Down Expand Up @@ -95,25 +94,23 @@ beforeAll(async () => {
// Closing and cleaning the database connection after running the test suite
//
afterAll(async () => {
if (mongoose.connection.readyState) {
await testHelper.close()
await testHelper.close()

// delete the agency
await testHelper.deleteAgency(AGENCY_ID)
// delete the agency
await testHelper.deleteAgency(AGENCY_ID)

// delete the location
await testHelper.deleteLocation(LOCATION_ID)
// delete the location
await testHelper.deleteLocation(LOCATION_ID)

// delete the property
await Property.deleteMany({ _id: { $in: [PROPERTY1_ID, PROPERTY2_ID] } })
// delete the property
await Property.deleteMany({ _id: { $in: [PROPERTY1_ID, PROPERTY2_ID] } })

// delete renters
await User.deleteMany({ _id: { $in: [RENTER1_ID, RENTER2_ID] } })
await Notification.deleteMany({ user: { $in: [RENTER1_ID, RENTER2_ID] } })
await NotificationCounter.deleteMany({ user: { $in: [RENTER1_ID, RENTER2_ID] } })
// delete renters
await User.deleteMany({ _id: { $in: [RENTER1_ID, RENTER2_ID] } })
await Notification.deleteMany({ user: { $in: [RENTER1_ID, RENTER2_ID] } })
await NotificationCounter.deleteMany({ user: { $in: [RENTER1_ID, RENTER2_ID] } })

await databaseHelper.close()
}
await databaseHelper.close()
})

//
Expand Down
7 changes: 2 additions & 5 deletions api/__tests__/country.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'dotenv/config'
import request from 'supertest'
import { nanoid } from 'nanoid'
import mongoose from 'mongoose'
import * as movininTypes from ':movinin-types'
import app from '../src/app'
import * as databaseHelper from '../src/common/databaseHelper'
Expand Down Expand Up @@ -39,10 +38,8 @@ beforeAll(async () => {
// Closing and cleaning the database connection after running the test suite
//
afterAll(async () => {
if (mongoose.connection.readyState) {
await testHelper.close()
await databaseHelper.close()
}
await testHelper.close()
await databaseHelper.close()
})

//
Expand Down
7 changes: 2 additions & 5 deletions api/__tests__/globalSetup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dotenv/config'
import bcrypt from 'bcrypt'
import mongoose from 'mongoose'
import * as movininTypes from ':movinin-types'
import * as env from '../src/config/env.config'
import * as logger from '../src/common/logger'
Expand All @@ -26,10 +25,8 @@ export default async function globalSetup() {
await admin.save()
logger.info('globalSetup: Admin user created:', admin.id)
}
if (mongoose.connection.readyState) {
await databaseHelper.close()
logger.info('Database connection closed')
}
await databaseHelper.close()
logger.info('Database connection closed')
}
} catch (err) {
logger.error('Error while running global setup', err)
Expand Down
5 changes: 1 addition & 4 deletions api/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dotenv/config'
import mongoose from 'mongoose'
import * as databaseHelper from '../src/common/databaseHelper'
import * as testHelper from './testHelper'
import * as env from '../src/config/env.config'
Expand All @@ -21,9 +20,7 @@ beforeAll(async () => {
// Closing and cleaning the database connection after running the test suite
//
afterAll(async () => {
if (mongoose.connection.readyState) {
await databaseHelper.close()
}
await databaseHelper.close()
})

const createBookingIndex = async (expireAfterSeconds: number): Promise<void> => {
Expand Down
7 changes: 2 additions & 5 deletions api/__tests__/location.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import url from 'url'
import path from 'path'
import fs from 'node:fs/promises'
import { nanoid } from 'nanoid'
import mongoose from 'mongoose'
import * as movininTypes from ':movinin-types'
import app from '../src/app'
import * as databaseHelper from '../src/common/databaseHelper'
Expand Down Expand Up @@ -71,10 +70,8 @@ afterAll(async () => {
await LocationValue.deleteMany({ _id: { $in: [countryValue1Id, countryValue2Id] } })
await Country.deleteOne({ _id: countryId })

if (mongoose.connection.readyState) {
await testHelper.close()
await databaseHelper.close()
}
await testHelper.close()
await databaseHelper.close()
})

//
Expand Down
7 changes: 2 additions & 5 deletions api/__tests__/middleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dotenv/config'
import request from 'supertest'
import mongoose from 'mongoose'
import * as movininTypes from ':movinin-types'
import * as databaseHelper from '../src/common/databaseHelper'
import app from '../src/app'
Expand Down Expand Up @@ -28,10 +27,8 @@ beforeAll(async () => {
// Closing and cleaning the database connection after running the test suite
//
afterAll(async () => {
if (mongoose.connection.readyState) {
await testHelper.close()
await databaseHelper.close()
}
await testHelper.close()
await databaseHelper.close()
})

describe('POST /api/sign-in/backend', () => {
Expand Down
5 changes: 1 addition & 4 deletions api/__tests__/miscellaneous.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dotenv/config'
import mongoose from 'mongoose'
import * as movininTypes from ':movinin-types'
import * as env from '../src/config/env.config'
import * as databaseHelper from '../src/common/databaseHelper'
Expand All @@ -22,9 +21,7 @@ beforeAll(async () => {
// Closing and cleaning the database connection after running the test suite
//
afterAll(async () => {
if (mongoose.connection.readyState) {
await databaseHelper.close()
}
await databaseHelper.close()
})

describe('Test User phone validation', () => {
Expand Down
15 changes: 6 additions & 9 deletions api/__tests__/notification.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dotenv/config'
import request from 'supertest'
import mongoose from 'mongoose'
import * as databaseHelper from '../src/common/databaseHelper'
import * as testHelper from './testHelper'
import Notification from '../src/models/Notification'
Expand Down Expand Up @@ -42,17 +41,15 @@ beforeAll(async () => {
// Closing and cleaning the database connection after running the test suite
//
afterAll(async () => {
if (mongoose.connection.readyState) {
await testHelper.close()
await testHelper.close()

await testHelper.deleteAgency(AGENCY_ID)
await testHelper.deleteAgency(AGENCY_ID)

// clear admin user notifications and notification counter
await Notification.deleteMany({ user: ADMIN_USER_ID })
await NotificationCounter.deleteOne({ user: ADMIN_USER_ID })
// clear admin user notifications and notification counter
await Notification.deleteMany({ user: ADMIN_USER_ID })
await NotificationCounter.deleteOne({ user: ADMIN_USER_ID })

await databaseHelper.close()
}
await databaseHelper.close()
})

//
Expand Down
19 changes: 8 additions & 11 deletions api/__tests__/property.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import url from 'url'
import path from 'path'
import fs from 'node:fs/promises'
import { nanoid } from 'nanoid'
import mongoose from 'mongoose'
import * as movininTypes from ':movinin-types'
import * as databaseHelper from '../src/common/databaseHelper'
import app from '../src/app'
Expand Down Expand Up @@ -61,19 +60,17 @@ beforeAll(async () => {
// Closing and cleaning the database connection after running the test suite
//
afterAll(async () => {
if (mongoose.connection.readyState) {
await testHelper.close()
await testHelper.close()

// delete agencies
await testHelper.deleteAgency(AGENCY1_ID)
await testHelper.deleteAgency(AGENCY2_ID)
// delete agencies
await testHelper.deleteAgency(AGENCY1_ID)
await testHelper.deleteAgency(AGENCY2_ID)

// delete locations
await testHelper.deleteLocation(LOCATION1_ID)
await testHelper.deleteLocation(LOCATION2_ID)
// delete locations
await testHelper.deleteLocation(LOCATION1_ID)
await testHelper.deleteLocation(LOCATION2_ID)

await databaseHelper.close()
}
await databaseHelper.close()
})

//
Expand Down
5 changes: 1 addition & 4 deletions api/__tests__/stripe.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dotenv/config'
import request from 'supertest'
import mongoose from 'mongoose'
import * as movininTypes from ':movinin-types'
import app from '../src/app'
import * as databaseHelper from '../src/common/databaseHelper'
Expand All @@ -23,9 +22,7 @@ beforeAll(async () => {
// Closing and cleaning the database connection after running the test suite
//
afterAll(async () => {
if (mongoose.connection.readyState) {
await databaseHelper.close()
}
await databaseHelper.close()
})

describe('POST /api/create-checkout-session', () => {
Expand Down
9 changes: 3 additions & 6 deletions api/__tests__/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import url from 'url'
import path from 'path'
import fs from 'node:fs/promises'
import { nanoid } from 'nanoid'
import mongoose from 'mongoose'
import * as movininTypes from ':movinin-types'
import app from '../src/app'
import * as databaseHelper from '../src/common/databaseHelper'
Expand Down Expand Up @@ -49,13 +48,11 @@ beforeAll(async () => {
// Closing and cleaning the database connection after running the test suite
//
afterAll(async () => {
if (mongoose.connection.readyState) {
await testHelper.close()
await testHelper.close()

await Token.deleteMany({ user: { $in: [ADMIN_ID] } })
await Token.deleteMany({ user: { $in: [ADMIN_ID] } })

await databaseHelper.close()
}
await databaseHelper.close()
})

//
Expand Down
7 changes: 4 additions & 3 deletions api/src/common/databaseHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ export const connect = async (uri: string, ssl: boolean, debug: boolean): Promis
* @returns {Promise<void>}
*/
export const close = async (force: boolean = false): Promise<void> => {
if (mongoose.connection.readyState) {
await mongoose.connection.close(force)
}
// if (mongoose.connection.readyState) {
// await mongoose.connection.close(force)
// }
await mongoose.connection.close(force)
}
/**
* Initialize locations.
Expand Down

0 comments on commit 01fe49e

Please sign in to comment.