Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
apricot13 committed Apr 24, 2024
1 parent b1a7824 commit ade9cb0
Show file tree
Hide file tree
Showing 23 changed files with 949 additions and 213 deletions.
File renamed without changes.
47 changes: 47 additions & 0 deletions __tests__/unit/db.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const { MongoClient } = require("mongodb")
const { connect, db } = require("./../../src/db")
const logger = require("./../../utils/logger")

jest.mock("mongodb")
jest.mock("./../../utils/logger")

describe("db", () => {
describe("connect", () => {
it.todo("should connect to the database and call the callback with the db")
// it("should connect to the database and call the callback with the db", async () => {
// const mockDb = { databaseName: "outpost_api_development" }
// const mockClient = { db: jest.fn().mockReturnValue(mockDb) }
// MongoClient.connect.mockResolvedValue(mockClient)

// const cb = jest.fn()
// await connect(cb)

// expect(MongoClient.connect).toHaveBeenCalledWith(process.env.DB_URI)
// expect(mockClient.db).toHaveBeenCalled()
// expect(cb).toHaveBeenCalledWith(mockDb)
// })

it.todo("should log an error if the connection fails")
// it("should log an error if the connection fails", async () => {
// const error = new Error("Connection failed")
// MongoClient.connect.mockRejectedValue(error)

// const cb = jest.fn()
// await connect(cb)

// expect(logger.error).toHaveBeenCalledWith(error)
// })

it.todo("should warn you if it connects but the database is 'test'")
})

describe("db", () => {
it("should throw an error if not connected", () => {
expect(() => db()).toThrow(
"db() called without being connected to the database. Please connect first, see application logs for more details."
)
})

it.todo("should return the db if connected")
})
})
244 changes: 244 additions & 0 deletions __tests__/unit/lib/filters.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
const filters = require("./../../../src/lib/filters")

describe("locationGeometry", () => {
it("should return an empty object if lat and lng are not provided", () => {
expect(filters.locationGeometry()).toEqual({})
})

it("should return a query object if lat and lng are provided", () => {
const lat = "40.7128"
const lng = "-74.0060"
const expectedQuery = {
"locations.geometry": {
$nearSphere: {
$geometry: {
type: "Point",
coordinates: [parseFloat(lng), parseFloat(lat)],
},
},
},
}
expect(filters.locationGeometry(lat, lng)).toEqual(expectedQuery)
})
})

describe("Calling visibleNow", () => {
it("should return a query that checks if a document is visible now", () => {
const query = filters.visibleNow()

// Check if the query contains the correct conditions
expect(query).toEqual([
{
$or: [
{ visible_from: null },
{ visible_from: { $lte: expect.any(Date) } },
],
},
{
$or: [{ visible_to: null }, { visible_to: { $gte: expect.any(Date) } }],
},
])
})
})

describe("filterKeywords", () => {
it("should return an empty object if keywords are not provided", async () => {
const result = await filters.filterKeywords()
expect(result).toEqual({})
})

it("should return a query object with $text if no locationInQuery is provided", async () => {
const keywords = "test"
const expectedQuery = { $text: { $search: keywords } }
const result = await filters.filterKeywords(keywords)
expect(result).toEqual(expectedQuery)
})

it.todo("should return a query object with _id if locationInQuery is true")
// it("should return a query object with _id if locationInQuery is true", async () => {
// const keywords = "test"
// const args = ["arg1", "arg2"]
// const expectedQuery = {
// _id: { $in: [new ObjectId("60d5ec9af682fbd39a892fb6")] },
// }
// const result = await filters.filterKeywords(keywords, ...args)
// expect(result).toEqual(expectedQuery)
// })
})

describe("filterAges", () => {
it("should return an empty array if min_age and max_age are not provided", () => {
expect(filters.filterAges()).toEqual([])
})

it("should return a query object with max_age if min_age is provided", () => {
const min_age = "10"
const expectedQuery = [
{
$or: [{ max_age: null }, { max_age: { $gte: parseInt(min_age) } }],
},
]
expect(filters.filterAges(min_age)).toEqual(expectedQuery)
})

it("should return a query object with min_age if max_age is provided", () => {
const max_age = "20"
const expectedQuery = [
{
$or: [{ min_age: null }, { min_age: { $lte: parseInt(max_age) } }],
},
]
expect(filters.filterAges(undefined, max_age)).toEqual(expectedQuery)
})

it("should return a query object with min_age and max_age if both are provided", () => {
const min_age = "10"
const max_age = "20"
const expectedQuery = [
{
$or: [{ max_age: null }, { max_age: { $gte: parseInt(min_age) } }],
},
{
$or: [{ min_age: null }, { min_age: { $lte: parseInt(max_age) } }],
},
]
expect(filters.filterAges(min_age, max_age)).toEqual(expectedQuery)
})
})

