Skip to content

Commit

Permalink
longer now includes null values (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
nshiab authored Mar 27, 2024
1 parent c2fef1b commit fdd64fc
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
11 changes: 6 additions & 5 deletions src/class/SimpleDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,11 +746,12 @@ export default class SimpleDB {
) {
await queryDB(
this,
`CREATE OR REPLACE TABLE ${table} AS SELECT * FROM (UNPIVOT ${table}
ON ${columns.map((d) => `"${d}"`).join(", ")}
INTO
NAME ${columnsTo}
VALUE ${valuesTo})`,
`CREATE OR REPLACE TABLE ${table} AS SELECT * FROM (
FROM "${table}" UNPIVOT INCLUDE NULLS (
"${valuesTo}"
for "${columnsTo}" in (${columns.map((d) => `"${d}"`).join(", ")})
)
)`,
mergeOptions(this, {
table,
method: "longer()",
Expand Down
29 changes: 29 additions & 0 deletions test/data/files/dataUntidyWithNulls.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[
{
"Department": "accounting",
"2015": null,
"2016": 9,
"2017": 15,
"2018": 11,
"2019": 25,
"2020": 32
},
{
"Department": "R&D",
"2015": 1,
"2016": 2,
"2017": null,
"2018": 2,
"2019": 2,
"2020": 3
},
{
"Department": "sales",
"2015": 2,
"2016": 7,
"2017": 15,
"2018": 32,
"2019": 45,
"2020": null
}
]
44 changes: 39 additions & 5 deletions test/unit/methods/longer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ describe("longer", () => {
let simpleNodeDB: SimpleNodeDB
before(async function () {
simpleNodeDB = new SimpleNodeDB()
await simpleNodeDB.loadData(
"dataUntidy",
"test/data/files/dataUntidy.json"
)
})
after(async function () {
await simpleNodeDB.done()
})

it("should tidy data by stacking mutiple columns", async () => {
await simpleNodeDB.loadData(
"dataUntidy",
"test/data/files/dataUntidy.json"
)
await simpleNodeDB.longer(
"dataUntidy",
["2015", "2016", "2017", "2018", "2019", "2020"],
Expand All @@ -23,7 +23,6 @@ describe("longer", () => {
)

const data = await simpleNodeDB.getData("dataUntidy")

assert.deepStrictEqual(data, [
{ Department: "accounting", year: "2015", employees: 10 },
{ Department: "accounting", year: "2016", employees: 9 },
Expand All @@ -45,4 +44,39 @@ describe("longer", () => {
{ Department: "sales", year: "2020", employees: 27 },
])
})
it("should tidy data by stacking mutiple columns and by including null values", async () => {
await simpleNodeDB.loadData(
"dataUntidyWithNulls",
"test/data/files/dataUntidyWithNulls.json"
)
await simpleNodeDB.longer(
"dataUntidyWithNulls",
["2015", "2016", "2017", "2018", "2019", "2020"],
"year",
"employees"
)

const data = await simpleNodeDB.getData("dataUntidyWithNulls")

assert.deepStrictEqual(data, [
{ Department: "accounting", year: "2015", employees: null },
{ Department: "accounting", year: "2016", employees: 9 },
{ Department: "accounting", year: "2017", employees: 15 },
{ Department: "accounting", year: "2018", employees: 11 },
{ Department: "accounting", year: "2019", employees: 25 },
{ Department: "accounting", year: "2020", employees: 32 },
{ Department: "R&D", year: "2015", employees: 1 },
{ Department: "R&D", year: "2016", employees: 2 },
{ Department: "R&D", year: "2017", employees: null },
{ Department: "R&D", year: "2018", employees: 2 },
{ Department: "R&D", year: "2019", employees: 2 },
{ Department: "R&D", year: "2020", employees: 3 },
{ Department: "sales", year: "2015", employees: 2 },
{ Department: "sales", year: "2016", employees: 7 },
{ Department: "sales", year: "2017", employees: 15 },
{ Department: "sales", year: "2018", employees: 32 },
{ Department: "sales", year: "2019", employees: 45 },
{ Department: "sales", year: "2020", employees: null },
])
})
})

0 comments on commit fdd64fc

Please sign in to comment.