<commit_before>#! /usr/bin/env python3

import config
config.import_libs()

import unittest
import socket
import json
import uuid

from rest import POST, PUT, DELETE, GET

class TestPlace(unittest.TestCase):
	"""
	Create a session and a testing place 'My Home'.
	"""
	def setUp(self):
		print("# setup")

		req = POST(config.ui_host, config.ui_port, "/auth")
		req.body(config.PERMIT_LOGIN)
		response = req()

		self.assertEqual(200, response.status)
		self.session = str(response.read(), "utf-8")
		print("# got session: " + self.session)

		place = json.dumps({"name": "My Home"})
		req = POST(config.ui_host, config.ui_port, "/place")
		req.authorize(self.session)
		req.body(place)
		response = req()

		self.assertEqual(200, response.status)
		self.place = json.loads(str(response.read(), "utf-8"))
		print("# got place: " + self.place["id"])

	"""
	Delete the place 'My Home' and destroy the session.
	"""
	def tearDown(self):
		print("# teardown")

		req = DELETE(config.ui_host, config.ui_port,
				"/place/" + self.place["id"])
		req.authorize(self.session)
		response = req()

		self.assertEqual(200, response.status)

		req = DELETE(config.ui_host, config.ui_port, "/auth")
		req.authorize(self.session)
		response = req()

		self.assertEqual(200, response.status)

	"""
	Request a non-existing place.
	"""
	def test1_get_non_existing(self):
		req = GET(config.ui_host, config.ui_port, "/place/any-id")
		req.authorize(self.session)
		response = req()

		data = str(response.read(), "utf-8")

		self.assertEqual(400, response.status)
		self.assertEqual(0, len(data))

	def test2_get_non_existing_uuid(self):
		id = str(uuid.uuid4())
		req = GET(config.ui_host, config.ui_port, "/place/" + id)
		req.authorize(self.session)
		response = req()

		data = str(response.read(), "utf-8")

		self.assertEqual(404, response.status)
		self.assertEqual(0, len(data))

	"""
	Request a non-existing place.
	"""
	def test3_get_existing(self):
		req = GET(config.ui_host, config.ui_port,
				"/place/" + self.place["id"])
		req.authorize(self.session)
		response = req()

		self.assertEqual(200, response.status)
		data = json.loads(str(response.read(), "utf-8"))
		self.assertEqual(self.place["name"], data["name"])
		self.assertIn(self.place["id"], data["id"])

	"""
	Update a non-existing place. Should return 404 and it must
	not create such place.
	"""
	def test4_update_non_existing_place(self):
		id = str(uuid.uuid4())

		req = PUT(config.ui_host, config.ui_port, "/place/" + id)

		data = {}
		data["name"] = "Non-existing"

		req.body(json.dumps(data))
		req.authorize(self.session)
		response = req()

		self.assertEqual(404, response.status)
		self.assertEqual(0, len(response.read()))

		req = GET(config.ui_host, config.ui_port, "/place/" + id)
		req.authorize(self.session)
		response = req()

		self.assertEqual(404, response.status)
		self.assertEqual(0, len(response.read()))

	"""
	Test a regular place update.
	"""
	def test5_update_place(self):
		req = GET(config.ui_host, config.ui_port,
				"/place/" + self.place["id"])
		req.authorize(self.session)
		response = req()

		data = json.loads(str(response.read(), "utf-8"))

		self.assertEqual(200, response.status)
		self.assertEqual(self.place["name"], data["name"])
		self.assertIn(self.place["id"], data["id"])

		self.assertNotEqual("XXXYYY", data["name"])
		data["name"] = "XXXYYY"

		req = PUT(config.ui_host, config.ui_port,
				"/place/" + self.place["id"])
		req.body(json.dumps(data))
		req.authorize(self.session)
		response = req()

		result = json.loads(str(response.read(), "utf-8"))

		self.assertEqual(self.place["id"], result["id"])
		self.assertEqual("XXXYYY", result["name"])

	"""
	Test update is performed on the place defined by ID in URI
	and not in the JSON body.
	"""
	def test6_update_place_wrong_id(self):
		place = json.dumps({"name": "My Second Home"})
		req = POST(config.ui_host, config.ui_port, "/place")
		req.authorize(self.session)
		req.body(place)
		response = req()

		secondHome = json.loads(str(response.read(), "utf-8"))

		self.assertEqual(200, response.status)
		self.assertTrue(len(secondHome["id"]) > 0)
		self.assertIsNotNone(uuid.UUID(secondHome["id"]))
		self.assertNotEqual(self.place["id"], secondHome["id"])

		req = PUT(config.ui_host, config.ui_port,
				"/place/" + self.place["id"])
		req.body(json.dumps(secondHome))
		req.authorize(self.session)
		response = req()

		result = json.loads(str(response.read(), "utf-8"))

		self.assertEqual(self.place["id"], result["id"])
		self.assertNotEqual(self.place["id"], secondHome["id"])
		self.assertEqual("My Second Home", result["name"])

if __name__ == '__main__':
	import sys
	import taprunner
	unittest.main(testRunner=taprunner.TAPTestRunner(stream = sys.stdout))
<commit_msg>t: ui: fix typo in comment<commit_after>#! /usr/bin/env python3

import config
config.import_libs()

import unittest
import socket
import json
import uuid

