summaryrefslogtreecommitdiff
path: root/spec/PostRunner_spec.rb
blob: 08ed686affd1d98294e6d145387841f93be182e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#!/usr/bin/env ruby -w
# encoding: UTF-8
#
# = PostRunner_spec.rb -- PostRunner - Manage the data from your Garmin sport devices.
#
# Copyright (c) 2014, 2015, 2016 by Chris Schlaeger <cs@taskjuggler.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#

require 'fileutils'

require 'spec_helper'
require 'postrunner/Main'

describe PostRunner::Main do

  def postrunner(args)
    args = [ '--dbdir', @db_dir ] + args
    begin
      old_stdout = $stdout
      old_stderr = $stderr
      $stdout = (stdout = StringIO.new)
      $stderr = (stderr = StringIO.new)
      GC.start
      retval = PostRunner::Main.new.main(args)
    ensure
      $stdout = old_stdout
      $stderr = old_stderr
    end

    stdout.rewind
    stderr.rewind
    { :retval => retval, :stdout => stdout.read, :stderr => stderr.read}
  end

  before(:all) do
    capture_stdio
    create_working_dirs

    @db_dir = File.join(@work_dir, '.postrunner')
    @opts = { :t => '2014-07-01-8:00', :speed => 11.0 }
    @file1 = create_fit_activity_file(@work_dir, @opts)
    @opts[:t] = '2014-07-02-8:00'
    @file2 = create_fit_activity_file(@work_dir, @opts)
    @opts[:t] = '2014-07-03-8:00'
    @opts[:speed] = 12.5
    @file3 = create_fit_activity_file(@work_dir, @opts)
  end

  after(:all) do
    cleanup
  end

  it 'should abort without arguments' do
    v = postrunner([])
    expect(v[:retval]).to eql(-1)
  end

  it 'should abort with bad command' do
    v = postrunner(%w( foobar))
    expect(v[:retval]).to eql(-1)
  end

  it 'should support the -v option' do
    postrunner(%w( --version ))
  end

  it 'should check a FIT file' do
    postrunner([ 'check', @file1 ])
  end

  it 'should list and empty archive' do
    postrunner(%w( list ))
  end

  it 'should import a FIT file' do
    postrunner([ 'import', @file1 ])
  end

  it 'should check the imported file' do
    postrunner(%w( check :1 ))
  end

  it 'should check a FIT file' do
    postrunner([ 'check', @file2 ])
  end

  it 'should list the imported file' do
    v = postrunner(%w( list ))
    expect(v[:stdout].index(File.basename(@file1))).to be_a(Fixnum)
  end

  it 'should import the 2nd FIT file' do
    postrunner([ 'import', @file2 ])
    v = postrunner(%w( list ))
    list = v[:stdout]
    expect(list.index(File.basename(@file1))).to be_a(Fixnum)
    expect(list.index(File.basename(@file2))).to be_a(Fixnum)
  end

  it 'should delete the first file' do
    postrunner(%w( delete :2 ))
    v = postrunner(%w( list ))
    list = v[:stdout]
    expect(list.index(File.basename(@file1))).to be_nil
    expect(list.index(File.basename(@file2))).to be_a(Fixnum)
  end

  it 'should not import the deleted file again' do
    postrunner([ 'import', @file1 ])
    v = postrunner(%w( list ))
    list = v[:stdout]
    expect(list.index(File.basename(@file1))).to be_nil
    expect(list.index(File.basename(@file2))).to be_a(Fixnum)
  end

  it 'should rename FILE2.FIT activity' do
    postrunner(%w( rename foobar :1 ))
    v = postrunner(%w( list ))
    list = v[:stdout]
    expect(list.index(File.basename(@file2))).to be_nil
    expect(list.index('foobar')).to be_a(Fixnum)
  end

  it 'should fail when setting bad attribute' do
    v = postrunner(%w( set foo bar :1))
    expect(v[:retval]).to eql(-1)
  end

  it 'should set name for 2nd activity' do
    postrunner(%w( set name foobar :1 ))
    v = postrunner(%w( list ))
    list = v[:stdout]
    expect(list.index(@file2)).to be_nil
    expect(list.index('foobar')).to be_a(Fixnum)
  end

  it 'should set activity type for 2nd activity' do
    postrunner(%w( set type Cycling :1 ))
    v = postrunner(%w( summary :1 ))
    list = v[:stdout]
    expect(list.index('Running')).to be_nil
    expect(list.index('Cycling')).to be_a(Fixnum)
  end

  it 'should list the events of an activity' do
    postrunner(%w( events :1 ))
  end

  it 'should list the data sources of an activity' do
    postrunner(%w( sources :1 ))
  end

  it 'should fail when setting bad activity type' do
    v = postrunner(%w( set type foobar :1))
    expect(v[:retval]).to eql(-1)
  end

  it 'should set activity subtype for FILE2.FIT activity' do
    postrunner(%w( set subtype Road :1 ))
    v = postrunner(%w( summary :1 ))
    list = v[:stdout]
    expect(list.index('Generic')).to be_nil
    expect(list.index('Road')).to be_a(Fixnum)
  end

  it 'should fail when setting bad activity subtype' do
    v = postrunner(%w( set subtype foobar :1))
    expect(v[:retval]).to eql(-1)
  end

  it 'should dump an activity from the archive' do
    postrunner(%w( dump :1 ))
  end

  it 'should dump a FIT file' do
    postrunner([ 'dump', @file1 ])
  end

  it 'should switch to statute units' do
    postrunner(%w( units statute ))
  end

  it 'should switch back to metric units' do
    postrunner(%w( units metric ))
  end

  it 'should list records' do
    # Add slow running activity
    postrunner([ 'import', '--force', @file1 ])
    v = postrunner([ 'records' ])
    list = v[:stdout]
    expect(list.index(File.basename(@file1))).to be_a(Fixnum)

    # Add fast running activity
    postrunner([ 'import', @file3 ])
    v =postrunner([ 'records' ])
    list = v[:stdout]
    expect(list.index(File.basename(@file3))).to be_a(Fixnum)
    expect(list.index(File.basename(@file1))).to be_nil
  end

  it 'should ignore records of an activity' do
    postrunner(%w( set norecord true :1 ))
    v = postrunner([ 'records' ])
    list = v[:stdout]
    expect(list.index(File.basename(@file1))).to be_a(Fixnum)
    expect(list.index(File.basename(@file3))).to be_nil
  end

  it 'should support the daily command' do
    postrunner([ 'daily' ])
  end

  it 'should supoprt the monthly command' do
    postrunner([ 'monthly' ])
  end

end