|
| 1 | +require 'spec_helper' |
| 2 | +require 'dynamoid/log/formatter' |
| 3 | + |
| 4 | +describe Dynamoid::Log::Formatter::Debug do |
| 5 | + describe '#format' do |
| 6 | + let(:subject) { described_class.new } |
| 7 | + |
| 8 | + let(:logger) { Logger.new(buffer) } |
| 9 | + let(:buffer) { StringIO.new } |
| 10 | + |
| 11 | + let(:request) do |
| 12 | + <<~JSON |
| 13 | + { |
| 14 | + "TableName": "dynamoid_tests_items", |
| 15 | + "KeySchema": [ |
| 16 | + { |
| 17 | + "AttributeName": "id", |
| 18 | + "KeyType": "HASH" |
| 19 | + } |
| 20 | + ], |
| 21 | + "AttributeDefinitions": [ |
| 22 | + { |
| 23 | + "AttributeName": "id", |
| 24 | + "AttributeType": "S" |
| 25 | + } |
| 26 | + ], |
| 27 | + "BillingMode": "PROVISIONED", |
| 28 | + "ProvisionedThroughput": { |
| 29 | + "ReadCapacityUnits": 100, |
| 30 | + "WriteCapacityUnits": 20 |
| 31 | + } |
| 32 | + } |
| 33 | + JSON |
| 34 | + end |
| 35 | + |
| 36 | + let(:response_pattern) do |
| 37 | + Regexp.compile <<~JSON |
| 38 | + \\{ |
| 39 | + "TableDescription": \\{ |
| 40 | + "AttributeDefinitions": \\[ |
| 41 | + \\{ |
| 42 | + "AttributeName": "id", |
| 43 | + "AttributeType": "S" |
| 44 | + \\} |
| 45 | + \\], |
| 46 | + "TableName": "dynamoid_tests_items", |
| 47 | + "KeySchema": \\[ |
| 48 | + \\{ |
| 49 | + "AttributeName": "id", |
| 50 | + "KeyType": "HASH" |
| 51 | + \\} |
| 52 | + \\], |
| 53 | + "TableStatus": "ACTIVE", |
| 54 | + "CreationDateTime": .+?, |
| 55 | + "ProvisionedThroughput": \\{ |
| 56 | + "LastIncreaseDateTime": 0.0, |
| 57 | + "LastDecreaseDateTime": 0.0, |
| 58 | + "NumberOfDecreasesToday": 0, |
| 59 | + "ReadCapacityUnits": 100, |
| 60 | + "WriteCapacityUnits": 20 |
| 61 | + \\}, |
| 62 | + "TableSizeBytes": 0, |
| 63 | + "ItemCount": 0, |
| 64 | + "TableArn": ".+?" |
| 65 | + \\} |
| 66 | + \\} |
| 67 | + JSON |
| 68 | + end |
| 69 | + |
| 70 | + before do |
| 71 | + @log_formatter = Dynamoid.config.log_formatter |
| 72 | + @logger = Dynamoid.config.logger |
| 73 | + |
| 74 | + Dynamoid.config.log_formatter = subject |
| 75 | + Dynamoid.config.logger = logger |
| 76 | + Dynamoid.adapter.connect! # clear cached client |
| 77 | + end |
| 78 | + |
| 79 | + after do |
| 80 | + Dynamoid.config.log_formatter = @log_formatter |
| 81 | + Dynamoid.config.logger = @logger |
| 82 | + Dynamoid.adapter.connect! # clear cached client |
| 83 | + end |
| 84 | + |
| 85 | + it 'logs request and response JSON body' do |
| 86 | + new_class(table_name: 'items').create_table |
| 87 | + |
| 88 | + expect(buffer.string).to include(request) |
| 89 | + expect(buffer.string).to match(response_pattern) |
| 90 | + end |
| 91 | + end |
| 92 | +end |
0 commit comments