123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868 |
- #!/usr/bin/python
- # flake8: noqa
-
- import unittest
- from sznqalibs import hoover
- import copy
- import json
- import unittest
-
-
- class DictPathTest(unittest.TestCase):
-
- def setUp(self):
- super(DictPathTest, self).setUp()
-
- class PathyDict(dict, hoover.DictPath):
- pass
-
- testDict = {
- 's': 11,
- 'x': {
- 'a': 55,
- 'hello': {
- 'world': 1, 'sun': 3, 'blackhole': None
- },
- 'b': 59
- },
- }
-
- self.pdict = PathyDict(testDict)
-
- def testDelSun(self):
- oracle = copy.deepcopy(self.pdict)
- result = copy.deepcopy(self.pdict)
- del oracle['x']['hello']['sun']
- result.delpath("/x/hello/sun")
- self.assertEqual(oracle, result)
-
- def testGetHello(self):
- oracle = copy.deepcopy(self.pdict['x']['hello'])
- result = self.pdict.getpath("/x/hello")
- self.assertEqual(oracle, result)
-
- def testSetHello(self):
- oracle = copy.deepcopy(self.pdict)
- result = copy.deepcopy(self.pdict)
- oracle['x']['hello']['sun'] = 'moon'
- result.setpath("/x/hello/sun", 'moon')
- self.assertEqual(oracle, result)
-
- def testSetNewItem(self):
- oracle = copy.deepcopy(self.pdict)
- result = copy.deepcopy(self.pdict)
- oracle['x']['hullo'] = 'NEW'
- result.setpath("/x/hullo", 'NEW')
- self.assertEqual(oracle, result)
-
- def testHelloExists(self):
- self.assertTrue(self.pdict.ispath('/x/hello'))
-
- def testWorldNotExists(self):
- self.assertFalse(self.pdict.ispath('/x/world'))
-
- def testDelBadPath(self):
- fn = lambda: self.pdict.delpath('/x/hullo')
- self.assertRaises(KeyError, fn)
-
- def testGetBadPath(self):
- fn = lambda: self.pdict.getpath('/x/hullo')
- self.assertRaises(KeyError, fn)
-
- def testSetBadPath(self):
- fn = lambda: self.pdict.setpath('/x/hullo/newthing', 1)
- self.assertRaises(KeyError, fn)
-
- # the scary None
-
- def testDelNone(self):
- oracle = copy.deepcopy(self.pdict)
- result = copy.deepcopy(self.pdict)
- del oracle['x']['hello']['blackhole']
- result.delpath("/x/hello/blackhole")
- self.assertEqual(oracle, result)
-
- def testSetNone(self):
- oracle = copy.deepcopy(self.pdict)
- result = copy.deepcopy(self.pdict)
- oracle['x']['hello']['sun'] = None
- result.setpath("/x/hello/sun", None)
- self.assertEqual(oracle, result)
-
- def testSetNewNone(self):
- oracle = copy.deepcopy(self.pdict)
- result = copy.deepcopy(self.pdict)
- oracle['x']['hullo'] = None
- result.setpath("/x/hullo", None)
- self.assertEqual(oracle, result)
-
- def testGetNone(self):
- oracle = copy.deepcopy(self.pdict['x']['hello']['blackhole'])
- result = self.pdict.getpath("/x/hello/blackhole")
- self.assertEqual(oracle, result)
-
- def testNoneExists(self):
- result = self.pdict.ispath('/x/hello/blackhole')
- self.assertTrue(result)
-
-
- class CartmanTest(unittest.TestCase):
-
- def sdiff(self, a, b):
- sa = [json.dumps(i) for i in a]
- sb = [json.dumps(i) for i in b]
- diff = set(sa) - set(sb)
- return [json.loads(i) for i in diff]
-
- def chkoracle(self, o):
- def dups(self):
- so = [json.dumps(i) for i in o]
- return len(o) != len(set(so))
- if dups(o):
- self.logger.warn("duplicates in oracle!")
-
- def setUp(self):
- super(CartmanTest, self).setUp()
-
- def compare(self, source, scheme, oracle):
- self.chkoracle(oracle)
-
- def fmtdiff(diff, word):
- strn = ""
- if diff:
- strn = ("\n----------------------"
- "\n %s elements (%s):\n%s"
- % (word, len(diff), pretty(diff)))
- return strn
-
- pretty = hoover.jsDump
-
- cm = hoover.Cartman(source, scheme)
- result = [i for i in cm]
-
- xtra = self.sdiff(result, oracle)
- miss = self.sdiff(oracle, result)
-
- err = ""
- err += fmtdiff(miss, 'missing')
- err += fmtdiff(xtra, 'extra')
- return err
-
- def test_Flat_2of3(self):
-
- scheme = {
- 'a': hoover.Cartman.Iterable,
- 'b': hoover.Cartman.Iterable,
- }
- source = {
- 'a': [1, 2, 3],
- 'b': ['i', 'ii', 'iii'],
- 'c': ['I', 'II', 'III'],
- }
- oracle = [
- {'a': 1, 'b': 'i'},
- {'a': 1, 'b': 'ii'},
- {'a': 1, 'b': 'iii'},
- {'a': 2, 'b': 'i'},
- {'a': 2, 'b': 'ii'},
- {'a': 2, 'b': 'iii'},
- {'a': 3, 'b': 'i'},
- {'a': 3, 'b': 'ii'},
- {'a': 3, 'b': 'iii'},
- ]
-
- err = self.compare(source, scheme, oracle)
- self.assertFalse(err, "errors found: %s\n" % err)
-
- def test_Deep1(self):
-
- scheme = {
- 'a': hoover.Cartman.Iterable,
- 'b': hoover.Cartman.Iterable,
- 'x': {
- 'h1': hoover.Cartman.Iterable,
- 'h2': hoover.Cartman.Iterable,
- }
- }
- source = {
- 'a': [1, 2, 3],
- 'b': ['i', 'ii', 'iii'],
- 'x': {'h1': [101, 102], 'h2': [201, 202]}
- }
- oracle = [
-
- {'a': 1, 'b': 'i', 'x': {'h1': 101, 'h2': 201}},
- {'a': 1, 'b': 'i', 'x': {'h1': 101, 'h2': 202}},
- {'a': 1, 'b': 'i', 'x': {'h1': 102, 'h2': 201}},
- {'a': 1, 'b': 'i', 'x': {'h1': 102, 'h2': 202}},
- {'a': 1, 'b': 'ii', 'x': {'h1': 101, 'h2': 201}},
- {'a': 1, 'b': 'ii', 'x': {'h1': 101, 'h2': 202}},
- {'a': 1, 'b': 'ii', 'x': {'h1': 102, 'h2': 201}},
- {'a': 1, 'b': 'ii', 'x': {'h1': 102, 'h2': 202}},
- {'a': 1, 'b': 'iii', 'x': {'h1': 101, 'h2': 201}},
- {'a': 1, 'b': 'iii', 'x': {'h1': 101, 'h2': 202}},
- {'a': 1, 'b': 'iii', 'x': {'h1': 102, 'h2': 201}},
- {'a': 1, 'b': 'iii', 'x': {'h1': 102, 'h2': 202}},
-
- {'a': 2, 'b': 'i', 'x': {'h1': 101, 'h2': 201}},
- {'a': 2, 'b': 'i', 'x': {'h1': 101, 'h2': 202}},
- {'a': 2, 'b': 'i', 'x': {'h1': 102, 'h2': 201}},
- {'a': 2, 'b': 'i', 'x': {'h1': 102, 'h2': 202}},
- {'a': 2, 'b': 'ii', 'x': {'h1': 101, 'h2': 201}},
- {'a': 2, 'b': 'ii', 'x': {'h1': 101, 'h2': 202}},
- {'a': 2, 'b': 'ii', 'x': {'h1': 102, 'h2': 201}},
- {'a': 2, 'b': 'ii', 'x': {'h1': 102, 'h2': 202}},
- {'a': 2, 'b': 'iii', 'x': {'h1': 101, 'h2': 201}},
- {'a': 2, 'b': 'iii', 'x': {'h1': 101, 'h2': 202}},
- {'a': 2, 'b': 'iii', 'x': {'h1': 102, 'h2': 201}},
- {'a': 2, 'b': 'iii', 'x': {'h1': 102, 'h2': 202}},
-
- {'a': 3, 'b': 'i', 'x': {'h1': 101, 'h2': 201}},
- {'a': 3, 'b': 'i', 'x': {'h1': 101, 'h2': 202}},
- {'a': 3, 'b': 'i', 'x': {'h1': 102, 'h2': 201}},
- {'a': 3, 'b': 'i', 'x': {'h1': 102, 'h2': 202}},
- {'a': 3, 'b': 'ii', 'x': {'h1': 101, 'h2': 201}},
- {'a': 3, 'b': 'ii', 'x': {'h1': 101, 'h2': 202}},
- {'a': 3, 'b': 'ii', 'x': {'h1': 102, 'h2': 201}},
- {'a': 3, 'b': 'ii', 'x': {'h1': 102, 'h2': 202}},
- {'a': 3, 'b': 'iii', 'x': {'h1': 101, 'h2': 201}},
- {'a': 3, 'b': 'iii', 'x': {'h1': 101, 'h2': 202}},
- {'a': 3, 'b': 'iii', 'x': {'h1': 102, 'h2': 201}},
- {'a': 3, 'b': 'iii', 'x': {'h1': 102, 'h2': 202}},
- ]
-
- err = self.compare(source, scheme, oracle)
- self.assertFalse(err, "errors found: %s\n" % err)
-
- def test_Scalar(self):
-
- scheme = {
- 'a': hoover.Cartman.Iterable,
- 'b': hoover.Cartman.Iterable,
- 'il': hoover.Cartman.Scalar,
- 'id': hoover.Cartman.Scalar,
- 'ii': hoover.Cartman.Scalar,
- }
- source = {
- 'a': [1, 2, 3],
- 'b': ['i', 'ii', 'iii'],
- 'il': [2, 7],
- 'id': {'a': 1},
- 'ii': 42
- }
- invars = {
- 'il': [2, 7], 'id': {'a': 1}, 'ii': 42
- }
- oracle = [
- {'a': 1, 'b': 'i'},
- {'a': 1, 'b': 'ii'},
- {'a': 1, 'b': 'iii'},
- {'a': 2, 'b': 'i'},
- {'a': 2, 'b': 'ii'},
- {'a': 2, 'b': 'iii'},
- {'a': 3, 'b': 'i'},
- {'a': 3, 'b': 'ii'},
- {'a': 3, 'b': 'iii'},
- ]
- for o in oracle:
- o.update(invars)
-
- err = self.compare(source, scheme, oracle)
- self.assertFalse(err, "errors found: %s\n" % err)
-
- def test_dataDangling(self):
-
- scheme = {
- 'a': hoover.Cartman.Iterable,
- 'b': hoover.Cartman.Iterable,
- }
- source = {
- 'a': [1, 2, 3],
- 'b': ['i', 'ii', 'iii'],
- 'dangling_str': "tr",
- 'dangling_dict': {'a': 1},
- 'dangling_list': []
- }
- oracle = [
- {'a': 1, 'b': 'i'},
- {'a': 1, 'b': 'ii'},
- {'a': 1, 'b': 'iii'},
- {'a': 2, 'b': 'i'},
- {'a': 2, 'b': 'ii'},
- {'a': 2, 'b': 'iii'},
- {'a': 3, 'b': 'i'},
- {'a': 3, 'b': 'ii'},
- {'a': 3, 'b': 'iii'},
- ]
-
- err = self.compare(source, scheme, oracle)
- self.assertFalse(err, "errors found: %s\n" % err)
-
- def test_dataMissing(self):
-
- scheme = {
- 'a': hoover.Cartman.Iterable,
- 'b': hoover.Cartman.Iterable,
- 'MIA': hoover.Cartman.Iterable,
- }
- source = {
- 'a': [1, 2, 3],
- 'b': ['i', 'ii', 'iii'],
- }
- oracle = [
- {'a': 1, 'b': 'i'},
- {'a': 1, 'b': 'ii'},
- {'a': 1, 'b': 'iii'},
- {'a': 2, 'b': 'i'},
- {'a': 2, 'b': 'ii'},
- {'a': 2, 'b': 'iii'},
- {'a': 3, 'b': 'i'},
- {'a': 3, 'b': 'ii'},
- {'a': 3, 'b': 'iii'},
- ]
-
- err = self.compare(source, scheme, oracle)
- self.assertFalse(err, "errors found: %s\n" % err)
-
- def test_withListIterator(self):
-
- scheme = {
- 'a': hoover.Cartman.Iterable,
- 'b': hoover.Cartman.Iterable,
- 'ITER': hoover.Cartman.Iterable,
- }
- source = {
- 'a': [1, 2, 3],
- 'b': ['i', 'ii', 'iii'],
- 'ITER': iter(['iterate', 'over', 'me'])
- }
- oracle = [
- {'a': 1, 'b': 'i', 'ITER': 'iterate'},
- {'a': 1, 'b': 'ii', 'ITER': 'iterate'},
- {'a': 1, 'b': 'iii', 'ITER': 'iterate'},
- {'a': 2, 'b': 'i', 'ITER': 'iterate'},
- {'a': 2, 'b': 'ii', 'ITER': 'iterate'},
- {'a': 2, 'b': 'iii', 'ITER': 'iterate'},
- {'a': 3, 'b': 'i', 'ITER': 'iterate'},
- {'a': 3, 'b': 'ii', 'ITER': 'iterate'},
- {'a': 3, 'b': 'iii', 'ITER': 'iterate'},
- {'a': 1, 'b': 'i', 'ITER': 'over'},
- {'a': 1, 'b': 'ii', 'ITER': 'over'},
- {'a': 1, 'b': 'iii', 'ITER': 'over'},
- {'a': 2, 'b': 'i', 'ITER': 'over'},
- {'a': 2, 'b': 'ii', 'ITER': 'over'},
- {'a': 2, 'b': 'iii', 'ITER': 'over'},
- {'a': 3, 'b': 'i', 'ITER': 'over'},
- {'a': 3, 'b': 'ii', 'ITER': 'over'},
- {'a': 3, 'b': 'iii', 'ITER': 'over'},
- {'a': 1, 'b': 'i', 'ITER': 'me'},
- {'a': 1, 'b': 'ii', 'ITER': 'me'},
- {'a': 1, 'b': 'iii', 'ITER': 'me'},
- {'a': 2, 'b': 'i', 'ITER': 'me'},
- {'a': 2, 'b': 'ii', 'ITER': 'me'},
- {'a': 2, 'b': 'iii', 'ITER': 'me'},
- {'a': 3, 'b': 'i', 'ITER': 'me'},
- {'a': 3, 'b': 'ii', 'ITER': 'me'},
- {'a': 3, 'b': 'iii', 'ITER': 'me'},
- ]
-
- err = self.compare(source, scheme, oracle)
- self.assertFalse(err, "errors found: %s\n" % err)
-
- def test_withCustomIterator_TypeA(self):
-
- class ITER_A:
-
- def __init__(self, items):
- self.items = items
- self.n = 0
-
- def __iter__(self):
- return self
-
- def __next__(self):
- try:
- item = self.items[self.n]
- except IndexError:
- raise StopIteration
- else:
- self.n += 1
- return item
-
- scheme = {
- 'd': {
- 'a': hoover.Cartman.Iterable,
- 'b': hoover.Cartman.Iterable,
- 'ITER_A': hoover.Cartman.Iterable
- }
- }
- source = {
- 'd': {
- 'a': [1, 2, 3],
- 'b': ['i', 'ii', 'iii'],
- 'ITER_A': ITER_A(['iterate', 'over', 'him'])
- }
- }
- oracle = [
- {'d': {'a': 1, 'b': 'i', 'ITER_A': 'iterate'}},
- {'d': {'a': 1, 'b': 'ii', 'ITER_A': 'iterate'}},
- {'d': {'a': 1, 'b': 'iii', 'ITER_A': 'iterate'}},
- {'d': {'a': 2, 'b': 'i', 'ITER_A': 'iterate'}},
- {'d': {'a': 2, 'b': 'ii', 'ITER_A': 'iterate'}},
- {'d': {'a': 2, 'b': 'iii', 'ITER_A': 'iterate'}},
- {'d': {'a': 3, 'b': 'i', 'ITER_A': 'iterate'}},
- {'d': {'a': 3, 'b': 'ii', 'ITER_A': 'iterate'}},
- {'d': {'a': 3, 'b': 'iii', 'ITER_A': 'iterate'}},
- {'d': {'a': 1, 'b': 'i', 'ITER_A': 'over'}},
- {'d': {'a': 1, 'b': 'ii', 'ITER_A': 'over'}},
- {'d': {'a': 1, 'b': 'iii', 'ITER_A': 'over'}},
- {'d': {'a': 2, 'b': 'i', 'ITER_A': 'over'}},
- {'d': {'a': 2, 'b': 'ii', 'ITER_A': 'over'}},
- {'d': {'a': 2, 'b': 'iii', 'ITER_A': 'over'}},
- {'d': {'a': 3, 'b': 'i', 'ITER_A': 'over'}},
- {'d': {'a': 3, 'b': 'ii', 'ITER_A': 'over'}},
- {'d': {'a': 3, 'b': 'iii', 'ITER_A': 'over'}},
- {'d': {'a': 1, 'b': 'i', 'ITER_A': 'him'}},
- {'d': {'a': 1, 'b': 'ii', 'ITER_A': 'him'}},
- {'d': {'a': 1, 'b': 'iii', 'ITER_A': 'him'}},
- {'d': {'a': 2, 'b': 'i', 'ITER_A': 'him'}},
- {'d': {'a': 2, 'b': 'ii', 'ITER_A': 'him'}},
- {'d': {'a': 2, 'b': 'iii', 'ITER_A': 'him'}},
- {'d': {'a': 3, 'b': 'i', 'ITER_A': 'him'}},
- {'d': {'a': 3, 'b': 'ii', 'ITER_A': 'him'}},
- {'d': {'a': 3, 'b': 'iii', 'ITER_A': 'him'}},
- ]
-
- err = self.compare(source, scheme, oracle)
- self.assertFalse(err, "errors found: %s\n" % err)
-
- def test_withCustomIterator_TypeB(self):
-
- class ITER_B:
-
- def __init__(self, items):
- self.items = items
-
- def __getitem__(self, n):
- return self.items[n]
-
- scheme = {
- 'd': {
- 'a': hoover.Cartman.Iterable,
- 'b': hoover.Cartman.Iterable,
- 'ITER_B': hoover.Cartman.Iterable
- }
- }
- source = {
- 'd': {
- 'a': [1, 2, 3],
- 'b': ['i', 'ii', 'iii'],
- 'ITER_B': ITER_B(['iterate', 'by', 'him'])
- }
- }
- oracle = [
- {'d': {'a': 1, 'b': 'i', 'ITER_B': 'iterate'}},
- {'d': {'a': 1, 'b': 'ii', 'ITER_B': 'iterate'}},
- {'d': {'a': 1, 'b': 'iii', 'ITER_B': 'iterate'}},
- {'d': {'a': 2, 'b': 'i', 'ITER_B': 'iterate'}},
- {'d': {'a': 2, 'b': 'ii', 'ITER_B': 'iterate'}},
- {'d': {'a': 2, 'b': 'iii', 'ITER_B': 'iterate'}},
- {'d': {'a': 3, 'b': 'i', 'ITER_B': 'iterate'}},
- {'d': {'a': 3, 'b': 'ii', 'ITER_B': 'iterate'}},
- {'d': {'a': 3, 'b': 'iii', 'ITER_B': 'iterate'}},
- {'d': {'a': 1, 'b': 'i', 'ITER_B': 'by'}},
- {'d': {'a': 1, 'b': 'ii', 'ITER_B': 'by'}},
- {'d': {'a': 1, 'b': 'iii', 'ITER_B': 'by'}},
- {'d': {'a': 2, 'b': 'i', 'ITER_B': 'by'}},
- {'d': {'a': 2, 'b': 'ii', 'ITER_B': 'by'}},
- {'d': {'a': 2, 'b': 'iii', 'ITER_B': 'by'}},
- {'d': {'a': 3, 'b': 'i', 'ITER_B': 'by'}},
- {'d': {'a': 3, 'b': 'ii', 'ITER_B': 'by'}},
- {'d': {'a': 3, 'b': 'iii', 'ITER_B': 'by'}},
- {'d': {'a': 1, 'b': 'i', 'ITER_B': 'him'}},
- {'d': {'a': 1, 'b': 'ii', 'ITER_B': 'him'}},
- {'d': {'a': 1, 'b': 'iii', 'ITER_B': 'him'}},
- {'d': {'a': 2, 'b': 'i', 'ITER_B': 'him'}},
- {'d': {'a': 2, 'b': 'ii', 'ITER_B': 'him'}},
- {'d': {'a': 2, 'b': 'iii', 'ITER_B': 'him'}},
- {'d': {'a': 3, 'b': 'i', 'ITER_B': 'him'}},
- {'d': {'a': 3, 'b': 'ii', 'ITER_B': 'him'}},
- {'d': {'a': 3, 'b': 'iii', 'ITER_B': 'him'}},
- ]
-
- err = self.compare(source, scheme, oracle)
- self.assertFalse(err, "errors found: %s\n" % err)
-
- def test_BadSchemeBelow(self):
-
- def fn():
-
- scheme = {
- 'h': 1,
- 'p': hoover.Cartman.Iterable,
- 'd': hoover.Cartman.Iterable
- }
- source = {
- 'h': {
- 'ua': ['ua1', 'ua2']
- },
- 'p': ['a', 'b'],
- 'd': [False, True]
- }
- next(iter(hoover.Cartman(source, scheme)))
-
- self.assertRaises(ValueError, fn)
-
- def test_BadSourceBelow(self):
-
- def fn():
-
- scheme = {
- 'h': {
- 'ua': hoover.Cartman.Iterable,
- },
- 'p': hoover.Cartman.Iterable,
- 'd': hoover.Cartman.Iterable
- }
- source = {
- 'h': 'NOT A CORRESPONDING OBJECT',
- 'p': ['a', 'b'],
- 'd': [False, True]
- }
- next(iter(hoover.Cartman(source, scheme)))
-
- self.assertRaises(ValueError, fn)
-
- def test_BadMark(self):
-
- def fn():
- class a_mark(hoover.Cartman._BaseMark):
- pass
- scheme = {
- 'a': hoover.Cartman.Iterable,
- 'b': hoover.Cartman.Iterable,
- 'c': a_mark
- }
- source = dict.fromkeys(scheme, [])
- next(iter(hoover.Cartman(source, scheme)))
-
- self.assertRaises(ValueError, fn)
-
-
- class RuleOpTest(unittest.TestCase):
-
- # basic cases
-
- def testAllEmpty(self):
- oracle = True
- result = hoover.RuleOp.Match((hoover.RuleOp.ALL, []), bool)
- self.assertEqual(oracle, result)
-
- def testAllTrue(self):
- oracle = True
- result = hoover.RuleOp.Match((hoover.RuleOp.ALL, [1, 1, 1]), bool)
- self.assertEqual(oracle, result)
-
- def testAllMixed(self):
- oracle = False
- result = hoover.RuleOp.Match((hoover.RuleOp.ALL, [1, 0, 1]), bool)
- self.assertEqual(oracle, result)
-
- def testAllFalse(self):
- oracle = False
- result = hoover.RuleOp.Match((hoover.RuleOp.ALL, [0, 0, 0]), bool)
- self.assertEqual(oracle, result)
-
- def testAnyEmpty(self):
- oracle = False
- result = hoover.RuleOp.Match((hoover.RuleOp.ANY, []), bool)
- self.assertEqual(oracle, result)
-
- def testAnyTrue(self):
- oracle = True
- result = hoover.RuleOp.Match((hoover.RuleOp.ANY, [1, 1, 1]), bool)
- self.assertEqual(oracle, result)
-
- def testAnyMixed(self):
- oracle = True
- result = hoover.RuleOp.Match((hoover.RuleOp.ANY, [1, 0, 1]), bool)
- self.assertEqual(oracle, result)
-
- def testAnyFalse(self):
- oracle = False
- result = hoover.RuleOp.Match((hoover.RuleOp.ANY, [0, 0, 0]), bool)
- self.assertEqual(oracle, result)
-
- # nesting
-
- def testAnyAllTrue(self):
- patt = (hoover.RuleOp.ANY, [(hoover.RuleOp.ALL, [1, 1]), 0, 0])
- oracle = True
- result = hoover.RuleOp.Match(patt, bool)
- self.assertEqual(oracle, result)
-
- def testAllAnyFalse(self):
- patt = (hoover.RuleOp.ALL, [1, (hoover.RuleOp.ANY, [0, 0]), 1, 1])
- oracle = False
- result = hoover.RuleOp.Match(patt, bool)
- self.assertEqual(oracle, result)
-
- # error handling
-
- def testBadOpClass(self):
- class bad_op:
- def _match(self):
- return True
- fn = lambda: hoover.RuleOp.Match(((bad_op, [])), bool)
- self.assertRaises(ValueError, fn)
-
- def testBadOpNonClass(self):
- fn = lambda: hoover.RuleOp.Match((("bad_op", [])), bool)
- self.assertRaises(ValueError, fn)
-
- def testBadPatternScalar(self):
- fn = lambda: hoover.RuleOp.Match(43, bool)
- self.assertRaises(ValueError, fn)
-
- def testBadPatternShort(self):
- fn = lambda: hoover.RuleOp.Match((43,), bool)
- self.assertRaises(ValueError, fn)
-
- def testBadPatternLong(self):
- fn = lambda: hoover.RuleOp.Match((43, 41, 42), bool)
- self.assertRaises(ValueError, fn)
-
- def testBadItems(self):
- fn = lambda: hoover.RuleOp.Match(((hoover.RuleOp.ALL, 1)), bool)
- self.assertRaises(ValueError, fn)
-
- # own operator
-
- def testOwnOp(self):
- class MYOP(hoover._BaseRuleOp):
- def _match(self):
- return True
- oracle = True
- result = hoover.RuleOp.Match((MYOP, [0, 1, 2]), bool)
- self.assertEqual(oracle, result)
-
-
- class JsDiff(unittest.TestCase):
-
- def setUp(self):
- self.a = {
- 'joe': 31,
- 'johnny': 55,
- 'twins': {
- 'al': 1,
- 'bo': 1,
- 'ww': 1
- },
- 'annie': 1,
- 'todo': [
- 'buy milk',
- 'visit aunt Emma',
- {'buy presents': [
- 'for daddy',
- 'for mommy',
- 'for sister',
- 'for brother'
- ]},
- 'stop smoking',
- 'make less promises'
- ],
- 'stones': [
- 'red stone',
- 'stone',
- 'get stoned'
- ]
- }
-
- self.b = {
- 'joe': 31,
- 'johnny': 55,
- 'twins': {
- 'al': 1,
- 'bo': 1,
- 'ww': 1
- },
- 'annie': 3,
- 'todo': [
- 'buy milk',
- {'buy presents': [
- 'for sister',
- 'for brother'
- ]},
- 'stop smoking',
- 'take over the world',
- 'make less promises'
- ],
- 'tamara': 110,
- 'stones': [
- 'red stone',
- 'moonstone',
- 'stone',
- 'get stoned'
- ]
- }
-
- def testDense(self):
- oracle = (
- 'aaa ~/A\n'
- ' {\n'
- 'a "annie": 1,\n'
- ' "stones": [\n'
- ' "todo": [\n'
- 'a "visit aunt Emma",\n'
- ' "buy presents": [\n'
- 'a "for daddy",\n'
- 'a "for mommy",\n'
- 'bbb ~/B\n'
- ' {\n'
- 'b "annie": 3,\n'
- ' "stones": [\n'
- 'b "moonstone",\n'
- 'b "tamara": 110,\n'
- ' "todo": [\n'
- ' "buy presents": [\n'
- 'b "take over the world",'
- )
- result = hoover.jsDiff(self.a, self.b)
- self.assertEqual(oracle, result)
-
-
- class DataMatch(unittest.TestCase):
-
- class sdict(dict):
- pass
-
- class slist(list):
- pass
-
- def setUp(self):
- super(DataMatch, self).setUp()
-
- # dict
-
- def test_Dict_Ok(self):
- p = { 1: 2 }
- r = { 1: 2, 3: 4 }
- self.assertTrue(hoover.dataMatch(p, r))
-
- def test_Dict_Nok(self):
- p = { 1: 2 }
- r = { 1: 3, 3: 4 }
- self.assertFalse(hoover.dataMatch(p, r))
-
- def test_DictDict_Ok(self):
- p = { 'a': { 'A': 'B' } }
- r = { 1: 2, 'a': { 'A': 'B' } }
- self.assertTrue(hoover.dataMatch(p, r))
-
- def test_DictDict_Nok(self):
- p = { 'a': { 'A': 'B' } }
- r = { 1: 2, 'a': { 'C': 'D' } }
- self.assertFalse(hoover.dataMatch(p, r))
-
- def test_DictList_Ok(self):
- p = { 3: [ 11, 12 ] }
- r = { 1: 2, 3: [ 10, 11, 12, 13 ] }
- self.assertTrue(hoover.dataMatch(p, r))
-
- def test_DictList_Nok(self):
- p = { 3: [ 11, 12 ] }
- r = { 1: 2, 3: [ 10, 11, 13 ] }
- self.assertFalse(hoover.dataMatch(p, r))
-
- # list
-
- def test_List_Ok(self):
- p = [ 101, 102 ]
- r = [ 101, 103, 102 ]
- self.assertTrue(hoover.dataMatch(p, r))
-
- def test_List_Nok(self):
- p = [ 101, 102 ]
- r = [ 101, 103 ]
- self.assertFalse(hoover.dataMatch(p, r))
-
- def test_ListList_Ok(self):
- p = [ 101, ['a', 'b'], 102 ]
- r = [ 101, [1, 'a', 2, 'b'], 103, 102 ]
- self.assertTrue(hoover.dataMatch(p, r))
-
- def test_ListDict_Ok(self):
- p = [ 101, {'a': 'A'}, 102 ]
- r = [ 101, {'a': 'A', 'b': 'B'}, 103, 102 ]
- self.assertTrue(hoover.dataMatch(p, r))
-
- def test_ListDict_Nok(self):
- p = [ 101, {'a': 'A'}, 102 ]
- r = [ 101, {'a': 'X', 'b': 'B'}, 103, 102 ]
- self.assertFalse(hoover.dataMatch(p, r))
-
- # dict/list subclass
-
- def test_DictPSub_Ok(self):
- p = self.sdict({ 1: 2 })
- r = { 1: 2, 3: 4 }
- self.assertTrue(hoover.dataMatch(p, r))
-
- def test_DictPSub_Nok(self):
- p = self.sdict({ 1: 2 })
- r = { 1: 3, 3: 4 }
- self.assertFalse(hoover.dataMatch(p, r))
-
- def test_DictRSub_Ok(self):
- p = { 1: 2 }
- r = self.sdict({ 1: 2, 3: 4 })
- self.assertTrue(hoover.dataMatch(p, r))
-
- def test_DictRSub_Nok(self):
- p = { 1: 2 }
- r = self.sdict({ 1: 3, 3: 4 })
- self.assertFalse(hoover.dataMatch(p, r))
-
- def test_DictPRSub_Ok(self):
- p = self.sdict({ 1: 2 })
- r = self.sdict({ 1: 2, 3: 4 })
- self.assertTrue(hoover.dataMatch(p, r))
-
- def test_DictPRSub_Nok(self):
- p = self.sdict({ 1: 2 })
- r = self.sdict({ 1: 3, 3: 4 })
- self.assertFalse(hoover.dataMatch(p, r))
-
- def test_ListPSub_Ok(self):
- p = self.slist([ 101, 102 ])
- r = [ 101, 103, 102 ]
- self.assertTrue(hoover.dataMatch(p, r))
-
- def test_ListPSub_Nok(self):
- p = self.slist([ 101, 102 ])
- r = [ 101, 103 ]
- self.assertFalse(hoover.dataMatch(p, r))
-
- def test_ListRSub_Ok(self):
- p = [ 101, 102 ]
- r = self.slist([ 101, 103, 102 ])
- self.assertTrue(hoover.dataMatch(p, r))
-
- def test_ListRSub_Nok(self):
- p = [ 101, 102 ]
- r = self.slist([ 101, 103 ])
- self.assertFalse(hoover.dataMatch(p, r))
-
- def test_ListPRSub_Ok(self):
- p = self.slist([ 101, 102 ])
- r = self.slist([ 101, 103, 102 ])
- self.assertTrue(hoover.dataMatch(p, r))
-
- def test_ListPRSub_Nok(self):
- p = self.slist([ 101, 102 ])
- r = self.slist([ 101, 103 ])
- self.assertFalse(hoover.dataMatch(p, r))
-
-
- if __name__ == "__main__":
- unittest.main()
|