-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for creating namespaced databases
- Loading branch information
Showing
3 changed files
with
163 additions
and
16 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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package db | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/xmtp/xmtpd/pkg/testutils" | ||
) | ||
|
||
func TestNamespacedDB(t *testing.T) { | ||
startingDsn := testutils.LocalTestDBDSNPrefix + "/foo" + testutils.LocalTestDBDSNSuffix | ||
newDBName := testutils.RandomString(32) | ||
// Create namespaced DB | ||
namespacedDB, err := NewNamespacedDB( | ||
context.Background(), | ||
startingDsn, | ||
newDBName, | ||
time.Second, | ||
time.Second, | ||
) | ||
t.Cleanup(func() { namespacedDB.Close() }) | ||
require.NoError(t, err) | ||
|
||
result, err := namespacedDB.Query("SELECT current_database();") | ||
require.NoError(t, err) | ||
defer result.Close() | ||
|
||
require.True(t, result.Next()) | ||
var dbName string | ||
err = result.Scan(&dbName) | ||
require.NoError(t, err) | ||
require.Equal(t, newDBName, dbName) | ||
} | ||
|
||
func TestNamespaceRepeat(t *testing.T) { | ||
startingDsn := testutils.LocalTestDBDSNPrefix + "/foo" + testutils.LocalTestDBDSNSuffix | ||
newDBName := testutils.RandomString(32) | ||
// Create namespaced DB | ||
db1, err := NewNamespacedDB( | ||
context.Background(), | ||
startingDsn, | ||
newDBName, | ||
time.Second, | ||
time.Second, | ||
) | ||
require.NoError(t, err) | ||
require.NotNil(t, db1) | ||
t.Cleanup(func() { db1.Close() }) | ||
|
||
// Create again with the same name | ||
db2, err := NewNamespacedDB( | ||
context.Background(), | ||
startingDsn, | ||
newDBName, | ||
time.Second, | ||
time.Second, | ||
) | ||
require.NoError(t, err) | ||
require.NotNil(t, db2) | ||
t.Cleanup(func() { db2.Close() }) | ||
} |
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