-
Notifications
You must be signed in to change notification settings - Fork 278
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
69d021d
commit 917bcab
Showing
2 changed files
with
165 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,43 +36,141 @@ func Test_NewMongoClient(t *testing.T) { | |
assert.NotNil(t, client) | ||
} | ||
|
||
func TestGenerateMongoURI(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
config Config | ||
expectedURI string | ||
expectedHost string | ||
expectedError string | ||
}{ | ||
{ | ||
name: "Valid Config", | ||
config: Config{ | ||
User: "admin", | ||
Password: "password", | ||
Host: "localhost", | ||
Port: 27017, | ||
Database: "mydb", | ||
}, | ||
expectedURI: "mongodb://admin:password@localhost:27017/mydb?authSource=admin", | ||
expectedHost: "localhost", | ||
expectedError: "", | ||
}, | ||
{ | ||
name: "Predefined URI", | ||
config: Config{ | ||
URI: "mongodb://admin:password@localhost:27017/mydb?authSource=admin", | ||
}, | ||
expectedURI: "mongodb://admin:password@localhost:27017/mydb?authSource=admin", | ||
expectedHost: "localhost", | ||
expectedError: "", | ||
}, | ||
{ | ||
name: "Empty Host", | ||
config: Config{ | ||
User: "admin", | ||
Password: "password", | ||
Port: 27017, | ||
Database: "mydb", | ||
}, | ||
expectedURI: "", | ||
expectedHost: "", | ||
expectedError: "missing required field in config: host is empty", | ||
}, | ||
{ | ||
name: "Invalid Port", | ||
config: Config{ | ||
User: "admin", | ||
Password: "password", | ||
Host: "localhost", | ||
Database: "mydb", | ||
}, | ||
expectedURI: "", | ||
expectedHost: "", | ||
expectedError: "missing required field in config: port is empty", | ||
}, | ||
{ | ||
name: "Empty Database", | ||
config: Config{ | ||
User: "admin", | ||
Password: "password", | ||
Host: "localhost", | ||
Port: 27017, | ||
}, | ||
expectedURI: "", | ||
expectedHost: "", | ||
expectedError: "missing required field in config: database is empty", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
client := Client{config: &test.config} | ||
uri, host, err := generateMongoURI(client.config) | ||
|
||
assert.Equal(t, test.expectedURI, uri, "Unexpected URI") | ||
assert.Equal(t, test.expectedHost, host, "Unexpected Host") | ||
|
||
if test.expectedError != "" { | ||
assert.EqualError(t, err, test.expectedError, "Unexpected error message") | ||
} else { | ||
assert.NoError(t, err, "Expected no error but got one") | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestGetDBHost(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
uri string | ||
expected string | ||
name string | ||
uri string | ||
expected string | ||
expectedErr string | ||
}{ | ||
{ | ||
name: "Valid URI with host and port", | ||
uri: "mongodb://username:password@hostname:27017/database?authSource=admin", | ||
expected: "hostname", | ||
name: "Valid URI with host and port", | ||
uri: "mongodb://username:password@hostname:27017/database?authSource=admin", | ||
expected: "hostname", | ||
expectedErr: "", | ||
}, | ||
{ | ||
name: "Valid URI with IP address as host", | ||
uri: "mongodb://username:[email protected]:27017/database?authSource=admin", | ||
expected: "192.168.1.1", | ||
name: "Valid URI with IP address as host", | ||
uri: "mongodb://username:[email protected]:27017/database?authSource=admin", | ||
expected: "192.168.1.1", | ||
expectedErr: "", | ||
}, | ||
{ | ||
name: "Invalid URI with no host", | ||
uri: "mongodb://username:password@:27017/database?authSource=admin", | ||
expected: "", | ||
name: "Invalid URI with no host", | ||
uri: "mongodb://username:password@:27017/database?authSource=admin", | ||
expected: "", | ||
expectedErr: "failed to parse host from MongoDB URI", | ||
}, | ||
{ | ||
name: "Empty URI", | ||
uri: "", | ||
expected: "", | ||
name: "Empty URI", | ||
uri: "", | ||
expected: "", | ||
expectedErr: "incorrect URI for mongo", | ||
}, | ||
{ | ||
name: "Malformed URI", | ||
uri: "mongodb:/username:password@hostname:27017/database?authSource=admin", | ||
expected: "", | ||
name: "Malformed URI", | ||
uri: "mongodb:/username:password@hostname:27017/database?authSource=admin", | ||
expected: "", | ||
expectedErr: "failed to parse host from MongoDB URI", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
result := getDBHost(tt.uri) | ||
assert.Equal(t, tt.expected, result, "Test case: %s", tt.name) | ||
host, err := getDBHost(tt.uri) | ||
|
||
assert.Equal(t, tt.expected, host, "Test case: %s", tt.name) | ||
|
||
if tt.expectedErr == "" { | ||
assert.NoError(t, err, "Test case: %s", tt.name) | ||
} else { | ||
assert.EqualError(t, err, tt.expectedErr, "Test case: %s", tt.name) | ||
} | ||
}) | ||
} | ||
} | ||
|