コード例 #1
0
 def test_write_changes_to_memory(self):
     # make a mock list of proposed changes
     STORAGE.PROPOSED_CHANGES_PATH = \
         "storage_manager_test_files/mock_changes_storage_file2"
     p = Planet.Planet("a")
     p.lastupdate = "16/11/20"
     p2 = Planet.Planet("b")
     p2.lastupdate = "16/11/20"
     c1 = Change.Addition("origin str", p)
     c2 = Change.Modification("Origin str", p, p2, "field", 10, 15)
     changes_list = [c1, c2]
     # write the list to memory
     STORAGE.write_changes_to_memory(changes_list)
     # retrieve the list and make sure that nothing have been changed
     retrieved = STORAGE.read_changes_from_memory()
     self.assertEquals(len(retrieved), 2)
     self.assertEquals(retrieved[0].__class__.__name__, "Addition")
     self.assertEquals(retrieved[0].origin, "origin str")
     self.assertEquals(retrieved[0].get_object_name(), p.name)
     self.assertEquals(retrieved[1].__class__.__name__, "Modification")
     self.assertEquals(retrieved[1].origin, "Origin str")
     self.assertEquals(retrieved[1].get_object_name(), p.name)
     self.assertEquals(retrieved[1].field_modified, "field")
     self.assertEquals(retrieved[1].value_in_origin_catalogue, 10)
     self.assertEquals(retrieved[1].value_in_OEC, 15)
コード例 #2
0
 def testInit(self):
     planet1 = Planet("testPlanet")
     self.assertEquals("testPlanet", planet1.getName())
     self.assertEquals(None, planet1.starObject)
     self.assertEquals(dict(), planet1.starObjectNamesToStar)
     self.assertEquals(dict(), planet1.errors)
     self.assertEquals([], planet1.otherNamesPlanet)
     self.assertEquals(dict(), planet1.data)
     self.assertEquals("", planet1.nameStar)
     self.assertEquals("00/00/00", planet1.lastupdate)
コード例 #3
0
 def test_config_set_get_object(self):
     STORAGE.CONFIG_PATH = "storage_manager_test_files/mock_config_file"
     STORAGE.clean_config_file()
     p = Planet.Planet("a")
     p.lastupdate = "16/11/20"
     test_object = Change.Addition("origin", p)
     STORAGE.config_set("key", test_object)
     retrieved = STORAGE.config_get("key")
     self.assertEqual(retrieved.__class__.__name__, "Addition")
     self.assertEqual(retrieved.origin, "origin")
コード例 #4
0
 def __init__(self, *args, **kwargs):
     super(testing_merge, self).__init__(*args, **kwargs)
     self.planet1 = Planet.Planet("A")
     self.planet2 = Planet.Planet("B")
     self.planet3 = Planet.Planet("C")
     self.planet4 = Planet.Planet("RR")
     self.planet5 = Planet.Planet("UU")
     self.planet6 = Planet.Planet("dddddd")
     self.p = Planet.Planet("Z")
コード例 #5
0
    def setUp(self):
        self.planet1 = Planet("planet1")
        self.planet1.addVal("mass", 10)
        self.planet2 = Planet("planet2")
        self.planet2.addVal("mass", 12)
        self.planet2.addVal("temperature", 145)
        self.Star1 = Star("star1")
        self.Star1.addVal("mass", 100)
        self.Star2 = Star("star2")
        self.Star2.addVal("mass", 112)
        self.Star3 = Star("star3")

        self.system1 = System("sys1")
        self.planet3 = Planet("planet3")
        self.planet3.addVal("temperature", 145)
        self.planet4 = Planet("planet4")
        self.Star1.planetObjects = [self.planet1, self.planet3]
        self.Star2.planetObjects = [self.planet1, self.planet3, self.planet4]
        self.Star3.planetObjects = [self.planet1, self.planet3]

        self.EStar1 = Star("empty1")
        self.EStar2 = Star("empty2")
