mongoms.spec.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { describe, expect, it } from 'vitest';
  2. import { replaceMongoDbName } from './mongoms';
  3. describe('replaceMongoDbName', () => {
  4. describe('single-host URIs', () => {
  5. it('should replace database name in basic URI', () => {
  6. const result = replaceMongoDbName('mongodb://localhost:27017/growi_test', 'new_db');
  7. expect(result).toBe('mongodb://localhost:27017/new_db');
  8. });
  9. it('should add database name when URI has no database', () => {
  10. const result = replaceMongoDbName('mongodb://localhost:27017', 'new_db');
  11. expect(result).toBe('mongodb://localhost:27017/new_db');
  12. });
  13. it('should add database name when URI ends with slash', () => {
  14. const result = replaceMongoDbName('mongodb://localhost:27017/', 'new_db');
  15. expect(result).toBe('mongodb://localhost:27017/new_db');
  16. });
  17. it('should preserve query parameters', () => {
  18. const result = replaceMongoDbName('mongodb://localhost:27017?param=value', 'new_db');
  19. expect(result).toBe('mongodb://localhost:27017/new_db?param=value');
  20. });
  21. it('should replace database name and preserve query parameters', () => {
  22. const result = replaceMongoDbName('mongodb://localhost:27017/growi_test?param=value', 'new_db');
  23. expect(result).toBe('mongodb://localhost:27017/new_db?param=value');
  24. });
  25. it('should handle authentication credentials', () => {
  26. const result = replaceMongoDbName('mongodb://user:pass@localhost:27017/growi_test', 'new_db');
  27. expect(result).toBe('mongodb://user:pass@localhost:27017/new_db');
  28. });
  29. it('should handle authentication credentials with query parameters', () => {
  30. const result = replaceMongoDbName('mongodb://user:pass@localhost:27017/growi_test?authSource=admin', 'new_db');
  31. expect(result).toBe('mongodb://user:pass@localhost:27017/new_db?authSource=admin');
  32. });
  33. it('should handle URL-encoded credentials', () => {
  34. const result = replaceMongoDbName('mongodb://user%40name:p%40ss@localhost:27017/growi_test', 'new_db');
  35. expect(result).toBe('mongodb://user%40name:p%40ss@localhost:27017/new_db');
  36. });
  37. });
  38. describe('replica set URIs (multiple hosts)', () => {
  39. it('should replace database name in replica set URI', () => {
  40. const result = replaceMongoDbName('mongodb://host1:27017,host2:27017/growi_test?replicaSet=rs0', 'new_db');
  41. expect(result).toBe('mongodb://host1:27017,host2:27017/new_db?replicaSet=rs0');
  42. });
  43. it('should add database name to replica set URI without database', () => {
  44. const result = replaceMongoDbName('mongodb://host1:27017,host2:27017,host3:27017?replicaSet=rs0', 'new_db');
  45. expect(result).toBe('mongodb://host1:27017,host2:27017,host3:27017/new_db?replicaSet=rs0');
  46. });
  47. it('should handle replica set URI with authentication', () => {
  48. const result = replaceMongoDbName('mongodb://user:pass@host1:27017,host2:27017/growi_test?replicaSet=rs0', 'new_db');
  49. expect(result).toBe('mongodb://user:pass@host1:27017,host2:27017/new_db?replicaSet=rs0');
  50. });
  51. it('should handle replica set URI without query parameters', () => {
  52. const result = replaceMongoDbName('mongodb://host1:27017,host2:27017/growi_test', 'new_db');
  53. expect(result).toBe('mongodb://host1:27017,host2:27017/new_db');
  54. });
  55. });
  56. describe('edge cases', () => {
  57. it('should handle different database names', () => {
  58. const result = replaceMongoDbName('mongodb://localhost:27017/growi_test', 'growi_test_1');
  59. expect(result).toBe('mongodb://localhost:27017/growi_test_1');
  60. });
  61. it('should handle database names with underscores and numbers', () => {
  62. const result = replaceMongoDbName('mongodb://localhost:27017/old_db_123', 'new_db_456');
  63. expect(result).toBe('mongodb://localhost:27017/new_db_456');
  64. });
  65. it('should preserve all query parameters', () => {
  66. const result = replaceMongoDbName(
  67. 'mongodb://localhost:27017/growi_test?authSource=admin&retryWrites=true&w=majority',
  68. 'new_db'
  69. );
  70. expect(result).toBe('mongodb://localhost:27017/new_db?authSource=admin&retryWrites=true&w=majority');
  71. });
  72. });
  73. describe('error handling', () => {
  74. it('should throw error for invalid URI protocol', () => {
  75. // mongodb-connection-string-url validates protocol
  76. expect(() => replaceMongoDbName('http://localhost:27017/db', 'new_db')).toThrow();
  77. });
  78. it('should throw error for malformed URI', () => {
  79. expect(() => replaceMongoDbName('not-a-uri', 'new_db')).toThrow();
  80. });
  81. });
  82. });