wrds-download

TUI/CLI tool for browsing and downloading WRDS data
Log | Files | Refs | README

test_db.py (1428B)


      1 """Tests for db module — query building and identifier quoting."""
      2 
      3 from wrds_dl.db import build_query, quote_ident
      4 
      5 
      6 class TestQuoteIdent:
      7     def test_simple(self):
      8         assert quote_ident("foo") == '"foo"'
      9 
     10     def test_double_quotes(self):
     11         assert quote_ident('foo"bar') == '"foo""bar"'
     12 
     13     def test_empty(self):
     14         assert quote_ident("") == '""'
     15 
     16     def test_spaces(self):
     17         assert quote_ident("my table") == '"my table"'
     18 
     19 
     20 class TestBuildQuery:
     21     def test_basic(self):
     22         q = build_query("crsp", "dsf")
     23         assert q == 'SELECT * FROM "crsp"."dsf"'
     24 
     25     def test_columns(self):
     26         q = build_query("crsp", "dsf", columns="permno,date,prc")
     27         assert q == 'SELECT "permno", "date", "prc" FROM "crsp"."dsf"'
     28 
     29     def test_where(self):
     30         q = build_query("crsp", "dsf", where="date = '2020-01-02'")
     31         assert q == """SELECT * FROM "crsp"."dsf" WHERE date = '2020-01-02'"""
     32 
     33     def test_limit(self):
     34         q = build_query("crsp", "dsf", limit=100)
     35         assert q == 'SELECT * FROM "crsp"."dsf" LIMIT 100'
     36 
     37     def test_all_options(self):
     38         q = build_query("comp", "funda", columns="gvkey,sale", where="fyear >= 2020", limit=1000)
     39         assert q == 'SELECT "gvkey", "sale" FROM "comp"."funda" WHERE fyear >= 2020 LIMIT 1000'
     40 
     41     def test_star_columns(self):
     42         q = build_query("crsp", "dsf", columns="*")
     43         assert q == 'SELECT * FROM "crsp"."dsf"'