|
1 |
| -import { describe, it, expect } from 'vitest' |
| 1 | +import { describe, it, expect, vi } from 'vitest' |
2 | 2 | import { PGlite } from '../dist/index.js'
|
| 3 | +import { expectToThrowAsync } from './test-utils.js' |
3 | 4 |
|
4 | 5 | describe('notify API', () => {
|
5 | 6 | it('notify', async () => {
|
@@ -41,4 +42,128 @@ describe('notify API', () => {
|
41 | 42 |
|
42 | 43 | await new Promise((resolve) => setTimeout(resolve, 1000))
|
43 | 44 | })
|
| 45 | + |
| 46 | + it('check notify case sensitivity + special chars as Postgresql', async () => { |
| 47 | + const pg = new PGlite() |
| 48 | + |
| 49 | + const allLower1 = vi.fn() |
| 50 | + await pg.listen('postgresdefaultlower', allLower1) |
| 51 | + await pg.query(`NOTIFY postgresdefaultlower, 'payload1'`) |
| 52 | + |
| 53 | + const autoLowerTest1 = vi.fn() |
| 54 | + await pg.listen('PostgresDefaultLower', autoLowerTest1) |
| 55 | + await pg.query(`NOTIFY PostgresDefaultLower, 'payload1'`) |
| 56 | + |
| 57 | + const autoLowerTest2 = vi.fn() |
| 58 | + await pg.listen('PostgresDefaultLower', autoLowerTest2) |
| 59 | + await pg.query(`NOTIFY postgresdefaultlower, 'payload1'`) |
| 60 | + |
| 61 | + const autoLowerTest3 = vi.fn() |
| 62 | + await pg.listen('postgresdefaultlower', autoLowerTest3) |
| 63 | + await pg.query(`NOTIFY PostgresDefaultLower, 'payload1'`) |
| 64 | + |
| 65 | + const caseSensitive1 = vi.fn() |
| 66 | + await pg.listen('"tesT2"', caseSensitive1) |
| 67 | + await pg.query(`NOTIFY "tesT2", 'paYloAd2'`) |
| 68 | + |
| 69 | + const caseSensitive2 = vi.fn() |
| 70 | + await pg.listen('"tesT3"', caseSensitive2) |
| 71 | + await pg.query(`NOTIFY tesT3, 'paYloAd2'`) |
| 72 | + |
| 73 | + const caseSensitive3 = vi.fn() |
| 74 | + await pg.listen('testNotCalled2', caseSensitive3) |
| 75 | + await pg.query(`NOTIFY "testNotCalled2", 'paYloAd2'`) |
| 76 | + |
| 77 | + const quotedWithSpaces = vi.fn() |
| 78 | + await pg.listen('"Quoted Channel With Spaces"', quotedWithSpaces) |
| 79 | + await pg.query(`NOTIFY "Quoted Channel With Spaces", 'payload1'`) |
| 80 | + |
| 81 | + const unquotedWithSpaces = vi.fn() |
| 82 | + await expectToThrowAsync( |
| 83 | + pg.listen('Unquoted Channel With Spaces', unquotedWithSpaces), |
| 84 | + ) |
| 85 | + await expectToThrowAsync( |
| 86 | + pg.query(`NOTIFY Unquoted Channel With Spaces, 'payload1'`), |
| 87 | + ) |
| 88 | + |
| 89 | + const otherCharsWithQuotes = vi.fn() |
| 90 | + await pg.listen('"test&me"', otherCharsWithQuotes) |
| 91 | + await pg.query(`NOTIFY "test&me", 'paYloAd2'`) |
| 92 | + |
| 93 | + const otherChars = vi.fn() |
| 94 | + await expectToThrowAsync(pg.listen('test&me', otherChars)) |
| 95 | + await expectToThrowAsync(pg.query(`NOTIFY test&me, 'payload1'`)) |
| 96 | + |
| 97 | + expect(allLower1).toHaveBeenCalledTimes(4) |
| 98 | + expect(autoLowerTest1).toHaveBeenCalledTimes(3) |
| 99 | + expect(autoLowerTest2).toHaveBeenCalledTimes(2) |
| 100 | + expect(autoLowerTest3).toHaveBeenCalledTimes(1) |
| 101 | + expect(caseSensitive1).toHaveBeenCalledOnce() |
| 102 | + expect(caseSensitive2).not.toHaveBeenCalled() |
| 103 | + expect(caseSensitive3).not.toHaveBeenCalled() |
| 104 | + expect(otherCharsWithQuotes).toHaveBeenCalledOnce() |
| 105 | + expect(quotedWithSpaces).toHaveBeenCalledOnce() |
| 106 | + expect(unquotedWithSpaces).not.toHaveBeenCalled() |
| 107 | + }) |
| 108 | + |
| 109 | + it('check unlisten case sensitivity + special chars as Postgresql', async () => { |
| 110 | + const pg = new PGlite() |
| 111 | + |
| 112 | + const allLower1 = vi.fn() |
| 113 | + { |
| 114 | + const unsub1 = await pg.listen('postgresdefaultlower', allLower1) |
| 115 | + await pg.query(`NOTIFY postgresdefaultlower, 'payload1'`) |
| 116 | + await unsub1() |
| 117 | + } |
| 118 | + |
| 119 | + const autoLowerTest1 = vi.fn() |
| 120 | + { |
| 121 | + const unsub2 = await pg.listen('PostgresDefaultLower', autoLowerTest1) |
| 122 | + await pg.query(`NOTIFY PostgresDefaultLower, 'payload1'`) |
| 123 | + await unsub2() |
| 124 | + } |
| 125 | + |
| 126 | + const autoLowerTest2 = vi.fn() |
| 127 | + { |
| 128 | + const unsub3 = await pg.listen('PostgresDefaultLower', autoLowerTest2) |
| 129 | + await pg.query(`NOTIFY postgresdefaultlower, 'payload1'`) |
| 130 | + await unsub3() |
| 131 | + } |
| 132 | + |
| 133 | + const autoLowerTest3 = vi.fn() |
| 134 | + { |
| 135 | + const unsub4 = await pg.listen('postgresdefaultlower', autoLowerTest3) |
| 136 | + await pg.query(`NOTIFY PostgresDefaultLower, 'payload1'`) |
| 137 | + await unsub4() |
| 138 | + } |
| 139 | + |
| 140 | + const caseSensitive1 = vi.fn() |
| 141 | + { |
| 142 | + await pg.listen('"CaSESEnsiTIvE"', caseSensitive1) |
| 143 | + await pg.query(`NOTIFY "CaSESEnsiTIvE", 'payload1'`) |
| 144 | + await pg.unlisten('"CaSESEnsiTIvE"') |
| 145 | + await pg.query(`NOTIFY "CaSESEnsiTIvE", 'payload1'`) |
| 146 | + } |
| 147 | + |
| 148 | + const quotedWithSpaces = vi.fn() |
| 149 | + { |
| 150 | + await pg.listen('"Quoted Channel With Spaces"', quotedWithSpaces) |
| 151 | + await pg.query(`NOTIFY "Quoted Channel With Spaces", 'payload1'`) |
| 152 | + await pg.unlisten('"Quoted Channel With Spaces"') |
| 153 | + } |
| 154 | + |
| 155 | + const otherCharsWithQuotes = vi.fn() |
| 156 | + { |
| 157 | + await pg.listen('"test&me"', otherCharsWithQuotes) |
| 158 | + await pg.query(`NOTIFY "test&me", 'payload'`) |
| 159 | + await pg.unlisten('"test&me"') |
| 160 | + } |
| 161 | + |
| 162 | + expect(allLower1).toHaveBeenCalledOnce() |
| 163 | + expect(autoLowerTest1).toHaveBeenCalledOnce() |
| 164 | + expect(autoLowerTest2).toHaveBeenCalledOnce() |
| 165 | + expect(autoLowerTest3).toHaveBeenCalledOnce() |
| 166 | + expect(caseSensitive1).toHaveBeenCalledOnce() |
| 167 | + expect(otherCharsWithQuotes).toHaveBeenCalledOnce() |
| 168 | + }) |
44 | 169 | })
|
0 commit comments