|
1 |
| -import { oO } from '@zmotivat0r/o0'; |
2 |
| -import { Cluster } from 'couchbase'; |
3 |
| - |
4 | 1 | import { CouchbaseConnectionFactory } from '../../src/couchbase/couchbase.connection.factory';
|
5 |
| -import { config } from '../../test/__stubs__'; |
| 2 | +import { sleep } from '../../src/utils'; |
| 3 | +import { config, bucketOptions } from '../__stubs__'; |
6 | 4 |
|
7 | 5 | describe('#couchbase', () => {
|
8 | 6 | describe('#CouchbaseConnectionFactory', () => {
|
9 |
| - describe('#createCluster', () => { |
| 7 | + it('should be defined', () => { |
| 8 | + expect(CouchbaseConnectionFactory).toBeDefined(); |
| 9 | + }); |
| 10 | + |
| 11 | + describe('#create', () => { |
10 | 12 | it('should be defined', () => {
|
11 |
| - expect(CouchbaseConnectionFactory.createCluster).toBeDefined(); |
| 13 | + expect(CouchbaseConnectionFactory.create).toBeDefined(); |
12 | 14 | });
|
13 |
| - it('should return cluster object', async () => { |
14 |
| - const [_, cluster] = await oO(CouchbaseConnectionFactory.createCluster(config)); |
15 |
| - expect(cluster).toBeDefined(); |
16 |
| - expect(cluster).toHaveProperty('auther'); |
| 15 | + it('should create an instance', async () => { |
| 16 | + const conn = await CouchbaseConnectionFactory.create(config); |
| 17 | + expect(conn).toBeInstanceOf(CouchbaseConnectionFactory); |
17 | 18 | });
|
18 |
| - it('should return mocked cluster object', async () => { |
19 |
| - const [_, cluster] = await oO( |
20 |
| - CouchbaseConnectionFactory.createCluster({ ...config, mock: true }), |
21 |
| - ); |
22 |
| - expect(cluster).toBeDefined(); |
23 |
| - expect(cluster).toHaveProperty('dsnObj'); |
| 19 | + it('should create an instance with mock', async () => { |
| 20 | + const conn = await CouchbaseConnectionFactory.create({ ...config, mock: true }); |
| 21 | + expect(conn).toBeInstanceOf(CouchbaseConnectionFactory); |
24 | 22 | });
|
25 | 23 | });
|
26 | 24 |
|
27 |
| - describe('#getBucket', () => { |
28 |
| - let cluster: Cluster; |
| 25 | + describe('#methods', () => { |
| 26 | + let conn: CouchbaseConnectionFactory; |
| 27 | + let mocked: CouchbaseConnectionFactory; |
| 28 | + |
| 29 | + async function removeBuckets() { |
| 30 | + const [_, buckets] = await conn.listBuckets(); |
| 31 | + if (buckets && buckets.length) { |
| 32 | + for (let i = 0; i < buckets.length; i++) { |
| 33 | + await conn.removeBucket(buckets[0].name); |
| 34 | + } |
| 35 | + } |
| 36 | + } |
29 | 37 |
|
30 | 38 | beforeAll(async () => {
|
31 |
| - cluster = await CouchbaseConnectionFactory.createCluster(config); |
| 39 | + conn = await CouchbaseConnectionFactory.create(config); |
| 40 | + mocked = await CouchbaseConnectionFactory.create({ ...config, mock: true }); |
| 41 | + await removeBuckets(); |
32 | 42 | });
|
33 | 43 |
|
34 |
| - it('should be defined', () => { |
35 |
| - expect(CouchbaseConnectionFactory.getBucket).toBeDefined(); |
36 |
| - }); |
37 |
| - it('should throw an error', async () => { |
38 |
| - const [err] = await oO( |
39 |
| - CouchbaseConnectionFactory.getBucket(cluster, { ...config, bucket: 'invalid' }), |
40 |
| - ); |
41 |
| - expect(err).toBeInstanceOf(Error); |
42 |
| - }); |
43 |
| - it('should return bucket object', async () => { |
44 |
| - const [_, bucket] = await oO( |
45 |
| - CouchbaseConnectionFactory.getBucket(cluster, config), |
46 |
| - ); |
47 |
| - expect(bucket).toBeDefined(); |
48 |
| - expect((bucket as any)._name).toBe('test'); |
49 |
| - bucket.disconnect(); |
50 |
| - }); |
51 |
| - it('should return mocked bucket object', async () => { |
52 |
| - const [_, bucket] = await oO( |
53 |
| - CouchbaseConnectionFactory.getBucket(cluster, { ...config, mock: true }), |
54 |
| - ); |
55 |
| - expect(bucket).toBeDefined(); |
56 |
| - expect((bucket as any)._name).toBe('default'); |
| 44 | + afterAll(async () => { |
| 45 | + await removeBuckets(); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('#createBucket', () => { |
| 49 | + it('should create a bucket', async () => { |
| 50 | + const [err, ok] = await conn.createBucket(config.bucket, bucketOptions); |
| 51 | + expect(err).toBeUndefined(); |
| 52 | + expect(ok).toBe(true); |
| 53 | + await sleep(3500); |
| 54 | + }); |
| 55 | + it('should return an error', async () => { |
| 56 | + const [err, _] = await conn.createBucket(config.bucket, bucketOptions); |
| 57 | + expect(err).toBeInstanceOf(Error); |
| 58 | + }); |
| 59 | + }); |
| 60 | + |
| 61 | + describe('#listBuckets', () => { |
| 62 | + it('should return an array of buckets', async () => { |
| 63 | + const [_, buckets] = await conn.listBuckets(); |
| 64 | + expect(Array.isArray(buckets)).toBe(true); |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + describe('#getBucket', () => { |
| 69 | + it('should return an error', async () => { |
| 70 | + const [err, _] = await conn.getBucket('invalid'); |
| 71 | + expect(err).toBeInstanceOf(Error); |
| 72 | + }); |
| 73 | + it('should return a bucket', async () => { |
| 74 | + const [_, bucket] = await conn.getBucket(config.bucket); |
| 75 | + expect(bucket).toBeDefined(); |
| 76 | + bucket.disconnect(); |
| 77 | + }); |
| 78 | + it('should return a bucket with mock', async () => { |
| 79 | + const [_, bucket] = await mocked.getBucket(config.bucket); |
| 80 | + expect(bucket).toBeDefined(); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('#removeBucket', () => { |
| 85 | + it('should return an error', async () => { |
| 86 | + const [err, _] = await conn.removeBucket('invalid'); |
| 87 | + expect(err).toBeInstanceOf(Error); |
| 88 | + }); |
| 89 | + it('should remove a bucket', async () => { |
| 90 | + const [_, ok] = await conn.removeBucket(config.bucket); |
| 91 | + expect(ok).toBe(true); |
| 92 | + }); |
57 | 93 | });
|
58 | 94 | });
|
59 | 95 | });
|
|
0 commit comments