コード例 #6
0
    def test_sort_order(self):
        self.p1 = Planet.Planet("a")
        self.p2 = Planet.Planet("b")
        self.p3 = Planet.Planet("c")
        self.p4 = Planet.Planet("d")
        self.p5 = Planet.Planet("e")
        self.p6 = Planet.Planet("f")
        a = Modification("aaa", self.p1, self.p, "fieldname", 10, 11)
        b = Addition("source", self.p2)
        c = Addition("source", self.p3)
        d = Modification("source", self.p4, self.p, "field", 5, 6)
        e = Modification("source", self.p5, self.p, "field", 1, 2)
        f = Modification("source", self.p6, self.p, "field", 3, 4)

        L = [b, a, d, c, f, e]
        result = merge_sort_changes(L)
        self.assertTrue(isinstance(result, list))
        self.assertEqual(len(result), 6)
        self.assertEqual(result[0].get_object_name(), a.get_object_name())
        self.assertEqual(result[1].get_object_name(), b.get_object_name())
        self.assertEqual(result[2].get_object_name(), c.get_object_name())
        self.assertEqual(result[3].get_object_name(), d.get_object_name())
        self.assertEqual(result[4].get_object_name(), e.get_object_name())
        self.assertEqual(result[5].get_object_name(), f.get_object_name())
