view src/layout/Layout.test.ts @ 114:4b33a479dc2f

fix layout test to match new layout return types. clean up UriPairMap
author drewp@bigasterisk.com
date Sat, 19 Mar 2022 16:12:49 -0700
parents cbcd82d21356
children dd3325cc023e
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: [
        {
          subjRows: [
            {
              subj: EX("a0"),
              predRows: [{ pred: EX("b0"), objs: [EX("c0")] }],
            },
            {
              subj: EX("d0"),
              predRows: [{ pred: EX("e0"), objs: [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") },
        // and doesn't include rdf:type as a column header here
      ]);
      expect(sec0.rowHeaders).toEqual([EX("a"), EX("b"), EX("c"), EX("e")]);
      expect(sec0.rows).toEqual([
        [[EX("red")], []],
        [[EX("blue")], []],
        [[], []],
        [[], [EX("small")]],
      ]);
    });
    it("leaves the rest ungrouped", async () => {
      expect(lr.sections[1]).toEqual({
        subjRows: [
          {
            subj: EX("d"),
            predRows: [
              { pred: EX("size"), objs: [EX("big")] },
              { pred: rdf.type, objs: [EX("T2")] },
            ],
          },
        ],
      });
    });
  });
  it.skip("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", () => {

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