@@ -3,22 +3,53 @@ import "reflect-metadata";
3
3
import { IRedisService } from "../../src/services/RedisService" ;
4
4
import { DataSourceInput } from "../../src/types/DataSourceInput" ;
5
5
import { GeolocationOutput } from "../../src/types/GeolocationOutput" ;
6
+
7
+ import { getFilePath } from "../../src/utils/getFilePath" ;
8
+ import { constants } from "../../src/app/constants/constants" ;
6
9
import { RedisInMemoryService } from "../services/RedisInMemoryService" ;
7
10
import { SqliteTransform } from "../../src/app/transforms/SqliteTranslator" ;
8
11
12
+ import {
13
+ ISqliteService ,
14
+ SqliteService ,
15
+ } from "../../src/shared/infra/SqliteService" ;
16
+ import { SqliteInMemoryService } from "../services/SqliteInMemoryService" ;
17
+ import { GeolocationResponseSqlite } from "../../src/types/GeolocationSqliteResponse" ;
18
+
9
19
describe ( "[SqliteTransform]" , ( ) => {
10
20
let redisService : IRedisService ;
21
+ let sqliteService : ISqliteService ;
22
+
11
23
let sqliteTransform : SqliteTransform ;
12
24
13
- let getLocationSpy : jest . SpyInstance ;
14
- let setLocationSpy : jest . SpyInstance ;
25
+ let getLocationFromRedisSpy : jest . SpyInstance ;
26
+ let setLocationFromRedisSpy : jest . SpyInstance ;
27
+
28
+ let getLocationFromSqliteSpy : jest . SpyInstance ;
15
29
16
30
beforeAll ( ( ) => {
17
31
redisService = new RedisInMemoryService ( ) ;
18
- sqliteTransform = new SqliteTransform ( redisService ) ;
19
32
20
- getLocationSpy = jest . spyOn ( RedisInMemoryService . prototype , "getLocation" ) ;
21
- setLocationSpy = jest . spyOn ( RedisInMemoryService . prototype , "setLocation" ) ;
33
+ sqliteService = new SqliteService (
34
+ getFilePath ( constants . TRANSLATOR_PATH , "IPs.sqlite" )
35
+ ) ;
36
+
37
+ sqliteTransform = new SqliteTransform ( redisService , sqliteService ) ;
38
+
39
+ getLocationFromSqliteSpy = jest . spyOn (
40
+ SqliteInMemoryService . prototype ,
41
+ "getLocation"
42
+ ) ;
43
+
44
+ getLocationFromRedisSpy = jest . spyOn (
45
+ RedisInMemoryService . prototype ,
46
+ "getLocation"
47
+ ) ;
48
+
49
+ setLocationFromRedisSpy = jest . spyOn (
50
+ RedisInMemoryService . prototype ,
51
+ "setLocation"
52
+ ) ;
22
53
} ) ;
23
54
24
55
describe ( "scenarios with geolocation already exists in cache" , ( ) => {
@@ -58,8 +89,65 @@ describe("[SqliteTransform]", () => {
58
89
}
59
90
} ) ;
60
91
61
- expect ( getLocationSpy ) . toHaveBeenCalledWith ( chunk . ip ) ;
62
- expect ( setLocationSpy ) . not . toHaveBeenCalled ( ) ;
92
+ expect ( getLocationFromRedisSpy ) . toHaveBeenCalledWith ( chunk . ip ) ;
93
+ expect ( setLocationFromRedisSpy ) . not . toHaveBeenCalled ( ) ;
94
+ } ) ;
95
+ } ) ;
96
+
97
+ describe ( "scenarios without geolocation in cache" , ( ) => {
98
+ const ip = "30.46.245.122" ;
99
+ const clientId = "95cdb0f2-9487-5bfd-aeda-bac27dd406fa" ;
100
+ const timestamp = new Date ( ) . getTime ( ) ;
101
+
102
+ const chunk : DataSourceInput = {
103
+ ip,
104
+ clientId,
105
+ timestamp,
106
+ } ;
107
+
108
+ const geolocationResponseSqlite : GeolocationResponseSqlite = {
109
+ ip : "30.46.245.122" ,
110
+ city : "Columbus" ,
111
+ state : "Ohio" ,
112
+ longitude : - 82.89573 ,
113
+ latitude : 39.97883 ,
114
+ country : "United States" ,
115
+ } ;
116
+
117
+ const geolotionOutput : GeolocationOutput = {
118
+ ip,
119
+ clientId,
120
+ timestamp,
121
+ city : geolocationResponseSqlite . city ,
122
+ region : geolocationResponseSqlite . state ,
123
+ country : geolocationResponseSqlite . country ,
124
+ latitude : geolocationResponseSqlite . latitude ,
125
+ longitude : geolocationResponseSqlite . longitude ,
126
+ } ;
127
+
128
+ it ( "should parse data source data to geolocation output" , async ( ) => {
129
+ setLocationFromRedisSpy . mockResolvedValueOnce ( undefined ) ;
130
+ getLocationFromSqliteSpy . mockResolvedValueOnce ( geolocationResponseSqlite ) ;
131
+
132
+ await sqliteTransform . _transform ( chunk , "utf-8" , ( error ) => {
133
+ if ( error ) {
134
+ console . error ( error ) ;
135
+ }
136
+ } ) ;
137
+
138
+ expect ( getLocationFromRedisSpy ) . toHaveBeenCalledWith ( chunk . ip ) ;
139
+ expect ( setLocationFromRedisSpy ) . toHaveBeenCalledWith ( geolotionOutput ) ;
140
+ } ) ;
141
+
142
+ it ( "should stop transform stream if sqlite return an error" , async ( ) => {
143
+ getLocationFromSqliteSpy . mockRejectedValueOnce ( null ) ;
144
+
145
+ await sqliteTransform . _transform ( chunk , "utf-8" , ( error ) => {
146
+ expect ( error ) . toBeInstanceOf ( Error ) ;
147
+ expect ( error ) . toEqual (
148
+ new Error ( "An error occurred while reading the data from sqlite" )
149
+ ) ;
150
+ } ) ;
63
151
} ) ;
64
152
} ) ;
65
153
} ) ;
0 commit comments