"""
Tests for formatting all kinds of fringy resource records
"""

import os
os.environ["GAVO_OOTTEST"] = "dontcare"

from gavo import api
from gavo.helpers import testhelpers
from gavo import utils


RD = api.getRD("registryrss/q")

makerss, _ =  utils.loadPythonModule(
	os.path.join(RD.resdir, "bin/makerss"))


def getResRow(**kwargs):
	res = {
		"ivoid": "ivo://org.gavo.dc/rr/q/create",
		"res_title": "GAVO RegTAP Service",
		"reference_url": "http://dc.zah.uni-heidelberg.de/browse/rr/q",
		"description": "Tables containing the information in the IVOA Registry.",
		"updated": api.parseISODT("2023-07-12T07:03:46"),
		"res_subject": ["virtual-observatories"],
		"creator_name": "GAVO Data Center"}
	
	res.update(kwargs)
	return res


class TootFormatTest(testhelpers.VerboseTest):
	def assertContains(self, needle, haystack):
		assert needle in haystack, f"'{needle}' not in '{haystack}'"

	def testEmpty(self):
		self.assertRaises(
			KeyError,
			makerss.getMicroblogText,
			{})
	
	def testEmptyRefurlIgnored(self):
		self.assertRaises(
			makerss.Ignore,
			makerss.getMicroblogText,
			{"reference_url": ""})

	def testBasic(self):
		self.assertEqual(
			makerss.getMicroblogText(getResRow()),
			"New in the #VirtualObservatory: “GAVO RegTAP Service” by GAVO Data Center\n"
			"http://dc.zah.uni-heidelberg.de/browse/rr/q\n"
			"#VirtualObservatories")

	def testEtal(self):
		self.assertContains(" Foo, B. et al.\n",
			makerss.getMicroblogText(getResRow(creator_name="Foo, B.; Quux, C.")))

	def testCroppedSubjects(self):
		self.assertTrue(
			makerss.getMicroblogText(getResRow(res_subject="abcdef")).endswith(
			"\n#A #B #C #D"))

	def testNoAuthors(self):
		self.assertContains("“GAVO RegTAP Service”\n",
			makerss.getMicroblogText(getResRow(creator_name=None)))


if __name__=="__main__":
	testhelpers.main(TootFormatTest)
