Skip to content

Commit

Permalink
[master][#1623]: CSV/JSON dumps to excludes deleted objects. Also add…
Browse files Browse the repository at this point in the history
…ed first CLI test!
  • Loading branch information
David Read committed Jan 18, 2012
1 parent 8314e97 commit c2c3427
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
6 changes: 2 additions & 4 deletions ckan/lib/cli.py
Expand Up @@ -209,8 +209,7 @@ def simple_dump_csv(self):
dump_filepath = self.args[1]
import ckan.lib.dumper as dumper
dump_file = open(dump_filepath, 'w')
query = model.Session.query(model.Package)
dumper.SimpleDumper().dump_csv(dump_file, query)
dumper.SimpleDumper().dump(dump_file, format='csv')

def simple_dump_json(self):
from ckan import model
Expand All @@ -220,8 +219,7 @@ def simple_dump_json(self):
dump_filepath = self.args[1]
import ckan.lib.dumper as dumper
dump_file = open(dump_filepath, 'w')
query = model.Session.query(model.Package)
dumper.SimpleDumper().dump_json(dump_file, query)
dumper.SimpleDumper().dump(dump_file, format='json')

def dump_rdf(self):
if len(self.args) < 3:
Expand Down
45 changes: 45 additions & 0 deletions ckan/tests/lib/test_cli.py
@@ -0,0 +1,45 @@
import os
import csv

from nose.tools import assert_equal

from ckan import model
from ckan.lib.cli import ManageDb
from ckan.lib.create_test_data import CreateTestData
from ckan.lib.helpers import json

class TestDb:
@classmethod
def setup_class(cls):
cls.db = ManageDb('db')
CreateTestData.create()

# delete warandpeace
rev = model.repo.new_revision()
model.Package.by_name(u'warandpeace').delete()
model.repo.commit_and_remove()

def test_simple_dump_csv(self):
csv_filepath = '/tmp/dump.tmp'
self.db.args = ('simple-dump-csv %s' % csv_filepath).split()
self.db.simple_dump_csv()
assert os.path.exists(csv_filepath), csv_filepath
f_obj = open(csv_filepath, "r")
reader = csv.reader(f_obj)
rows = [row for row in reader]
assert_equal(rows[0][:3], ['id', 'name', 'title'])
pkg_names = set(row[1] for row in rows[1:])
assert 'annakarenina' in pkg_names, pkg_names
assert 'warandpeace' not in pkg_names, pkg_names

def test_simple_dump_json(self):
json_filepath = '/tmp/dump.tmp'
self.db.args = ('simple-dump-json %s' % json_filepath).split()
self.db.simple_dump_json()
assert os.path.exists(json_filepath), json_filepath
f_obj = open(json_filepath, "r")
rows = json.loads(f_obj.read())
assert set(rows[0].keys()) > set(('id', 'name', 'title')), rows[0].keys()
pkg_names = set(row['name'] for row in rows)
assert 'annakarenina' in pkg_names, pkg_names
assert 'warandpeace' not in pkg_names, pkg_names

0 comments on commit c2c3427

Please sign in to comment.