The R Project SVN R-packages

Rev

Rev 4160 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3284 mrmanese 1
library(datasets)
2
 
3
# test if SQLiteDF successfully loaded. it should create (if it does not exist)
4
# or open workspace.db in the current directory (assuming workspace.db is 
5
# a valid workspace file)
6
library(SQLiteDF)
3684 mrmanese 7
stopifnot(file.exists(".SQLiteDF/workspace.db"))
3284 mrmanese 8
 
4160 mrmanese 9
# test all modes
10
iris <- cbind(iris, integers=1:10, logicals=c(T,F), chars=c("foo","bar"))
11
 
3284 mrmanese 12
# test creating unnamed sdfs
13
u1.sdf <- sqlite.data.frame(iris)
3684 mrmanese 14
stopifnot(file.exists(".SQLiteDF/data1.db"))
3284 mrmanese 15
u2.sdf <- sqlite.data.frame(attenu)
3684 mrmanese 16
stopifnot(file.exists(".SQLiteDF/data2.db"))
3284 mrmanese 17
 
3456 mrmanese 18
# test classes
19
stopifnot(class(u1.sdf) == "sqlite.data.frame",
20
          class(u2.sdf) == "sqlite.data.frame")
21
 
3457 mrmanese 22
compareSdfToDf <- function(sdf, df, with.names=TRUE) {
23
    ncols <- ncol(df)
24
    nrows <- nrow(df)
4160 mrmanese 25
 
26
    # test [.sqlite.data.frame
3457 mrmanese 27
    for (i in 1:nrows) { for (j in 1:ncols) {
28
        if (df[i,j] != sdf[i,j]) stop("Not equal on ", i, ",", j, "\n")
29
    }}
4160 mrmanese 30
 
31
    # test [[.sqlite.data.frame (int arg), [.sqlite.vector, 
32
    # length.sqlite.vector, has.typeSvec
3457 mrmanese 33
    for (j in 1:ncols) { 
34
        sv <- sdf[[j]]; # test sqlite.vector
35
        stopifnot(class(sv) == "sqlite.vector", has.typeSvec(sv, class(df[[j]])[1]))
36
        if (length(sv) != nrows) stop("Unexpected # of rows for col", j, "\n")
37
        for (i in 1:nrows)
3458 mrmanese 38
            if (sv[i] != df[i,j])
4160 mrmanese 39
                stop("Not equal on row ", i, ", col ", j, "\n")
4590 mrmanese 40
        all.equal(sv[c(T,F,F)], df[c(T,F,F),j])
3457 mrmanese 41
    }
4160 mrmanese 42
 
43
    # test names.sqlite.data.frame, [[.sqlite.data.frame (name arg), ...
3457 mrmanese 44
    if (with.names) for (j in names(df)) { 
45
        sv <- sdf[[j]]; # test sqlite.vector
46
        stopifnot(class(sv) == "sqlite.vector", has.typeSvec(sv, class(df[[j]])[1]))
47
        if (length(sv) != nrows) stop("Unexpected # of rows for col", j, "\n")
48
        for (i in 1:nrows)
3458 mrmanese 49
            if (sv[i] != df[i,j]) 
50
                stop("Not equal on", i, "on col", j, "\n")
3457 mrmanese 51
    }
4160 mrmanese 52
 
53
    # quote the 1st element of a vector suitable as arg to sdfSelect(where=)
54
    makewhere <- function(vect, name) {
55
        paste("[", name, "]=", 
56
              switch(class(vect),
57
                numeric=as.character(vect[1]),
58
                factor=as.character(as.integer(vect[1])),
59
                ordered=as.character(as.integer(vect[1])),
60
                character=paste("'", vect[1], "'", sep=""),
61
                integer=as.character(vect[1]), 
62
                logical=as.character(as.integer(vect[1]))),
63
              sep="")
64
    }
65
 
66
    # test sdfSelect returning vectors
67
    for (i in 1:ncols) {
68
        colname = paste("[",names(df)[i],"]", sep="")
69
        vect <- df[[i]]
70
 
71
        # test where arg with the 1st value of the vector
72
        testwhere = makewhere(vect, names(df)[i])
73
        print(testwhere)
74
 
75
        stopifnot(all.equal(sdfSelect(sdf, select=colname), vect),
76
                  all.equal(sdfSelect(sdf, select=colname, limit=nrows-1),
77
                            vect[1:(nrows-1)]),
78
                  all.equal(sdfSelect(sdf, select=colname, where=testwhere, debug=T),
79
                            vect[vect == vect[1]]))
80
    }
81
 
82
    # test sdfSelect returning data frames
83
    select = paste(paste("[", names(df), "]", sep=""), collapse=",")
84
    where = makewhere(df[[1]], names(df)[1])
85
 
86
    # row.names() not equal, & besides sdf returns char & 
87
    # R has integer row.names
88
    stopifnot(all.equal(sdfSelect(sdf, select, limit="9,5"), df[10:14,],
89
                        check.attributes=FALSE),  
90
              all.equal(sdfSelect(sdf, limit=nrows-1), df[-nrows,],
91
                        check.attributes=FALSE),
92
              all.equal(sdfSelect(sdf, where=where), df[df[[1]]==df[1,1],],
93
                        check.attributes=FALSE))
94
 
95
 
3457 mrmanese 96
}
97
 
