|
| 1 | +// Copyright 2015-2016, Google, Inc. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +'use strict'; |
| 15 | + |
| 16 | +const proxyquire = require(`proxyquire`).noPreserveCache(); |
| 17 | +const logging = proxyquire(`@google-cloud/logging`, {})(); |
| 18 | +const uuid = require(`node-uuid`); |
| 19 | + |
| 20 | +const logName = `nodejs-docs-samples-test-${uuid.v4()}`; |
| 21 | + |
| 22 | +describe(`logging:quickstart`, () => { |
| 23 | + let logMock, loggingMock, LoggingMock; |
| 24 | + |
| 25 | + after((done) => { |
| 26 | + logging.log(logName).delete(() => { |
| 27 | + // Ignore any error, the topic might not have been created |
| 28 | + done(); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + it(`should log an entry`, (done) => { |
| 33 | + const expectedlogName = `my-log`; |
| 34 | + |
| 35 | + logMock = { |
| 36 | + entry: sinon.stub().returns({}), |
| 37 | + write: (_entry) => { |
| 38 | + assert.deepEqual(_entry, {}); |
| 39 | + |
| 40 | + const log = logging.log(logName); |
| 41 | + const entry = log.entry({ type: `global` }, `Hello, world!`); |
| 42 | + log.write(entry, (err, apiResponse) => { |
| 43 | + assert.ifError(err); |
| 44 | + assert.notEqual(apiResponse, undefined); |
| 45 | + // Logs are eventually consistent |
| 46 | + setTimeout(done, 5000); |
| 47 | + }); |
| 48 | + } |
| 49 | + }; |
| 50 | + loggingMock = { |
| 51 | + log: (_logName) => { |
| 52 | + assert.equal(_logName, expectedlogName); |
| 53 | + return logMock; |
| 54 | + } |
| 55 | + }; |
| 56 | + LoggingMock = sinon.stub().returns(loggingMock); |
| 57 | + |
| 58 | + proxyquire(`../quickstart`, { |
| 59 | + '@google-cloud/logging': LoggingMock |
| 60 | + }); |
| 61 | + }); |
| 62 | +}); |
0 commit comments