cmd_db_exec_test.sh revision 1.1
1# Copyright 2011 Google Inc.
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8# * Redistributions of source code must retain the above copyright
9#   notice, this list of conditions and the following disclaimer.
10# * Redistributions in binary form must reproduce the above copyright
11#   notice, this list of conditions and the following disclaimer in the
12#   documentation and/or other materials provided with the distribution.
13# * Neither the name of Google Inc. nor the names of its contributors
14#   may be used to endorse or promote products derived from this software
15#   without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29
30utils_test_case one_arg
31one_arg_body() {
32    atf_check -s exit:0 -o save:metadata.csv -e empty \
33        kyua db-exec "SELECT * FROM metadata"
34    atf_check -s exit:0 -o ignore -e empty \
35        grep 'schema_version,.*timestamp' metadata.csv
36}
37
38
39utils_test_case many_args
40many_args_body() {
41    atf_check -s exit:0 -o save:metadata.csv -e empty \
42        kyua db-exec SELECT "*" FROM metadata
43    atf_check -s exit:0 -o ignore -e empty \
44        grep 'schema_version,.*timestamp' metadata.csv
45}
46
47
48utils_test_case no_args
49no_args_body() {
50    atf_check -s exit:3 -o empty -e match:"Not enough arguments" kyua db-exec
51    test ! -f .kyua/store.db || atf_fail "Database created but it should" \
52        "not have been"
53}
54
55
56utils_test_case invalid_statement
57invalid_statement_body() {
58    atf_check -s exit:1 -o empty -e match:"SQLite error.*foo" \
59        kyua db-exec foo
60    test -f .kyua/store.db || atf_fail "Database not created as part of" \
61        "initialization"
62}
63
64
65utils_test_case store_flag__default_home
66store_flag__default_home_body() {
67    HOME=home-dir
68    atf_check -s exit:0 -o save:metadata.csv -e empty \
69        kyua db-exec "SELECT * FROM metadata"
70    test -f home-dir/.kyua/store.db || atf_fail "Database not created in" \
71        "the home directory"
72    atf_check -s exit:0 -o ignore -e empty \
73        grep 'schema_version,.*timestamp' metadata.csv
74}
75
76
77utils_test_case store_flag__explicit__ok
78store_flag__explicit__ok_body() {
79    HOME=home-dir
80    atf_check -s exit:0 -o save:metadata.csv -e empty \
81        kyua --logfile=/dev/null db-exec -s store.db "SELECT * FROM metadata"
82    test ! -d home-dir/.kyua || atf_fail "Home directory created but this" \
83        "should not have happened"
84    test -f store.db || atf_fail "Database not created in expected directory"
85    atf_check -s exit:0 -o ignore -e empty \
86        grep 'schema_version,.*timestamp' metadata.csv
87}
88
89
90utils_test_case store_flag__explicit__fail
91store_flag__explicit__fail_head() {
92    atf_set "require.user" "unprivileged"
93}
94store_flag__explicit__fail_body() {
95    mkdir dir
96    chmod 555 dir
97    atf_check -s exit:2 -o empty -e match:"Cannot open.*dir/foo.db" \
98        kyua db-exec --store=dir/foo.db "SELECT * FROM metadata"
99}
100
101
102utils_test_case no_headers_flag
103no_headers_flag_body() {
104    atf_check kyua db-exec "CREATE TABLE data" \
105        "(a INTEGER PRIMARY KEY, b INTEGER, c TEXT)"
106    atf_check kyua db-exec "INSERT INTO data VALUES (65, 43, NULL)"
107    atf_check kyua db-exec "INSERT INTO data VALUES (23, 42, 'foo')"
108
109    cat >expout <<EOF
110a,b,c
11123,42,foo
11265,43,NULL
113EOF
114    atf_check -s exit:0 -o file:expout -e empty \
115        kyua db-exec "SELECT * FROM data ORDER BY a"
116
117    tail -n 2 <expout >expout2
118    atf_check -s exit:0 -o file:expout2 -e empty \
119        kyua db-exec --no-headers "SELECT * FROM data ORDER BY a"
120}
121
122
123atf_init_test_cases() {
124    atf_add_test_case one_arg
125    atf_add_test_case many_args
126    atf_add_test_case no_args
127    atf_add_test_case invalid_statement
128
129    atf_add_test_case store_flag__default_home
130    atf_add_test_case store_flag__explicit__ok
131    atf_add_test_case store_flag__explicit__fail
132
133    atf_add_test_case no_headers_flag
134}
135