|
| 1 | +// The MIT License (MIT) |
| 2 | +// |
| 3 | +// Copyright (c) 2017 Firebase |
| 4 | +// |
| 5 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +// of this software and associated documentation files (the "Software"), to deal |
| 7 | +// in the Software without restriction, including without limitation the rights |
| 8 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +// copies of the Software, and to permit persons to whom the Software is |
| 10 | +// furnished to do so, subject to the following conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be included in all |
| 13 | +// copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +// SOFTWARE. |
| 22 | + |
| 23 | +import { expect } from 'chai'; |
| 24 | + |
| 25 | +import * as functions from '../src/index'; |
| 26 | + |
| 27 | +describe('FunctionBuilder', () => { |
| 28 | + before(() => { |
| 29 | + process.env.GCLOUD_PROJECT = 'not-a-project'; |
| 30 | + }); |
| 31 | + |
| 32 | + after(() => { |
| 33 | + delete process.env.GCLOUD_PROJECT; |
| 34 | + }); |
| 35 | + |
| 36 | + it('should allow region to be set', () => { |
| 37 | + let fn = functions |
| 38 | + .region('my-region') |
| 39 | + .auth.user() |
| 40 | + .onCreate(user => user); |
| 41 | + |
| 42 | + expect(fn.__trigger.regions).to.deep.equal(['my-region']); |
| 43 | + }); |
| 44 | + |
| 45 | + it('should allow multiple regions to be set', () => { |
| 46 | + let fn = functions |
| 47 | + .region('my-region', 'my-other-region') |
| 48 | + .auth.user() |
| 49 | + .onCreate(user => user); |
| 50 | + |
| 51 | + expect(fn.__trigger.regions).to.deep.equal([ |
| 52 | + 'my-region', |
| 53 | + 'my-other-region', |
| 54 | + ]); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should allow runtime options to be set', () => { |
| 58 | + let fn = functions |
| 59 | + .runWith({ |
| 60 | + timeoutSeconds: 90, |
| 61 | + memory: '256MB', |
| 62 | + }) |
| 63 | + .auth.user() |
| 64 | + .onCreate(user => user); |
| 65 | + |
| 66 | + expect(fn.__trigger.availableMemoryMb).to.deep.equal(256); |
| 67 | + expect(fn.__trigger.timeout).to.deep.equal('90s'); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should allow both region and runtime options to be set', () => { |
| 71 | + let fn = functions |
| 72 | + .region('my-region') |
| 73 | + .runWith({ |
| 74 | + timeoutSeconds: 90, |
| 75 | + memory: '256MB', |
| 76 | + }) |
| 77 | + .auth.user() |
| 78 | + .onCreate(user => user); |
| 79 | + |
| 80 | + expect(fn.__trigger.regions).to.deep.equal(['my-region']); |
| 81 | + expect(fn.__trigger.availableMemoryMb).to.deep.equal(256); |
| 82 | + expect(fn.__trigger.timeout).to.deep.equal('90s'); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should allow both region and runtime options to be set (reverse order)', () => { |
| 86 | + let fn = functions |
| 87 | + .runWith({ |
| 88 | + timeoutSeconds: 90, |
| 89 | + memory: '256MB', |
| 90 | + }) |
| 91 | + .region('my-region') |
| 92 | + .auth.user() |
| 93 | + .onCreate(user => user); |
| 94 | + |
| 95 | + expect(fn.__trigger.regions).to.deep.equal(['my-region']); |
| 96 | + expect(fn.__trigger.availableMemoryMb).to.deep.equal(256); |
| 97 | + expect(fn.__trigger.timeout).to.deep.equal('90s'); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should throw an error if user chooses an unsupported memory allocation', () => { |
| 101 | + expect(() => { |
| 102 | + return functions.runWith({ |
| 103 | + memory: 'unsupported', |
| 104 | + } as any); |
| 105 | + }).to.throw(Error); |
| 106 | + |
| 107 | + expect(() => { |
| 108 | + return functions.region('some-region').runWith({ |
| 109 | + memory: 'unsupported', |
| 110 | + } as any); |
| 111 | + }).to.throw(Error); |
| 112 | + }); |
| 113 | +}); |
0 commit comments