|
| 1 | +package dsn_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "go.rtnl.ai/x/assert" |
| 7 | + "go.rtnl.ai/x/dsn" |
| 8 | +) |
| 9 | + |
| 10 | +func TestParse(t *testing.T) { |
| 11 | + t.Run("Valid", func(t *testing.T) { |
| 12 | + testCases := []struct { |
| 13 | + uri string |
| 14 | + expected *dsn.DSN |
| 15 | + }{ |
| 16 | + { |
| 17 | + "sqlite3:///path/to/test.db", |
| 18 | + &dsn.DSN{Provider: "sqlite3", Path: "path/to/test.db"}, |
| 19 | + }, |
| 20 | + { |
| 21 | + "sqlite3:////absolute/path/test.db", |
| 22 | + &dsn.DSN{Provider: "sqlite3", Path: "/absolute/path/test.db"}, |
| 23 | + }, |
| 24 | + { |
| 25 | + "leveldb:///path/to/db", |
| 26 | + &dsn.DSN{Provider: "leveldb", Path: "path/to/db"}, |
| 27 | + }, |
| 28 | + { |
| 29 | + "leveldb:////absolute/path/db", |
| 30 | + &dsn.DSN{Provider: "leveldb", Path: "/absolute/path/db"}, |
| 31 | + }, |
| 32 | + { |
| 33 | + "postgresql://janedoe:mypassword@localhost:5432/mydb?schema=sample", |
| 34 | + &dsn.DSN{Provider: "postgresql", User: &dsn.UserInfo{Username: "janedoe", Password: "mypassword"}, Host: "localhost", Port: 5432, Path: "mydb", Options: dsn.Options{"schema": "sample"}}, |
| 35 | + }, |
| 36 | + { |
| 37 | + "postgresql+psycopg2://janedoe:mypassword@localhost:5432/mydb?schema=sample", |
| 38 | + &dsn.DSN{Provider: "postgresql", Driver: "psycopg2", User: &dsn.UserInfo{Username: "janedoe", Password: "mypassword"}, Host: "localhost", Port: 5432, Path: "mydb", Options: dsn.Options{"schema": "sample"}}, |
| 39 | + }, |
| 40 | + { |
| 41 | + "mysql://janedoe:mypassword@localhost:3306/mydb", |
| 42 | + &dsn.DSN{Provider: "mysql", User: &dsn.UserInfo{Username: "janedoe", Password: "mypassword"}, Host: "localhost", Port: 3306, Path: "mydb"}, |
| 43 | + }, |
| 44 | + { |
| 45 | + "mysql+odbc://janedoe:mypassword@localhost:3306/mydb", |
| 46 | + &dsn.DSN{Provider: "mysql", Driver: "odbc", User: &dsn.UserInfo{Username: "janedoe", Password: "mypassword"}, Host: "localhost", Port: 3306, Path: "mydb"}, |
| 47 | + }, |
| 48 | + { |
| 49 | + "cockroachdb+postgresql://janedoe:mypassword@localhost:26257/mydb?schema=public", |
| 50 | + &dsn.DSN{Provider: "cockroachdb", Driver: "postgresql", User: &dsn.UserInfo{Username: "janedoe", Password: "mypassword"}, Host: "localhost", Port: 26257, Path: "mydb", Options: dsn.Options{"schema": "public"}}, |
| 51 | + }, |
| 52 | + { |
| 53 | + "mongodb+srv://root:[email protected]/myDatabase?retryWrites=true&w=majority", |
| 54 | + &dsn.DSN{Provider: "mongodb", Driver: "srv", User: &dsn.UserInfo{Username: "root", Password: "password"}, Host: "cluster0.ab1cd.mongodb.net", Path: "myDatabase", Options: dsn.Options{"retryWrites": "true", "w": "majority"}}, |
| 55 | + }, |
| 56 | + } |
| 57 | + |
| 58 | + for i, tc := range testCases { |
| 59 | + actual, err := dsn.Parse(tc.uri) |
| 60 | + assert.Ok(t, err, "test case %d failed", i) |
| 61 | + assert.Equal(t, tc.expected, actual, "test case %d failed", i) |
| 62 | + } |
| 63 | + |
| 64 | + }) |
| 65 | + |
| 66 | + t.Run("Invalid", func(t *testing.T) { |
| 67 | + testCases := []struct { |
| 68 | + uri string |
| 69 | + err error |
| 70 | + }{ |
| 71 | + {"", dsn.ErrCannotParseDSN}, |
| 72 | + {"sqlite3://", dsn.ErrCannotParseDSN}, |
| 73 | + {"postgresql://jdoe:<mypassword>@localhost:foo/mydb", dsn.ErrCannotParseDSN}, |
| 74 | + {"postgresql://localhost:foo/mydb", dsn.ErrCannotParseDSN}, |
| 75 | + {"mysql+odbc+sand://jdoe:mypassword@localhost:3306/mydb", dsn.ErrCannotParseProvider}, |
| 76 | + {"postgresql://jdoe:mypassword@localhost:656656/mydb", dsn.ErrCannotParsePort}, |
| 77 | + } |
| 78 | + |
| 79 | + for i, tc := range testCases { |
| 80 | + _, err := dsn.Parse(tc.uri) |
| 81 | + assert.ErrorIs(t, err, tc.err, "test case %d failed", i) |
| 82 | + } |
| 83 | + }) |
| 84 | +} |
| 85 | + |
| 86 | +func TestString(t *testing.T) { |
| 87 | + testCases := []struct { |
| 88 | + expected string |
| 89 | + uri *dsn.DSN |
| 90 | + }{ |
| 91 | + { |
| 92 | + "sqlite3:///path/to/test.db", |
| 93 | + &dsn.DSN{Provider: "sqlite3", Path: "path/to/test.db"}, |
| 94 | + }, |
| 95 | + { |
| 96 | + "sqlite3:////absolute/path/test.db", |
| 97 | + &dsn.DSN{Provider: "sqlite3", Path: "/absolute/path/test.db"}, |
| 98 | + }, |
| 99 | + { |
| 100 | + "leveldb:///path/to/db", |
| 101 | + &dsn.DSN{Provider: "leveldb", Path: "path/to/db"}, |
| 102 | + }, |
| 103 | + { |
| 104 | + "leveldb:////absolute/path/db", |
| 105 | + &dsn.DSN{Provider: "leveldb", Path: "/absolute/path/db"}, |
| 106 | + }, |
| 107 | + { |
| 108 | + "postgresql://localhost:5432/mydb?schema=sample", |
| 109 | + &dsn.DSN{Provider: "postgresql", Host: "localhost", Port: 5432, Path: "mydb", Options: dsn.Options{"schema": "sample"}}, |
| 110 | + }, |
| 111 | + { |
| 112 | + "postgresql+psycopg2://janedoe:mypassword@localhost:5432/mydb?schema=sample", |
| 113 | + &dsn.DSN{Provider: "postgresql", Driver: "psycopg2", User: &dsn.UserInfo{Username: "janedoe", Password: "mypassword"}, Host: "localhost", Port: 5432, Path: "mydb", Options: dsn.Options{"schema": "sample"}}, |
| 114 | + }, |
| 115 | + { |
| 116 | + "mysql://janedoe:mypassword@localhost:3306/mydb", |
| 117 | + &dsn.DSN{Provider: "mysql", User: &dsn.UserInfo{Username: "janedoe", Password: "mypassword"}, Host: "localhost", Port: 3306, Path: "mydb"}, |
| 118 | + }, |
| 119 | + { |
| 120 | + "mysql+odbc://janedoe@localhost:3306/mydb", |
| 121 | + &dsn.DSN{Provider: "mysql", Driver: "odbc", User: &dsn.UserInfo{Username: "janedoe"}, Host: "localhost", Port: 3306, Path: "mydb"}, |
| 122 | + }, |
| 123 | + { |
| 124 | + "cockroachdb+postgresql://janedoe:mypassword@localhost:26257/mydb?schema=public", |
| 125 | + &dsn.DSN{Provider: "cockroachdb", Driver: "postgresql", User: &dsn.UserInfo{Username: "janedoe", Password: "mypassword"}, Host: "localhost", Port: 26257, Path: "mydb", Options: dsn.Options{"schema": "public"}}, |
| 126 | + }, |
| 127 | + { |
| 128 | + "mongodb+srv://root:[email protected]/myDatabase?retryWrites=true&w=majority", |
| 129 | + &dsn.DSN{Provider: "mongodb", Driver: "srv", User: &dsn.UserInfo{Username: "root", Password: "password"}, Host: "cluster0.ab1cd.mongodb.net", Path: "myDatabase", Options: dsn.Options{"retryWrites": "true", "w": "majority"}}, |
| 130 | + }, |
| 131 | + { |
| 132 | + "cockroachdb://localhost:26257/mydb", |
| 133 | + &dsn.DSN{Driver: "cockroachdb", Host: "localhost", Port: 26257, Path: "mydb"}, |
| 134 | + }, |
| 135 | + { |
| 136 | + "//localhost:26257/mydb", |
| 137 | + &dsn.DSN{Host: "localhost", Port: 26257, Path: "mydb"}, |
| 138 | + }, |
| 139 | + { |
| 140 | + "/mydb", |
| 141 | + &dsn.DSN{Path: "mydb"}, |
| 142 | + }, |
| 143 | + } |
| 144 | + |
| 145 | + for i, tc := range testCases { |
| 146 | + actual := tc.uri.String() |
| 147 | + assert.Equal(t, tc.expected, actual, "test case %d failed", i) |
| 148 | + } |
| 149 | +} |
0 commit comments