@@ -3,36 +3,142 @@ import Store from ".";
3
3
4
4
import { Datastore } from "@google-cloud/datastore" ;
5
5
6
- // TODO: fix e2e tests, we need to skip this for now, e2e tests are broken
7
- describe . skip ( "google-store" , function ( ) {
6
+ type Price = {
7
+ id ?: string ;
8
+ timestamp : number ;
9
+ identifier : string ;
10
+ price : number ;
11
+ } ;
12
+ function makeId ( data : Price ) {
13
+ return [ data . identifier , data . timestamp . toString ( ) . padStart ( 4 , "0" ) ] . join ( "!" ) ;
14
+ }
15
+ function sortPrices ( a : Price , b : Price ) {
16
+ return makeId ( a ) <= makeId ( b ) ? - 1 : 1 ;
17
+ }
18
+ const prices : Price [ ] = [
19
+ { timestamp : 10 , identifier : "a" , price : 100 } ,
20
+ { timestamp : 11 , identifier : "a" , price : 99 } ,
21
+ { timestamp : 9 , identifier : "a" , price : 98 } ,
22
+ { timestamp : 2 , identifier : "a" , price : 82 } ,
23
+ { timestamp : 13 , identifier : "a" , price : 101 } ,
24
+
25
+ { timestamp : 10 , identifier : "b" , price : 1000 } ,
26
+ { timestamp : 11 , identifier : "b" , price : 990 } ,
27
+ { timestamp : 9 , identifier : "b" , price : 980 } ,
28
+ { timestamp : 2 , identifier : "b" , price : 820 } ,
29
+ { timestamp : 13 , identifier : "b" , price : 1001 } ,
30
+ ] ;
31
+ const sortedPrices = [ ...prices ] . sort ( sortPrices ) ;
32
+
33
+ describe ( "google-store" , function ( ) {
8
34
let store : any ;
9
35
let datastore : Datastore ;
10
36
test ( "init" , function ( ) {
11
37
datastore = new Datastore ( ) ;
12
- store = Store ( "testing" , datastore ) ;
38
+ store = Store ( "testing-prices " , datastore ) ;
13
39
assert ( store ) ;
14
40
} ) ;
15
- test ( "delete " , async function ( ) {
41
+ test ( "clear " , async function ( ) {
16
42
try {
17
- await store . delete ( "test" ) ;
43
+ await store . clear ( ) ;
18
44
} catch ( err ) {
19
45
// do nothing
20
46
}
21
47
} ) ;
22
- test ( "set" , async function ( ) {
23
- await store . set ( "test" , { testing : true } ) ;
48
+ test ( "add prices" , async ( ) => {
49
+ for ( const price of prices ) {
50
+ await store . set ( makeId ( price ) , price ) ;
51
+ }
52
+ } ) ;
53
+ test ( "entries" , async function ( ) {
54
+ const result = await store . entries ( ) ;
55
+ assert . equal ( result . length , prices . length ) ;
56
+ } ) ;
57
+ test ( "keys" , async function ( ) {
58
+ const result = await store . keys ( ) ;
59
+ assert . equal ( result . length , prices . length ) ;
60
+ result . forEach ( ( id : string , i : number ) => {
61
+ assert . equal ( id , makeId ( sortedPrices [ i ] ) ) ;
62
+ } ) ;
63
+ } ) ;
64
+ test ( "values" , async function ( ) {
65
+ const result = await store . values ( ) ;
66
+ assert . equal ( result . length , prices . length ) ;
67
+ result . forEach ( ( price : Price , i : number ) => {
68
+ assert . equal ( price . identifier , sortedPrices [ i ] . identifier ) ;
69
+ assert . equal ( price . timestamp , sortedPrices [ i ] . timestamp ) ;
70
+ assert . equal ( price . price , sortedPrices [ i ] . price ) ;
71
+ } ) ;
24
72
} ) ;
25
73
test ( "get" , async function ( ) {
26
- const result = await store . get ( "test" ) ;
27
- assert . deepEqual ( result , { testing : true } ) ;
74
+ const result = await store . get ( makeId ( prices [ 0 ] ) ) ;
75
+ assert . deepEqual ( result , prices [ 0 ] ) ;
28
76
} ) ;
29
77
test ( "has" , async function ( ) {
30
- const result = await store . has ( "test" ) ;
78
+ let result = await store . has ( makeId ( prices [ 0 ] ) ) ;
31
79
assert . equal ( result , true ) ;
80
+
81
+ result = await store . has ( "dne" ) ;
82
+ assert . equal ( result , false ) ;
83
+ } ) ;
84
+ // not supported
85
+ test ( "size" , async function ( ) {
86
+ try {
87
+ await store . size ( ) ;
88
+ } catch ( err ) {
89
+ assert . ok ( err ) ;
90
+ }
91
+ } ) ;
92
+ test ( "between a" , async ( ) => {
93
+ const result = await store . between ( "a" , "b" ) ;
94
+ const answer = sortedPrices . filter ( ( x ) => x . identifier === "a" ) ;
95
+ assert . equal ( result . length , 5 ) ;
96
+ result . forEach ( ( price : Price , i : number ) => {
97
+ assert . equal ( price . identifier , answer [ i ] . identifier ) ;
98
+ assert . equal ( price . timestamp , answer [ i ] . timestamp ) ;
99
+ assert . equal ( price . price , answer [ i ] . price ) ;
100
+ } ) ;
101
+ } ) ;
102
+ test ( "between b" , async ( ) => {
103
+ const result = await store . between ( "b" , "b~" ) ;
104
+ const answer = sortedPrices . filter ( ( x ) => x . identifier === "b" ) ;
105
+ assert . equal ( result . length , 5 ) ;
106
+ result . forEach ( ( price : Price , i : number ) => {
107
+ assert . equal ( price . identifier , answer [ i ] . identifier ) ;
108
+ assert . equal ( price . timestamp , answer [ i ] . timestamp ) ;
109
+ assert . equal ( price . price , answer [ i ] . price ) ;
110
+ } ) ;
111
+ } ) ;
112
+ test ( "slice a" , async ( ) => {
113
+ const len = 4 ;
114
+ const result = await store . slice ( "a" , len ) ;
115
+ assert . equal ( result . length , len ) ;
116
+
117
+ const answer = sortedPrices . filter ( ( x ) => x . identifier === "a" ) . slice ( 0 , len ) ;
118
+ result . forEach ( ( price : Price , i : number ) => {
119
+ assert . equal ( price . identifier , answer [ i ] . identifier ) ;
120
+ assert . equal ( price . timestamp , answer [ i ] . timestamp ) ;
121
+ assert . equal ( price . price , answer [ i ] . price ) ;
122
+ } ) ;
123
+ } ) ;
124
+ test ( "slice b" , async ( ) => {
125
+ const len = 3 ;
126
+ const result = await store . slice ( "b" , len ) ;
127
+ assert . equal ( result . length , len ) ;
128
+
129
+ const answer = sortedPrices . filter ( ( x ) => x . identifier === "b" ) . slice ( 0 , len ) ;
130
+ result . forEach ( ( price : Price , i : number ) => {
131
+ assert . equal ( price . identifier , answer [ i ] . identifier ) ;
132
+ assert . equal ( price . timestamp , answer [ i ] . timestamp ) ;
133
+ assert . equal ( price . price , answer [ i ] . price ) ;
134
+ } ) ;
32
135
} ) ;
33
136
test ( "delete" , async function ( ) {
34
- await store . delete ( "test" ) ;
35
- const result = await store . has ( "test" ) ;
137
+ await store . delete ( makeId ( prices [ 0 ] ) ) ;
138
+ const result = await store . has ( makeId ( prices [ 0 ] ) ) ;
36
139
assert . equal ( result , false ) ;
37
140
} ) ;
141
+ test ( "clear" , async function ( ) {
142
+ await store . clear ( ) ;
143
+ } ) ;
38
144
} ) ;
0 commit comments