def testErrorMessages(self): s = yayv.ByExample("{id: AUTO_INC, name: REQUIRED}") s.IsValid("first_name: Boo") self.AssertEqual("# first_name is not in the schema in ROOT", s.ErrorMessage()) s = yayv.ByExample("{id: AUTO_INC, name: REQUIRED}") s.IsValid("name:") self.AssertEqual("# REQUIRED field missing in ROOT.name", s.ErrorMessage()) s = yayv.ByExample("{id: AUTO_INC, name: REQUIRED}") s.IsValid("") self.AssertEqual("# Expect yaml_obj to be a dict but it is: None in ROOT", s.ErrorMessage()) s = yayv.ByExample("- {id: AUTO_INC, name: OPTIONAL}") s.IsValid("[ name: arthur, name: olga, names: sarah ]") self.AssertEqual("# names is not in the schema in ROOT[2]", s.ErrorMessage()) s = yayv.ByExample("- {id: AUTO_INC, name: OPTIONAL}") s.IsValid("[ name: arthur, name: olga, names: sarah ]") self.AssertEqual("# names is not in the schema in ROOT[2]", s.ErrorMessage()) s = yayv.ByExample("- {id: AUTO_INC, name: OPTIONAL}") s.IsValid("[ id: 1, id: 2 ]") self.AssertEqual("# ", s.ErrorMessage()) s = yayv.ByExample("- {id: AUTO_INC, name: OPTIONAL}") s.IsValid("[ id: 1, id: 1 ]") self.AssertEqual("# duplicate AUTO_INC 1 found in ROOT[1].id", s.ErrorMessage()) s = yayv.ByExample("- {id1: AUTO_INC 1, id2: AUTO_INC 2, name: UNIQUE a, name2: UNIQUE b}") s.IsValid("[ {id1: 1, name: arthur, name2: sarah}, {id2: 1, name: sarah, name2: arthur} ]") self.AssertEqual("# ", s.ErrorMessage()) s = yayv.ByExample("- {name: OPTIONAL, schedule: [ {daypart: UNIQUE 1, room: OPTIONAL} ]}") s.IsValid("[ {name: foo, schedule: [ {daypart: monday a, room: 200}, {daypart: tuesday a, room: 200}]}]") self.AssertEqual("# ", s.ErrorMessage()) s = yayv.ByExample("- {name: OPTIONAL, schedule: [ {daypart: UNIQUE 1, room: OPTIONAL} ]}") s.IsValid("[ {name: foo, schedule: [ {daypart: monday a, room: 200}, {daypart: monday a, room: 300}]}]") self.AssertEqual("# Duplicate Value monday a in UNIQUE 1 in ROOT[0].schedule[1].daypart", s.ErrorMessage())
def testAutoInc(self): s = yayv.ByExample("[ {id: AUTO_INC, name: REQUIRED} ]") self.Assert(s, "[name: arthur, name: sarah, name: olga]", True) s = yayv.ByExample("[ {id: AUTO_INC, name: REQUIRED} ]") self.Assert(s, "[{name: arthur, id: 7}, name: sarah, name: olga]", True) if s.counters[''] != 7: self.FailWithMessage(s, "n/a", "counter starts at 8", s.counters['']) self.Assert(s, "[{name: arthur, id: 7}, name: sarah, name: olga]", True)
def testList(self): s = yayv.ByExample("[OPTIONAL]") self.Assert(s, "[a, b, c]", True) self.Assert(s, "a: b", False) self.Assert(s, "[]", True) self.Assert(s, "", True) self.Assert(s, "[a, a, a]", True)
def Students(): return yayv.ByExample("- email: UNIQUE\n" " first: REQUIRED\n" " last: REQUIRED\n" " current_grade: REQUIRED\n" " current_homeroom: REQUIRED\n" " edtechid: OPTIONAL\n")
def testRealisticIsValidFails(self): s = yayv.ByExample( "- name: REQUIRED\n" " id: AUTO_INC\n" " instructor: OPTIONAL\n" " max_enrollment: REQUIRED\n" " prerequisites:\n" " - current_grade: OPTIONAL\n" " email: OPTIONAL\n" " group: OPTIONAL\n" " schedule:\n" " - daypart: OPTIONAL\n" " location: OPTIONAL\n") s.IsValid( "- name: 3D Printing\n" " instructor: Mr. Brown\n" " max_enrollment: 16\n" " prerequisites: []\n" " - current_grade: 8\n" " schedule:\n" " - daypart: Fri A\n" " location: 25\n") self.AssertTrue(s.ErrorMessage().startswith("# Traceback")) self.AssertTrue("- name: 3D Printing" in s.ErrorMessage()) self.AssertTrue("- current_grade: 8" in s.ErrorMessage())
def testDict(self): s = yayv.ByExample("{ abc: REQUIRED, xyz: OPTIONAL }") self.Assert(s, "{abc: Hello, xyz: There}", True) self.Assert(s, "{abc: Hello}", True) self.Assert(s, "{xyz: Hello}", False) self.Assert(s, "{foo: Hello}", False) self.Assert(s, "foo", False) self.Assert(s, "[a, b, c]", False)
def AutoRegister(): return yayv.ByExample( "- class: OPTIONAL\n" # for human use " class_id: REQUIRED\n" # should match id from Classes " applies_to:\n" " - current_grade: OPTIONAL\n" " email: OPTIONAL\n" " group: OPTIONAL\n" " id: OPTIONAL\n" " exempt:\n" " - OPTIONAL\n")
def Requirements(): return yayv.ByExample("- name: REQUIRED\n" " applies_to:\n" " - current_grade: OPTIONAL\n" " email: OPTIONAL\n" " group: OPTIONAL\n" " id: AUTO_INC\n" " exempt:\n" " - OPTIONAL\n" " class_or_group_options:\n" " - \n" " - OPTIONAL\n")
def testRealisticUpdate(self): schema = yayv.ByExample( "- name: REQUIRED\n" " id: AUTO_INC\n" " instructor: OPTIONAL\n" " max_enrollment: REQUIRED\n" " prerequisites:\n" " - current_grade: OPTIONAL\n" " email: OPTIONAL\n" " group: OPTIONAL\n" " schedule:\n" " - daypart: REQUIRED\n" " location: REQUIRED\n") str = schema.Update('\n'.join([ "- instructor: Mr. McCartney", " max_enrollment: 81", " name: 8th Grade Core", " prerequisites:", " - current_grade: 8", " schedule:", " - daypart: Mon A", " location: Homeroom", " - daypart: Mon B", " location: Homeroom", " - daypart: Thurs A", " location: Homeroom"])) # run the unit under test actual = schema.Update(str) expected = '\n'.join([ "- id: 1", " instructor: Mr. McCartney", " max_enrollment: 81", " name: 8th Grade Core", " prerequisites:", " - current_grade: 8", " schedule:", " - daypart: Mon A", " location: Homeroom", " - daypart: Mon B", " location: Homeroom", " - daypart: Thurs A", " location: Homeroom", ""]) self.AssertEqual(expected, actual)
def Classes(): return yayv.ByExample("- name: REQUIRED\n" " id: AUTO_INC\n" " instructor: OPTIONAL\n" " max_enrollment: REQUIRED\n" " prerequisites:\n" " - current_grade: OPTIONAL\n" " email: OPTIONAL\n" " group: OPTIONAL\n" " schedule:\n" " - daypart: REQUIRED\n" " location: REQUIRED\n" " description: OPTIONAL\n" " donation: OPTIONAL\n" " exclude_from_catalog: OPTIONAL\n" " owners:\n" " - OPTIONAL\n" " open_enrollment: OPTIONAL\n" " fitness: OPTIONAL\n")
def testUniq(self): s = yayv.ByExample("[{ id: UNIQUE, xyz: OPTIONAL }]") self.Assert(s, "[{id: Hello, xyz: There}, {id: There, xyz: Blue}]", True) self.Assert(s, "[{id: Hello, xyz: Hello}, {id: There, xyz: Blue}]", True) self.Assert(s, "[{id: Hello, xyz: There}, {id: Hello, xyz: Blue}]", False) self.Assert(s, "[{id: Hello, xyz: There}, {xyz: Blue}]", False)
def ClassGroups(): return yayv.ByExample("- name: REQUIRED\n" " id: AUTO_INC\n" " classes:\n" " - name: OPTIONAL\n" # for human use " id: REQUIRED\n")
def Teachers(): return yayv.ByExample("- email: UNIQUE\n" " first: REQUIRED\n" " last: REQUIRED\n" " current_homeroom: OPTIONAL\n")
def testNumbers(self): s = yayv.ByExample("OPTIONAL") self.Assert(s, "123", True) s = yayv.ByExample("REQUIRED") self.Assert(s, "123", True)
def testRequired(self): s = yayv.ByExample("REQUIRED") self.Assert(s, "abc", True); self.Assert(s, "", False); self.Assert(s, "a: b", False); self.Assert(s, "[a, b, c]", False);
def testSyntax(self): s = yayv.ByExample("OPTIONAL") self.Assert(s, "[ a, b", False) self.Assert(s, "- a\n- b\nc:d\n", False) self.Assert(s, "", True)
def testValue(self): s = yayv.ByExample("OPTIONAL") self.Assert(s, "abc", True); self.Assert(s, "", True); self.Assert(s, "a: b", False); self.Assert(s, "[a, b, c]", False);
def testBadSchema(self): s = yayv.ByExample("[a, b]") self.AssertThrows(s, "[a, b]")
def StudentGroups(): return yayv.ByExample("- group_name: REQUIRED\n" " emails:\n" " - REQUIRED\n")
def testUniqList(self): s = yayv.ByExample("[ UNIQUE ]") self.Assert(s, "[a, a, a]", False) self.Assert(s, "[a, b, c]", True)
def ServingRules(): return yayv.ByExample("- name: REQUIRED\n" " allow:\n" " - current_grade: OPTIONAL\n" " current_homeroom: OPTIONAL\n" " email: OPTIONAL\n")
def Links(): return yayv.ByExample("- name: REQUIRED\n" " url: REQUIRED\n")
def testAutoIncCounter(self): s = yayv.ByExample("- {id: AUTO_INC, name: REQUIRED}") n = s.Update("[{name: foo}, {name: bar}]") n = yaml.load(n) self.AssertEqual({"name": "foo", "id": 1}, n[0]) self.AssertEqual({"name": "bar", "id": 2}, n[1])
def Dayparts(): return yayv.ByExample("- name: UNIQUE\n" " row: REQUIRED\n" " col: REQUIRED\n" " rowspan: OPTIONAL\n" " colspan: OPTIONAL\n")