3284 mrmanese 98
# test creating named sdfs
99
iris.sdf <- sqlite.data.frame(iris, "iris")
3684 mrmanese 100
stopifnot(file.exists(".SQLiteDF/iris.db"))
3284 mrmanese 101
iris2.sdf <- sqlite.data.frame(iris, "iris")
3684 mrmanese 102
stopifnot(file.exists(".SQLiteDF/iris1.db"))
3284 mrmanese 103
 
104
# test lsSdf(), w/c queries workspace.db
105
lsSdf() 
3419 mrmanese 106
stopifnot((sort(lsSdf()) == c("data1", "data2", "iris", "iris1")))
3284 mrmanese 107
 
108
# test attach/detach sdf
109
detachSdf("data1")
3419 mrmanese 110
stopifnot(sort(lsSdf()) == c("data2", "iris", "iris1"))
3684 mrmanese 111
attachSdf(".SQLiteDF/data1.db")
3419 mrmanese 112
stopifnot(sort(lsSdf()) == sort(c("data2", "iris", "iris1", "data1")))
3284 mrmanese 113
 
3458 mrmanese 114
# test row.names, head, tail, "==.character"
115
stopifnot(all(row.names(u1.sdf)==row.names(iris)),
116
          all(row.names(u2.sdf)==row.names(attenu)),
117
          all(head(u1.sdf) == head(iris)),
118
          all(head(u2.sdf) == head(attenu)),
119
          all(tail(u1.sdf) == tail(iris)),
120
          all(tail(u2.sdf) == tail(attenu)))
121
 
3284 mrmanese 122
# test sdf methods
3324 mrmanese 123
stopifnot(length(iris.sdf) == length(iris),
124
          length(row.names(iris.sdf)) == length(row.names(iris)),
125
          all(dim(iris.sdf) == dim(iris)),
126
          all(names(iris.sdf) == names(iris)))
3284 mrmanese 127
 
128
# test sdf indexers
3457 mrmanese 129
compareSdfToDf(iris.sdf, iris)
3458 mrmanese 130
#compareSdfToDf(u2.sdf, attenu) # problem with factors
3471 mrmanese 131
stopifnot(all.equal(unlist(iris.sdf),unlist(iris.sdf[])),
132
          all.equal(unlist(iris.sdf),unlist(iris.sdf[,])))
4590 mrmanese 133
tmp <- iris.sdf[1]     # sdf w/ ncol=1
3471 mrmanese 134
stopifnot(nrow(tmp) == nrow(iris.sdf),
135
          names(tmp) == names(iris.sdf)[1],
136
          all.equal(tmp[,1], iris.sdf[,1]))
3358 mrmanese 137
 
3471 mrmanese 138
 
3358 mrmanese 139
# test operators
3471 mrmanese 140
stopifnot(all.equal(iris.sdf[,1] + iris.sdf[,2], iris[,1] + iris[,2]),
141
          all.equal(iris.sdf[,1]*10 %/% iris.sdf[,2],iris[,1]*10 %/% iris[,2]),
142
          all.equal(signif(log(iris.sdf[,1],17)), signif(log(iris[,1],17))),
143
          all.equal(round(log(iris.sdf[,1]),5), round(log(iris[,1]),5)),
144
          all.equal(with(iris.sdf, Sepal.Length*Sepal.Width - Petal.Length/Petal.Width), 
145
                    with(iris, Sepal.Length*Sepal.Width - Petal.Length/Petal.Width)),
146
          all.equal(sort(iris.sdf[,2]), sort(iris[,2])),
3473 mrmanese 147
          all.equal(quantile(iris.sdf[,3]), quantile(iris[,3])),
148
          all.equal(sapply(iris.sdf[,1:4],mean), sapply(iris[,1:4],mean)))
3426 mrmanese 149
 
3432 mrmanese 150
stopifnot(sapply(iris.sdf[,1:4],sum) == sapply(iris[,1:4],sum))
3419 mrmanese 151
 
4590 mrmanese 152
 
153
# test [<-.sqlite.vector
154
ref <- data.frame(
155
        integer=1:10, real=as.real(1:10), 
156
        logical=rep(c(T,F,T),length.out=10), 
157
        character=I(c("one", "two", "three", "four", "five", 
158
                       "six", "seven", "eight", "nine", "ten")))
