@@ -3,96 +3,106 @@ import os from "os";
33import path from "path" ;
44import { createClient } from "redis" ;
55import { getImage } from "../../../testcontainers/src/utils/test-helper" ;
6- import { StartedValkeyContainer , ValkeyContainer } from "./valkey-container" ;
6+ import { ValkeyContainer } from "./valkey-container" ;
77
88const IMAGE = getImage ( __dirname ) ;
99
1010describe ( "ValkeyContainer" , { timeout : 240_000 } , ( ) => {
1111 it ( "should connect and execute set-get" , async ( ) => {
12+ // valkeyStartContainer {
1213 await using container = await new ValkeyContainer ( IMAGE ) . start ( ) ;
1314
14- const client = await connectTo ( container ) ;
15+ const client = createClient ( { url : container . getConnectionUrl ( ) } ) ;
16+ await client . connect ( ) ;
1517
1618 await client . set ( "key" , "val" ) ;
1719 expect ( await client . get ( "key" ) ) . toBe ( "val" ) ;
1820
19- await client . disconnect ( ) ;
21+ client . destroy ( ) ;
22+ // }
2023 } ) ;
2124
2225 it ( "should connect with password and execute set-get" , async ( ) => {
2326 await using container = await new ValkeyContainer ( IMAGE ) . withPassword ( "test" ) . start ( ) ;
2427
25- const client = await connectTo ( container ) ;
28+ const client = createClient ( { url : container . getConnectionUrl ( ) } ) ;
29+ await client . connect ( ) ;
2630
2731 await client . set ( "key" , "val" ) ;
2832 expect ( await client . get ( "key" ) ) . toBe ( "val" ) ;
2933
30- await client . disconnect ( ) ;
34+ client . destroy ( ) ;
3135 } ) ;
3236
3337 it ( "should reconnect with volume and persistence data" , async ( ) => {
38+ // valkeyWithPersistentData {
3439 const sourcePath = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "valkey-" ) ) ;
40+
3541 await using container = await new ValkeyContainer ( IMAGE ) . withPassword ( "test" ) . withPersistence ( sourcePath ) . start ( ) ;
36- let client = await connectTo ( container ) ;
42+
43+ let client = createClient ( { url : container . getConnectionUrl ( ) } ) ;
44+ await client . connect ( ) ;
3745
3846 await client . set ( "key" , "val" ) ;
39- await client . disconnect ( ) ;
47+ client . destroy ( ) ;
48+
4049 await container . restart ( ) ;
41- client = await connectTo ( container ) ;
50+ client = createClient ( { url : container . getConnectionUrl ( ) } ) ;
51+ await client . connect ( ) ;
52+
4253 expect ( await client . get ( "key" ) ) . toBe ( "val" ) ;
4354
44- await client . disconnect ( ) ;
45- try {
46- fs . rmSync ( sourcePath , { force : true , recursive : true } ) ;
47- } catch ( e ) {
48- //Ignore clean up, when have no access on fs.
49- console . log ( e ) ;
50- }
55+ client . destroy ( ) ;
56+ fs . rmSync ( sourcePath , { force : true , recursive : true } ) ;
57+ // }
5158 } ) ;
5259
5360 it ( "should load initial data and can read it" , async ( ) => {
61+ // valkeyWithPredefinedData {
5462 await using container = await new ValkeyContainer ( IMAGE )
5563 . withPassword ( "test" )
5664 . withInitialData ( path . join ( __dirname , "initData.valkey" ) )
5765 . start ( ) ;
58- const client = await connectTo ( container ) ;
66+
67+ const client = createClient ( { url : container . getConnectionUrl ( ) } ) ;
68+ await client . connect ( ) ;
69+
5970 const user = {
6071 first_name : "David" ,
6172 last_name : "Bloom" ,
6273 dob : "03-MAR-1981" ,
6374 } ;
6475 expect ( await client . get ( "user:002" ) ) . toBe ( JSON . stringify ( user ) ) ;
6576
66- await client . disconnect ( ) ;
77+ client . destroy ( ) ;
78+ // }
6779 } ) ;
6880
6981 it ( "should start with credentials and login" , async ( ) => {
82+ // valkeyWithCredentials {
7083 const password = "testPassword" ;
7184
7285 await using container = await new ValkeyContainer ( IMAGE ) . withPassword ( password ) . start ( ) ;
86+
7387 expect ( container . getConnectionUrl ( ) ) . toEqual ( `redis://:${ password } @${ container . getHost ( ) } :${ container . getPort ( ) } ` ) ;
88+ // }
7489
75- const client = await connectTo ( container ) ;
90+ const client = createClient ( { url : container . getConnectionUrl ( ) } ) ;
91+ await client . connect ( ) ;
7692
7793 await client . set ( "key" , "val" ) ;
7894 expect ( await client . get ( "key" ) ) . toBe ( "val" ) ;
7995
80- await client . disconnect ( ) ;
96+ client . destroy ( ) ;
8197 } ) ;
8298
8399 it ( "should execute container cmd and return the result" , async ( ) => {
100+ // valkeyExecuteCommand {
84101 await using container = await new ValkeyContainer ( IMAGE ) . start ( ) ;
85102
86103 const queryResult = await container . executeCliCmd ( "info" , [ "clients" ] ) ;
104+
87105 expect ( queryResult ) . toEqual ( expect . stringContaining ( "connected_clients:1" ) ) ;
106+ // }
88107 } ) ;
89-
90- async function connectTo ( container : StartedValkeyContainer ) {
91- const client = createClient ( {
92- url : container . getConnectionUrl ( ) ,
93- } ) ;
94- await client . connect ( ) ;
95- expect ( client . isOpen ) . toBeTruthy ( ) ;
96- return client ;
97- }
98108} ) ;
0 commit comments