The R Project SVN R-packages

Rev

Rev 3808 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3808 Rev 4160
Line 4... Line 4...
4
# or open workspace.db in the current directory (assuming workspace.db is 
4
# or open workspace.db in the current directory (assuming workspace.db is 
5
# a valid workspace file)
5
# a valid workspace file)
6
library(SQLiteDF)
6
library(SQLiteDF)
7
stopifnot(file.exists(".SQLiteDF/workspace.db"))
7
stopifnot(file.exists(".SQLiteDF/workspace.db"))
8
 
8
 
-
 
9
# test all modes
-
 
10
iris <- cbind(iris, integers=1:10, logicals=c(T,F), chars=c("foo","bar"))
-
 
11
 
9
# test creating unnamed sdfs
12
# test creating unnamed sdfs
10
u1.sdf <- sqlite.data.frame(iris)
13
u1.sdf <- sqlite.data.frame(iris)
11
stopifnot(file.exists(".SQLiteDF/data1.db"))
14
stopifnot(file.exists(".SQLiteDF/data1.db"))
12
u2.sdf <- sqlite.data.frame(attenu)
15
u2.sdf <- sqlite.data.frame(attenu)
13
stopifnot(file.exists(".SQLiteDF/data2.db"))
16
stopifnot(file.exists(".SQLiteDF/data2.db"))
14
 
17
 
15
# test classes
18
# test classes
16
stopifnot(class(u1.sdf) == "sqlite.data.frame",
19
stopifnot(class(u1.sdf) == "sqlite.data.frame",
17
          class(u2.sdf) == "sqlite.data.frame")
20
          class(u2.sdf) == "sqlite.data.frame")
18
 
21
 
19
 
-
 
20
 
-
 