from rest import POST, PUT, DELETE, GET

class TestPlace(unittest.TestCase):
	"""
	Create a session and a testing place 'My Home'.
	"""
	def setUp(self):
		print("# setup")

		req = POST(config.ui_host, config.ui_port, "/auth")
		req.body(config.PERMIT_LOGIN)
		response = req()

		self.assertEqual(200, response.status)
		self.session = str(response.read(), "utf-8")
		print("# got session: " + self.session)

		place = json.dumps({"name": "My Home"})
		req = POST(config.ui_host, config.ui_port, "/place")
		req.authorize(self.session)
		req.body(place)
		response = req()

		self.assertEqual(200, response.status)
		self.place = json.loads(str(response.read(), "utf-8"))
		print("# got place: " + self.place["id"])

	"""
	Delete the place 'My Home' and destroy the session.
	"""
	def tearDown(self):
		print("# teardown")

		req = DELETE(config.ui_host, config.ui_port,
				"/place/" + self.place["id"])
		req.authorize(self.session)
		response = req()

		self.assertEqual(200, response.status)

		req = DELETE(config.ui_host, config.ui_port, "/auth")
		req.authorize(self.session)
		response = req()

		self.assertEqual(200, response.status)

	"""
	Request a non-existing place.
	"""
	def test1_get_non_existing(self):
		req = GET(config.ui_host, config.ui_port, "/place/any-id")
		req.authorize(self.session)
		response = req()

		data = str(response.read(), "utf-8")

		self.assertEqual(400, response.status)
		self.assertEqual(0, len(data))

	def test2_get_non_existing_uuid(self):
		id = str(uuid.uuid4())
		req = GET(config.ui_host, config.ui_port, "/place/" + id)
		req.authorize(self.session)
		response = req()

		data = str(response.read(), "utf-8")

		self.assertEqual(404, response.status)
		self.assertEqual(0, len(data))

	"""
	Request an existing place.
	"""
	def test3_get_existing(self):
		req = GET(config.ui_host, config.ui_port,
				"/place/" + self.place["id"])
		req.authorize(self.session)
		response = req()

		self.assertEqual(200, response.status)
		data = json.loads(str(response.read(), "utf-8"))
		self.assertEqual(self.place["name"], data["name"])
		self.assertIn(self.place["id"], data["id"])

	"""
	Update a non-existing place. Should return 404 and it must
	not create such place.
	"""
	def test4_update_non_existing_place(self):
		id = str(uuid.uuid4())

		req = PUT(config.ui_host, config.ui_port, "/place/" + id)

		data = {}
		data["name"] = "Non-existing"

		req.body(json.dumps(data))
		req.authorize(self.session)
		response = req()

		self.assertEqual(404, response.status)
		self.assertEqual(0, len(response.read()))

		req = GET(config.ui_host, config.ui_port, "/place/" + id)
		req.authorize(self.session)
		response = req()

		self.assertEqual(404, response.status)
		self.assertEqual(0, len(response.read()))

	"""
	Test a regular place update.
	"""
	def test5_update_place(self):
		req = GET(config.ui_host, config.ui_port,
				"/place/" + self.place["id"])
		req.authorize(self.session)
		response = req()

		data = json.loads(str(response.read(), "utf-8"))

		self.assertEqual(200, response.status)
		self.assertEqual(self.place["name"], data["name"])
		self.assertIn(self.place["id"], data["id"])

		self.assertNotEqual("XXXYYY", data["name"])
		data["name"] = "XXXYYY"

		req = PUT(config.ui_host, config.ui_port,
				"/place/" + self.place["id"])
		req.body(json.dumps(data))
		req.authorize(self.session)
		response = req()

		result = json.loads(str(response.read(), "utf-8"))

		self.assertEqual(self.place["id"], result["id"])
		self.assertEqual("XXXYYY", result["name"])

	"""
	Test update is performed on the place defined by ID in URI
	and not in the JSON body.
	"""
	def test6_update_place_wrong_id(self):
		place = json.dumps({"name": "My Second Home"})
		req = POST(config.ui_host, config.ui_port, "/place")
		req.authorize(self.session)
		req.body(place)
		response = req()

		secondHome = json.loads(str(response.read(), "utf-8"))

		self.assertEqual(200, response.status)
		self.assertTrue(len(secondHome["id"]) > 0)
		self.assertIsNotNone(uuid.UUID(secondHome["id"]))
		self.assertNotEqual(self.place["id"], secondHome["id"])

		req = PUT(config.ui_host, config.ui_port,
				"/place/" + self.place["id"])
		req.body(json.dumps(secondHome))
		req.authorize(self.session)
		response = req()

		result = json.loads(str(response.read(), "utf-8"))

		self.assertEqual(self.place["id"], result["id"])
		self.assertNotEqual(self.place["id"], secondHome["id"])
		self.assertEqual("My Second Home", result["name"])

if __name__ == '__main__':
	import sys
	import taprunner
	unittest.main(testRunner=taprunner.TAPTestRunner(stream = sys.stdout))

{'fraction_non_alphanumeric': 0.17504205214465937, 'fraction_numerical': 0.0124053826745164, 'mean_word_length': 4.445334859759588, 'pattern_counts': {'":': 4, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3614091', 'n_tokens_mistral': 3703, 'n_tokens_neox': 3346, 'n_words': 663}