def test_combining(self): # Test2: combining, resources specified by global config # (maybe we should use smaller CSS+JS files? Test takes 2+ seconds...) s = os.sep want = {'css':[s.join(['rsrc', 'css','combined.css'])], 'js':[s.join(['rsrc', 'js','combined.js'])], 'img': [], 'xml':[s.join(['rsrc', 'resources.xml'])] } testcss = ["css/ferenda.css", "res/css/fake1.css", "res/css/fake2.css"] testjs = ["js/ferenda.js", "res/js/fake1.js", "res/js/fake2.js"] resources = Resources([staticmockclass(),staticmockclass2()],self.tempdir+os.sep+'rsrc', combineresources=True, cssfiles=testcss, jsfiles=testjs, sitename="Blahonga", sitedescription="A non-default value") rl = resources.resourceloader testcssfiles = [] testjsfiles = [] for cssfile in testcss: try: testcssfiles.append(rl.filename(cssfile)) except errors.ResourceNotFound: util.writefile(cssfile, "/* this is a faked css file: %s */" % cssfile*1000) testcssfiles.append(cssfile) for jsfile in testjs: try: testjsfiles.append(rl.filename(jsfile)) except errors.ResourceNotFound: util.writefile(jsfile, "/* this is a faked js file: %s */" % jsfile*1000) testjsfiles.append(jsfile) got = resources.make(api=False) self.assertEqual(want,got) tree = ET.parse(self.tempdir+'/'+got['xml'][0]) stylesheets=tree.find("stylesheets").getchildren() self.assertEqual(len(stylesheets),1) self.assertEqual(stylesheets[0].attrib['href'],'rsrc/css/combined.css') javascripts=tree.find("javascripts").getchildren() self.assertEqual(len(javascripts),1) self.assertEqual(javascripts[0].attrib['src'],'rsrc/js/combined.js') self.assertEqual(tree.find("sitename").text,"Blahonga") self.assertEqual(tree.find("sitedescription").text,"A non-default value") self.assertTrue(os.path.exists(self.tempdir+'/rsrc/css/combined.css')) self.assertTrue(os.path.exists(self.tempdir+'/rsrc/js/combined.js')) # check that the combining/minifying indeed saved us some space self.assertLess(os.path.getsize(self.tempdir+'/rsrc/css/combined.css'), sum([os.path.getsize(x) for x in testcssfiles])) self.assertLess(os.path.getsize(self.tempdir+'/rsrc/js/combined.js'), sum([os.path.getsize(x) for x in testjsfiles]))
def test_frontpage_staticsite(self): test = staticmockclass(datadir=self.tempdir) test2 = staticmockclass2(datadir=self.tempdir) outfile = self.tempdir + '/index.html' Resources([test, test2], self.tempdir + '/rsrc').make() manager.frontpage([test, test2], path=outfile, staticsite=True, removeinvalidlinks=False) # print("\n============== OUTFILE =====================") # print(util.readfile(outfile)) # print("==============================================") t = ET.parse(outfile) header = t.find(".//div[@class='navbar-header']/a") self.assertEqual(header.get("href"), 'index.html') headernavlinks = t.findall(".//ul[@class='nav navbar-nav']/li/a") self.assertEqual(headernavlinks[0].get("href"), 'staticmock/toc/index.html') self.assertEqual(headernavlinks[1].get("href"), 'staticmock2/toc/index.html') css = t.findall("head/link[@rel='stylesheet']") self.assertRegex(css[-1].get('href'), '^rsrc/css')
def test_windows_paths(self): # test8: test win32 path generation on all OS:es, including one full URL test = staticmockclass() test.config.cssfiles.append('http://example.org/css/main.css') want = { 'css': ['rsrc\\css\\test.css', 'http://example.org/css/main.css'], 'js': ['rsrc\\js\\test.js'], 'img': ['rsrc\\img\\test.png'], 'xml': ['rsrc\\resources.xml'] } try: realsep = os.sep os.sep = "\\" from ferenda import resources resources.os.sep = "\\" got = Resources([test], self.tempdir + os.sep + 'rsrc', cssfiles=[], jsfiles=[], imgfiles=[]).make(api=False) self.assertEqual(want, got) finally: os.sep = realsep
def test_basic(self): # Test1: No combining, resources specified by docrepos only # (skip the default css/js files) s = os.sep want = {'css':[s.join(['rsrc', 'css','test.css'])], 'js':[s.join(['rsrc', 'js','test.js'])], 'img':[s.join(['rsrc', 'img','test.png'])], 'json': [s.join(['rsrc','api','context.json']), s.join(['rsrc','api','common.json']), s.join(['rsrc','api','terms.json'])], 'xml':[s.join(['rsrc', 'resources.xml'])] } got = Resources([staticmockclass(),staticmockclass2()], self.tempdir+os.sep+'rsrc', cssfiles=[], jsfiles=[], imgfiles=[]).make() self.assertEqual(want, got) tree = ET.parse(self.tempdir+os.sep+got['xml'][0]) stylesheets=tree.find("stylesheets").getchildren() self.assertEqual(len(stylesheets),1) self.assertEqual(stylesheets[0].attrib['href'],'rsrc/css/test.css') javascripts=tree.find("javascripts").getchildren() self.assertEqual(len(javascripts),1) self.assertEqual(javascripts[0].attrib['src'],'rsrc/js/test.js') # the javascript tag must have some text content to avoid it # being self-closed (which doesn't go well with HTML5) self.assertTrue(javascripts[0].text) self.assertEqual(tree.find("sitename").text,"MySite") self.assertEqual(tree.find("sitedescription").text,"Just another Ferenda site") self.assertEqual(tree.find("url").text,"http://localhost:8000/") self.assertTrue(os.path.exists(self.tempdir+'/rsrc/css/test.css')) self.assertTrue(os.path.exists(self.tempdir+'/rsrc/js/test.js')) tabs=tree.find("tabs") self.assertTrue(tabs is not None) search=tree.find("search") self.assertTrue(search is not None)
def test_files(self): # it'd be better to test _get_context, _get_common_graph and # _get_term_graph in isolation, but _create_api_files contains # common code to serialize the structures to files. Easier to # test all three. # # don't include all default ontologies to cut down on test # time, SKOS+FOAF ought to be enough. self.repo.ns = ({"foaf": "http://xmlns.com/foaf/0.1/", 'rdfs': 'http://www.w3.org/2000/01/rdf-schema#', 'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'owl': 'http://www.w3.org/2002/07/owl#', 'skos': 'http://www.w3.org/2004/02/skos/core#'}) with silence(): got = Resources([self.repo], self.datadir + "/data/rsrc", legacyapi=True).make(css=False, js=False, img=False, xml=False, api=True) s = os.sep want = {'json': [s.join(['rsrc','api','context.json']), s.join(['rsrc','api','common.json']), s.join(['rsrc','api','terms.json'])]} self.assertEqual(want, got) with open(self.datadir + "/data/rsrc/api/context.json") as fp: got = json.load(fp) with open("test/files/api/jsonld-context.json") as fp: want = json.load(fp) self.assertEqual(want, got) with open(self.datadir + "/data/rsrc/api/terms.json") as fp: got = json.load(fp) with open("test/files/api/var-terms.json") as fp: want = json.load(fp) self.assertEqual(want, got) got = json.load(open(self.datadir + "/data/rsrc/api/common.json")) want = json.load(open("test/files/api/var-common.json")) self.assertEqual(want,got)
def test_combining(self): # Test2: combining, resources specified by global config # (maybe we should use smaller CSS+JS files? Test takes 2+ seconds...) s = os.sep want = { 'css': [s.join(['rsrc', 'css', 'combined.css'])], 'js': [s.join(['rsrc', 'js', 'combined.js'])], 'img': [], 'xml': [s.join(['rsrc', 'resources.xml'])] } testcss = ["css/ferenda.css", "res/css/fake1.css", "res/css/fake2.css"] testjs = ["js/ferenda.js", "res/js/fake1.js", "res/js/fake2.js"] resources = Resources( [staticmockclass(), staticmockclass2()], self.tempdir + os.sep + 'rsrc', combineresources=True, cssfiles=testcss, jsfiles=testjs, sitename="Blahonga", sitedescription="A non-default value") rl = resources.resourceloader testcssfiles = [] testjsfiles = [] for cssfile in testcss: try: testcssfiles.append(rl.filename(cssfile)) except errors.ResourceNotFound: util.writefile( cssfile, "/* this is a faked css file: %s */" % cssfile * 1000) testcssfiles.append(cssfile) for jsfile in testjs: try: testjsfiles.append(rl.filename(jsfile)) except errors.ResourceNotFound: util.writefile( jsfile, "/* this is a faked js file: %s */" % jsfile * 1000) testjsfiles.append(jsfile) got = resources.make(api=False) self.assertEqual(want, got) tree = ET.parse(self.tempdir + '/' + got['xml'][0]) stylesheets = tree.find("stylesheets").getchildren() self.assertEqual(len(stylesheets), 1) self.assertEqual(stylesheets[0].attrib['href'], 'rsrc/css/combined.css') javascripts = tree.find("javascripts").getchildren() self.assertEqual(len(javascripts), 1) self.assertEqual(javascripts[0].attrib['src'], 'rsrc/js/combined.js') self.assertEqual(tree.find("sitename").text, "Blahonga") self.assertEqual( tree.find("sitedescription").text, "A non-default value") self.assertTrue(os.path.exists(self.tempdir + '/rsrc/css/combined.css')) self.assertTrue(os.path.exists(self.tempdir + '/rsrc/js/combined.js')) # check that the combining/minifying indeed saved us some space self.assertLess( os.path.getsize(self.tempdir + '/rsrc/css/combined.css'), sum([os.path.getsize(x) for x in testcssfiles])) self.assertLess(os.path.getsize(self.tempdir + '/rsrc/js/combined.js'), sum([os.path.getsize(x) for x in testjsfiles]))
def test_external_combine(self): # test6: include one external resource but with combine=True, which is unsupported test = staticmockclass() test.config.cssfiles.append('http://example.org/css/main.css') with self.assertRaises(errors.ConfigurationError): got = Resources([test],self.tempdir+os.sep+'rsrc', combineresources=True).make()