<commit_before># -*- coding: utf-8 -*-
# Copyright (c) 2019, Frappe Technologies and Contributors
# See license.txt
from __future__ import unicode_literals

import frappe
import unittest
import time
from frappe import _
from frappe.frappeclient import FrappeClient
from frappe.events_streaming.doctype.event_producer.event_producer import pull_from_node, get_current_node

def create_event_producer():
	event_producer = frappe.new_doc('Event Producer')
	event_producer.producer_url = 'http://test_site_producer:8000'
	event_producer.append('event_configuration', {
		'ref_doctype': 'ToDo',
		'use_same_name': 1
	})
	event_producer.user = 'Administrator'
	event_producer.insert()

class TestEventProducer(unittest.TestCase):
	def setUp(self):
		if not frappe.db.exists('Event Producer', 'http://test_site_producer:8000'):
			create_event_producer()
		frappe.db.sql('delete from tabToDo')
		frappe.db.sql('delete from tabNote')

	def test_insert(self):
		self.subscribe_to_doctypes(['ToDo'])
		producer = get_remote_site()
		producer_doc = self.insert_into_producer(producer, 'test creation 1 sync')
		self.pull_producer_data()
		self.assertTrue(frappe.db.exists('ToDo', producer_doc.name))
		
	def test_update(self):
		self.subscribe_to_doctypes(['ToDo'])
		producer = get_remote_site()
		producer_doc = self.insert_into_producer(producer, 'test update 1')
		producer_doc['description'] = 'test update 2'
		producer_doc = producer.update(producer_doc)
		self.pull_producer_data()
		local_doc = frappe.get_doc(producer_doc.doctype, producer_doc.name)
		self.assertEqual(local_doc.description, producer_doc.description)

	def test_delete(self):
		self.subscribe_to_doctypes(['ToDo'])
		producer = get_remote_site()
		producer_doc = self.insert_into_producer(producer, 'test delete sync')
		self.pull_producer_data()
		self.assertTrue(frappe.db.exists('ToDo', producer_doc.name))
		producer.delete('ToDo', producer_doc.name)
		self.pull_producer_data()
		self.assertFalse(frappe.db.exists('ToDo', producer_doc.name))

	def test_multiple_doctypes_sync(self):
		self.subscribe_to_doctypes(['ToDo', 'Note'])
		producer = get_remote_site()

		#insert todo and note in producer
		producer_todo = self.insert_into_producer(producer, 'test multiple doc sync')
		producer_note1 = frappe.get_doc(dict(doctype='Note', title='test multiple doc sync 1'))
		delete_on_remote_if_exists(producer, 'Note', {'title': producer_note1.title})
		frappe.db.delete('Note', {'title': producer_note1.title})
		producer_note1 = producer.insert(producer_note1)
		producer_note2 = frappe.get_doc(dict(doctype='Note', title='test multiple doc sync 2'))
		delete_on_remote_if_exists(producer, 'Note', {'title': producer_note2.title})
		frappe.db.delete('Note', {'title': producer_note2.title})
		producer_note2 = producer.insert(producer_note2)

		#update in producer
		producer_todo['description'] = 'test multiple doc update sync'
		producer_todo = producer.update(producer_todo)
		producer_note1['content'] = 'testing update sync'
		producer_note1 = producer.update(producer_note1)

		producer.delete('Note', producer_note2.name)

		self.pull_producer_data()

		#check inserted
		self.assertTrue(frappe.db.exists('ToDo', producer_todo.name))

		#check update
		local_todo = frappe.get_doc('ToDo', producer_todo.name)
		self.assertEqual(local_todo.description, producer_todo.description)
		local_note1 = frappe.get_doc('Note', producer_note1.name)
		self.assertEqual(local_note1.content, producer_note1.content)

		#check delete
		self.assertFalse(frappe.db.exists('Note', producer_note2.name))

	def test_child_table_sync_with_dependencies(self):
		self.subscribe_to_doctypes(['Note'])
		producer = get_remote_site()
		producer_user = frappe.get_doc(dict(doctype='User', email='<EMAIL>', first_name='<NAME>'))
		producer_user.enabled = 1
		delete_on_remote_if_exists(producer, 'User', {'email': producer_user.email})
		frappe.db.delete('User', {'email':producer_user.email})
		producer_user = producer.insert(producer_user)
		producer_note = frappe.get_doc(dict(doctype='Note', title='test child table dependency sync'))
		producer_note.append('seen_by', {
			'user': producer_user.name
		})
		producer_user.append('roles', {
			'role': 'System Manager'
		})
		delete_on_remote_if_exists(producer, 'Note', {'title': producer_note.title})
		frappe.db.delete('Note', {'title': producer_note.title})
		producer_note = producer.insert(producer_note)
		self.pull_producer_data()
		self.assertTrue(frappe.db.exists('User', producer_user.name))
		if self.assertTrue(frappe.db.exists('Note', producer_note.name)):
			local_note = frappe.get_doc('Note', producer_note.name)
			self.assertEqual(len(local_note.seen_by), 1)

	def test_dynamic_link_dependencies_synced(self):
		self.subscribe_to_doctypes(['ToDo'])
		producer = get_remote_site()
		producer_link_doc = frappe.get_doc(dict(doctype='Note', title='Test Dynamic Link 1'))

		delete_on_remote_if_exists(producer, 'Note', {'title': producer_link_doc.title})
		frappe.db.delete('Note', {'title': producer_link_doc.title})
		producer_link_doc = producer.insert(producer_link_doc)
		producer_doc = frappe.get_doc(dict(doctype='ToDo', description='Test Dynamic Link 2', assigned_by='Administrator', reference_type='Note', reference_name=producer_link_doc.name))
		producer_doc = producer.insert(producer_doc)

		self.pull_producer_data()

		#check dynamic link dependency created
		self.assertTrue(frappe.db.exists('Note', producer_link_doc.name))
		self.assertEqual(producer_link_doc.name, frappe.db.get_value('ToDo', producer_doc.name, 'reference_name'))<commit_msg>fix: test child table dependency sync<commit_after># -*- coding: utf-8 -*-
