@@ -371,22 +371,49 @@ test_that("Collect DataFrame with complex types", {
371371 expect_equal(bob $ height , 176.5 )
372372})
373373
374- test_that(" read.json()/jsonFile() on a local file returns a DataFrame" , {
374+ test_that(" read/write json files" , {
375+ # Test read.df
376+ df <- read.df(sqlContext , jsonPath , " json" )
377+ expect_is(df , " DataFrame" )
378+ expect_equal(count(df ), 3 )
379+
380+ # Test read.df with a user defined schema
381+ schema <- structType(structField(" name" , type = " string" ),
382+ structField(" age" , type = " double" ))
383+
384+ df1 <- read.df(sqlContext , jsonPath , " json" , schema )
385+ expect_is(df1 , " DataFrame" )
386+ expect_equal(dtypes(df1 ), list (c(" name" , " string" ), c(" age" , " double" )))
387+
388+ # Test loadDF
389+ df2 <- loadDF(sqlContext , jsonPath , " json" , schema )
390+ expect_is(df2 , " DataFrame" )
391+ expect_equal(dtypes(df2 ), list (c(" name" , " string" ), c(" age" , " double" )))
392+
393+ # Test read.json
375394 df <- read.json(sqlContext , jsonPath )
376395 expect_is(df , " DataFrame" )
377396 expect_equal(count(df ), 3 )
378- # read.json()/jsonFile() works with multiple input paths
397+
398+ # Test write.df
379399 jsonPath2 <- tempfile(pattern = " jsonPath2" , fileext = " .json" )
380400 write.df(df , jsonPath2 , " json" , mode = " overwrite" )
381- jsonDF1 <- read.json(sqlContext , c(jsonPath , jsonPath2 ))
401+
402+ # Test write.json
403+ jsonPath3 <- tempfile(pattern = " jsonPath3" , fileext = " .json" )
404+ write.json(df , jsonPath3 )
405+
406+ # Test read.json()/jsonFile() works with multiple input paths
407+ jsonDF1 <- read.json(sqlContext , c(jsonPath2 , jsonPath3 ))
382408 expect_is(jsonDF1 , " DataFrame" )
383409 expect_equal(count(jsonDF1 ), 6 )
384410 # Suppress warnings because jsonFile is deprecated
385- jsonDF2 <- suppressWarnings(jsonFile(sqlContext , c(jsonPath , jsonPath2 )))
411+ jsonDF2 <- suppressWarnings(jsonFile(sqlContext , c(jsonPath2 , jsonPath3 )))
386412 expect_is(jsonDF2 , " DataFrame" )
387413 expect_equal(count(jsonDF2 ), 6 )
388414
389415 unlink(jsonPath2 )
416+ unlink(jsonPath3 )
390417})
391418
392419test_that(" jsonRDD() on a RDD with json string" , {
@@ -454,6 +481,9 @@ test_that("insertInto() on a registered table", {
454481 expect_equal(count(sql(sqlContext , " select * from table1" )), 2 )
455482 expect_equal(first(sql(sqlContext , " select * from table1 order by age" ))$ name , " Bob" )
456483 dropTempTable(sqlContext , " table1" )
484+
485+ unlink(jsonPath2 )
486+ unlink(parquetPath2 )
457487})
458488
459489test_that(" table() returns a new DataFrame" , {
@@ -848,33 +878,6 @@ test_that("column calculation", {
848878 expect_equal(count(df2 ), 3 )
849879})
850880
851- test_that(" read.df() from json file" , {
852- df <- read.df(sqlContext , jsonPath , " json" )
853- expect_is(df , " DataFrame" )
854- expect_equal(count(df ), 3 )
855-
856- # Check if we can apply a user defined schema
857- schema <- structType(structField(" name" , type = " string" ),
858- structField(" age" , type = " double" ))
859-
860- df1 <- read.df(sqlContext , jsonPath , " json" , schema )
861- expect_is(df1 , " DataFrame" )
862- expect_equal(dtypes(df1 ), list (c(" name" , " string" ), c(" age" , " double" )))
863-
864- # Run the same with loadDF
865- df2 <- loadDF(sqlContext , jsonPath , " json" , schema )
866- expect_is(df2 , " DataFrame" )
867- expect_equal(dtypes(df2 ), list (c(" name" , " string" ), c(" age" , " double" )))
868- })
869-
870- test_that(" write.df() as parquet file" , {
871- df <- read.df(sqlContext , jsonPath , " json" )
872- write.df(df , parquetPath , " parquet" , mode = " overwrite" )
873- df2 <- read.df(sqlContext , parquetPath , " parquet" )
874- expect_is(df2 , " DataFrame" )
875- expect_equal(count(df2 ), 3 )
876- })
877-
878881test_that(" test HiveContext" , {
879882 ssc <- callJMethod(sc , " sc" )
880883 hiveCtx <- tryCatch({
@@ -895,6 +898,8 @@ test_that("test HiveContext", {
895898 df3 <- sql(hiveCtx , " select * from json2" )
896899 expect_is(df3 , " DataFrame" )
897900 expect_equal(count(df3 ), 3 )
901+
902+ unlink(jsonPath2 )
898903})
899904
900905test_that(" column operators" , {
@@ -1333,6 +1338,9 @@ test_that("join() and merge() on a DataFrame", {
13331338 expect_error(merge(df , df3 ),
13341339 paste(" The following column name: name_y occurs more than once in the 'DataFrame'." ,
13351340 " Please use different suffixes for the intersected columns." , sep = " " ))
1341+
1342+ unlink(jsonPath2 )
1343+ unlink(jsonPath3 )
13361344})
13371345
13381346test_that(" toJSON() returns an RDD of the correct values" , {
@@ -1396,6 +1404,8 @@ test_that("unionAll(), rbind(), except(), and intersect() on a DataFrame", {
13961404
13971405 # Test base::intersect is working
13981406 expect_equal(length(intersect(1 : 20 , 3 : 23 )), 18 )
1407+
1408+ unlink(jsonPath2 )
13991409})
14001410
14011411test_that(" withColumn() and withColumnRenamed()" , {
@@ -1440,31 +1450,35 @@ test_that("mutate(), transform(), rename() and names()", {
14401450 detach(airquality )
14411451})
14421452
1443- test_that(" write.df() on DataFrame and works with read.parquet" , {
1444- df <- read.json(sqlContext , jsonPath )
1453+ test_that(" read/write Parquet files" , {
1454+ df <- read.df(sqlContext , jsonPath , " json" )
1455+ # Test write.df and read.df
14451456 write.df(df , parquetPath , " parquet" , mode = " overwrite" )
1446- parquetDF <- read.parquet(sqlContext , parquetPath )
1447- expect_is(parquetDF , " DataFrame" )
1448- expect_equal(count(df ), count(parquetDF ))
1449- })
1457+ df2 <- read.df(sqlContext , parquetPath , " parquet" )
1458+ expect_is(df2 , " DataFrame" )
1459+ expect_equal(count(df2 ), 3 )
14501460
1451- test_that(" read.parquet()/parquetFile() works with multiple input paths" , {
1452- df <- read.json(sqlContext , jsonPath )
1453- write.df(df , parquetPath , " parquet" , mode = " overwrite" )
1461+ # Test write.parquet/saveAsParquetFile and read.parquet/parquetFile
14541462 parquetPath2 <- tempfile(pattern = " parquetPath2" , fileext = " .parquet" )
1455- write.df(df , parquetPath2 , " parquet" , mode = " overwrite" )
1456- parquetDF <- read.parquet(sqlContext , c(parquetPath , parquetPath2 ))
1463+ write.parquet(df , parquetPath2 )
1464+ parquetPath3 <- tempfile(pattern = " parquetPath3" , fileext = " .parquet" )
1465+ suppressWarnings(saveAsParquetFile(df , parquetPath3 ))
1466+ parquetDF <- read.parquet(sqlContext , c(parquetPath2 , parquetPath3 ))
14571467 expect_is(parquetDF , " DataFrame" )
14581468 expect_equal(count(parquetDF ), count(df ) * 2 )
1459- parquetDF2 <- suppressWarnings(parquetFile(sqlContext , parquetPath , parquetPath2 ))
1469+ parquetDF2 <- suppressWarnings(parquetFile(sqlContext , parquetPath2 , parquetPath3 ))
14601470 expect_is(parquetDF2 , " DataFrame" )
14611471 expect_equal(count(parquetDF2 ), count(df ) * 2 )
14621472
14631473 # Test if varargs works with variables
14641474 saveMode <- " overwrite"
14651475 mergeSchema <- " true"
1466- parquetPath3 <- tempfile(pattern = " parquetPath3" , fileext = " .parquet" )
1467- write.df(df , parquetPath2 , " parquet" , mode = saveMode , mergeSchema = mergeSchema )
1476+ parquetPath4 <- tempfile(pattern = " parquetPath3" , fileext = " .parquet" )
1477+ write.df(df , parquetPath3 , " parquet" , mode = saveMode , mergeSchema = mergeSchema )
1478+
1479+ unlink(parquetPath2 )
1480+ unlink(parquetPath3 )
1481+ unlink(parquetPath4 )
14681482})
14691483
14701484test_that(" describe() and summarize() on a DataFrame" , {
0 commit comments