21
compareSdfToDf <- function(sdf, df, with.names=TRUE) {
22
compareSdfToDf <- function(sdf, df, with.names=TRUE) {
22
    ncols <- ncol(df)
23
    ncols <- ncol(df)
23
    nrows <- nrow(df)
24
    nrows <- nrow(df)
-
 
25
 
-
 
26
    # test [.sqlite.data.frame
24
    for (i in 1:nrows) { for (j in 1:ncols) {
27
    for (i in 1:nrows) { for (j in 1:ncols) {
25
        if (df[i,j] != sdf[i,j]) stop("Not equal on ", i, ",", j, "\n")
28
        if (df[i,j] != sdf[i,j]) stop("Not equal on ", i, ",", j, "\n")
26
    }}
29
    }}
-
 
30
 
-
 
31
    # test [[.sqlite.data.frame (int arg), [.sqlite.vector, 
-
 
32
    # length.sqlite.vector, has.typeSvec
27
    for (j in 1:ncols) { 
33
    for (j in 1:ncols) { 
28
        sv <- sdf[[j]]; # test sqlite.vector
34
        sv <- sdf[[j]]; # test sqlite.vector
29
        stopifnot(class(sv) == "sqlite.vector", has.typeSvec(sv, class(df[[j]])[1]))
35
        stopifnot(class(sv) == "sqlite.vector", has.typeSvec(sv, class(df[[j]])[1]))
30
        if (length(sv) != nrows) stop("Unexpected # of rows for col", j, "\n")
36
        if (length(sv) != nrows) stop("Unexpected # of rows for col", j, "\n")
31
        for (i in 1:nrows)
37
        for (i in 1:nrows)
32
            if (sv[i] != df[i,j])
38
            if (sv[i] != df[i,j])
33
                stop("Not equal on", i, "on col", j, "\n")
39
                stop("Not equal on row ", i, ", col ", j, "\n")
34
    }
40
    }
-
 
41
 
-
 
42
    # test names.sqlite.data.frame, [[.sqlite.data.frame (name arg), ...
35
    if (with.names) for (j in names(df)) { 
43
    if (with.names) for (j in names(df)) { 
36
        sv <- sdf[[j]]; # test sqlite.vector
44
        sv <- sdf[[j]]; # test sqlite.vector
37
        stopifnot(class(sv) == "sqlite.vector", has.typeSvec(sv, class(df[[j]])[1]))
45
        stopifnot(class(sv) == "sqlite.vector", has.typeSvec(sv, class(df[[j]])[1]))
38
        if (length(sv) != nrows) stop("Unexpected # of rows for col", j, "\n")
46
        if (length(sv) != nrows) stop("Unexpected # of rows for col", j, "\n")
39
        for (i in 1:nrows)
47
        for (i in 1:nrows)
40
            if (sv[i] != df[i,j]) 
48
            if (sv[i] != df[i,j]) 
41
                stop("Not equal on", i, "on col", j, "\n")
49
                stop("Not equal on", i, "on col", j, "\n")
42
    }
50
    }
-
 
51
 
-
 
52
    # quote the 1st element of a vector suitable as arg to sdfSelect(where=)
-
 
53
    makewhere <- function(vect, name) {
-
 
54
        paste("[", name, "]=", 
-
 
55
              switch(class(vect),
-
 
56
                numeric=as.character(vect[1]),
-
 
57
                factor=as.character(as.integer(vect[1])),
-
 
58
                ordered=as.character(as.integer(vect[1])),
-
 
59
                character=paste("'", vect[1], "'", sep=""),
-
 
60
                integer=as.character(vect[1]), 
-
 
61
                logical=as.character(as.integer(vect[1]))),
-
 
62
              sep="")
-
 
63
    }
-
 
64
 
-
 
65
    # test sdfSelect returning vectors
-
 
66
    for (i in 1:ncols) {
-
 
67
        colname = paste("[",names(df)[i],"]", sep="")
-
 
68
        vect <- df[[i]]
-
 
69
 
-
 
70
        # test where arg with the 1st value of the vector
-
 
71
        testwhere = makewhere(vect, names(df)[i])
-
 
72
        print(testwhere)
-
 
73
            
-
 
74
        stopifnot(all.equal(sdfSelect(sdf, select=colname), vect),
-
 
75
                  all.equal(sdfSelect(sdf, select=colname, limit=nrows-1),
-
 
76
                            vect[1:(nrows-1)]),
-
 
77
                  all.equal(sdfSelect(sdf, select=colname, where=testwhere, debug=T),
-
 
78
                            vect[vect == vect[1]]))
-
 
79
    }
-
 
80
 
-
 
81
    # test sdfSelect returning data frames
-
 
82
    select = paste(paste("[", names(df), "]", sep=""), collapse=",")
-
 
83
    where = makewhere(df[[1]], names(df)[1])
-
 
84
 
-
 
85
    # row.names() not equal, & besides sdf returns char & 
-
 
86
    # R has integer row.names
-
 
87
    stopifnot(all.equal(sdfSelect(sdf, select, limit="9,5"), df[10:14,],
-
 
88
                        check.attributes=FALSE),  
-
 
89
              all.equal(sdfSelect(sdf, limit=nrows-1), df[-nrows,],
-
 
90
                        check.attributes=FALSE),
-
 
91
              all.equal(sdfSelect(sdf, where=where), df[df[[1]]==df[1,1],],
-
 
92
                        check.attributes=FALSE))
-
 
93
 
-
 
94
    
43
}
95
}
44
 
96
 
45
# test creating named sdfs
97
# test creating named sdfs
46
iris.sdf <- sqlite.data.frame(iris, "iris")
98
iris.sdf <- sqlite.data.frame(iris, "iris")
47
stopifnot(file.exists(".SQLiteDF/iris.db"))
99
stopifnot(file.exists(".SQLiteDF/iris.db"))
Line 135... Line 187...
135
stopifnot(typeSvec(iris.smat) == mode(iris.mat),
187
stopifnot(typeSvec(iris.smat) == mode(iris.mat),
136
          length(iris.smat) == length(iris.mat),
188
          length(iris.smat) == length(iris.mat),
137
          all(colnames(iris.smat) == colnames(iris.mat)))
189
          all(colnames(iris.smat) == colnames(iris.mat)))
138
          #all(rownames(iris.smat) == rownames(iris.mat))) # still bug here
190
          #all(rownames(iris.smat) == rownames(iris.mat))) # still bug here
139
 
191
 
140
 
-