コード例 #7
0
class TestComparator(unittest.TestCase):
    def setUp(self):
        self.planet1 = Planet("planet1")
        self.planet1.addVal("mass", 10)
        self.planet2 = Planet("planet2")
        self.planet2.addVal("mass", 12)
        self.planet2.addVal("temperature", 145)
        self.Star1 = Star("star1")
        self.Star1.addVal("mass", 100)
        self.Star2 = Star("star2")
        self.Star2.addVal("mass", 112)
        self.Star3 = Star("star3")

        self.system1 = System("sys1")
        self.planet3 = Planet("planet3")
        self.planet3.addVal("temperature", 145)
        self.planet4 = Planet("planet4")
        self.Star1.planetObjects = [self.planet1, self.planet3]
        self.Star2.planetObjects = [self.planet1, self.planet3, self.planet4]
        self.Star3.planetObjects = [self.planet1, self.planet3]

        self.EStar1 = Star("empty1")
        self.EStar2 = Star("empty2")

    def testCreateComparatorWithNonPlanetaryObjects(self):
        try:
            rip = Comparator("egg", 1, "eu")
        except ObjectTypeMismatchException:
            self.assertTrue(True)

    def testCreateComparatorWithDifferentPlanetaryObjects(self):
        try:
            rip = Comparator(self.planet1, self.Star1, "eu")
        except ObjectTypeMismatchException:
            self.assertTrue(True)

    def testSQLjoin(self):
        comparator = Comparator(self.planet2, self.planet1, "eu")
        result = comparator.sqlJoin(True)
        a = result["data"]
        b = ["mass", "temperature"]
        self.assertTrue(
            len(a) == len(b) and all(a.count(i) == b.count(i) for i in a))
        c = result["left"]
        d = [12, 145]
        self.assertTrue(
            len(c) == len(d) and all(c.count(i) == d.count(i) for i in c))
        e = result["right"]
        f = [10, "N/A"]
        self.assertTrue(
            len(e) == len(f) and all(e.count(i) == f.count(i) for i in e))

    def testInnerJoinDiffFieldMatch(self):
        comparator = Comparator(self.Star1, self.Star2, "eu")
        inner = comparator.innerJoinDiff()
        self.assertEqual(inner, {'mass': (100, 112)})

    def testInnerJoinDiffFieldDiff(self):
        comparator = Comparator(self.planet1, self.planet3, "eu")
        inner = comparator.innerJoinDiff()
        self.assertEqual(inner, {})

    def testStarCompareWithNonStarObjects(self):
        comparator = Comparator(self.planet1, self.planet2, "eu")
        try:
            comparator.starCompare()
        except ObjectTypeIncompatibleException:
            self.assertTrue(True)

    def testStarCompareEmptyStarsStarC(self):
        comparator = Comparator(self.EStar1, self.EStar2, "eu")
        result = comparator.starCompare()
        self.assertEqual(result["starC"], {})

    def testStarCompareEmptyStarsStarN(self):
        comparator = Comparator(self.EStar1, self.EStar2, "eu")
        result = comparator.starCompare()
        self.assertEqual(result["starN"]["data"], [])
        self.assertEqual(result["starN"]["left"], [])
        self.assertEqual(result["starN"]["right"], [])

    def testStarCompareEmptyStarsPlanetN(self):
        comparator = Comparator(self.EStar1, self.EStar2, "eu")
        result = comparator.starCompare()
        self.assertEqual(result["planetN"]["left"], [])
        self.assertEqual(result["planetN"]["right"], [])

    def testStarCompareEmptyStarsPlanetDN(self):
        comparator = Comparator(self.EStar1, self.EStar2, "eu")
        result = comparator.starCompare()
        self.assertEqual(result["planetDN"], {})

    def testStarCompareEmptyStarsPlanetDC(self):
        comparator = Comparator(self.EStar1, self.EStar2, "eu")
        result = comparator.starCompare()
        self.assertEqual(result["planetDC"], {})

    def testStarCompareEmptyStarsPlanetA(self):
        comparator = Comparator(self.EStar1, self.EStar2, "eu")
        result = comparator.starCompare()
        self.assertEqual(result["planetA"], {})

    def testStarCompareStarWithOneFieldStarC(self):
        comparator = Comparator(self.Star1, self.Star2, "eu")
        result = comparator.starCompare()
        starC = result["starC"]
        answer = {"mass": (100.0, 112.0)}
        self.assertEqual(starC, answer)

    def testStarCompareStarWithOneFieldStarN(self):
        comparator = Comparator(self.Star1, self.Star2, "eu")
        result = comparator.starCompare()
        starN = result["starN"]
        answer = {"data": ["mass"], "left": [100.0], "right": [112.0]}
        self.assertEqual(starN, answer)

    def testStarCompareStarWithOneFieldStarN2(self):
        comparator = Comparator(self.Star1, self.Star2, "eu")
        result = comparator.starCompare()
        planetN = result["planetN"]
        answer = {"left": [], "right": [self.planet4]}
        self.assertEqual(planetN, answer)

    def testStarCompareStarWithOneFieldPlanetDN(self):
        comparator = Comparator(self.Star1, self.Star2, "eu")
        result = comparator.starCompare()
        planetDN = result["planetDN"]
        answer = {}
        self.assertEqual(planetDN, answer)

    def testStarCompareStarWithOneFieldPlanetDC(self):
        comparator = Comparator(self.Star1, self.Star2, "eu")
        result = comparator.starCompare()
        planetDC = result["planetDC"]
        answer = {}
        self.assertEqual(planetDC, answer)

    def testStarCompareStarWithOneFieldPlanetA(self):
        comparator = Comparator(self.Star1, self.Star2, "eu")
        result = comparator.starCompare()
        planetA = result["planetA"]
        answer = {"planet1": self.planet1, "planet3": self.planet3}
        self.assertEqual(planetA, answer)

    def testproposedChangeStarCompare(self):
        comparator = Comparator(self.Star3, self.Star2, "eu")
        result = comparator.proposedChangeStarCompare()
        resultNames = [
            result[0].get_object_name(), result[1].get_object_name()
        ]
        answer = [self.planet1.name, self.planet3.name]
        print(resultNames, answer)
        self.assertTrue(
            len(resultNames) == len(answer) and all(
                resultNames.count(i) == answer.count(i) for i in resultNames))