# Copyright (c) 2019, <NAME> and Contributors
# See license.txt
from __future__ import unicode_literals

import frappe
import unittest
import time
from frappe import _
from frappe.frappeclient import FrappeClient
from frappe.events_streaming.doctype.event_producer.event_producer import pull_from_node, get_current_node

def create_event_producer():
	event_producer = frappe.new_doc('Event Producer')
	event_producer.producer_url = 'http://test_site_producer:8000'
	event_producer.append('event_configuration', {
		'ref_doctype': 'ToDo',
		'use_same_name': 1
	})
	event_producer.user = 'Administrator'
	event_producer.insert()

class TestEventProducer(unittest.TestCase):
	def setUp(self):
		if not frappe.db.exists('Event Producer', 'http://test_site_producer:8000'):
			create_event_producer()
		frappe.db.sql('delete from tabToDo')
		frappe.db.sql('delete from tabNote')

	def test_insert(self):
		self.subscribe_to_doctypes(['ToDo'])
		producer = get_remote_site()
		producer_doc = self.insert_into_producer(producer, 'test creation 1 sync')
		self.pull_producer_data()
		self.assertTrue(frappe.db.exists('ToDo', producer_doc.name))
		
	def test_update(self):
		self.subscribe_to_doctypes(['ToDo'])
		producer = get_remote_site()
		producer_doc = self.insert_into_producer(producer, 'test update 1')
		producer_doc['description'] = 'test update 2'
		producer_doc = producer.update(producer_doc)
		self.pull_producer_data()
		local_doc = frappe.get_doc(producer_doc.doctype, producer_doc.name)
		self.assertEqual(local_doc.description, producer_doc.description)

	def test_delete(self):
		self.subscribe_to_doctypes(['ToDo'])
		producer = get_remote_site()
		producer_doc = self.insert_into_producer(producer, 'test delete sync')
		self.pull_producer_data()
		self.assertTrue(frappe.db.exists('ToDo', producer_doc.name))
		producer.delete('ToDo', producer_doc.name)
		self.pull_producer_data()
		self.assertFalse(frappe.db.exists('ToDo', producer_doc.name))

	def test_multiple_doctypes_sync(self):
		self.subscribe_to_doctypes(['ToDo', 'Note'])
		producer = get_remote_site()

		#insert todo and note in producer
		producer_todo = self.insert_into_producer(producer, 'test multiple doc sync')
		producer_note1 = frappe.get_doc(dict(doctype='Note', title='test multiple doc sync 1'))
		delete_on_remote_if_exists(producer, 'Note', {'title': producer_note1.title})
		frappe.db.delete('Note', {'title': producer_note1.title})
		producer_note1 = producer.insert(producer_note1)
		producer_note2 = frappe.get_doc(dict(doctype='Note', title='test multiple doc sync 2'))
		delete_on_remote_if_exists(producer, 'Note', {'title': producer_note2.title})
		frappe.db.delete('Note', {'title': producer_note2.title})
		producer_note2 = producer.insert(producer_note2)

		#update in producer
		producer_todo['description'] = 'test multiple doc update sync'
		producer_todo = producer.update(producer_todo)
		producer_note1['content'] = 'testing update sync'
		producer_note1 = producer.update(producer_note1)

		producer.delete('Note', producer_note2.name)

		self.pull_producer_data()

		#check inserted
		self.assertTrue(frappe.db.exists('ToDo', producer_todo.name))

		#check update
		local_todo = frappe.get_doc('ToDo', producer_todo.name)
		self.assertEqual(local_todo.description, producer_todo.description)
		local_note1 = frappe.get_doc('Note', producer_note1.name)
		self.assertEqual(local_note1.content, producer_note1.content)

		#check delete
		self.assertFalse(frappe.db.exists('Note', producer_note2.name))

	def test_child_table_sync_with_dependencies(self):
		self.subscribe_to_doctypes(['Note'])
		producer = get_remote_site()
		producer_user = frappe.get_doc(dict(doctype='User', email='<EMAIL>', first_name='<NAME>'))
		delete_on_remote_if_exists(producer, 'User', {'email': producer_user.email})
		frappe.db.delete('User', {'email':producer_user.email})
		producer_user.enabled = 1
		producer_user.append('roles', {
			'role': 'System Manager'
		})
		producer_user = producer.insert(producer_user)
		producer_note = frappe.get_doc(dict(doctype='Note', title='test child table dependency sync'))
		producer_note.append('seen_by', {
			'user': producer_user.name
		})
		delete_on_remote_if_exists(producer, 'Note', {'title': producer_note.title})
		frappe.db.delete('Note', {'title': producer_note.title})
		producer_note = producer.insert(producer_note)
		self.pull_producer_data()
		self.assertTrue(frappe.db.exists('User', producer_user.name))
		if self.assertTrue(frappe.db.exists('Note', producer_note.name)):
			local_note = frappe.get_doc('Note', producer_note.name)
			self.assertEqual(len(local_note.seen_by), 1)

	def test_dynamic_link_dependencies_synced(self):
		self.subscribe_to_doctypes(['ToDo'])
		producer = get_remote_site()
		producer_link_doc = frappe.get_doc(dict(doctype='Note', title='Test Dynamic Link 1'))

		delete_on_remote_if_exists(producer, 'Note', {'title': producer_link_doc.title})
		frappe.db.delete('Note', {'title': producer_link_doc.title})
		producer_link_doc = producer.insert(producer_link_doc)
		producer_doc = frappe.get_doc(dict(doctype='ToDo', description='Test Dynamic Link 2', assigned_by='Administrator', reference_type='Note', reference_name=producer_link_doc.name))
		producer_doc = producer.insert(producer_doc)

		self.pull_producer_data()

		#check dynamic link dependency created
		self.assertTrue(frappe.db.exists('Note', producer_link_doc.name))
		self.assertEqual(producer_link_doc.name, frappe.db.get_value('ToDo', producer_doc.name, 'reference_name'))
{'fraction_non_alphanumeric': 0.12861362212476507, 'fraction_numerical': 0.007518124049046809, 'mean_word_length': 6.8085255066387145, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, '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': '5326403', 'n_tokens_mistral': 4099, 'n_tokens_neox': 3897, 'n_words': 613}