def test_invalid_addition(self): Port.add_or_update([{ "portdir": "categoryA/port-A1", "name": "port-A1", "version": "1.2", "maintainers": [{ "email": { "domain": "email.com", "name": "user" }, "github": "user" }, { "email": { "domain": "email.com", "name": "user" }, "github": "user" }] }]) self.assertEquals( Port.objects.get(name="port-A1").maintainers.all().count(), 1) self.assertEquals(Maintainer.objects.all().count(), 6)
def test_existing_maintainer(self): # adding a maintainer that already exists to another port Port.add_or_update([{ "portdir": "categoryA\/port-C1", "name": "port-A1", "version": "1.2.3", "maintainers": [{ "email": { "domain": "macports.org", "name": "user3" }, "github": "user3" }, { "email": { "domain": "email.com", "name": "user" }, "github": "user" }] }]) self.assertEquals( Port.objects.get(name="port-A1").maintainers.all().count(), 2) self.assertEquals(Maintainer.objects.all().count(), 6)
def test_deleted(self): # mark_deleted function expects everything to be in lower case Port.mark_deleted({'categorya/port-a1': {'port-a1'}}) port_status_subport = Port.objects.get(name='port-A1-subport').active port_status_mainport = Port.objects.get(name='port-A1').active self.assertEquals(port_status_mainport, True) self.assertEquals(port_status_subport, False)
def test_remove_maintainer(self): # adding a maintainer that already exists to another port Port.add_or_update([{ "portdir": "categoryA\/port-C1", "name": "port-A1", "version": "1.2.3", }]) self.assertEquals( Port.objects.get(name="port-A1").maintainers.all().count(), 0) self.assertEquals(Maintainer.objects.all().count(), 6)
def test_relations(self): t_port = 'port-A1' t_category = 'categoryA' # try to add a category and then remove it from a port Port.add_or_update([{ "portdir": "categoryA\/port-A1", "platforms": "darwin", "name": "port-A1", "categories": ["categoryNEW"], "version": "1.0.0", }]) self.assertEquals(Category.objects.all().count(), 4) # we must get just one object for categoryNEW self.assertEquals( Port.objects.get(categories__name='categoryNEW').name, t_port) # check relations of categoryA now broken = False ports = Category.objects.get(name=t_category).ports.all() for port in ports: if port.name == 'port-A1': broken = True self.assertEquals(broken, False) # now add the category back again Port.add_or_update([{ "portdir": "categoryA\/port-A1", "platforms": "darwin", "name": "port-A1", "categories": ["categoryNEW", t_category], "version": "1.0.0", }]) self.assertEquals(Category.objects.all().count(), 4) self.assertEquals( Port.objects.get(categories__name='categoryNEW').name, t_port) # check relations of categoryA now broken = True ports = Category.objects.get(name=t_category).ports.all() for port in ports: if port.name == 'port-A1': broken = False self.assertEquals(broken, False)
def test_different_case(self): Port.add_or_update([{ "portdir": "categoryA\/port-A1", "platforms": "darwin", "name": "port-A1", "categories": ["categoryA"], "version": "1.0.0", "revision": "0", "vinfo": [{ "variant": "UNIVERSAL" }] }]) self.assertEquals( Port.objects.get(name='port-A1').variants.all().count(), 1) self.assertEquals(Variant.objects.all().count(), 8)
def test_invalid_case(self): # try to update a port with the category in upper case Port.add_or_update([{ "portdir": "categoryA\/port-A1", "platforms": "darwin", "name": "port-A1-subport", "categories": ["CATEGORYA"], "version": "1.0.0", }]) self.assertEquals(Category.objects.all().count(), 3) self.assertEquals( Category.objects.get(name='categoryA').name, 'categoryA') self.assertEquals( Category.objects.get(name__iexact='CATEGORYA').name, 'categoryA')
def test_updates(self): updated_port = [{ "name": "port-A5", "version": "1.0.0", "portdir": "categoryA/port-A5", "depends_extract": ["bin:port-C1:port-C1"], "depends_run": ["port:port-A1"], }] Port.add_or_update(updated_port) dependencies = Dependency.objects.filter( port_name__name__iexact='port-A5') self.assertEquals( dependencies.get(type='run').dependencies.all().count(), 1) self.assertEquals( dependencies.get(type='run').dependencies.all().first().name, 'port-A1') self.assertEquals(dependencies.count(), 2)
def handle(self, *args, **options): type_of_run = options['type'] if type_of_run == 'full': git_update.refresh_portindex_json() data = git_update.get_portindex_json() if data is None: raise CommandError("Failed to parse portindex.json") Port.add_or_update(data['ports']) Port.mark_deleted_full_run(data['ports']) LastPortIndexUpdate.update_or_create_first_object( data['info']['commit']) return # It is an incremental update # An incremental update is only possible when the database has a # history of the commit till which it is up to date old_commit_object = LastPortIndexUpdate.objects.all().first() if old_commit_object is None: raise CommandError( "Failed to run incremental update. No old commit found, cannot generate range of commits." ) updated_portdirs = git_update.get_updated_portdirs() # fetch the latest version of PortIndex.json and open the file data = git_update.get_portindex_json() if data is None: raise CommandError("Failed to parse portindex.json") # Generate a dictionary containing all the portdirs and initialise their values # with empty sets. The set would contain the ports under that portdir. dict_of_portdirs_with_ports = {} for portdir in updated_portdirs: dict_of_portdirs_with_ports[portdir] = set() # Using the received set of updated portdirs, find corresponding JSON objects for all ports under # that portdir. ports_to_be_updated_json = [] for port in data['ports']: portdir = port['portdir'].lower() portname = port['name'].lower() if portdir in updated_portdirs: ports_to_be_updated_json.append(port) dict_of_portdirs_with_ports[portdir].add(portname) # Mark deleted ports Port.mark_deleted(dict_of_portdirs_with_ports) # Run updates Port.add_or_update(ports_to_be_updated_json) # Write the commit hash into database LastPortIndexUpdate.update_or_create_first_object( data['info']['commit'])
def test_add_new_maintainer(self): # add another maintainer to the same port Port.add_or_update([{ "portdir": "categoryA/port-A1", "name": "port-A1", "version": "1.2", "maintainers": [{ "email": { "domain": "email.com", "name": "user" }, "github": "user" }, { "github": "user2" }] }]) self.assertEquals( Port.objects.get(name="port-A1").maintainers.all().count(), 2) self.assertEquals(Maintainer.objects.all().count(), 7) # Now remove the maintainer, this should not delete the object from Maintainer model, # and should only delete the relation Port.add_or_update([{ "portdir": "categoryA/port-A1", "name": "port-A1", "version": "1.2", "maintainers": [{ "email": { "domain": "email.com", "name": "user" }, "github": "user" }] }]) self.assertEquals( Port.objects.get(name="port-A1").maintainers.all().count(), 1) self.assertEquals(Maintainer.objects.all().count(), 7)
def test_time_travel(self): time_now = datetime.datetime.now(tz=datetime.timezone.utc) # Go back in time 35 days time_35_days_ago = time_now - datetime.timedelta(days=35) submission = QUICK_SUBMISSION_JSON # Make a submission dated 35 days ago submission_id = Submission.populate(submission, time_35_days_ago) PortInstallation.populate(submission['active_ports'], submission_id) # Call for stats between 30-60 days response1 = self.client.get(reverse('port_stats', kwargs={'name': 'port-B1'}), data={ 'days': 30, 'days_ago': 30 }) # Call for stats between 30-37 days response2 = self.client.get(reverse('port_stats', kwargs={'name': 'port-B1'}), data={ 'days': 7, 'days_ago': 30 }) # Call for stats of some other port between 30-60 days response3 = self.client.get(reverse('port_stats', kwargs={'name': 'port-A4'}), data={ 'days': 30, 'days_ago': 30 }) Port.add_or_update([ { "name": "port-A4", "version": "1.2.3", "portdir": "categoryA/port-A4" } ]) self.assertEquals(response1.context['count']['all'], 1) self.assertEquals(response1.context['count']['requested'], 0) self.assertEquals(response2.context['count']['all'], 1) self.assertEquals(response2.context['count']['requested'], 0) self.assertEquals(response3.context['count']['all'], 0)
def test_full_deleted_run(self): Port.mark_deleted_full_run([ { 'name': 'port-A1' }, { 'name': 'port-A2' }, { 'name': 'port-A1-subport' }, { 'name': 'PORT-C1' }, { 'name': 'PORT-B1' }, { 'name': 'pOrT-A5' }, ]) self.assertEquals(Port.objects.filter(active=True).count(), 6)
def test_added_back(self): Port.mark_deleted({'categoryA/port-A1': {'port-A1'}}) Port.add_or_update([{ "variants": ["universal"], "portdir": "categoryA\/port-A1", "depends_fetch": ["bin:port-A4:port-A4"], "description": "This is port A1 of categoryA", "homepage": "http:\/\/portA1.website\/", "epoch": "0", "platforms": "darwin", "name": "port-A1-subport", "depends_lib": ["lib:pq:port-A2", "port:port-A3-diff"], "long_description": "Just a test port written to test something.", "license": "MIT", "maintainers": [{ "email": { "domain": "email.com", "name": "user" }, "github": "user" }], "categories": ["categoryA"], "version": "1.0.0", "revision": "0" }]) port_status = Port.objects.get(name='port-A1-subport').active self.assertEquals(port_status, True)
def test_removed(self): Port.add_or_update([{ "portdir": "categoryA\/port-A1", "platforms": "darwin", "name": "port-A1", "categories": ["categoryA"], "version": "1.0.0", "revision": "0" }]) self.assertEquals( Port.objects.get(name='port-A1').variants.all().count(), 0) self.assertEquals(Variant.objects.all().count(), 7) Port.add_or_update([{ "portdir": "categoryA\/port-A1", "platforms": "darwin", "name": "port-A1", "categories": ["categoryA"], "version": "1.0.0", "revision": "0", "vinfo": [{ "variant": "universal" }, { "variant": "gcc" }] }]) self.assertEquals( Port.objects.get(name='port-A1').variants.all().count(), 2) self.assertEquals(Variant.objects.all().count(), 9)
def parser(request): data1=json.loads(open('port/testing2.json').read()) count=0 objs=[] names=[] try: id=Port.objects.latest("portid").portid id = id + 1 except Port.DoesNotExist: id=1 for b in data1: port_name=b['name'] if 'description' in b: desc=b['description'] else: desc='' if 'variants' in b: variant=b['variants'] else: variant='' if 'portdir' in b: port_dir=b['portdir'] else: port_dir='' if 'platforms' in b: platform=b['platforms'] else: platform='' if 'version' in b: version=b['version'] else: version='' if 'homepage' in b: homepage=b['homepage'] else: homepage='' if 'license' in b: license=b['license'] else: license='' if 'long_description' in b: long_description=b['long_description'] else: long_description='' # c = Port.objects.create(portid=id, portname=port_name, description=desc, variant=variant, portdir=port_dir,homepage=homepage,platform=platform,cur_version=version,license=license,long_desc=long_description) # This is for chekcing repetition..works but slow # if not port_name in names: # objs.append(Port(portid=id, portname=port_name, description=desc, variant=variant, portdir=port_dir,homepage=homepage,platform=platform,cur_version=version,license=license,long_desc=long_description)) # id +=1 # names.append(port_name) objs.append(Port(portid=id, portname=port_name, description=desc, variant=variant, portdir=port_dir,homepage=homepage,platform=platform,cur_version=version,license=license,long_desc=long_description)) id +=1 count +=1 #print("completed the INSERT") #print(c.fetchall()) Port.objects.bulk_create(objs) ''' b = jsonparser(k) port_name = b['name'] desc = b['description'] variant = b['variants'] port_dir = b['portdir'] c = Port.objects.create(portname=port_name, description=desc, variant=variant, portdir=port_dir) ''' #from django.db import connection #to print the query in console #data= Port.objects.filter(portname=port_name) #print connection.queries[-1] #return render(request,'port/data.html',{"port1":data.last()}) return render(request,'port/data.html',{"port1":count})
def setup_test_data(): with open(TEST_PORTINDEX_JSON, 'r') as file: data = json.load(file) Port.add_or_update(data.get('ports', []))
def test_moved(self): Port.add_or_update([{ "variants": ["universal"], "portdir": "categoryA/port-A1", "depends_fetch": ["bin:port-A4:port-A4"], "description": "This is port A1 of categoryA", "homepage": "http:\/\/portA1.website\/", "epoch": "0", "platforms": "darwin", "name": "port-A2-subport", "depends_lib": ["lib:pq:port-A2", "port:port-A3-diff"], "long_description": "Just a test port written to test something.", "license": "MIT", "maintainers": [{ "email": { "domain": "email.com", "name": "user" }, "github": "user" }], "categories": ["categoryA"], "version": "1.0.0", "revision": "0" }, { "variants": ["universal"], "portdir": "categoryA/port-A1", "depends_fetch": ["bin:port-A4:port-A4"], "description": "This is port A1 of categoryA", "homepage": "http:\/\/portA1.website\/", "epoch": "0", "platforms": "darwin", "name": "port-A3-subport", "depends_lib": ["lib:pq:port-A2", "port:port-A3-diff"], "long_description": "Just a test port written to test something.", "license": "MIT", "maintainers": [{ "email": { "domain": "email.com", "name": "user" }, "github": "user" }], "categories": ["categoryA"], "version": "1.0.0", "revision": "0" }]) port_status_subport1 = Port.objects.get(name='port-A1-subport').active port_status_subport2 = Port.objects.get(name='port-A2-subport').active port_status_subport3 = Port.objects.get(name='port-A3-subport').active self.assertEquals(port_status_subport1, True) self.assertEquals(port_status_subport2, True) self.assertEquals(port_status_subport3, True) # Entire categoryA/port-A1 portdir is removed, but the subports move to other directory # All ports under category/port-A1 would be deleted. Port.mark_deleted({'categoryA/port-A1': {}}) port_status_subport1 = Port.objects.get(name='port-A1-subport').active port_status_subport2 = Port.objects.get(name='port-A2-subport').active port_status_subport3 = Port.objects.get(name='port-A3-subport').active self.assertEquals(port_status_subport1, False) self.assertEquals(port_status_subport2, False) self.assertEquals(port_status_subport3, False) # The moved ports would be found at the new location and will be appended to the list of JSON objects Port.add_or_update([{ "variants": ["universal"], "portdir": "categoryTemp/newPorts", "depends_fetch": ["bin:port-A4:port-A4"], "description": "This is port A1 of categoryA", "homepage": "http:\/\/portA1.website\/", "epoch": "0", "platforms": "darwin", "name": "port-A2-subport", "depends_lib": ["lib:pq:port-A2", "port:port-A3-diff"], "long_description": "Just a test port written to test something.", "license": "MIT", "maintainers": [{ "email": { "domain": "email.com", "name": "user" }, "github": "user" }], "categories": ["categoryA"], "version": "1.0.0", "revision": "0" }, { "variants": ["universal"], "portdir": "categoryTemp/newPorts", "depends_fetch": ["bin:port-A4:port-A4"], "description": "This is port A1 of categoryA", "homepage": "http:\/\/portA1.website\/", "epoch": "0", "platforms": "darwin", "name": "port-A3-subport", "depends_lib": ["lib:pq:port-A2", "port:port-A3-diff"], "long_description": "Just a test port written to test something.", "license": "MIT", "maintainers": [{ "email": { "domain": "email.com", "name": "user" }, "github": "user" }], "categories": ["categoryA"], "version": "1.0.0", "revision": "0" }]) port_status_subport2 = Port.objects.get(name='port-A2-subport').active port_status_subport3 = Port.objects.get(name='port-A3-subport').active self.assertEquals(port_status_subport2, True) self.assertEquals(port_status_subport3, True)