Explorar el Código

Extends exports fixtures

version-14
Luís Fernandes hace 10 años
padre
commit
5b8af5c64e
Se han modificado 3 ficheros con 305 adiciones y 4 borrados
  1. +42
    -0
      frappe/core/page/data_import_tool/exporter.py
  2. +257
    -0
      frappe/core/page/data_import_tool/test_exporter_fixtures.py
  3. +6
    -4
      frappe/utils/fixtures.py

+ 42
- 0
frappe/core/page/data_import_tool/exporter.py Ver fichero

@@ -5,14 +5,34 @@ from __future__ import unicode_literals


import frappe, json, os import frappe, json, os
import frappe.permissions import frappe.permissions
import re
from frappe.utils.csvutils import UnicodeWriter from frappe.utils.csvutils import UnicodeWriter
from frappe.utils import cstr, cint, flt from frappe.utils import cstr, cint, flt


from frappe.core.page.data_import_tool.data_import_tool import data_keys from frappe.core.page.data_import_tool.data_import_tool import data_keys


reflags = {
"I":re.I,
"L":re.L,
"M":re.M,
"U":re.U,
"S":re.S,
"X":re.X,
"D": re.DEBUG
}

@frappe.whitelist() @frappe.whitelist()
def get_template(doctype=None, parent_doctype=None, all_doctypes="No", with_data="No"): def get_template(doctype=None, parent_doctype=None, all_doctypes="No", with_data="No"):
all_doctypes = all_doctypes=="Yes" all_doctypes = all_doctypes=="Yes"
docs_to_export = {}
if doctype:
if isinstance(doctype, basestring):
doctype = [doctype];
if len(doctype) > 1:
docs_to_export = doctype[1]#dictionary for selective exports
#here, doctype is allways a list. Was converted to list in module fixtures.py
doctype = doctype[0]
if not parent_doctype: if not parent_doctype:
parent_doctype = doctype parent_doctype = doctype