コード例 #8
0
def buildSystemFromXML(path="../storage/OEC_XML.gz"):
    '''
    () -> ([System], [Star], [Planet], {systemName: System}, {starName: Star},
           {planetName: Planet})
    Parse the xml from the big xml document
    that contains every system into a tuple of system objects, star objects,
    and planet objects. Each Planetary Object dictionary contains the fields
    that are present from the xml it was parsed from, no more, no less.
    The system have a list of references to its stars, the stars have a list of
    references to its planets, and the planets have a reference to the star
    and system it is in, and stars have a refernece to the system it is in
    REQ: Valid internet connection
    '''
    # initialize empty lists that will be returned at the end  of all
    # planetary objects
    oec = readXML(path)
    allSystems = []
    allStars = []
    allPlanets = []
    allSystemsDict = dict()
    allStarsDict = dict()
    allPlanetsDict = dict()
    # loop through each system in the xml
    for systemXML in oec.findall(".//system"):
        # loop through teach tag in the system that is name
        i = 0
        for child in systemXML.findall(".//name"):
            cleanNameSystem = ''.join(
                ch for ch in child.text if ch.isalnum()).lower()
            if child.tag == "name":
                # if it is the first name, create a System object with that
                # main name
                if i == 0:
                    systemName = child.text
                    system = System(systemName)
                    allSystemsDict[child.text] = system
                    system.otherNamesSystem.append(cleanNameSystem)
                # if there are more names, create / add them to other names list
                elif i == 1:
                    system.otherNamesSystem.append(child.text)
                    system.otherNamesSystem.append(cleanNameSystem)
                    allSystemsDict[child.text] = system
                else:
                    system.otherNamesSystem.append(child.text)
                    system.otherNamesSystem.append(cleanNameSystem)
                    allSystemsDict[child.text] = system
                i += 1
            else:
                system.otherNamesSystem.append(child.text)
                system.otherNamesSystem.append(cleanNameSystem)
                allSystemsDict[child.text] = system

        # build the system data dictionary mapping the tag name to the tag value
        # in the system
        for child in systemXML:
            if (child.tag.lower() != "star") and (child.tag.lower() != "name"):
                system.addVal(child.tag, child.text)

        # build a list of stars that are in the system
        stars = []
        # loop through each star in the system
        localStarsDict = dict()
        for starXML in systemXML.findall(".//star"):
            ii = 0
            # loop through teach tag in the star that is name
            for child in starXML.findall(".//name"):
                cleanNameStar = ''.join(
                    ch for ch in child.text if ch.isalnum()).lower()
                if child.tag == "name":
                    # if it is the first name, create a Star object with that
                    # main name
                    if ii == 0:
                        star = Star(child.text)
                        allStarsDict[child.text] = star
                        localStarsDict[child.text] = star
                        localStarsDict[cleanNameStar] = star
                        star.otherNamesStar.append(cleanNameStar)
                    # if there are more names, create / add them to other names
                    # list
                    elif ii == 1:
                        star.otherNamesStar.append(child.text)
                        star.otherNamesStar.append(cleanNameStar)
                        allStarsDict[child.text] = star
                        localStarsDict[child.text] = star
                        localStarsDict[cleanNameStar] = star
                    else:
                        star.otherNamesStar.append(child.text)
                        star.otherNamesStar.append(cleanNameStar)
                        allStarsDict[child.text] = star
                        localStarsDict[child.text] = star
                        localStarsDict[cleanNameStar] = star
                    ii += 1
                else:
                    star.otherNamesStar.append(child.text)
                    star.otherNamesStar.append(cleanNameStar)
                    allStarsDict[child.text] = star
                    localStarsDict[child.text] = star
                    localStarsDict[cleanNameStar] = star

            # build the star data dictionary mapping the tag name to the tag
            # value in the system
            for child in starXML:
                if (child.tag.lower() != "planet") and (
                            child.tag.lower() != "name"):
                    star.addVal(child.tag, child.text)
                    for attribute in child.attrib:
                        if "error" in attribute or "limit" in attribute:
                            star.errors[child.tag + attribute] = child.attrib[
                                attribute]

            # build a list of planets that are in the star
            planets = []
            # loop through each planet in the star
            localPlanetsDict = dict()
            for planetXML in starXML.findall(".//planet"):
                iii = 0
                # loop through teach tag in the planet that is name
                for child in planetXML.findall(".//name"):
                    cleanNamePlanets = ''.join(
                        ch for ch in child.text if ch.isalnum()).lower()
                    if child.tag == "name":
                        # if it is the first name, create a Planet object with
                        # that main name
                        if iii == 0:
                            planet = Planet(child.text)
                            allPlanetsDict[child.text] = planet
                            localPlanetsDict[child.text] = planet
                            localPlanetsDict[cleanNamePlanets] = planet
                            planet.otherNamesPlanet.append(cleanNamePlanets)
                        # if there are more names, create / add them to other
                        # names list
                        elif iii == 1:
                            planet.otherNamesPlanet.append(child.text)
                            planet.otherNamesPlanet.append(cleanNamePlanets)
                            allPlanetsDict[child.text] = planet
                            localPlanetsDict[child.text] = planet
                            localPlanetsDict[cleanNamePlanets] = planet
                        else:
                            planet.otherNamesPlanet.append(child.text)
                            planet.otherNamesPlanet.append(cleanNamePlanets)
                            allPlanetsDict[child.text] = planet
                            localPlanetsDict[child.text] = planet
                            localPlanetsDict[cleanNamePlanets] = planet
                        iii += 1
                    else:
                        planet.otherNamesPlanet.append(child.text)
                        planet.otherNamesPlanet.append(cleanNamePlanets)
                        allPlanetsDict[child.text] = planet
                        localPlanetsDict[child.text] = planet
                        localPlanetsDict[cleanNamePlanets] = planet

                # build the planet data dictionary mapping the tag name to the
                # tag value in the system
                for child in planetXML:
                    if (child.tag.lower() != "name") and (
                                child.tag.lower() != "lastupdate"):
                        planet.addVal(child.tag, child.text)
                        for attribute in child.attrib:
                            if "error" in attribute or "limit" in attribute:
                                planet.errors[child.tag + attribute] = \
                                    child.attrib[
                                        attribute]

                # add the star name that the planet is in
                planet.nameStar = star.name
                planet.starObjectNamesToStar[star.name] = star
                planet.starObjectNamesToStar[''.join(
                    ch for ch in star.name if ch.isalnum()).lower()] = star

                starData = star.getData()
                # and others if there are any
                planet.otherNamesStar = star.otherNamesStar
                for starObject in star.otherNamesStar:
                    planet.starObjectNamesToStar[
                        starObject] = star
                    planet.starObjectNamesToStar[
                        ''.join(ch for ch in starObject if
                                ch.isalnum()).lower()] = star
                # add this planet to the list of planets in the star
                planets.append(planet)
                # and all planets list
                allPlanets.append(planet)
                # add the star reference in the planet
                planet.starObject = star

            # add the list of planets in the star to the star
            star.planetObjects = planets
            # add the name of the system that the star is in
            star.nameSystem = system.name
            star.systemObjectNamesToSystem[star.nameSystem] = system
            star.systemObjectNamesToSystem[
                ''.join(ch for ch in star.nameSystem if
                        ch.isalnum()).lower()] = system
            star.nameToPlanet = localPlanetsDict
            systemData = system.getData()
            # and others if there are any
            star.otherNamesSystem = system.otherNamesSystem
            for systemObject in system.otherNamesSystem:
                star.systemObjectNamesToSystem[
                    systemObject] = system
                star.systemObjectNamesToSystem[
                    ''.join(ch for ch in systemObject if
                            ch.isalnum()).lower()] = system
            # add the stars to the list of stars in the system
            stars.append(star)
            # and all stars list
            allStars.append(star)
            # add the system reference in the star
            star.systemObject = system
            # add the list of stars in the system to the system
            system.starObjects = stars
            system.nameToStar = localStarsDict
        # add the system to the list of all systems list
        allSystems.append(system)

    return (allSystems, allStars, allPlanets, allSystemsDict, allStarsDict,
            allPlanetsDict)
コード例 #9
0
 def __init__(self, *args, **kwargs):
     super(testing_constructors, self).__init__(*args, **kwargs)
     self.planet = Planet.Planet("A")
     self.star = Star.Star("B")
     self.system = System.System("C")
     self.p = Planet.Planet("Z")