describe("filterOnly", () => {
it("should return an empty array if only is not provided", () => {
expect(filters.filterOnly()).toEqual([])
})

it('should return a query object with free: true if only includes "free"', () => {
const only = ["free"]
const expectedQuery = [{ free: true }]
expect(filters.filterOnly(only)).toEqual(expectedQuery)
})
})

describe("filterTaxonomies", () => {
it("should return an empty array if taxonomies is not provided", () => {
expect(filters.filterTaxonomies()).toEqual([])
})

it("should return a query array if taxonomies is provided", () => {
const taxonomies = [
"things-to-do",
"clubs-and-groups,holiday-activities,dance-drama-and-music",
"youth-clubs,gaming,sports-courses-and-camps,dance-drama-and-music",
]
const expectedQuery = [
{ "taxonomies.slug": { $in: ["things-to-do"] } },
{
"taxonomies.slug": {
$in: [
"clubs-and-groups",
"holiday-activities",
"dance-drama-and-music",
],
},
},
{
"taxonomies.slug": {
$in: [
"youth-clubs",
"gaming",
"sports-courses-and-camps",
"dance-drama-and-music",
],
},
},
]
expect(filters.filterTaxonomies(taxonomies)).toEqual(expectedQuery)
})
})

describe("filterTargetDirectories", () => {
it("should return an empty object if targetDirectories is not provided", () => {
expect(filters.filterTargetDirectories()).toEqual({})
})

it("should return an empty object if targetDirectories is an empty array", () => {
expect(filters.filterTargetDirectories([])).toEqual({})
})

it("should return a query object if targetDirectories is provided", () => {
const targetDirectories = ["bfis", "bod"]
const expectedQuery = {
"target_directories.label": { $in: targetDirectories },
}
expect(filters.filterTargetDirectories(targetDirectories)).toEqual(
expectedQuery
)
})
})

describe("filterSuitabilities", () => {
it("should return an empty object if suitabilities is not provided", () => {
expect(filters.filterSuitabilities()).toEqual({})
})

it("should return a query object if suitabilities is provided", () => {
const suitabilities = [
"physical-disabilities",
"mental-health-acquired-brain-injury",
]
const expectedQuery = {
"suitabilities.slug": { $in: suitabilities },
}
expect(filters.filterSuitabilities(suitabilities)).toEqual(expectedQuery)
})
})

describe("filterNeeds", () => {
it("should return an empty object if needs is not provided", () => {
expect(filters.filterNeeds()).toEqual({})
})

it("should return a query object if needs is provided", () => {
const needs = ["visual", "autism"]
const expectedQuery = {
"send_needs.slug": { $in: needs },
}
expect(filters.filterNeeds(needs)).toEqual(expectedQuery)
})
})

describe("filterAccessibilities", () => {
it("should return an empty object if accessibilities is not provided", () => {
expect(filters.filterAccessibilities()).toEqual({})
})

it("should return a query object if accessibilities is provided", () => {
const accessibilities = [
"accessible-toilet-facilities",
"wheelchair-accessible-entrance",
]
const expectedQuery = {
"locations.accessibilities.slug": { $in: accessibilities },
}
expect(filters.filterAccessibilities(accessibilities)).toEqual(
expectedQuery
)
})
})

describe("filterDays", () => {
it("should return an empty object if days is not provided", () => {
expect(filters.filterDays()).toEqual({})
})

it("should return an empty object if days is an empty array", () => {
expect(filters.filterDays([])).toEqual({})
})

it("should return a query object if days is provided", () => {
const days = ["Monday", "Tuesday"]
const expectedQuery = {
"regular_schedules.weekday": { $in: days },
}
expect(filters.filterDays(days)).toEqual(expectedQuery)
})
})
19 changes: 16 additions & 3 deletions __tests__/unit/lib/index.test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
const Index = require("../../../lib/index")
const Index = require("./../../../src/lib/index")

describe("Calling calculateDistance", () => {
it("should return the minimum distance between the query and the locations", () => {
const query = { lat: 0, lng: 0 }
const locations = [
{ geometry: { coordinates: [0, 1] } },
{ geometry: { coordinates: [0, 2] } },
]
const distance = Index.calculateDistance(query, locations)
const distance = Index.calculateDistance(0, 0, locations)

// Check if the distance is the minimum distance
expect(distance).toBeCloseTo(69.1, 1) // The distance between (0, 0) and (0, 1) is approximately 69.1 miles
})
})

describe("Calling geocode", () => {
it.todo("should return a json object")
it.todo("should throw an error if GOOGLE_API_KEY or location is not set")
})

describe("Calling projection", () => {
it("should return the projection object", () => {
const projection = Index.projection

// Check if the projection object is correct
expect(projection).toEqual({ _id: 0, visible_from: 0, visible_to: 0 })
})
})
Loading

0 comments on commit ade9cb0

Please sign in to comment.