summaryrefslogtreecommitdiff
path: root/spec/PostRunner_spec.rb
blob: 437438e51dc091ccfab4c231d6f144d528a4b1c3 (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
#!/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
    old_stdout = $stdout
    $stdout = (stdout = StringIO.new)
    @postrunner = nil
    GC.start
    @postrunner = PostRunner::Main.new(args)
    $stdout = old_stdout
    stdout.string
  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
    expect { postrunner([]) }.to raise_error(Fit4Ruby::Error)
  end

  it 'should abort with bad command' do
    expect { postrunner(%w( foobar)) }.to raise_error(Fit4Ruby::Error)
  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
    expect(postrunner(%w( list )).index(File.basename(@file1))).to be_a(Fixnum)
  end

  it 'should import the 2nd FIT file' do
    postrunner([ 'import', @file2 ])
    list = postrunner(%w( list ))
    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 ))
    list = postrunner(%w( list ))
    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 ])
    list = postrunner(%w( list ))
    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 ))
    list = postrunner(%w( list ))
    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
    expect { postrunner(%w( set foo bar :1)) }.to raise_error(Fit4Ruby::Error)
  end

  it 'should set name for 2nd activity' do
    postrunner(%w( set name foobar :1 ))
    list = postrunner(%w( list ))
    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 ))
    list = postrunner(%w( summary :1 ))
    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
    expect { postrunner(%w( set type foobar :1)) }.to raise_error(Fit4Ruby::Error)
  end

  it 'should set activity subtype for FILE2.FIT activity' do
    postrunner(%w( set subtype Road :1 ))
    list = postrunner(%w( summary :1 ))
    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
    expect { postrunner(%w( set subtype foobar :1)) }.to raise_error(Fit4Ruby::Error)
  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 ])
    list = postrunner([ 'records' ])
    expect(list.index(File.basename(@file1))).to be_a(Fixnum)

    # Add fast running activity
    postrunner([ 'import', @file3 ])
    list = postrunner([ 'records' ])
    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 ))
    list = postrunner([ 'records' ])
    expect(list.index(File.basename(@file1))).to be_a(Fixnum)
    expect(list.index(File.basename(@file3))).to be_nil
  end

end