File size: 2,736 Bytes
29eea0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""Unit tests for the big-schema generator. Run with the other lib tests."""
import sys
import unittest
from pathlib import Path

sys.dont_write_bytecode = True
sys.path.insert(0, str(Path(__file__).resolve().parent))
import bigschema  # noqa: E402
from schema_check import check_text  # noqa: E402

TEXT = """
[area ownership]
Team: slug, name, createdAt:datetime
Services: service_id, name, tier:int, created_on:date
OWNS: Team > Services | since:date
~ pascal: cost centre, budget line, approval
~ snake: audit entry, change request
[area data]
Dataset: urn, title
OWNED_BY: Dataset > Team
~ plural: quota, entitlement, category
"""


class Generator(unittest.TestCase):
    def test_same_seed_same_schema_and_files(self):
        a, b = bigschema.build(TEXT, 7), bigschema.build(TEXT, 7)
        self.assertEqual(a.check_dict(), b.check_dict())
        self.assertEqual(bigschema.render_neo4j(a, 7), bigschema.render_neo4j(b, 7))
        self.assertNotEqual(a.check_dict(), bigschema.build(TEXT, 8).check_dict())

    def test_core_lines_are_kept_exactly(self):
        d = bigschema.build(TEXT, 7).check_dict()
        self.assertEqual(d["nodes"]["Services"], {"service_id": "STRING", "name": "STRING", "tier": "INTEGER", "created_on": "DATE"})
        self.assertEqual(d["rels"]["OWNS"], {"endpoints": [["Team", "Services"]], "props": {"since": "DATE"}})
        self.assertEqual(d["rels"]["OWNED_BY"]["endpoints"], [["Dataset", "Team"]])

    def test_filler_styles(self):
        s = bigschema.build(TEXT, 7)
        self.assertIn("CostCentre", s.nodes)
        self.assertEqual(s.nodes["CostCentre"].key, "costCentreId")
        self.assertEqual(s.nodes["audit_entry"].key, "id")
        self.assertEqual(s.nodes["Categories"].key, "category_id")
        self.assertEqual(len({r.type for r in s.rels}), len(s.rels))       # filler type names are unique
        for r in s.rels:
            self.assertIn(r.src, s.nodes)
            self.assertIn(r.dst, s.nodes)

    def test_rendered_ingest_agrees_with_the_schema(self):
        """Every pattern the Neo4j ingest scripts contain must itself pass the checker."""
        s = bigschema.build(TEXT, 7)
        for path, text in bigschema.render_neo4j(s, 7).items():
            self.assertEqual(check_text(text, s.check_dict()).problems, [], path)

    def test_ladybug_ddl_lists_every_table(self):
        s = bigschema.build(TEXT, 7, rel_name=bigschema.pascal)
        ddl = "\n".join(bigschema.render_ladybug(s, 7).values())
        for label in s.nodes:
            self.assertIn(f"CREATE NODE TABLE {label}(", ddl)
        for r in s.rels:
            self.assertIn(f"CREATE REL TABLE {r.type}(FROM {r.src} TO {r.dst}", ddl)


if __name__ == "__main__":
    unittest.main()