159
ref.sdf <- sqlite.data.frame(ref)
160
.assign <- function (svec, idx, values, eqvalues=values) {
161
    idxlen <- if (is.logical(idx)) sum(rep(idx, length.out=length(svec))) else length(idx) 
162
    if (is.logical(idx)) idx <- rep(idx, length.out=length(svec))
163
    if (length(values) != idxlen) 
164
        values <- rep(values, length.out=idxlen)
165
    if (length(eqvalues) != idxlen)
166
        eqvalues <- rep(eqvalues, length.out=idxlen)
167
    svec[idx] <- values 
168
    all.equal(svec[idx], eqvalues)
169
}
170
 
171
## test real <- real, int, logical
172
col = ref.sdf[["real"]]
173
stopifnot(.assign(col, 1, 25),
174
          .assign(col, c(1,3,5,7,9), 100),
175
          .assign(col, c(1,3,5,7,9), c(25, 100)),
176
          .assign(col, c(2,4,6,8,10), c(T,F,T), as.real(c(T,F,T))), # w/ logicals
177
          .assign(col, c(4,5,6,7,8), 1:5, as.real(1:5)),            # w/ ints
178
          .assign(col, c(T,F), 1:5, as.real(1:5)))
179
 
180
# test int <- int, logical
181
col = ref.sdf[["integer"]]
182
stopifnot(.assign(col, 1, as.integer(25)),
183
          .assign(col, c(1,3,5,7,9), as.integer(100)),
184
          .assign(col, c(1,3,5,7,9), as.integer(c(25, 100))),
185
          .assign(col, c(2,4,6,8,10), c(T,F,T), as.integer(c(1,0,1))), # w/ logicals
186
          .assign(col, c(4,5,6,7,8), 1:5),                             # w/ ints
187
          .assign(col, c(T,F), 1:5))
188
 
189
# test logical <- logical
190
col = ref.sdf[["logical"]]
191
stopifnot(.assign(col, 1, F),
192
          .assign(col, c(1,3,5,7,9), F),
193
          .assign(col, c(F,T), T))
194
 
195
# test character <- character, real, int, logical
196
col = ref.sdf[["character"]]
197
stopifnot(.assign(col, 1, "isa"),
198
          .assign(col, c(1,3,5,7,9), c("uno", "tatlo", "lima", "pito", "syam")),
199
          .assign(col, 1, 25, "25"),
200
          .assign(col, 1, 3/4, "0.75"),                  # w/ reals
201
          .assign(col, 1:5, as.integer(3), "3"),         # w/ integers
202
          .assign(col, 4:8, c(T,F), c("TRUE","FALSE")))  # w/ logicals
3473 mrmanese 203
# test sdfImportDBI
3458 mrmanese 204
#if (require(RSQLite)) {
205
#    dr <- SQLite()
206
#    con <- dbConnect(dr, dbname="example.db")
207
#    i1 <- sdfImportDBI(con, "select * from iris")
208
#    compareSdfToDf(i1[,1:4], iris[,1:4], with.names=FALSE)
209
#    stopifnot(all(as.character(i1[,5]) == as.character(iris[,5])))
210
#
211
#    i2 <- sdfImportDBI(con, "select * from iris", 30)  # test rbindSdf
212
#    compareSdfToDf(i2[,1:4], iris[,1:4], with.names=FALSE)
213
#    stopifnot(all(as.character(i2[,5]) == as.character(iris[,5])))
214
#}
3457 mrmanese 215
 
3458 mrmanese 216
# test rbindSdf
217
rbindSdf(u2.sdf, attenu)
218
stopifnot(2*nrow(attenu)==nrow(u2.sdf))
219
#print((nrow(attenu)+1):(nrow(u2.sdf)))
220
#compareSdfToDf(u2.sdf[(nrow(attenu)+1):(nrow(u2.sdf)),], attenu)
3457 mrmanese 221
 
3473 mrmanese 222
# test sdfImportSQLite
223
k <- sdfImportSQLite("example.db", "iris")
224
k1 <- k[,2:6]
225
for (i in 1:4) stopifnot(all.equal(k1[,i],iris[,i]))
226
stopifnot(all.equal(levels(k1[,5]),levels(iris[,5])),
227
          all.equal(as.integer(k1[,5]), as.integer(iris[,5])))
228
 
3419 mrmanese 229
# test summary
3473 mrmanese 230
for (j in 1:5) stopifnot(all(summary(iris.sdf[,j]) == summary(iris[,j])))
231
iris.sdf.summary <- summary(iris.sdf)
232
iris.summary <- summary(iris)
233
stopifnot(all(iris.sdf.summary[,1:4] == iris.summary[,1:4]))
234
stopifnot(all(na.exclude(iris.sdf.summary[,5])==na.exclude(iris.summary[,5])))
3509 mrmanese 235
 
3808 mrmanese 236
# test matrices on sdf's
237
iris.mat <- as.matrix(iris)
238
iris.smat <- sqlite.matrix(iris)
239
stopifnot(typeSvec(iris.smat) == mode(iris.mat),
240
          length(iris.smat) == length(iris.mat),
241
          all(colnames(iris.smat) == colnames(iris.mat)))
242
          #all(rownames(iris.smat) == rownames(iris.mat))) # still bug here
3509 mrmanese 243