Skip to content

Commit

Permalink
copy new tests from dotnet to js and py
Browse files Browse the repository at this point in the history
  • Loading branch information
HLWeil committed Jun 28, 2024
1 parent 0b64894 commit 302d69a
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 18 deletions.
5 changes: 0 additions & 5 deletions src/FsSpreadsheet/Json/Worksheet.fs
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,13 @@ let encodeColumns noNumbering (sheet:FsWorksheet) =

let decodeColumns : Decoder<FsWorksheet> =
Decode.object (fun builder ->
printfn "Decode Worksheet"
let mutable colIndex = 0
let n = builder.Required.Field name Decode.string
let ts = builder.Optional.Field tables (Decode.seq Table.decode)
let cs = builder.Required.Field columns (Decode.seq Column.decode)
let sheet = new FsWorksheet(n)
printfn "Start columning"
cs
|> Seq.iter (fun (colI,cells) ->
printfn "\tColumn: %OcolI" colI
let mutable rowIndex = 0
let colI =
match colI with
Expand All @@ -130,12 +127,10 @@ let decodeColumns : Decoder<FsWorksheet> =
let col = sheet.Column(colI)
cells
|> Seq.iter (fun cell ->
printfn "\t\tstart Cell: %O" cell
let rowI =
match cell.RowNumber with
| 0 -> rowIndex + 1
| i -> i
printfn "\t\trow: %i" rowI
rowIndex <- rowI
let c = col[rowIndex]
c.Value <- cell.Value
Expand Down
121 changes: 112 additions & 9 deletions tests/FsSpreadsheet.Js.Tests/Json.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@ open FsSpreadsheet
open FsSpreadsheet.Js
open Fable.Pyxpecto

let getFilledTestWb() =
let wb = new FsWorkbook()
let ws = FsWorkbook.initWorksheet "MySheet" wb
let r1 = ws.Row(1)
r1.[1].SetValueAs "A1"
r1.[2].SetValueAs "B1"
let r2 = ws.Row(2)
r2.[1].SetValueAs "A2"
r2.[2].SetValueAs "B2"
wb


let rows =
testList "Rows" [

testCase "Read Standard" <| fun _ ->
// Dependent on https://github.com/fable-compiler/Fable/issues/3858
ptestCase "Standard Read" <| fun _ ->
// Read object taken from https://spreadsheet.dsl.builders/#_sheets_and_rows
let s = """{
"sheets": [
Expand All @@ -19,7 +31,7 @@ let rows =
"number": 5,
"cells": [
{
"value": "Line Number Five"
"value": "Line 5"
}
]
},
Expand All @@ -28,7 +40,7 @@ let rows =
{
"cells": [
{
"value": "Line Number Seven"
"value": "Line 7"
}
]
}
Expand All @@ -43,7 +55,7 @@ let rows =
Expect.isTrue (sheet.ContainsRowAt 5) "Row 5"
let row1 = sheet.Row(5)
Expect.isTrue (row1.HasCellAt 1) "Row 5 cell"
Expect.equal (row1.[1].Value) "Line Number Five" "Row 5 cell value"
Expect.equal (row1.[1].Value) "Line 5" "Row 5 cell value"

Expect.isTrue (sheet.ContainsRowAt 6) "Row 6"
let row1 = sheet.Row(6)
Expand All @@ -52,9 +64,55 @@ let rows =
Expect.isTrue (sheet.ContainsRowAt 7) "Row 7"
let row2 = sheet.Row(7)
Expect.isTrue (row2.HasCellAt 1) "Row 7 cell"
Expect.equal (row2.[1].Value) "Line Number Seven" "Row 7 cell value"
Expect.equal (row2.[1].Value) "Line 7" "Row 7 cell value"

testCase "NoNumber Filled Write" <| fun _ ->
let wb = getFilledTestWb()
let expectedString = """{
"sheets": [
{
"name": "MySheet",
"rows": [
{
"cells": [
{
"value": "A1"
},
{
"value": "B1"
}
]
},
{
"cells": [
{
"value": "A2"
},
{
"value": "B2"
}
]
}
]
}
]
}"""
let s = wb.ToRowsJsonString(noNumbering = true)
Expect.stringEqual s expectedString "NoNumber Filled Write-Read"

testCase "NoNumber Filled Write-Read" <| fun _ ->
let wb = getFilledTestWb()
let s = wb.ToRowsJsonString(noNumbering = true)
let wb2 = FsWorkbook.fromRowsJsonString(s)
Expect.workSheetEqual (wb2.GetWorksheetAt(1)) (wb.GetWorksheetAt(1)) "NoNumber Filled Write-Read"

testCase "NoNumber DefaultTestObject Write-Read_Success" <| fun _ ->
let dto = DefaultTestObject.defaultTestObject()
let s = dto.ToRowsJsonString(noNumbering = true)
let dto2 = FsWorkbook.fromRowsJsonString(s)
()

testCase "Read-Write DefaultTestObject" <| fun _ ->
testCase "DefaultTestObject Write-Read" <| fun _ ->
let dto = DefaultTestObject.defaultTestObject()
let s = dto.ToRowsJsonString()
let dto2 = FsWorkbook.fromRowsJsonString(s)
Expand All @@ -64,12 +122,57 @@ let rows =
let columns =
testList "Columns" [

testCase "Read-Write DefaultTestObject" <| fun _ ->
testCase "NoNumber Filled Write" <| fun _ ->
let wb = getFilledTestWb()
let expectedString = """{
"sheets": [
{
"name": "MySheet",
"columns": [
{
"cells": [
{
"value": "A1"
},
{
"value": "A2"
}
]
},
{
"cells": [
{
"value": "B1"
},
{
"value": "B2"
}
]
}
]
}
]
}"""
let s = wb.ToColumnsJsonString(noNumbering = true)
Expect.stringEqual s expectedString "NoNumber Filled Write-Read"

testCase "NoNumber Filled Write-Read" <| fun _ ->
let wb = getFilledTestWb()
let s = wb.ToColumnsJsonString(noNumbering = true)
let wb2 = FsWorkbook.fromColumnsJsonString(s)
Expect.workSheetEqual (wb2.GetWorksheetAt(1)) (wb.GetWorksheetAt(1)) "NoNumber Filled Write-Read"

testCase "NoNumber DefaultTestObject Write-Read_Success" <| fun _ ->
let dto = DefaultTestObject.defaultTestObject()
let s = dto.ToColumnsJsonString(noNumbering = true)
let dto2 = FsWorkbook.fromColumnsJsonString(s)
()

testCase "DefaultTestObject Write-Read" <| fun _ ->
let dto = DefaultTestObject.defaultTestObject()
let s = dto.ToColumnsJsonString()
let dto2 = FsWorkbook.fromColumnsJsonString(s)
Expect.isDefaultTestObject dto2

]

let main = testList "Json" [
Expand Down
2 changes: 1 addition & 1 deletion tests/FsSpreadsheet.Net.Tests/Json.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ let columns =
let dto2 = FsWorkbook.fromColumnsJsonString(s)
()

testCase "Write-Read DefaultTestObject" <| fun _ ->
testCase "DefaultTestObject Write-Read" <| fun _ ->
let dto = DefaultTestObject.defaultTestObject()
let s = dto.ToColumnsJsonString()
System.IO.File.WriteAllText(DefaultTestObject.FsSpreadsheetJSON.asRelativePath,s)
Expand Down
110 changes: 107 additions & 3 deletions tests/FsSpreadsheet.Py.Tests/Json.Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@ open FsSpreadsheet
open FsSpreadsheet.Py
open Fable.Pyxpecto

let getFilledTestWb() =
let wb = new FsWorkbook()
let ws = FsWorkbook.initWorksheet "MySheet" wb
let r1 = ws.Row(1)
r1.[1].SetValueAs "A1"
r1.[2].SetValueAs "B1"
let r2 = ws.Row(2)
r2.[1].SetValueAs "A2"
r2.[2].SetValueAs "B2"
wb


let rows =
testList "Rows" [
testCase "Read Standard" <| fun _ ->
testCase "Standard Read" <| fun _ ->
// Read object taken from https://spreadsheet.dsl.builders/#_sheets_and_rows
let s = """{
"sheets": [
Expand Down Expand Up @@ -53,7 +65,53 @@ let rows =
Expect.isTrue (row2.HasCellAt 1) "Row 7 cell"
Expect.equal (row2.[1].Value) "Line 7" "Row 7 cell value"

testCase "Read-Write DefaultTestObject" <| fun _ ->
testCase "NoNumber Filled Write" <| fun _ ->
let wb = getFilledTestWb()
let expectedString = """{
"sheets": [
{
"name": "MySheet",
"rows": [
{
"cells": [
{
"value": "A1"
},
{
"value": "B1"
}
]
},
{
"cells": [
{
"value": "A2"
},
{
"value": "B2"
}
]
}
]
}
]
}"""
let s = wb.ToRowsJsonString(noNumbering = true)
Expect.stringEqual s expectedString "NoNumber Filled Write-Read"

testCase "NoNumber Filled Write-Read" <| fun _ ->
let wb = getFilledTestWb()
let s = wb.ToRowsJsonString(noNumbering = true)
let wb2 = FsWorkbook.fromRowsJsonString(s)
Expect.workSheetEqual (wb2.GetWorksheetAt(1)) (wb.GetWorksheetAt(1)) "NoNumber Filled Write-Read"

testCase "NoNumber DefaultTestObject Write-Read_Success" <| fun _ ->
let dto = DefaultTestObject.defaultTestObject()
let s = dto.ToRowsJsonString(noNumbering = true)
let dto2 = FsWorkbook.fromRowsJsonString(s)
()

testCase "DefaultTestObject Write-Read" <| fun _ ->
let dto = DefaultTestObject.defaultTestObject()
let s = dto.ToRowsJsonString()
let dto2 = FsWorkbook.fromRowsJsonString(s)
Expand All @@ -63,7 +121,53 @@ let rows =
let columns =
testList "Columns" [

testCase "Read-Write DefaultTestObject" <| fun _ ->
testCase "NoNumber Filled Write" <| fun _ ->
let wb = getFilledTestWb()
let expectedString = """{
"sheets": [
{
"name": "MySheet",
"columns": [
{
"cells": [
{
"value": "A1"
},
{
"value": "A2"
}
]
},
{
"cells": [
{
"value": "B1"
},
{
"value": "B2"
}
]
}
]
}
]
}"""
let s = wb.ToColumnsJsonString(noNumbering = true)
Expect.stringEqual s expectedString "NoNumber Filled Write-Read"

testCase "NoNumber Filled Write-Read" <| fun _ ->
let wb = getFilledTestWb()
let s = wb.ToColumnsJsonString(noNumbering = true)
let wb2 = FsWorkbook.fromColumnsJsonString(s)
Expect.workSheetEqual (wb2.GetWorksheetAt(1)) (wb.GetWorksheetAt(1)) "NoNumber Filled Write-Read"

testCase "NoNumber DefaultTestObject Write-Read_Success" <| fun _ ->
let dto = DefaultTestObject.defaultTestObject()
let s = dto.ToColumnsJsonString(noNumbering = true)
let dto2 = FsWorkbook.fromColumnsJsonString(s)
()

testCase "DefaultTestObject Write-Read" <| fun _ ->
let dto = DefaultTestObject.defaultTestObject()
let s = dto.ToColumnsJsonString()
let dto2 = FsWorkbook.fromColumnsJsonString(s)
Expand Down

0 comments on commit 302d69a

Please sign in to comment.