Skip to content

Commit

Permalink
Merge pull request #29 from emirozer/f/custom_db_generation
Browse files Browse the repository at this point in the history
F/custom db generation
  • Loading branch information
emirozer committed Dec 3, 2015
2 parents 1654859 + 663b5db commit 4e3ba63
Show file tree
Hide file tree
Showing 13 changed files with 488 additions and 42 deletions.
29 changes: 25 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ mysql connector is needed for mysql db generation:

##### Usage

It is as follows, at this point in time, fake2db accepts rows and db argument.

*--rows* argument is pretty clear...
*--rows* argument is pretty clear :) integer

*--db* argument takes 6 possible options : sqlite, mysql, postgresql, mongodb, redis, couchdb

Expand All @@ -66,7 +65,10 @@ It is as follows, at this point in time, fake2db accepts rows and db argument.

*--port* argument is OPTIONAL. Port to use for database connection. Not used for sqlite.

*--password* argument is OPTIONAL. Password for root. Only supported for mysql.
*--username* argument is OPTIONAL. Username for the databse user.

*--password* argument is OPTIONAL. Password for database user. Only supported for mysql & postgresql.


> fake2db --rows 200 --db sqlite
<br>
Expand All @@ -79,9 +81,28 @@ In addition to the databases supported in the db argument, you can also run fake

> fake2db --rows --db postgresql --port 15432

##### Custom Database Generation

If you want to create a custom db/table, you have to provide **--custom** parameter followed by the column item you want. At the point in time, i mapped all the possible column items you can use here:

<https://github.com/emirozer/fake2db/blob/f/custom_db_generation/fake2db/custom.py>

Feed any keys you want to the custom flag:

> fake2db.py --rows 250 --db mysql --username mysql --password somepassword --custom name date country
<br>
>fake2db.py --rows 1500 --db mysql --password randompassword --custom currency_code credit_card_full credit_card_provider
<br>
>fake2db.py --rows 20 --db mongodb --custom name date country

