@@ -3,64 +3,69 @@ import os from "os";
33import path from "path" ;
44import { createClient } from "redis" ;
55import { getImage } from "../../../testcontainers/src/utils/test-helper" ;
6- import { RedisContainer , StartedRedisContainer } from "./redis-container" ;
6+ import { RedisContainer } from "./redis-container" ;
77
88const IMAGE = getImage ( __dirname ) ;
99
1010describe ( "RedisContainer" , { timeout : 240_000 } , ( ) => {
11- // startContainer {
1211 it ( "should connect and execute set-get" , async ( ) => {
12+ // redisStartContainer {
1313 await using container = await new RedisContainer ( IMAGE ) . start ( ) ;
1414
15- const client = await connectTo ( container ) ;
15+ const client = createClient ( { url : container . getConnectionUrl ( ) } ) ;
16+ await client . connect ( ) ;
1617
1718 await client . set ( "key" , "val" ) ;
1819 expect ( await client . get ( "key" ) ) . toBe ( "val" ) ;
1920
2021 client . destroy ( ) ;
22+ // }
2123 } ) ;
22- // }
2324
2425 it ( "should connect with password and execute set-get" , async ( ) => {
2526 await using container = await new RedisContainer ( IMAGE ) . withPassword ( "test" ) . start ( ) ;
2627
27- const client = await connectTo ( container ) ;
28+ const client = createClient ( { url : container . getConnectionUrl ( ) } ) ;
29+ await client . connect ( ) ;
2830
2931 await client . set ( "key" , "val" ) ;
3032 expect ( await client . get ( "key" ) ) . toBe ( "val" ) ;
3133
3234 client . destroy ( ) ;
3335 } ) ;
3436
35- // persistentData {
3637 it ( "should reconnect with volume and persistence data" , async ( ) => {
38+ // persistentData {
3739 const sourcePath = fs . mkdtempSync ( path . join ( os . tmpdir ( ) , "redis-" ) ) ;
40+
3841 await using container = await new RedisContainer ( IMAGE ) . withPassword ( "test" ) . withPersistence ( sourcePath ) . start ( ) ;
39- let client = await connectTo ( container ) ;
4042
43+ let client = createClient ( { url : container . getConnectionUrl ( ) } ) ;
44+ await client . connect ( ) ;
4145 await client . set ( "key" , "val" ) ;
4246 client . destroy ( ) ;
47+
4348 await container . restart ( ) ;
44- client = await connectTo ( container ) ;
49+ client = createClient ( { url : container . getConnectionUrl ( ) } ) ;
50+ await client . connect ( ) ;
51+
4552 expect ( await client . get ( "key" ) ) . toBe ( "val" ) ;
4653
4754 client . destroy ( ) ;
48- try {
49- fs . rmSync ( sourcePath , { force : true , recursive : true } ) ;
50- } catch ( e ) {
51- //Ignore clean up, when have no access on fs.
52- console . log ( e ) ;
53- }
55+ fs . rmSync ( sourcePath , { force : true , recursive : true } ) ;
56+ // }
5457 } ) ;
55- // }
5658
57- // initial data import {
5859 it ( "should load initial data and can read it" , async ( ) => {
60+ // withPredefinedData {
5961 await using container = await new RedisContainer ( IMAGE )
6062 . withPassword ( "test" )
6163 . withInitialData ( path . join ( __dirname , "initData.redis" ) )
6264 . start ( ) ;
63- const client = await connectTo ( container ) ;
65+
66+ const client = createClient ( { url : container . getConnectionUrl ( ) } ) ;
67+ await client . connect ( ) ;
68+
6469 const user = {
6570 first_name : "David" ,
6671 last_name : "Bloom" ,
@@ -69,58 +74,51 @@ describe("RedisContainer", { timeout: 240_000 }, () => {
6974 expect ( await client . get ( "user:002" ) ) . toBe ( JSON . stringify ( user ) ) ;
7075
7176 client . destroy ( ) ;
77+ // }
7278 } ) ;
73- // }
7479
75- // startWithCredentials {
7680 it ( "should start with credentials and login" , async ( ) => {
81+ // redisStartWithCredentials {
7782 const password = "testPassword" ;
7883
79- // Test authentication
8084 await using container = await new RedisContainer ( IMAGE ) . withPassword ( password ) . start ( ) ;
85+
8186 expect ( container . getConnectionUrl ( ) ) . toEqual ( `redis://:${ password } @${ container . getHost ( ) } :${ container . getPort ( ) } ` ) ;
87+ // }
8288
83- const client = await connectTo ( container ) ;
89+ const client = createClient ( { url : container . getConnectionUrl ( ) } ) ;
90+ await client . connect ( ) ;
8491
8592 await client . set ( "key" , "val" ) ;
8693 expect ( await client . get ( "key" ) ) . toBe ( "val" ) ;
8794
8895 client . destroy ( ) ;
8996 } ) ;
90- // }
9197
92- // executeCommand {
9398 it ( "should execute container cmd and return the result" , async ( ) => {
99+ // executeCommand {
94100 await using container = await new RedisContainer ( IMAGE ) . start ( ) ;
95101
96102 const queryResult = await container . executeCliCmd ( "info" , [ "clients" ] ) ;
103+
97104 expect ( queryResult ) . toEqual ( expect . stringContaining ( "connected_clients:1" ) ) ;
105+ // }
98106 } ) ;
99- // }
100107
101- // startWithRedisStack {
102108 it ( "should start with redis-stack-server and json module" , async ( ) => {
109+ // startWithRedisStack {
103110 await using container = await new RedisContainer ( "redis/redis-stack-server:7.4.0-v4" )
104111 . withPassword ( "testPassword" )
105112 . start ( ) ;
106- const client = await connectTo ( container ) ;
113+
114+ const client = createClient ( { url : container . getConnectionUrl ( ) } ) ;
115+ await client . connect ( ) ;
107116
108117 await client . json . set ( "key" , "$" , { name : "test" } ) ;
109118 const result = await client . json . get ( "key" ) ;
110119 expect ( result ) . toEqual ( { name : "test" } ) ;
111120
112121 client . destroy ( ) ;
122+ // }
113123 } ) ;
114- // }
115-
116- // simpleConnect {
117- async function connectTo ( container : StartedRedisContainer ) {
118- const client = createClient ( {
119- url : container . getConnectionUrl ( ) ,
120- } ) ;
121- await client . connect ( ) ;
122- expect ( client . isOpen ) . toBeTruthy ( ) ;
123- return client ;
124- }
125- // }
126124} ) ;
0 commit comments