Skip to content
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

Weather and OSM Routes Tests #919

Merged
merged 2 commits into from
May 11, 2024
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
2 changes: 1 addition & 1 deletion server/src/controllers/weather/getWeather.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function getWeatherRoute() {
.input(z.object({ lat: z.number(), lon: z.number() }))
.query(async (opts) => {
const { lat, lon } = opts.input;
const { env }: any = opts.ctx;
const { env } = opts.ctx;
return await getWeatherService(
env.WEATHER_URL,
env.OPENWEATHER_KEY,
Expand Down
37 changes: 7 additions & 30 deletions server/src/drizzle/methods/Node.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { eq } from 'drizzle-orm';
import { createDb } from '../../db/client';
import { DbClient } from '../../db/client';
import { node } from '../../db/schema';
import { getDB } from '../../trpc/context';

export class Node {
async createInstance() {
const dbInstance = await createDb(getDB());
return dbInstance;
}

async update(
data: any,
id: string,
filter = eq(node.id, id),
returning = null,
) {
return (await this.createInstance())
return DbClient.instance
.update(node)
.set(data)
.where(filter)
Expand All @@ -24,39 +18,22 @@ export class Node {
}

async delete(id: string, filter = eq(node.id, id)) {
return (await this.createInstance())
.delete(node)
.where(filter)
.returning()
.get();
return DbClient.instance.delete(node).where(filter).returning().get();
}

async findById(id: string, filter = eq(node.id, id)) {
return (await this.createInstance())
.select()
.from(node)
.where(filter)
.limit(1)
.get();
return DbClient.instance.select().from(node).where(filter).limit(1).get();
}

async findMany(filter = null) {
return (await this.createInstance())
.select()
.from(node)
.where(filter)
.get();
return DbClient.instance.select().from(node).where(filter).get();
}

async findUniqueNode(query) {
return (await this.createInstance()).query.node.findFirst(query);
return DbClient.instance.query.node.findFirst(query);
}

async create(data: any) {
return (await this.createInstance())
.insert(node)
.values(data)
.returning()
.get();
return DbClient.instance.insert(node).values(data).returning().get();
}
}
33 changes: 7 additions & 26 deletions server/src/drizzle/methods/Way.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { eq } from 'drizzle-orm';
import { createDb } from '../../db/client';
import { DbClient } from '../../db/client';
import { way } from '../../db/schema';
import { getDB } from '../../trpc/context';

export class Way {
async createInstance() {
const dbInstance = await createDb(getDB());
return dbInstance;
}

async update(
data: any,
id: string,
filter = eq(way.id, id),
returning = null,
) {
return (await this.createInstance())
return DbClient.instance
.update(way)
.set(data)
.where(filter)
Expand All @@ -24,35 +18,22 @@ export class Way {
}

async delete(id: string, filter = eq(way.id, id)) {
return (await this.createInstance())
.delete(way)
.where(filter)
.returning()
.get();
return DbClient.instance.delete(way).where(filter).returning().get();
}

async findById(id: string, filter = eq(way.id, id)) {
return (await this.createInstance())
.select()
.from(way)
.where(filter)
.limit(1)
.get();
return DbClient.instance.select().from(way).where(filter).limit(1).get();
}

async findMany(filter = null) {
return (await this.createInstance()).select().from(way).where(filter).get();
return DbClient.instance.select().from(way).where(filter).get();
}

async findUniqueWay(query) {
return (await this.createInstance()).query.way.findFirst(query);
return DbClient.instance.query.way.findFirst(query);
}

async create(data: any) {
return (await this.createInstance())
.insert(way)
.values(data)
.returning()
.get();
return DbClient.instance.insert(way).values(data).returning().get();
}
}
2 changes: 1 addition & 1 deletion server/src/services/osm/getParksOSMService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export async function getParksOSMService(lat, lon, radius, osmUri) {

const data = await response.json();
const geojsonData = osmtogeojson(data);
console.log('geojsonData==============', geojsonData);
// console.log('geojsonData==============', geojsonData);

updateDatabaseWithGeoJSONDataFromOverpass(geojsonData);

Expand Down
160 changes: 160 additions & 0 deletions server/src/tests/routes/osm.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import { describe, it, expect, beforeAll, beforeEach } from 'vitest';
import { setupTest } from '../testHelpers';
import type { trpcCaller } from '../testHelpers';
import { env } from 'cloudflare:test';
import { Node as NodeClass } from '../../drizzle/methods/Node';

interface GeojsonType {
type: string;
properties: Object;
geometry: {
type: 'Point';
coordinates: any; // from mongoose.Schema.Types.Mixed
};
}

describe('OSM routes', () => {
let caller: trpcCaller;
const nodeClass = new NodeClass();

beforeAll(async () => {
caller = await setupTest(env);
});

describe('getPhotonResults', () => {
it(
'should get photon results based on search string',
{
timeout: 10000,
},
async () => {
const results = await caller.getPhotonResults({
searchString: 'test',
});
expect(results).toBeDefined();
},
);
});

describe('getTrailsOSM', () => {
it(
'should get trails',
{
timeout: 10000,
},
async () => {
const results = await caller.getTrailsOSM({
lat: 20.5937,
lon: 78.9629,
});
expect(results).toBeDefined();
},
);
});

describe('getParksOSM', () => {
it(
'should get parks',
{
timeout: 10000,
},
async () => {
const results = await caller.getParksOSM({
lat: 20.5937,
lon: 78.9629,
});
expect(results).toBeDefined();
},
);
});

describe('getOsm', () => {
it(
'should get Osm routes based on start and end points',
{
timeout: 10000,
},
async () => {
const results = await caller.getOsm({
activityType: 'hiking',
startPoint: {
latitude: 20.5937,
longitude: 78.9629,
},
endPoint: {
latitude: 20.5937,
longitude: 78.9629,
},
});
expect(results).toBeDefined();
},
);
});

describe('postSingleGeoJSON', () => {
it(
'should post geojson',
{
timeout: 10000,
},
async () => {
const geojson: GeojsonType = {
properties: {
osm_id: 1,
osm_type: 'node',
},
type: 'Feature',
geometry: {
coordinates: [],
type: 'Point',
},
};
const results = await caller.postSingleGeoJSON({
geojson,
});
expect(results).toBeDefined();
},
);
});

describe('getDestination', () => {
it(
'should get destination route',
{
timeout: 10000,
},
async () => {
const node = await nodeClass.create({
osm_id: 2311693,
lat: 22.231897,
lon: 60.435018,
tags: '{}',
});
const results = await caller.getDestination({ id: node.id });
expect(results).toBeDefined();
},
);
});

describe('getPhotonDetails', () => {
it(
'should get photon details',
{
timeout: 10000,
},
async () => {
const node = await nodeClass.create({
osm_id: 2311693,
lat: 22.231897,
lon: 60.435018,
tags: '{}',
});
const results = await caller.getPhotonDetails({
id: node.id,
type: 'node',
});
expect(results).toBeDefined();
},
);
});
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { describe, it, expect, beforeEach } from 'vitest';
import { describe, it, expect, beforeAll } from 'vitest';
import { setupTest } from '../utils/testHelpers';
import type { trpcCaller } from '../utils/testHelpers';
import { env } from 'cloudflare:test';

describe('Get weather information', () => {
let caller: trpcCaller;

beforeEach(async () => {
beforeAll(async () => {
caller = await setupTest(env);
});

Expand All @@ -15,6 +15,7 @@ describe('Get weather information', () => {
lat: 20.5937,
lon: 78.9629,
};

const weather = await caller.getWeather(coordinates);
expect(weather).toBeDefined();
expect(weather?.coord?.lon).toEqual(coordinates?.lon);
Expand All @@ -28,7 +29,7 @@ describe('Get weather information', () => {
};
const weather = await caller.getWeatherWeek(coordinates);
expect(weather).toBeDefined();
expect(weather?.city?.coord?.lon).toEqual(coordinates?.lon);
expect(weather?.city?.coord?.lat).toEqual(coordinates?.lat);
expect(weather?.coord?.lon).toEqual(coordinates?.lon);
expect(weather?.coord?.lat).toEqual(coordinates?.lat);
});
});
});
Loading
Loading