<br>
##### Sample output - sqlite
![Screenshot](https://raw.github.com/emirozer/fake2db/master/docs/fake2db_example_sqlite.png)

<br>
![Screenshot](https://raw.github.com/emirozer/fake2db/master/docs/mysql_fake2db.png)
<br>
![Screenshot](https://raw.github.com/emirozer/fake2db/master/docs/pg_fake2db.png)

<br>
Binary file added docs/mysql_fake2db.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/pg_fake2db.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion fake2db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"""

__title__ = 'fake2db'
__version__ = '0.4.1'
__version__ = '0.5.0'
__author__ = 'Emir Ozer'
__license__ = 'GNU General Public License v2'
34 changes: 33 additions & 1 deletion fake2db/couchdb_handler.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import couchdb
import sys
from custom import faker_options_container
from helpers import fake2db_logger, lower_str_generator, rnd_id_generator

logger, extra_information = fake2db_logger()
Expand All @@ -15,12 +17,16 @@
class Fake2dbCouchdbHandler():
faker = Factory.create()

def fake2db_couchdb_initiator(self, number_of_rows, name=None):
def fake2db_couchdb_initiator(self, number_of_rows, name=None, custom=None):
'''Main handler for the operation
'''
rows = number_of_rows
db = self.database_caller_creator(name)

if custom:
self.custom_db_creator(rows, db, custom)
sys.exit(0)

self.data_filler_simple_registration(rows, db)
self.data_filler_detailed_registration(rows, db)
self.data_filler_company(rows, db)
Expand All @@ -45,6 +51,32 @@ def database_caller_creator(self, name=None):

return db

def custom_db_creator(self, number_of_rows, db, custom):
try:
data_list = list()
custom_d = faker_options_container()

for c in custom:
if custom_d.get(c):
logger.warning("fake2db found valid custom key provided: %s" % c, extra=d)
else:
logger.error("fake2db does not support the custom key you provided.", extra=d )
sys.exit(1)

for i in range(0, number_of_rows):
dict_c = {"id": rnd_id_generator(self)}
for c in custom:
dict_c[c] = getattr(self.faker, c)()

db.save(dict_c)


logger.warning('custom Commits are successful after write job!', extra=d)

except Exception as e:
logger.error(e, extra=d)


def data_filler_simple_registration(self, number_of_rows, db):
'''creates and fills the table with simple regis. information
'''
Expand Down
151 changes: 151 additions & 0 deletions fake2db/custom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
def faker_options_container():
'''
Following dictionary is mapped with fake-factory
providers, so that custom db creation be made with
faker.
for detailed info for providers:
http://fake-factory.readthedocs.org/en/latest/#
'''
faker_options = {"name_male": "varchar(200)",
"address": "varchar(300)",
"am_pm": "varchar(3)",
"boolean": "varchar(10)",
"opera": "varchar(300)",
"paragraph": "varchar(300)",
"bs": "varchar(300)",
"building_number": "INT",
"password": "varchar(300)",
"century": "varchar(50)",
"phone_number": "varchar(250)",
"chrome": "varchar(300)",
"postalcode": "varchar(300)",
"city": "varchar(300)",
"postalcode_plus4": "varchar(300)",
"city_prefix": "varchar(300)",
"postcode": "varchar(300)",
"city_suffix": "varchar(300)",
"prefix": "varchar(300)",
"color_name": "varchar(300)",
"prefix_female": "varchar(300)",
"company": "varchar(300)",
"prefix_male": "varchar(300)",
"company_email": "varchar(300)",
"profile": "varchar(300)",
"company_suffix": "varchar(300)",
"provider": "varchar(300)",
"country": "varchar(300)",
"country_code": "varchar(300)",
"credit_card_expire": "varchar(300)",
"credit_card_full": "varchar(300)",
"credit_card_number": "varchar(300)",
"credit_card_provider": "varchar(300)",
"credit_card_security_code": "varchar(300)",
"currency_code": "varchar(30)",
"date": "varchar(300)",
"date_time_ad": "varchar(300)",
"date_time_between": "varchar(300)",
"date_time_between_dates": "varchar(300)",
"random_digit": "INT",
"random_digit_not_null": "varchar(300)",
"date_time_this_decade": "varchar(300)",
"random_digit_not_null_or_empty": "varchar(300)",
"date_time_this_month": "varchar(300)",
"random_digit_or_empty": "varchar(300)",
"date_time_this_year": "varchar(300)",
"random_element": "varchar(300)",
"day_of_month": "varchar(300)",
"random_int": "INT",
"day_of_week": "varchar(300)",
"random_letter": "varchar(5)",
"domain_name": "varchar(300)",
"random_number": "INT",
"domain_word": "varchar(300)",
"ean": "varchar(300)",
"rgb_color": "varchar(300)",
"ean13": "varchar(300)",
"rgb_color_list": "varchar(300)",
"ean8": "varchar(300)",
"prgb_css_color": "varchar(300)",
"email": "varchar(300)",
"safari": "varchar(300)",
"file_extension": "varchar(300)",
"safe_color_name": "varchar(300)",
"file_name": "varchar(300)",
"safe_email": "varchar(300)",
"firefox": "varchar(300)",
"safe_hex_color": "varchar(300)",
"first_name": "varchar(300)",
"secondary_address": "varchar(300)",
"first_name_female": "varchar(300)",
"first_name_male": "varchar(300)",
"sentence": "varchar(300)",
"free_email": "varchar(300)",
"free_email_domain": "varchar(300)",
"sha1": "varchar(300)",
"geo_coordinate": "varchar(300)",
"sha256": "varchar(300)",
"slug": "varchar(300)",
"hex_color": "varchar(300)",
"ssn": "varchar(300)",
"image_url": "varchar(300)",
"state": "varchar(300)",
"internet_explorer": "varchar(300)",
"state_abbr": "varchar(300)",
"ipv4": "varchar(300)",
"street_address": "varchar(300)",
"ipv6": "varchar(300)",
"street_name": "varchar(300)",
"iso8601": "varchar(300)",
"street_suffix": "varchar(300)",
"job": "varchar(300)",
"suffix": "varchar(300)",
"language_code": "varchar(300)",
"suffix_female": "varchar(300)",
"last_name": "varchar(300)",
"suffix_male": "varchar(300)",
"last_name_female": "varchar(300)",
"text": "varchar(300)",
"last_name_male": "varchar(300)",
"time": "varchar(300)",
"latitude": "varchar(300)",
"time_delta": "varchar(300)",
"timezone": "varchar(300)",
"linux_platform_token": "varchar(300)",
"tld": "varchar(300)",
"linux_processor": "varchar(300)",
"unix_time": "varchar(300)",
"locale": "varchar(300)",
"uri": "varchar(300)",
"longitude": "varchar(300)",
"uri_extension": "varchar(300)",
"mac_address": "varchar(300)",
"uri_page": "varchar(300)",
"mac_platform_token": "varchar(300)",
"uri_path": "varchar(300)",
"mac_processor": "varchar(300)",
"url": "varchar(300)",
"md5": "varchar(300)",
"user_agent": "varchar(300)",
"military_apo": "varchar(300)",
"user_name": "varchar(300)",
"military_dpo": "varchar(300)",
"uuid4": "varchar(300)",
"military_ship": "varchar(300)",
"windows_platform_token": "varchar(300)",
"military_state": "varchar(300)",
"word": "varchar(300)",
"mime_type": "varchar(300)",
"words": "varchar(300)",
"month": "varchar(300)",
"year": "varchar(300)",
"month_name": "varchar(300)",
"zipcode": "varchar(300)",
"name": "varchar(300)",
"zipcode_plus4": "varchar(300)",
"name_female": "varchar(300)",
}

return faker_options
Loading

0 comments on commit 4e3ba63

Please sign in to comment.