view src/Layout.test.ts @ 104:1aea03d306af

WIP Layout. tests are passing but they're a little wrong
author drewp@bigasterisk.com
date Sat, 12 Mar 2022 22:41:43 -0800
parents f12feced00ce
children
line wrap: on
line source

import { Quad, Store, Term } from "n3";
import { n3Graph } from "./fetchAndParse";
import { AlignedTable, Layout, LayoutResult } from "./Layout";
import { EX, rdf } from "./namespaces";
import { ViewConfig } from "./ViewConfig";

const twoStatements = async (): Promise<Store> => {
  return n3Graph(`
  @prefix : <http://example.com/> .
  :g1 {
    :a0 :b0 :c0 .
    :d0 :e0 :f0 .
  }
  `);
};

const typedStatements = async (): Promise<Store> => {
  return n3Graph(`
  @prefix : <http://example.com/> .
  :g1 {
    :a a :T1 ; :color :red .
    :b a :T1 ; :color :blue .
    :c a :T1 .
    :d a :T2 ; :size :big .
    :e a :T1,:T2; :size :small
  }
  `);
};
function G1(s: Term, p: Term, o: Term): Quad {
  return new Quad(s, p, o, EX("g1"));
}

describe("Layout", () => {
  it("accepts a ViewConfig", async () => {
    const vc = new ViewConfig();
    await vc.readFromGraph(`
      @prefix ex: <http://example.com/> .
      @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

      <> a ex:View; rdfs:label "repos" .`);
    const layout = new Layout(vc);
    const lr = layout.plan(await twoStatements());
  });
  it("defaults to putting all triples in the ungrouped list", async () => {
    const layout = new Layout();
    const lr = layout.plan(await twoStatements());
    expect(lr).toEqual({
      sections: [
        {
          statements: [
            G1(EX("a0"), EX("b0"), EX("c0")),
            G1(EX("d0"), EX("e0"), EX("f0")),
          ],
        },
      ],
    });
  });
  describe("makes a table as requested by ViewConfig", () => {
    let lr: LayoutResult;

    beforeAll(async () => {
      const vc = new ViewConfig();
      await vc.readFromGraph(`
        @prefix : <http://example.com/> .
        @prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
  
        <> a :View; :table [ :primaryType :T1 ] .`);
      const layout = new Layout(vc);
      lr = layout.plan(await typedStatements());
    });
    it("returns 2 sections", ()=>{
      expect(lr.sections).toHaveLength(2);
    })
    it("puts the right type in the table", async () => {
      const sec0 = lr.sections[0] as AlignedTable;
      expect(sec0.columnHeaders).toEqual([
        { rdfType: EX("T1"), pred: EX("color") },
        { rdfType: EX("T1"), pred: EX("size") }
    ])
      expect(sec0.rows).toEqual([
          [EX("a"), EX("red"), null],
          [EX("b"), EX("blue"),null],
          [EX("c"), null, null],
          [EX("e"), null, EX('small')],
        ]);
    });
    it("leaves the rest ungrouped", async () => {
      expect(lr.sections[1]).toEqual({
        statements: [
          G1(EX("d"), rdf.type, EX("T2")),
          G1(EX("d"), EX("size"), EX("big")),
        ],
      });
    });
  });
  it("makes a table out of ungrouped triples with the same type", async () => {});
});

// describe("equality", () => {
//   test("investigation of https://github.com/rdfjs/N3.js/issues/265", () => {
//     const x = namedNode("x");
//     const x2 = namedNode("x");
//     // (NamedNode.prototype as any).hashCode = () => 0;
//     // expect((x as any).hashCode()).toEqual((x2 as any).hashCode())
//     expect(x === x2).toBeFalsy();
//     expect(x == x2).toBeFalsy();
//     expect(x.equals(x2)).toBeTruthy();
//     let imap = Immutable.Map();
//     imap = imap.set(x, 11);
//     imap = imap.set(x, 22);
//     imap = imap.set(x2, 33);
//     expect(imap.has(x)).toBeTruthy();
//     expect(imap.has(x2)).toBeTruthy();
//     expect(imap.size).toEqual(1);
//   });
// });

// describe("groupByRdfType", () => {
//   test("finds multiple graphs", () => {});
//   test("works", async () => {
//     const store = new Store();

//     const parser = new Parser();
//     await new Promise((res, rej) => {
//       parser.parse(
//         `PREFIX : <urn:>
//         :rs1 a :Foo; :pred1 "obj1" .
//         :rs2 a :Foo; :pred1 "obj2" .
//         :rs3 a :Bar .
//         :rs4 :pred1 "obj4" .
// `,
//         (error, quad: Quad, prefixes: Prefixes) => {
//           if (quad) {
//             store.addQuad(quad);
//           } else {
//             res(undefined);
//           }
//         }
//       );
//     });
//     const grouped = groupByRdfType(store);
//     expect(Array.from(grouped.byType.keys())).toHaveLength(2);
//     expect(grouped.byType.get(namedNode("urn:Foo"))).toEqual(
//       Immutable.Set([namedNode("urn:rs1"), namedNode("urn:rs2")])
//     );
//     expect(grouped.byType.get(namedNode("urn:Bar"))).toEqual(
//       Immutable.Set([namedNode("urn:rs3")])
//     );
//     expect(grouped.untypedSubjs).toEqual([namedNode("urn:rs4")]);
//   });

//   describe("MultiSubjsTypeBlockLayout", () => {
//     test("gathers subjs", () => {

//     });
//     test("gathers preds", () => {

//     });
//     test("cells reports filled cells", () => {

//     });
//   });
// });