Skip to content

Commit

Permalink
Update tests to have more coverage, clean thingsu p
Browse files Browse the repository at this point in the history
  • Loading branch information
victorquinn committed Oct 8, 2024
1 parent f450f57 commit f53e287
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 12 deletions.
25 changes: 25 additions & 0 deletions src/cache.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Cache } from './cache';

describe('Cache', () => {
let cache: Cache;

beforeEach(() => {
cache = new Cache();
});

it('should store and retrieve data', async () => {
const key = 'test-key';
const value = { data: 'test-value' };
const ttl = 3600; // e.g., 1 hour
await cache.set(key, JSON.stringify(value), ttl);
const result = await cache.get(key);
expect(JSON.parse(result!)).toEqual(value);
});

it('should return null for nonexistent keys', async () => {
const result = await cache.get('nonexistent-key');
expect(result).toBeNull();
});

// ... add more tests as needed ...
});
5 changes: 0 additions & 5 deletions src/geohash.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/__tests__/weatherPlus.test.ts → src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import WeatherPlus from '../index';
import WeatherPlus from './index';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import { IWeatherData } from '../interfaces';
import { IWeatherData } from './interfaces';

jest.mock('redis', () => {
const mGet = jest.fn();
Expand Down
9 changes: 9 additions & 0 deletions src/weatherService.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { WeatherService, InvalidProviderLocationError } from './weatherService';
import * as nwsClient from './providers/nws/client';
import geohash from 'ngeohash';

jest.mock('./cache', () => {
return {
Expand All @@ -20,6 +21,14 @@ jest.mock('./providers/nws/client', () => {
};
});

jest.mock('ngeohash', () => ({
encode: jest.fn().mockReturnValue('dqcjq'), // mock geohash string
decode: jest.fn().mockReturnValue({
latitude: 38.8977,
longitude: -77.0365,
}),
}));

describe('WeatherService', () => {
let weatherService: WeatherService;

Expand Down
10 changes: 5 additions & 5 deletions src/weatherService.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { RedisClientType } from 'redis';
import { getGeohash } from './geohash';
import geohash from 'ngeohash';
import { Cache } from './cache';
import * as nws from './providers/nws/client';
import debug from 'debug';
import { z } from 'zod';
import { Feature, Geometry, Point, GeoJsonProperties } from 'geojson';
import booleanPointInPolygon from '@turf/boolean-point-in-polygon';
import { feature } from 'topojson-client';
import { Topology } from 'topojson-specification';
import usAtlasData from 'us-atlas/states-10m.json';
import { Polygon, MultiPolygon } from 'geojson';

Expand Down Expand Up @@ -104,14 +103,15 @@ export class WeatherService {
}

log(`Getting weather for (${lat}, ${lng})`);
const geohash = getGeohash(lat, lng, 6);
const precision = 5; // or desired precision
const locationGeohash = geohash.encode(lat, lng, precision);

const cachedWeather = await this.cache.get(geohash);
const cachedWeather = await this.cache.get(locationGeohash);
if (cachedWeather) {
return JSON.parse(cachedWeather);
} else {
const weather = await this.providers[this.provider].getWeather(lat, lng);
await this.cache.set(geohash, JSON.stringify(weather), 300); // Cache for 5 mins
await this.cache.set(locationGeohash, JSON.stringify(weather), 300); // Cache for 5 mins
return weather;
}
}
Expand Down

0 comments on commit f53e287

Please sign in to comment.