@@ -148,6 +168,28 @@ def get_template(doctype=None, parent_doctype=None, all_doctypes="No", with_data
# get permitted data only # get permitted data only
data = frappe.get_list(doctype, fields=["*"], limit_page_length=None) data = frappe.get_list(doctype, fields=["*"], limit_page_length=None)
for doc in data: for doc in data:
op = docs_to_export.get("op")
names = docs_to_export.get("name")
if names and op:
if op == '=' and doc.name not in names:
continue
elif op == '!=' and doc.name in names:
continue
elif names:
try:
sflags = docs_to_export.get("flags", "I,U").upper()
flags = 0
for a in re.split('\W+',sflags):
flags = flags | reflags.get(a,0)
c = re.compile(names, flags)
m = c.match(doc.name)
if not m:
continue
except:
if doc.name not in names:
continue
# add main table # add main table
row_group = [] row_group = []




+ 257
- 0
frappe/core/page/data_import_tool/test_exporter_fixtures.py Ver fichero

@@ -0,0 +1,257 @@
# Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
# MIT License. See license.txt

import frappe
import frappe.defaults
from frappe.core.page.data_import_tool.data_import_tool import export_csv
import unittest

class TestDataImportFixtures(unittest.TestCase):
def setUp(self):
print "\nTeste for export explicit fixtures"
print "see fixtures csv test files in sites folder"
#start test for Custom Script
def test_Custom_Script_fixture_simple(self):
fixture = "Custom Script"
path = frappe.scrub(fixture) + "_original_style.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Custom_Script_fixture_simple_name_equal_default(self):
fixture = ["Custom Script", {"name":["Item-Client"]}]
path = frappe.scrub(fixture[0]) + "_simple_name_equal_default.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Custom_Script_fixture_simple_name_equal(self):
fixture = ["Custom Script", {"name":["Item-Client"],"op":"="}]
path = frappe.scrub(fixture[0]) + "_simple_name_equal.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Custom_Script_fixture_simple_name_not_equal(self):
fixture = ["Custom Script", {"name":["Item-Client"],"op":"!="}]
path = frappe.scrub(fixture[0]) + "_simple_name_not_equal.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
#without [] around the name...
def test_Custom_Script_fixture_simple_name_at_least_equal(self):
fixture = ["Custom Script", {"name":"Item-Cli"}]
path = frappe.scrub(fixture[0]) + "_simple_name_at_least_equal.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Custom_Script_fixture_multi_name_equal(self):
fixture = ["Custom Script", {"name":["Item-Client", "Customer-Client"],"op":"="}]
path = frappe.scrub(fixture[0]) + "_multi_name_equal.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Custom_Script_fixture_multi_name_not_equal(self):
fixture = ["Custom Script", {"name":["Item-Client", "Customer-Client"],"op":"!="}]
path = frappe.scrub(fixture[0]) + "_multi_name_not_equal.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Custom_Script_fixture_empty_object(self):
fixture = ["Custom Script", {}]
path = frappe.scrub(fixture[0]) + "_empty_object_should_be_all.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Custom_Script_fixture_just_list(self):
fixture = ["Custom Script"]
path = frappe.scrub(fixture[0]) + "_just_list_should_be_all.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
# Custom Script regular expression
def test_Custom_Script_fixture_rex_no_flags(self):
fixture = ["Custom Script", {"name":r"^[i|A]"}]
path = frappe.scrub(fixture[0]) + "_rex_no_flags.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Custom_Script_fixture_rex_with_flags(self):
fixture = ["Custom Script", {"name":r"^[i|A]", "flags":"L,M"}]
path = frappe.scrub(fixture[0]) + "_rex_with_flags.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
#start test for Custom Field
def test_Custom_Field_fixture_simple(self):
fixture = "Custom Field"
path = frappe.scrub(fixture) + "_original_style.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Custom_Field_fixture_simple_name_equal_default(self):
fixture = ["Custom Field", {"name":["Item-vat"]}]
path = frappe.scrub(fixture[0]) + "_simple_name_equal_default.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Custom_Field_fixture_simple_name_equal(self):
fixture = ["Custom Field", {"name":["Item-vat"],"op":"="}]
path = frappe.scrub(fixture[0]) + "_simple_name_equal.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Custom_Field_fixture_simple_name_not_equal(self):
fixture = ["Custom Field", {"name":["Item-vat"],"op":"!="}]
path = frappe.scrub(fixture[0]) + "_simple_name_not_equal.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
#without [] around the name...
def test_Custom_Field_fixture_simple_name_at_least_equal(self):
fixture = ["Custom Field", {"name":"Item-va"}]
path = frappe.scrub(fixture[0]) + "_simple_name_at_least_equal.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Custom_Field_fixture_multi_name_equal(self):
fixture = ["Custom Field", {"name":["Item-vat", "Bin-vat"],"op":"="}]
path = frappe.scrub(fixture[0]) + "_multi_name_equal.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Custom_Field_fixture_multi_name_not_equal(self):
fixture = ["Custom Field", {"name":["Item-vat", "Bin-vat"],"op":"!="}]
path = frappe.scrub(fixture[0]) + "_multi_name_not_equal.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Custom_Field_fixture_empty_object(self):
fixture = ["Custom Field", {}]
path = frappe.scrub(fixture[0]) + "_empty_object_should_be_all.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Custom_Field_fixture_just_list(self):
fixture = ["Custom Field"]
path = frappe.scrub(fixture[0]) + "_just_list_should_be_all.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
# Custom Field regular expression
def test_Custom_Field_fixture_rex_no_flags(self):
fixture = ["Custom Field", {"name":r"^[r|L]"}]
path = frappe.scrub(fixture[0]) + "_rex_no_flags.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Custom_Field_fixture_rex_with_flags(self):
fixture = ["Custom Field", {"name":r"^[i|A]", "flags":"L,M"}]
path = frappe.scrub(fixture[0]) + "_rex_with_flags.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
#start test for Doctype
def test_Doctype_fixture_simple(self):
fixture = "Customer"
path = "Doctype_" + frappe.scrub(fixture) + "_original_style.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Doctype_fixture_simple_name_equal_default(self):
fixture = ["Customer", {"name":["_Test Lead"]}]
path = "Doctype_" + frappe.scrub(fixture[0]) + "_simple_name_equal_default.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Doctype_fixture_simple_name_equal(self):
fixture = ["Customer", {"name":["_Test Customer 1"],"op":"="}]
path = "Doctype_" + frappe.scrub(fixture[0]) + "_simple_name_equal.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Doctype_simple_name_not_equal(self):
fixture = ["Customer", {"name":["_Test Customer 2"],"op":"!="}]
path = "Doctype_" + frappe.scrub(fixture[0]) + "_simple_name_not_equal.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
#without [] around the name...
def test_Doctype_fixture_simple_name_at_least_equal(self):
fixture = ["Customer", {"name":"_Test Customer"}]
path = "Doctype_" + frappe.scrub(fixture[0]) + "_simple_name_at_least_equal.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Doctype_multi_name_equal(self):
fixture = ["Customer", {"name":["_Test Customer 1", "_Test Customer 2"],"op":"="}]
path = "Doctype_" + frappe.scrub(fixture[0]) + "_multi_name_equal.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Doctype_multi_name_not_equal(self):
fixture = ["Customer", {"name":["_Test Customer 1", "_Test Customer 2"],"op":"!="}]
path = "Doctype_" + frappe.scrub(fixture[0]) + "_multi_name_not_equal.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Doctype_fixture_empty_object(self):
fixture = ["Customer", {}]
path = "Doctype_" + frappe.scrub(fixture[0]) + "_empty_object_should_be_all.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Doctype_fixture_just_list(self):
fixture = ["Customer"]
path = "Doctype_" + frappe.scrub(fixture[0]) + "_just_list_should_be_all.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
# Doctype regular expression
def test_Doctype_fixture_rex_no_flags(self):
fixture = ["Customer", {"name":r"_Test (?=L)"}]
path = "Doctype_" + frappe.scrub(fixture[0]) + "_rex_no_flags.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)
def test_Doctype_fixture_rex_with_flags(self):
fixture = ["Customer", {"name":r"_Test (?!C)", "flags":"L,M"}]
path = "Doctype_" + frappe.scrub(fixture[0]) + "_rex_with_flags.csv"
print "teste done {}".format(path)
export_csv(fixture, path)
self.assertTrue(True)

+ 6
- 4
frappe/utils/fixtures.py Ver fichero

@@ -22,10 +22,12 @@ def sync_fixtures(app=None):
def export_fixtures(): def export_fixtures():
for app in frappe.get_installed_apps(): for app in frappe.get_installed_apps():
for fixture in frappe.get_hooks("fixtures", app_name=app): for fixture in frappe.get_hooks("fixtures", app_name=app):
print "Exporting " + fixture
print "Exporting {0}".format(fixture)
if not os.path.exists(frappe.get_app_path(app, "fixtures")): if not os.path.exists(frappe.get_app_path(app, "fixtures")):
os.mkdir(frappe.get_app_path(app, "fixtures")) os.mkdir(frappe.get_app_path(app, "fixtures"))
if frappe.db.get_value("DocType", fixture, "issingle"):
export_fixture(fixture, fixture, app)
if isinstance(fixture, basestring):
fixture = [fixture];
if frappe.db.get_value("DocType", fixture[0], "issingle"):
export_fixture(fixture[0], fixture[0], app)
else: else:
export_csv(fixture, frappe.get_app_path(app, "fixtures", frappe.scrub(fixture) + ".csv"))
export_csv(fixture, frappe.get_app_path(app, "fixtures", frappe.scrub(fixture[0]) + ".csv"))

Cargando…
Cancelar
Guardar